Global static input handling

This commit is contained in:
Maximilian Giller 2023-05-08 18:20:55 +02:00
parent adc2246bb7
commit 7b0560f6fd
8 changed files with 64 additions and 14 deletions

View file

@ -41,7 +41,12 @@ set(SOURCES
src/game/input/input_mapper.cpp src/game/input/input_mapper.cpp
src/game/input/input_mapper.h src/game/input/input_mapper.h
src/game/input/action_controller.cpp src/game/input/action_controller.cpp
src/game/input/action_controller.h src/game/input/direction.h src/game/input/direction.cpp) src/game/input/action_controller.h
src/game/input/direction.h
src/game/input/direction.cpp
src/game/player/player.cpp
src/game/player/player.hpp
src/game/input/game_inputs.hpp)
set(PHYSICS_00_SOURCES set(PHYSICS_00_SOURCES
src/prototypes/physics_00.cpp) src/prototypes/physics_00.cpp)

View file

@ -3,17 +3,18 @@
#include <SFML/Graphics/RenderWindow.hpp> #include <SFML/Graphics/RenderWindow.hpp>
#include "game.h" #include "game.h"
#include "../config.h"
Game::Game(std::shared_ptr<sf::RenderWindow> window) : window(std::move(window)), Game::Game(std::shared_ptr<sf::RenderWindow> window) : window(std::move(window)),
gameObjects(), gameObjects(),
actionController(new ActionController(this)), actionController(new ActionController(this))
inputMapper(new InputMapper(this))
{ {
InputMapper::setGame(this);
} }
Game::~Game() Game::~Game()
{ {
delete actionController;
for (auto &gameObject: gameObjects) for (auto &gameObject: gameObjects)
{ {
delete gameObject; delete gameObject;
@ -28,14 +29,14 @@ void Game::run()
while (window->isOpen()) while (window->isOpen())
{ {
inputMapper->processEvents(); InputMapper::processEvents();
TimeSinceLastUpdate += clock.restart(); TimeSinceLastUpdate += clock.restart();
while (TimeSinceLastUpdate >= FRAME_TIME) while (TimeSinceLastUpdate >= FRAME_TIME)
{ {
TimeSinceLastUpdate -= FRAME_TIME; TimeSinceLastUpdate -= FRAME_TIME;
update(); update();
inputMapper->processEvents(); InputMapper::processEvents();
} }
drawFrame(); drawFrame();
} }

View file

@ -10,7 +10,6 @@
class ActionController; class ActionController;
class InputMapper;
class Game class Game
@ -28,7 +27,6 @@ public:
std::shared_ptr<sf::RenderWindow> window; std::shared_ptr<sf::RenderWindow> window;
ActionController *actionController; ActionController *actionController;
InputMapper *inputMapper;
private: private:
std::vector<GameObject *> gameObjects; std::vector<GameObject *> gameObjects;

View file

@ -0,0 +1,25 @@
#ifndef HOLESOME_GAME_INPUTS_HPP
#define HOLESOME_GAME_INPUTS_HPP
enum MenuInputs
{
MENU_NONE,
MENU_UP,
MENU_DOWN,
MENU_LEFT,
MENU_RIGHT,
MENU_SELECT,
MENU_BACK,
MENU_OPEN
};
enum GameInputs
{
GAME_NONE,
GAME_HIDE,
GAME_MOVE,
GAME_RUN
};
#endif //HOLESOME_GAME_INPUTS_HPP

View file

@ -1,11 +1,12 @@
#include "input_mapper.h" #include "input_mapper.h"
InputMapper::InputMapper(Game *game) : game(game) void InputMapper::setGame(Game *game)
{ {
InputMapper::game = game;
} }
void InputMapper::processEvents() void InputMapper::processEvents()
{ {
sf::Event event{}; sf::Event event{};

View file

@ -13,14 +13,14 @@ class Game;
class InputMapper class InputMapper
{ {
public: public:
explicit InputMapper(Game *game); static void setGame(Game *game);
void processEvents(); static void processEvents();
private: private:
Game *game; static inline Game *game = nullptr;
void handleKeyPress(sf::Event::KeyEvent event); static void handleKeyPress(sf::Event::KeyEvent event);
}; };

View file

@ -0,0 +1 @@
#include "player.hpp"

View file

@ -0,0 +1,19 @@
#ifndef HOLESOME_PLAYER_HPP
#define HOLESOME_PLAYER_HPP
#include "../game_object.h"
class Player : GameObject
{
public:
Player();
~Player();
void draw(sf::RenderWindow *window) const override;
void update() const override;
};
#endif //HOLESOME_PLAYER_HPP