ImagoEngine
A C++ 2D game engine built on a sparse-set ECS, with scenes, scheduled systems, events, and action-based input
Loading...
Searching...
No Matches
Game.hpp
Go to the documentation of this file.
1
6
7#pragma once
8
9#include <string>
10#include <SFML/Graphics/RenderWindow.hpp> // for the window lifetime and rendering
11#include <cstdint> // for int32_t
12
20
21// SDL forward declaration, SDL is a C library so this lives at a global scope
22struct _SDL_GameController;
23
24namespace Imago::Engine
25{
37 class Game {
38 private:
39
40 // NOTE: member order matters in this class. Members are destroyed in reverse in C++, so the SceneManager
41 // needs to unload its Scenes while the window, EngineControl, and context are all still alive. The SceneManager
42 // holds a const SceneContext& so _ctx must be declared before it.
43
44 GameConfig _config;
45 bool _isRunning = false;
46 sf::RenderWindow _window;
47 bool _hasFocus = true;
48 InputHandler _inputHandler;
49 //TODO AssetManager
50 bool _gamepadReady = false;
51 _SDL_GameController* _controller = nullptr;
52 int32_t _controllerId = -1;
53 EngineControl _engineControl;
54 SceneContext _ctx;
55 SceneManager _sceneManager;
56
71 void apply_pacing();
72
76 void poll_events();
77
81 void poll_gamepad_events();
82
90 void process_requests();
91
92 // --- controller dis/connecting ---
93
97 bool try_open_controller(int index);
98
102 void close_controller();
103
107 void adopt_any_controller();
108
109
110 public:
111
116 explicit Game(const GameConfig& config);
117
121 ~Game();
122
123 // explicitly disallow copy and move behavior
124 Game(const Game&) = delete;
125 Game& operator=(const Game&) = delete;
126 Game(Game&&) = delete;
127 Game& operator=(Game&&) = delete;
128
134 template <typename T>
135 void register_scene(const std::string& name) { _sceneManager.register_scene<T>(name); }
136
141 ActionId register_action(const std::string& name);
142
144 void add_input(const std::string& action, sf::Keyboard::Scancode scancode);
145 void add_input(const std::string& action, sf::Mouse::Button button);
146 void add_input(const std::string& action, GamepadButton button);
147
153 void add_input(const std::string& action, GamepadAxis axis, float deadZone = 0.2f, float threshold = 0.5f);
154
155
156
161 void run();
162 };
163}
Define EngineControl which is the object that the user calls quit and scene change methdos through.
Defines the settings Game is constructed with.
Defines InputHandler, which owns the inputs and the InputState and turns raw events into action state...
Defines the raw inputs that drive an action.
Defines InputState, the read-only view of every action that sytems query.
uint32_t ActionId
Id of a registered action, resolve a name once with action_id() and cache it for the fast path.
Definition InputState.hpp:18
Defines the SceneContext, how systems reach persistent engine objects.
Defines the SceneManager, which is responsible for creating, switching between, and updating Scenes.
The user's only way to talk to the Game from inside a system or Scene load/unload.
Definition EngineControl.hpp:20
void register_scene(const std::string &name)
Registers a Scene type under a name. Simply forwards to its SceneManager.
Definition Game.hpp:135
void add_input(const std::string &action, sf::Keyboard::Scancode scancode)
adds a key press, mouse button, or gamepad button to an action.
Definition Game.cpp:405
Game(const GameConfig &config)
Construct a new Game object, building a new window based on GameSettings and filling in the SceneCont...
Definition Game.cpp:95
~Game()
closes any controllers and quits SDL
Definition Game.cpp:118
ActionId register_action(const std::string &name)
Registers an action name and returns its id. Forwards to the InputHandler.
Definition Game.cpp:400
void run()
Starts config.firstScene and runs the main loop until the game quits.
Definition Game.cpp:338
Owned by Game. Handles registering actions and adding raw inputs to them. It updates InputState based...
Definition InputHandler.hpp:26
The manager is the only object that creates, switches, and updates Scenes.
Definition SceneManager.hpp:25
Everything the Game needs to start. Default values for now but will later be filled by a YAML config ...
Definition GameConfig.hpp:17
Gives systems access to the persistent engine objects they could need.
Definition SceneContext.hpp:30