ReOrginasation

main
Johannes Hendrik Gerard van der Weide 2023-10-12 17:15:35 +02:00
parent fce1de6303
commit 4fa8157eac
2 changed files with 82 additions and 79 deletions

81
src/Game.cpp Normal file
View File

@ -0,0 +1,81 @@
#include <SFML/Graphics.hpp>
#include "Game.hpp"
const float Game::PlayerSpeed = 100.f;
const sf::Time Game::TimePerFrame = sf::seconds(1.f/60.f);
Game::Game()
: mWindow(sf::VideoMode(640, 480), "SFML Application")
, mTexture()
, mPlayer() {
if(!mTexture.loadFromFile("Assets/blobcat_hertog.png")) {
// Handle loading errors
}
mPlayer.setTexture(mTexture);
mPlayer.setScale(0.25f, 0.25f);
mPlayer.setPosition(100.f, 100.f);
}
void Game::run() {
sf::Clock clock;
sf::Time timeSinceLastUpdate = sf::Time::Zero;
while (mWindow.isOpen()) {
processEvents();
timeSinceLastUpdate += clock.restart();
while (timeSinceLastUpdate > TimePerFrame) {
timeSinceLastUpdate -= TimePerFrame;
processEvents();
update(TimePerFrame);
}
render();
}
}
void Game::processEvents() {
sf::Event event;
while (mWindow.pollEvent(event)) {
switch (event.type) {
//For each time the while loop iterates, it means a new event that was registered by the window is being handled. While there can be many different events, we will only check for some types of events, which are of our interest right now.
case sf::Event::KeyPressed:
handlePlayerInput(event.key.code, true);
break;
case sf::Event::KeyReleased:
handlePlayerInput(event.key.code, false);
break;
case sf::Event::Closed:
mWindow.close();
break;
}
}
}
void Game::handlePlayerInput(sf::Keyboard::Key key,bool isPressed) {
if (key == sf::Keyboard::W)
mIsMovingUp = isPressed;
else if (key == sf::Keyboard::S)
mIsMovingDown = isPressed;
else if (key == sf::Keyboard::A)
mIsMovingLeft = isPressed;
else if (key == sf::Keyboard::D)
mIsMovingRight = isPressed;
}
void Game::update(sf::Time deltaTime) {
sf::Vector2f movement(0.f, 0.f);
if (mIsMovingUp)
movement.y -= 100.f;
if (mIsMovingDown)
movement.y += 100.f;
if (mIsMovingLeft)
movement.x -= 100.f;
if (mIsMovingRight)
movement.x += 100.f;
mPlayer.move(movement * deltaTime.asSeconds());
}
void Game::render() {
mWindow.clear();
mWindow.draw(mPlayer);
mWindow.display();
}

View File

@ -1,85 +1,7 @@
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
#include "Game.hpp" #include "Game.hpp"
const float Game::PlayerSpeed = 100.f;
const sf::Time Game::TimePerFrame = sf::seconds(1.f/60.f);
Game::Game()
: mWindow(sf::VideoMode(640, 480), "SFML Application")
, mTexture()
, mPlayer() {
if(!mTexture.loadFromFile("Assets/blobcat_hertog.png")) {
// Handle loading errors
}
mPlayer.setTexture(mTexture);
mPlayer.setScale(0.25f, 0.25f);
mPlayer.setPosition(100.f, 100.f);
}
void Game::run() {
sf::Clock clock;
sf::Time timeSinceLastUpdate = sf::Time::Zero;
while (mWindow.isOpen()) {
processEvents();
timeSinceLastUpdate += clock.restart();
while (timeSinceLastUpdate > TimePerFrame) {
timeSinceLastUpdate -= TimePerFrame;
processEvents();
update(TimePerFrame);
}
render();
}
}
void Game::processEvents() {
sf::Event event;
while (mWindow.pollEvent(event)) {
switch (event.type) {
//For each time the while loop iterates, it means a new event that was registered by the window is being handled. While there can be many different events, we will only check for some types of events, which are of our interest right now.
case sf::Event::KeyPressed:
handlePlayerInput(event.key.code, true);
break;
case sf::Event::KeyReleased:
handlePlayerInput(event.key.code, false);
break;
case sf::Event::Closed:
mWindow.close();
break;
}
}
}
void Game::handlePlayerInput(sf::Keyboard::Key key,bool isPressed) {
if (key == sf::Keyboard::W)
mIsMovingUp = isPressed;
else if (key == sf::Keyboard::S)
mIsMovingDown = isPressed;
else if (key == sf::Keyboard::A)
mIsMovingLeft = isPressed;
else if (key == sf::Keyboard::D)
mIsMovingRight = isPressed;
}
void Game::update(sf::Time deltaTime) {
sf::Vector2f movement(0.f, 0.f);
if (mIsMovingUp)
movement.y -= 100.f;
if (mIsMovingDown)
movement.y += 100.f;
if (mIsMovingLeft)
movement.x -= 100.f;
if (mIsMovingRight)
movement.x += 100.f;
mPlayer.move(movement * deltaTime.asSeconds());
}
void Game::render() {
mWindow.clear();
mWindow.draw(mPlayer);
mWindow.display();
}
int main() { int main() {
Game game; Game game;
game.run(); game.run();
} }