SDEngine
Game Engine
Loading...
Searching...
No Matches
LayerList.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <vulkan/vulkan.hpp>
4
5#include "LayerNode.hpp"
6#include "SD/arena.hpp"
8#include "SD/core/type_id.hpp"
9#include "SD/export.hpp"
10
11namespace sd {
12
13struct Layer;
14
17 m_arena = arena_alloc(ArenaParams{.reserve_size = kb(64uz), .name = "LayerListArena"});
18 }
20 if (m_arena)
21 arena_release(m_arena);
22 }
23 LayerList(const LayerList&) = delete;
24 LayerList& operator=(const LayerList&) = delete;
25 LayerList(LayerList&& other) noexcept : m_arena(other.m_arena), m_layers(other.m_layers) {
26 other.m_arena = nullptr;
27 other.m_layers.clear();
28 }
30 if (this != &other) {
31 if (m_arena)
32 arena_release(m_arena);
33 m_arena = other.m_arena;
34 m_layers = other.m_layers;
35 other.m_arena = nullptr;
36 other.m_layers.clear();
37 }
38 return *this;
39 }
40
42 using const_iterator = const LayerNode*;
43
44 iterator begin() { return m_layers.begin(); }
45 iterator end() { return m_layers.end(); }
46 const_iterator begin() const { return m_layers.begin(); }
47 const_iterator end() const { return m_layers.end(); }
48
49 template<typename T, typename... Args>
50 requires std::is_base_of_v<Layer, T>
51 T& push_layer(Args&&... args) {
52 T* data = m_arena->push_array<T>(1);
53 if constexpr (!std::is_trivially_constructible_v<T, Args...>)
54 new (data) T(std::forward<Args>(args)...);
55
57 node.data = data;
58 node.type_id = type_id_of<T>();
59 node.is_active = data->is_active;
60 node.debug_name = data->debug_name;
61 node.scene = data->scene;
62 node.app = data->app;
63 node.stage_id = data->stage_id;
64 node.view_id = data->view_id;
65 node.view = data->view;
66
68
69 m_layers.push(m_arena, node);
70 node.on_attach();
71 return *data;
72 }
73
74 template<typename T, typename... Args>
75 requires std::is_base_of_v<Layer, T>
77 T* data = m_arena->push_array<T>(1);
78 if constexpr (!std::is_trivially_constructible_v<T, Args...>)
79 new (data) T(std::forward<Args>(args)...);
80
82 node.data = data;
83 node.type_id = type_id_of<T>();
84 node.is_active = data->is_active;
85 node.debug_name = data->debug_name;
86 node.scene = data->scene;
87 node.app = data->app;
88 node.stage_id = data->stage_id;
89 node.view_id = data->view_id;
90 node.view = data->view;
91
93
94 m_layers.push(m_arena, node);
95 node.on_attach();
96 return *data;
97 }
98
99 template<typename T>
100 requires std::is_base_of_v<Layer, T>
101 T* Get() {
103 for (U64 i = 0; i < m_layers.count; ++i) {
104 if (m_layers[i].type_id == tid)
105 return static_cast<T*>(m_layers[i].data);
106 }
107 return nullptr;
108 }
109
110 template<typename T>
111 requires std::is_base_of_v<Layer, T>
114 for (I64 i = static_cast<I64>(m_layers.count) - 1; i >= 0; --i) {
115 if (m_layers[i].type_id == tid) {
116 m_layers[i].on_detach();
117 T* result = static_cast<T*>(m_layers[i].data);
118 for (U64 j = static_cast<U64>(i); j < m_layers.count - 1; ++j)
119 m_layers[j] = m_layers[j + 1];
120 m_layers.count--;
121 return result;
122 }
123 }
124 return nullptr;
125 }
126
127 void clear() {
128 for (I64 i = static_cast<I64>(m_layers.count) - 1; i >= 0; --i) {
129 m_layers[i].on_detach();
130 if (m_layers[i].destroy_fn)
131 m_layers[i].destroy_fn(m_layers[i].data);
132 }
133 m_layers.clear();
134 if (m_arena)
135 m_arena->clear();
136 }
137
138 void update(float dt) const {
139 for (U64 i = 0; i < m_layers.count; ++i) {
140 if (m_layers[i].is_active)
141 m_layers[i].on_update(dt);
142 }
143 }
144
145 void on_fixed_update(double dt) const {
146 for (U64 i = 0; i < m_layers.count; ++i) {
147 if (m_layers[i].is_active)
148 m_layers[i].on_fixed_update(dt);
149 }
150 }
151
152 void gui_render() const {
153 for (U64 i = 0; i < m_layers.count; ++i) {
154 if (m_layers[i].is_active)
155 m_layers[i].on_gui_render();
156 }
157 }
158
159 void on_imGui_menu_bar() const {
160 for (U64 i = 0; i < m_layers.count; ++i) {
161 if (m_layers[i].is_active)
162 m_layers[i].on_im_gui_menu_bar();
163 }
164 }
165
167 for (I64 i = static_cast<I64>(m_layers.count) - 1; i >= 0; --i) {
168 if (m_layers[i].is_active) {
169 m_layers[i].on_event(e);
170 if (e.handled)
171 break;
172 }
173 }
174 }
175
177 for (U64 i = 0; i < m_layers.count; ++i) {
178 if (m_layers[i].is_active)
179 m_layers[i].on_swapchain_recreated();
180 }
181 }
182
183 void on_shader_reload() const {
184 for (U64 i = 0; i < m_layers.count; ++i) {
185 if (m_layers[i].is_active)
186 m_layers[i].on_shader_reload();
187 }
188 }
189
190 void on_render(vk::CommandBuffer cmd) const {
191 for (U64 i = 0; i < m_layers.count; ++i) {
192 if (m_layers[i].is_active)
193 m_layers[i].on_render(cmd);
194 }
195 }
196
197 U64 size() const { return m_layers.count; }
198
199
200 template<typename T>
202 if constexpr (!std::is_trivially_destructible_v<T>)
203 node.destroy_fn = [](void* d) {
204 static_cast<T*>(d)->~T();
205 };
206 if constexpr (requires(T& t, EventVariant& e) { t.on_event(e); })
207 node.on_event_fn = [](void* d, EventVariant& e) {
208 static_cast<T*>(d)->on_event(e);
209 };
210 if constexpr (requires(T& t, float dt) { t.on_update(dt); })
211 node.on_update_fn = [](void* d, float dt) {
212 static_cast<T*>(d)->on_update(dt);
213 };
214 if constexpr (requires(T& t, double dt) { t.on_fixed_update(dt); })
215 node.on_fixed_update_fn = [](void* d, double dt) {
216 static_cast<T*>(d)->on_fixed_update(dt);
217 };
218 if constexpr (requires(T& t, vk::CommandBuffer cmd) { t.on_render(cmd); })
219 node.on_render_fn = [](void* d, vk::CommandBuffer cmd) {
220 static_cast<T*>(d)->on_render(cmd);
221 };
222 if constexpr (requires(T& t) { t.on_gui_render(); })
223 node.on_gui_render_fn = [](void* d) {
224 static_cast<T*>(d)->on_gui_render();
225 };
226 if constexpr (requires(T& t) { t.on_im_gui_menu_bar(); })
227 node.on_im_gui_menu_bar_fn = [](void* d) {
228 static_cast<T*>(d)->on_im_gui_menu_bar();
229 };
230 if constexpr (requires(T& t) { t.on_attach(); })
231 node.on_attach_fn = [](void* d) {
232 static_cast<T*>(d)->on_attach();
233 };
234 if constexpr (requires(T& t) { t.on_detach(); })
235 node.on_detach_fn = [](void* d) {
236 static_cast<T*>(d)->on_detach();
237 };
238 if constexpr (requires(T& t) { t.on_activate(); })
239 node.on_activate_fn = [](void* d) {
240 static_cast<T*>(d)->on_activate();
241 };
242 if constexpr (requires(T& t) { t.on_deactivate(); })
243 node.on_deactivate_fn = [](void* d) {
244 static_cast<T*>(d)->on_deactivate();
245 };
246 if constexpr (requires(T& t) { t.on_swapchain_recreated(); })
247 node.on_swapchain_recreated_fn = [](void* d) {
248 static_cast<T*>(d)->on_swapchain_recreated();
249 };
250 if constexpr (requires(T& t) { t.on_shader_reload(); })
251 node.on_shader_reload_fn = [](void* d) {
252 static_cast<T*>(d)->on_shader_reload();
253 };
254 }
255
256 Arena* m_arena = nullptr;
258};
259
260} // namespace sd
SD_EXPORT Arena * arena_alloc(ArenaParams params={})
Definition arena.cpp:91
consteval U64 kb(U64 n)
Definition arena.hpp:16
SD_EXPORT void arena_release(Arena *arena)
Definition arena.cpp:142
Definition Application.hpp:22
Definition arena.hpp:67
U64 reserve_size
Definition arena.hpp:69
Definition arena_vec.hpp:6
Definition arena.hpp:85
Definition EventVariant.hpp:19
Definition LayerList.hpp:15
void on_swapchain_recreated() const
Definition LayerList.hpp:176
LayerList(LayerList &&other) noexcept
Definition LayerList.hpp:25
void on_fixed_update(double dt) const
Definition LayerList.hpp:145
iterator begin()
Definition LayerList.hpp:44
iterator end()
Definition LayerList.hpp:45
LayerList & operator=(const LayerList &)=delete
void update(float dt) const
Definition LayerList.hpp:138
void on_event(EventVariant &e)
Definition LayerList.hpp:166
T & push_layer(Args &&... args)
Definition LayerList.hpp:51
T * pop_layer()
Definition LayerList.hpp:112
T * Get()
Definition LayerList.hpp:101
LayerList(const LayerList &)=delete
void gui_render() const
Definition LayerList.hpp:152
LayerList & operator=(LayerList &&other) noexcept
Definition LayerList.hpp:29
LayerList()
Definition LayerList.hpp:16
void on_shader_reload() const
Definition LayerList.hpp:183
void on_imGui_menu_bar() const
Definition LayerList.hpp:159
ArenaVec< LayerNode > m_layers
Definition LayerList.hpp:257
T & push_bottom(Args &&... args)
Definition LayerList.hpp:76
void on_render(vk::CommandBuffer cmd) const
Definition LayerList.hpp:190
U64 size() const
Definition LayerList.hpp:197
static void setup_fn_ptrs(LayerNode &node)
Definition LayerList.hpp:201
const_iterator begin() const
Definition LayerList.hpp:46
const_iterator end() const
Definition LayerList.hpp:47
~LayerList()
Definition LayerList.hpp:19
void clear()
Definition LayerList.hpp:127
Definition LayerNode.hpp:15
void * data
Definition LayerNode.hpp:16
consteval U64 type_id_of()
Definition type_id.hpp:6
std::uint64_t U64
Definition types.hpp:16
std::int64_t I64
Definition types.hpp:11