Compare commits

...

2 Commits

Author SHA1 Message Date
Johannes Hendrik Gerard van der Weide f3b323d71a Update 2023-10-11 12:51:24 +02:00
Johannes Hendrik Gerard van der Weide 723475e2a5 Ordering 2023-10-11 10:22:08 +02:00
4 changed files with 104 additions and 22 deletions

View File

@ -26,6 +26,7 @@
default = pkgs.mkShell { default = pkgs.mkShell {
# The Nix packages provided in the environment # The Nix packages provided in the environment
packages = with pkgs; [ packages = with pkgs; [
gdb
sfml sfml
cmake cmake
gcc gcc

14
src/Game.hpp Normal file
View File

@ -0,0 +1,14 @@
class Game
{
public:Game();
void run();
private:
void processEvents();
void update();
void render();
private:
sf::RenderWindow mWindow;
sf::CircleShape mPlayer;
};

89
src/ProjectName.cpp Normal file
View File

@ -0,0 +1,89 @@
#include <SFML/Graphics.hpp>
#include "Game.hpp"
Game::Game()
: mWindow(sf::VideoMode(640, 480), "SFML Application")
, mPlayer()
{
mPlayer.setRadius(40.f);
mPlayer.setPosition(100.f, 100.f);
mPlayer.setFillColor(sf::Color::Cyan);
}
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 -= 1.f;
if (mIsMovingDown)
movement.y += 1.f;
if (mIsMovingLeft)
movement.x -= 1.f;
if (mIsMovingRight)
movement.x += 1.f;
mPlayer.move(movement * deltaTime.asSeconds());
}
void Game::render()
{
mWindow.clear();
mWindow.draw(mPlayer);
mWindow.display();
}
int main()
{
Game game;
game.run();
}

View File

@ -1,22 +0,0 @@
#include <SFML/Graphics.hpp>
int main()
{
sf::RenderWindow window(sf::VideoMode(640, 480), "SFML Application");
sf::CircleShape shape;
shape.setRadius(40.f);
shape.setPosition(100.f, 100.f);
shape.setFillColor(sf::Color::Cyan);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
window.draw(shape);
window.display();
}
}