Flint Engine
Modular C++20 2D game engine powered by SDL3
Loading...
Searching...
No Matches
state.h
1// SPDX-License-Identifier: MIT
2// SPDX-FileCopyrightText: 2025-2026 Manuel Osorio
3#pragma once
4
5#include "flint/core/api.h"
6#include "flint/core/state-ops.h"
7#include "flint/graphics/renderer.h"
8#include "flint/window/IWindow.h"
9
10#include <deque>
11#include <memory>
12#include <unordered_map>
13#include <unordered_set>
14#include <vector>
15
16namespace Flint {
17
25 class State {
26 public:
27 virtual ~State() = default;
33 virtual void handleInput(IWindow &window) = 0;
34
40 virtual void update(float deltaTime) = 0;
41
48 virtual void render(GPURenderer &gpuRenderer, IWindow &window) = 0;
49
55 virtual void onEnter() {
56 }
57
63 virtual void onExit() {
64 }
65
66 protected:
67 std::string m_contextName;
68 };
69
78 public:
79 StateManager() = default;
80 virtual ~StateManager() = default;
81
82 template <typename T>
83 void registerStateType() {
84 m_stateOpsRegistry.registerOps(makeStateOps<T>());
85 }
86
87 FLINT_API void handleInput(IWindow &window);
88 FLINT_API void update(float deltaTime);
89 FLINT_API void render(GPURenderer &renderer, IWindow &window);
90
94 enum class TransitionType {
95 PUSH,
96 POP,
97 REPLACE,
98 CLEAR,
99 RESET
100 };
101
107 std::unique_ptr<State> state;
108 };
109
117 FLINT_API void pushState(std::unique_ptr<State> state);
118
124 FLINT_API void popState();
125
133 FLINT_API void replaceState(std::unique_ptr<State> state);
134
140 [[nodiscard]] FLINT_API State *currentState() const;
141
147 [[nodiscard]] FLINT_API bool empty() const;
148
155 FLINT_API void queueTransition(TransitionType type, std::unique_ptr<State> newState);
156
164 FLINT_API void applyTransitions();
165
169 FLINT_API void clearStates();
170
176 FLINT_API void resetStates(std::unique_ptr<State> state);
177
183 FLINT_API const std::vector<std::unique_ptr<State>> &getStates() const;
184
195 FLINT_API void renderAllBelow(GPURenderer &gpuRenderer, IWindow &window,
196 State *topState) const;
197
204 FLINT_API State *peekBelow(const State *top) const;
205
206 FLINT_API bool canInstallStateOps(const StateOps *operations, std::size_t count) const;
207
208 FLINT_API void installStateOps(const StateOps *operations, std::size_t count);
209
210 private:
211 std::vector<std::unique_ptr<State>> m_states;
212 std::deque<PendingTransition> m_transitionQueue;
213
214 std::unordered_set<StateTypeId> collectRequiredTypeIds() const;
215 [[nodiscard]] const StateOps *getStateOps(const State *state) const;
216
217 void dispatchOnEnter(State *state);
218 void dispatchOnExit(State *state);
219 void dispatchRender(State *state, GPURenderer &renderer, IWindow &window) const;
220
221 StateOpsRegistry m_stateOpsRegistry;
222
223 std::unordered_map<const State *, StateTypeId> m_stateTypes;
224 };
225}
High-level SDL GPU renderer managing pipeline state, batching, and frame presentation.
Definition renderer.h:40
Abstract interface for a platform window.
Definition IWindow.h:27
Manages a stack of States and applies queued transitions.
Definition state.h:77
FLINT_API State * peekBelow(const State *top) const
Return the state directly below top on the stack.
FLINT_API void resetStates(std::unique_ptr< State > state)
Clears all states from the stack and pushes a new root state.
FLINT_API void pushState(std::unique_ptr< State > state)
Push a new state onto the stack and activate it.
FLINT_API bool empty() const
Check if there are any states in the stack.
FLINT_API void renderAllBelow(GPURenderer &gpuRenderer, IWindow &window, State *topState) const
Render all states below topState in bottom-up order.
FLINT_API void queueTransition(TransitionType type, std::unique_ptr< State > newState)
Enqueue a state transition to be applied on the next applyTransitions() call.
FLINT_API void popState()
Pop the current state and resume the one beneath.
FLINT_API void applyTransitions()
Execute all pending transitions in FIFO order.
FLINT_API const std::vector< std::unique_ptr< State > > & getStates() const
Return read-only access to the full state deque.
FLINT_API void replaceState(std::unique_ptr< State > state)
Replace the current state with a new one.
TransitionType
Describes the kind of stack operation to perform on transition.
Definition state.h:94
FLINT_API State * currentState() const
Get currently active (top) state.
FLINT_API void clearStates()
Remove all states from the stack, calling onExit() on each.
Abstract base for a single game state (scene / screen).
Definition state.h:25
virtual void update(float deltaTime)=0
Advance the simulation logic for this state by deltaTime seconds.
virtual void onExit()
Called immediately before this state is removed from the top of the stack.
Definition state.h:63
virtual void onEnter()
Called immediately after this state becomes the top of the stack.
Definition state.h:55
virtual void render(GPURenderer &gpuRenderer, IWindow &window)=0
Issue all draw calls necessary to visualize this state.
virtual void handleInput(IWindow &window)=0
Process raw input events and translate them into state-specific actions.
Transition description queued for later application.
Definition state.h:105
std::unique_ptr< State > state
Definition state.h:107