Rendering Engine 0.2.9
Modular Graphics Rendering Engine | v0.2.9
scene.cpp
Go to the documentation of this file.
1#include "scene.hpp"
2#include "scene_manager.hpp"
3#include "camera_2d.hpp"
4#include "camera.hpp"
5#include "drawable_2d.hpp"
6#include "drawable_3d.hpp"
8#include "actor.hpp"
9#include "actor_2d.hpp"
10#include "logger.hpp"
11#include "utility.hpp"
12#include "stats_overlay.hpp"
13#include "i_application.hpp"
14
15namespace rendering_engine
16{
17
18
20 :
21 mSceneManager(sceneManager),
22 mStatsOverlay(nullptr)
23{}
24
26{
27 mActiveCamera3D = std::make_shared<Camera>(*mSceneManager.GetApplication());
28 mActiveCamera3D->Initialize();
29
30 mActiveCamera2D = std::make_shared<Camera2D>(*mSceneManager.GetApplication());
31 mActiveCamera2D->Initialize();
32
34 if (appConfig.showStatsOverlay)
35 {
36 mStatsOverlay = SpawnActor2D<StatsOverlay>();
37 const float screenWidth = mSceneManager.GetApplication()->GetScreenSettings().width;
38 const float screenHeight = mSceneManager.GetApplication()->GetScreenSettings().height;
39 const float x = screenWidth / 2 - 200.0f;
40 const float y = -screenHeight / 2 + 130.0f;
41 mStatsOverlay->SetPosition(glm::vec2(x, y));
42 }
43}
44
45void Scene::Update(float deltaTime)
46{
47 // Update cameras
48 if (mActiveCamera2D)
49 {
50 mActiveCamera2D->Update(deltaTime);
51 }
52 if (mActiveCamera3D)
53 {
54 // TO DO - uncomment
55 //mActiveCamera3D->Update(deltaTime);
56 }
57
58 for (auto& actor : mActors)
59 {
60 if (!actor->IsPendingDestroy())
61 {
62 actor->Update(deltaTime);
63 }
64 }
65 for (auto& actor2D : mActors2D)
66 {
67 if (!actor2D->IsPendingDestroy())
68 {
69 actor2D->Update(deltaTime);
70 }
71 }
72
73 for (auto& drawable2D : mDrawables2D)
74 {
75 drawable2D->Update(deltaTime);
76 }
77 for (auto& drawable3D : mDrawables3D)
78 {
79 drawable3D->Update(deltaTime);
80 }
81
82 FlushDestroyed();
83}
84
86{
87 if (mActiveCamera3D)
88 {
89 for (auto& drawable3D : mDrawables3D)
90 {
91 drawable3D->Draw(*mActiveCamera3D);
92 }
93 }
94
95 if (mActiveCamera2D)
96 {
97 for (auto& drawable2D : mDrawables2D)
98 {
99 drawable2D->Draw(*mActiveCamera2D);
100 }
101 }
102}
103
105{
106 // Finish any queued destroys first (so we don't double-delete later)
107 FlushDestroyed();
108
109 // Destroy drawables FIRST (they may reference actor transforms as parents)
110 for (auto* d : mDrawables2D) { if (d) { d->Shutdown(); delete d; } }
111 mDrawables2D.clear();
112
113 for (auto* d : mDrawables3D) { if (d) { d->Shutdown(); delete d; } }
114 mDrawables3D.clear();
115
116 // Now destroy actors
117 for (auto* a : mActors) { if (a) { a->Shutdown(); delete a; } }
118 mActors.clear();
119
120 for (auto* a : mActors2D) { if (a) { a->Shutdown(); delete a; } }
121 mActors2D.clear();
122
123 mPendingDestroy2D.clear();
124 mPendingDestroy3D.clear();
125 mPendingDestroyActors.clear();
126 mPendingDestroyActors2D.clear();
127}
128
130{
131 return mSceneManager;
132}
133
134void Scene::LoadScene(std::string newSceneName)
135{
136 mSceneManager.LoadScene(newSceneName);
137}
138
139std::shared_ptr<Camera> Scene::GetActiveCamera3D()
140{
141 return mActiveCamera3D;
142}
143
144std::shared_ptr<Camera2D> Scene::GetActiveCamera2D()
145{
146 return mActiveCamera2D;
147}
148
150{
151 mPendingDestroy3D.push_back(drawable3D);
152}
153
155{
156 mPendingDestroy2D.push_back(drawable2D);
157}
158
160{
161 mPendingDestroyActors.push_back(actor);
162}
163
165{
166 mPendingDestroyActors2D.push_back(actor2D);
167}
168
169void Scene::FlushDestroyed()
170{
171 for (auto* d : mPendingDestroy3D)
172 {
173 auto it = std::find(mDrawables3D.begin(), mDrawables3D.end(), d);
174 if (it != mDrawables3D.end())
175 {
176 (*it)->Shutdown();
177 delete* it;
178 mDrawables3D.erase(it);
179 }
180 }
181 mPendingDestroy3D.clear();
182
183 for (auto* d : mPendingDestroy2D)
184 {
185 auto it = std::find(mDrawables2D.begin(), mDrawables2D.end(), d);
186 if (it != mDrawables2D.end())
187 {
188 (*it)->Shutdown();
189 delete* it;
190 mDrawables2D.erase(it);
191 }
192 }
193 mPendingDestroy2D.clear();
194
195 for (auto* actor : mPendingDestroyActors)
196 {
197 auto it = std::find(mActors.begin(), mActors.end(), actor);
198 if (it != mActors.end())
199 {
200 (*it)->Shutdown();
201 delete* it;
202 mActors.erase(it);
203 }
204 }
205 mPendingDestroyActors.clear();
206
207 for (auto* actor2D : mPendingDestroyActors2D)
208 {
209 auto it = std::find(mActors2D.begin(), mActors2D.end(), actor2D);
210 if (it != mActors2D.end())
211 {
212 (*it)->Shutdown();
213 delete* it;
214 mActors2D.erase(it);
215 }
216 }
217 mPendingDestroyActors2D.clear();
218}
219
220void Scene::SetBackgroundColor(glm::vec3 color)
221{
222 mSceneManager.GetRenderResourceContext().renderer->SetDefaultColor(color.r, color.g, color.b);
223}
224
225} // namespace rendering_engine
Base class representing a 2D entity within a Scene.
Definition: actor_2d.hpp:46
void SetPosition(const glm::vec2 &position)
Sets the actor's position in world space.
Definition: actor_2d.cpp:24
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
virtual ScreenSettings GetScreenSettings() const =0
Retrieves the current screen or window settings.
virtual void SetDefaultColor(float r, float g, float b)=0
Sets the renderer clear color.
Manages scenes, resource caches, and scene transitions within the rendering engine.
IApplication * GetApplication()
Returns the application associated with this SceneManager.
void LoadScene(std::string sceneName)
Loads a new scene by name.
RenderResourceContext GetRenderResourceContext() const
Retrieves the current RenderResourceContext.
void SetBackgroundColor(glm::vec3 color)
Sets the scene background clear color.
Definition: scene.cpp:220
virtual void Shutdown()
Releases scene-specific resources and prepares for shutdown.
Definition: scene.cpp:104
std::shared_ptr< Camera2D > GetActiveCamera2D()
Returns the active 2D camera of the scene.
Definition: scene.cpp:144
virtual void Update(float deltaTime)
Updates all objects within the scene.
Definition: scene.cpp:45
void DestroyDrawable3D(Drawable3D *drawable3D)
Schedules a 3D drawable for deferred destruction.
Definition: scene.cpp:149
void LoadScene(std::string newSceneName)
Requests to load another scene.
Definition: scene.cpp:134
SceneManager & GetSceneManager()
Gets a reference to the SceneManager that owns this scene.
Definition: scene.cpp:129
void DestroyActor(Actor *actor)
Schedules a 3D actor for deferred destruction.
Definition: scene.cpp:159
std::shared_ptr< Camera > GetActiveCamera3D()
Returns the active 3D camera of the scene.
Definition: scene.cpp:139
void DestroyDrawable2D(Drawable2D *drawable2D)
Schedules a 2D drawable for deferred destruction.
Definition: scene.cpp:154
virtual void Initialize()
Initializes scene resources and drawables.
Definition: scene.cpp:25
Scene(SceneManager &sceneManager)
Constructs a Scene instance associated with a SceneManager.
Definition: scene.cpp:19
virtual void Draw()
Renders all 3D and 2D drawables in the scene.
Definition: scene.cpp:85
static AppConfig ReadConfigFile()
Reads application settings from the JSON config file.
Definition: utility.cpp:34
Engine-wide logging system for runtime diagnostics and performance tracking.
Basic application settings loaded from a configuration file.
Definition: utility.hpp:27
bool showStatsOverlay
Enable on-screen statistics overlay.
Definition: utility.hpp:47
unsigned int width
Screen or window width in pixels.
unsigned int height
Screen or window height in pixels.