Update
parent
723475e2a5
commit
f3b323d71a
|
@ -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
|
||||||
|
|
|
@ -1,5 +1,86 @@
|
||||||
#include <SFML/Graphics.hpp>
|
#include <SFML/Graphics.hpp>
|
||||||
#include <Game.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()
|
int main()
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue