Rendering Engine 0.2.9
Modular Graphics Rendering Engine | v0.2.9
scene.hpp
Go to the documentation of this file.
1// This file is part of the Rendering Engine project.
2// Author: Alexander Obzherin <alexanderobzherin@gmail.com>
3// Copyright (c) 2026 Alexander Obzherin
4// Distributed under the terms of the zlib License. See LICENSE.md for details.
5
6#pragma once
7
8#include <memory>
9#include <vector>
10#include <string>
11
14
15namespace rendering_engine
16{
17class SceneManager;
18class Camera;
19class Camera2D;
20class Drawable3D;
21class Drawable2D;
22class Actor;
23class Actor2D;
24class StatsOverlay;
25
26/**
27 * @class Scene
28 * @brief Base class representing a renderable scene.
29 *
30 * A Scene acts as a container for drawable objects (2D and 3D) and defines
31 * the update and draw lifecycle of a game or rendering context.
32 *
33 * Derived scene classes should override Initialize(), Update(), and Draw()
34 * to implement specific logic and populate the drawable lists.
35 *
36 * @note Scenes are created and managed by SceneManager.
37 * @see SceneManager, Drawable3D, Drawable2D, Camera, Camera2D
38 *
39 * The Scene implements a deferred destruction mechanism to guarantee
40 * safe removal of actors and drawables during update and render passes.
41 * All objects spawned via the Scene are owned and lifetime-managed by it.
42 */
44{
45 friend class Drawable3D;
46 friend class Drawable2D;
47 friend class Actor;
48 friend class Actor2D;
49public:
50 /**
51 * @brief Constructs a Scene instance associated with a SceneManager.
52 * @param sceneManager Reference to the SceneManager that owns this Scene.
53 */
54 Scene(SceneManager& sceneManager);
55 /**
56 * @brief Virtual destructor for safe polymorphic destruction.
57 */
58 ~Scene() = default;
59 /**
60 * @brief Initializes scene resources and drawables.
61 *
62 * Called once when the scene becomes active.
63 * Derived classes should override this to load assets and create objects.
64 */
65 virtual void Initialize();
66 /**
67 * @brief Updates all objects within the scene.
68 * @param deltaTime Time elapsed since the last frame (in milliseconds).
69 *
70 * Called once per frame. Override to implement per-frame logic such as
71 * animation, physics updates, or scene transitions.
72 */
73 virtual void Update(float deltaTime);
74 /**
75 * @brief Renders all 3D and 2D drawables in the scene.
76 *
77 * The base implementation can iterate through mDrawables3D and mDrawables2D
78 * to call each object�s Draw() method, using the active cameras.
79 */
80 virtual void Draw();
81 /**
82 * @brief Releases scene-specific resources and prepares for shutdown.
83 *
84 * Called when the scene is being unloaded or replaced.
85 */
86 virtual void Shutdown();
87 /**
88 * @brief Gets a reference to the SceneManager that owns this scene.
89 * @return Reference to the owning SceneManager.
90 */
91 SceneManager& GetSceneManager();
92 /**
93 * @brief Requests to load another scene.
94 * @param newSceneName The name of the registered scene to load next.
95 *
96 * This function delegates to SceneManager::LoadScene().
97 */
98 void LoadScene(std::string newSceneName);
99
100 /**
101 * @brief Returns the active 3D camera of the scene.
102 * @return Active 3D camera.
103 */
104 std::shared_ptr<Camera> GetActiveCamera3D();
105
106 /**
107 * @brief Returns the active 2D camera of the scene.
108 * @return Active 2D camera.
109 */
110 std::shared_ptr<Camera2D> GetActiveCamera2D();
111
112 /**
113 * @brief Spawns and registers a drawable of type T.
114 *
115 * All drawables must be created via this function to ensure
116 * correct Scene-level ownership and lifecycle management.
117 * Creation logic is provided by explicit template specializations.
118 *
119 * @tparam T Drawable type.
120 * @tparam V Construction argument type.
121 * @param arg Construction argument.
122 * @return Pointer to the created drawable.
123 */
124 template <typename T, typename V>
125 T* Spawn(V arg);
126
127 /**
128 * @brief Spawns and registers a 3D actor of type T.
129 *
130 * The actor is:
131 * - Allocated and owned by the Scene.
132 * - Stored internally in the 3D actor list.
133 * - Automatically initialized via Initialize().
134 *
135 * The Scene manages the full lifecycle of the actor, including
136 * deferred destruction and Shutdown() invocation.
137 *
138 * @tparam T Type deriving from Actor.
139 * @return Pointer to the created actor instance.
140 *
141 * @note The returned pointer is owned by the Scene.
142 * Do not manually delete the actor.
143 *
144 * @see DestroyActor(), SpawnActor2D()
145 */
146 template <typename T>
147 T* SpawnActor();
148
149 /**
150 * @brief Spawns and registers a 2D actor of type T.
151 *
152 * The actor is:
153 * - Allocated and owned by the Scene.
154 * - Stored internally in the 2D actor list.
155 * - Automatically initialized via Initialize().
156 *
157 * This function mirrors SpawnActor() but operates in the 2D domain.
158 * 2D actors use SceneComponent2D for hierarchical transformations.
159 *
160 * @tparam T Type deriving from Actor2D.
161 * @return Pointer to the created 2D actor instance.
162 *
163 * @note The returned pointer is owned by the Scene.
164 * Do not manually delete the actor.
165 *
166 * @see SpawnActor(), DestroyActor(Actor2D*)
167 */
168 template <typename T>
169 T* SpawnActor2D();
170
171protected:
172 /**
173 * @brief Schedules a 3D drawable for deferred destruction.
174 *
175 * The drawable will be shut down and removed safely during
176 * the next Scene flush cycle.
177 *
178 * @param drawable3D Pointer to the drawable to destroy.
179 */
180 void DestroyDrawable3D(Drawable3D* drawable3D);
181 /**
182 * @brief Schedules a 2D drawable for deferred destruction.
183 *
184 * The drawable will be shut down and removed safely during
185 * the next Scene flush cycle.
186 *
187 * @param drawable2D Pointer to the drawable to destroy.
188 */
189 void DestroyDrawable2D(Drawable2D* drawable2D);
190 /**
191 * @brief Schedules a 3D actor for deferred destruction.
192 *
193 * The actor is not destroyed immediately. Instead, it is placed into
194 * a pending destruction queue and safely removed during the next
195 * Scene update cycle.
196 *
197 * This ensures that destruction does not invalidate iterators or
198 * interfere with the current update/draw pass.
199 *
200 * @param actor Pointer to the actor to destroy.
201 *
202 * @note Do not delete actors manually. Always use Actor::Destroy().
203 */
204 void DestroyActor(Actor* actor);
205 /**
206 * @brief Schedules a 2D actor for deferred destruction.
207 *
208 * Mirrors DestroyActor(Actor*) but operates on the 2D actor list.
209 * The actor will be safely shut down and deleted during
210 * the next flush phase.
211 *
212 * @param actor2D Pointer to the 2D actor to destroy.
213 *
214 * @note Do not delete actors manually. Always use Actor2D::Destroy().
215 */
216 void DestroyActor(Actor2D * actor2D);
217 /**
218 * @brief Sets the scene background clear color.
219 *
220 * @param color Linear-space RGB color.
221 */
222 void SetBackgroundColor(glm::vec3 color);
223
224private:
225 /**
226 * @brief Processes all pending destruction queues.
227 *
228 * This method:
229 * - Shuts down and deletes pending 3D drawables
230 * - Shuts down and deletes pending 2D drawables
231 * - Shuts down and deletes pending 3D actors
232 * - Shuts down and deletes pending 2D actors
233 *
234 * Called internally during Scene::Update() and Scene::Shutdown().
235 *
236 * @note Ensures safe destruction without invalidating update/draw loops.
237 */
238 void FlushDestroyed();
239
240private:
241 SceneManager& mSceneManager;
242
243 std::shared_ptr<Camera> mActiveCamera3D;
244 std::shared_ptr<Camera2D> mActiveCamera2D;
245
246 std::vector<Drawable3D*> mDrawables3D;
247 std::vector<Drawable2D*> mDrawables2D;
248
249 std::vector<Drawable3D*> mPendingDestroy3D;
250 std::vector<Drawable2D*> mPendingDestroy2D;
251
252 std::vector<Actor*> mActors;
253 std::vector<Actor*> mPendingDestroyActors;
254
255 std::vector<Actor2D*> mActors2D;
256 std::vector<Actor2D*> mPendingDestroyActors2D;
257
258 StatsOverlay* mStatsOverlay;
259};
260
261template <typename T>
263{
264 static_assert(std::is_base_of_v<Actor, T>,
265 "SpawnActor<T>: T must derive from Actor");
266
267 mActors.push_back(new T(*this));
268 T* ptr = static_cast<T*>(mActors.back());
269
270 ptr->Initialize();
271 return ptr;
272}
273
274template <typename T>
276{
277 static_assert(std::is_base_of_v<Actor2D, T>,
278 "SpawnActor<T>: T must derive from Actor2D");
279
280 mActors2D.push_back(new T(*this));
281 T* ptr = static_cast<T*>(mActors2D.back());
282
283 ptr->Initialize();
284 return ptr;
285}
286
287} // namespace rendering_engine
Base class representing a 2D entity within a Scene.
Definition: actor_2d.hpp:46
Base class representing a 3D entity within a Scene.
Definition: actor.hpp:41
2D drawable component for rendering objects in 2D space.
Definition: drawable_2d.hpp:27
3D drawable component for rendering objects in 3D space.
Definition: drawable_3d.hpp:27
Manages scenes, resource caches, and scene transitions within the rendering engine.
Base class representing a renderable scene.
Definition: scene.hpp:44
T * SpawnActor()
Spawns and registers a 3D actor of type T.
Definition: scene.hpp:262
T * SpawnActor2D()
Spawns and registers a 2D actor of type T.
Definition: scene.hpp:275
T * Spawn(V arg)
Spawns and registers a drawable of type T.
~Scene()=default
Virtual destructor for safe polymorphic destruction.
Displays real-time frame statistics as a 2D overlay.
class RE_API Camera2D
Definition: drawable_2d.hpp:14
#define RE_API