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#include "flint/graphics/renderer.h"
5#include "flint/window/IWindow.h"
6
7#include <deque>
8#include <memory>
9#include <vector>
10
11namespace Flint {
12
20 class State {
21 public:
22 virtual ~State() = default;
28 virtual void handleInput(IWindow &window) = 0;
29
35 virtual void update(float deltaTime) = 0;
36
43 virtual void render(GPURenderer &gpuRenderer, IWindow &window) = 0;
44
50 virtual void onEnter() {
51 }
52
58 virtual void onExit() {
59 }
60
61 protected:
62 std::string m_contextName;
63 };
64
73 public:
74 StateManager() = default;
75 virtual ~StateManager() = default;
79 enum class TransitionType {
80 PUSH,
81 POP,
82 REPLACE,
83 CLEAR
84 };
85
91 std::unique_ptr<State> state;
92 };
93
101 void pushState(std::unique_ptr<State> state);
102
108 void popState();
109
117 void replaceState(std::unique_ptr<State> state);
118
124 [[nodiscard]] State *currentState() const;
125
131 [[nodiscard]] bool empty() const;
132
139 void queueTransition(TransitionType type, std::unique_ptr<State> newState);
140
149
154
160 const std::vector<std::unique_ptr<State>> &getStates() const;
161
172 void renderAllBelow(GPURenderer &gpuRenderer, IWindow &window, State *topState) const;
173
180 State *peekBelow(const State *top) const;
181
182 private:
183 std::vector<std::unique_ptr<State>> m_states;
184 std::deque<PendingTransition> m_transitionQueue;
185 };
186}
High-level SDL GPU renderer managing pipeline state, batching, and frame presentation.
Definition renderer.h:39
Abstract interface for a platform window.
Definition IWindow.h:27
void applyTransitions()
Execute all pending transitions in FIFO order.
void replaceState(std::unique_ptr< State > state)
Replace the current state with a new one.
State * currentState() const
Get currently active (top) state.
void pushState(std::unique_ptr< State > state)
Push a new state onto the stack and activate it.
State * peekBelow(const State *top) const
Return the state directly below top on the stack.
void popState()
Pop the current state and resume the one beneath.
void queueTransition(TransitionType type, std::unique_ptr< State > newState)
Enqueue a state transition to be applied on the next applyTransitions() call.
const std::vector< std::unique_ptr< State > > & getStates() const
Return read-only access to the full state deque.
bool empty() const
Check if there are any states in the stack.
TransitionType
Describes the kind of stack operation to perform on transition.
Definition state.h:79
void renderAllBelow(GPURenderer &gpuRenderer, IWindow &window, State *topState) const
Render all states below topState in bottom-up order.
Manages a stack of States and applies queued transitions.
Definition state.h:72
void clearStates()
Remove all states from the stack, calling onExit() on each.
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:58
virtual void onEnter()
Called immediately after this state becomes the top of the stack.
Definition state.h:50
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.
Abstract base for a single game state (scene / screen).
Definition state.h:20
std::unique_ptr< State > state
Definition state.h:91
Transition description queued for later application.
Definition state.h:89