Compare commits

..

No commits in common. "f3b323d71a10e6fa7156617d532f7401692fccbc" and "60a371cdca0de7c9158fa3cc25ba0585160e8506" have entirely different histories.

4 changed files with 22 additions and 104 deletions

View File

@ -26,7 +26,6 @@
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

View File

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

View File

@ -1,89 +0,0 @@
#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();
}

22
src/main.cpp Normal file
View File

@ -0,0 +1,22 @@
#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();
}
}