Flint Engine
Modular C++20 2D game engine powered by SDL3
Loading...
Searching...
No Matches
state-ops.h
1// SPDX-License-Identifier: MIT
2// SPDX-FileCopyrightText: 2025-2026 Manuel Osorio
3#pragma once
4
5#include <cstddef>
6#include <cstdint>
7#include <string_view>
8#include <typeinfo>
9#include <unordered_map>
10#include <utility>
11
12namespace Flint {
13
14 class State;
15 class IWindow;
16 class GPURenderer;
17
18 using StateTypeId = std::uint64_t;
19
20 [[nodiscard]] constexpr StateTypeId hashStateTypeName(std::string_view name) {
21 StateTypeId hash = 14695981039346656037ull;
22
23 for (const char character : name) {
24 hash ^= static_cast<unsigned char>(character);
25 hash *= 1099511628211ull;
26 }
27
28 return hash;
29 }
30
31 template <typename T>
32 StateTypeId stateTypeId() {
33 return hashStateTypeName(typeid(T).name());
34 }
35
36 struct StateOps {
37 StateTypeId typeId;
38
39 std::size_t size;
40 std::size_t alignment;
41
42 void (*handleInput)(State *, IWindow &);
43 void (*update)(State *, float);
44 void (*render)(State *, GPURenderer &, IWindow &);
45
46 void (*onEnter)(State *);
47 void (*onExit)(State *);
48 };
49
50 template <typename T>
51 StateOps makeStateOps() {
52 return {
53 .typeId = stateTypeId<T>(),
54 .size = sizeof(T),
55 .alignment = alignof(T),
56
57 .handleInput = [](State *state,
58 IWindow &window) { static_cast<T *>(state)->T::handleInput(window); },
59
60 .update = [](State *state,
61 float deltaTime) { static_cast<T *>(state)->T::update(deltaTime); },
62
63 .render = [](State *state, GPURenderer &renderer,
64 IWindow &window) { static_cast<T *>(state)->T::render(renderer, window); },
65
66 .onEnter = [](State *state) { static_cast<T *>(state)->T::onEnter(); },
67
68 .onExit = [](State *state) { static_cast<T *>(state)->T::onExit(); },
69 };
70 }
71
73 public:
74 void registerOps(StateOps ops) {
75 m_registeredOperations[ops.typeId] = ops;
76 }
77
78 void replaceModuleOps(const StateOps *operations, std::size_t count) {
79 std::unordered_map<StateTypeId, StateOps> next;
80 next.reserve(count);
81
82 for (std::size_t i = 0; i < count; ++i) {
83 next[operations[i].typeId] = operations[i];
84 }
85
86 m_moduleOperations = std::move(next);
87 }
88
89 [[nodiscard]] const StateOps *get(StateTypeId typeId) const {
90 if (const auto it = m_moduleOperations.find(typeId); it != m_moduleOperations.end()) {
91 return &it->second;
92 }
93
94 if (const auto it = m_registeredOperations.find(typeId);
95 it != m_registeredOperations.end()) {
96 return &it->second;
97 }
98
99 return nullptr;
100 }
101
102 [[nodiscard]] bool hasModuleOps(StateTypeId typeId) const {
103 return m_moduleOperations.contains(typeId);
104 }
105
106 private:
107 std::unordered_map<StateTypeId, StateOps> m_registeredOperations;
108 std::unordered_map<StateTypeId, StateOps> m_moduleOperations;
109 };
110
111}
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
Abstract base for a single game state (scene / screen).
Definition state.h:25