Rendering Engine 0.2.9
Modular Graphics Rendering Engine | v0.2.9
rendering_engine::Scene Class Reference

Base class representing a renderable scene. More...

#include <scene.hpp>

Public Member Functions

 Scene (SceneManager &sceneManager)
 Constructs a Scene instance associated with a SceneManager. More...
 
 ~Scene ()=default
 Virtual destructor for safe polymorphic destruction. More...
 
virtual void Initialize ()
 Initializes scene resources and drawables. More...
 
virtual void Update (float deltaTime)
 Updates all objects within the scene. More...
 
virtual void Draw ()
 Renders all 3D and 2D drawables in the scene. More...
 
virtual void Shutdown ()
 Releases scene-specific resources and prepares for shutdown. More...
 
SceneManagerGetSceneManager ()
 Gets a reference to the SceneManager that owns this scene. More...
 
void LoadScene (std::string newSceneName)
 Requests to load another scene. More...
 
std::shared_ptr< CameraGetActiveCamera3D ()
 Returns the active 3D camera of the scene. More...
 
std::shared_ptr< Camera2DGetActiveCamera2D ()
 Returns the active 2D camera of the scene. More...
 
template<typename T , typename V >
T * Spawn (V arg)
 Spawns and registers a drawable of type T. More...
 
template<typename T >
T * SpawnActor ()
 Spawns and registers a 3D actor of type T. More...
 
template<typename T >
T * SpawnActor2D ()
 Spawns and registers a 2D actor of type T. More...
 
template<>
StaticMeshSpawn (StaticMeshParams param)
 
template<>
StaticMeshSpawn (StaticMeshParams param)
 

Protected Member Functions

void DestroyDrawable3D (Drawable3D *drawable3D)
 Schedules a 3D drawable for deferred destruction. More...
 
void DestroyDrawable2D (Drawable2D *drawable2D)
 Schedules a 2D drawable for deferred destruction. More...
 
void DestroyActor (Actor *actor)
 Schedules a 3D actor for deferred destruction. More...
 
void DestroyActor (Actor2D *actor2D)
 Schedules a 2D actor for deferred destruction. More...
 
void SetBackgroundColor (glm::vec3 color)
 Sets the scene background clear color. More...
 

Friends

class Drawable3D
 
class Drawable2D
 
class Actor
 
class Actor2D
 

Detailed Description

Base class representing a renderable scene.

A Scene acts as a container for drawable objects (2D and 3D) and defines the update and draw lifecycle of a game or rendering context.

Derived scene classes should override Initialize(), Update(), and Draw() to implement specific logic and populate the drawable lists.

Note
Scenes are created and managed by SceneManager.
See also
SceneManager, Drawable3D, Drawable2D, Camera, Camera2D

The Scene implements a deferred destruction mechanism to guarantee safe removal of actors and drawables during update and render passes. All objects spawned via the Scene are owned and lifetime-managed by it.

Definition at line 43 of file scene.hpp.

Constructor & Destructor Documentation

◆ Scene()

rendering_engine::Scene::Scene ( SceneManager sceneManager)

Constructs a Scene instance associated with a SceneManager.

Parameters
sceneManagerReference to the SceneManager that owns this Scene.

Definition at line 19 of file scene.cpp.

20 :
21 mSceneManager(sceneManager),
22 mStatsOverlay(nullptr)
23{}

◆ ~Scene()

rendering_engine::Scene::~Scene ( )
default

Virtual destructor for safe polymorphic destruction.

Member Function Documentation

◆ DestroyActor() [1/2]

void rendering_engine::Scene::DestroyActor ( Actor actor)
protected

Schedules a 3D actor for deferred destruction.

The actor is not destroyed immediately. Instead, it is placed into a pending destruction queue and safely removed during the next Scene update cycle.

This ensures that destruction does not invalidate iterators or interfere with the current update/draw pass.

Parameters
actorPointer to the actor to destroy.
Note
Do not delete actors manually. Always use Actor::Destroy().

Definition at line 159 of file scene.cpp.

160{
161 mPendingDestroyActors.push_back(actor);
162}

◆ DestroyActor() [2/2]

void rendering_engine::Scene::DestroyActor ( Actor2D actor2D)
protected

Schedules a 2D actor for deferred destruction.

Mirrors DestroyActor(Actor*) but operates on the 2D actor list. The actor will be safely shut down and deleted during the next flush phase.

Parameters
actor2DPointer to the 2D actor to destroy.
Note
Do not delete actors manually. Always use Actor2D::Destroy().

Definition at line 164 of file scene.cpp.

165{
166 mPendingDestroyActors2D.push_back(actor2D);
167}

◆ DestroyDrawable2D()

void rendering_engine::Scene::DestroyDrawable2D ( Drawable2D drawable2D)
protected

Schedules a 2D drawable for deferred destruction.

The drawable will be shut down and removed safely during the next Scene flush cycle.

Parameters
drawable2DPointer to the drawable to destroy.

Definition at line 154 of file scene.cpp.

155{
156 mPendingDestroy2D.push_back(drawable2D);
157}

◆ DestroyDrawable3D()

void rendering_engine::Scene::DestroyDrawable3D ( Drawable3D drawable3D)
protected

Schedules a 3D drawable for deferred destruction.

The drawable will be shut down and removed safely during the next Scene flush cycle.

Parameters
drawable3DPointer to the drawable to destroy.

Definition at line 149 of file scene.cpp.

150{
151 mPendingDestroy3D.push_back(drawable3D);
152}

◆ Draw()

void rendering_engine::Scene::Draw ( )
virtual

Renders all 3D and 2D drawables in the scene.

The base implementation can iterate through mDrawables3D and mDrawables2D to call each object�s Draw() method, using the active cameras.

Definition at line 85 of file scene.cpp.

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}

◆ GetActiveCamera2D()

std::shared_ptr< Camera2D > rendering_engine::Scene::GetActiveCamera2D ( )

Returns the active 2D camera of the scene.

Returns
Active 2D camera.

Definition at line 144 of file scene.cpp.

145{
146 return mActiveCamera2D;
147}

◆ GetActiveCamera3D()

std::shared_ptr< Camera > rendering_engine::Scene::GetActiveCamera3D ( )

Returns the active 3D camera of the scene.

Returns
Active 3D camera.

Definition at line 139 of file scene.cpp.

140{
141 return mActiveCamera3D;
142}

◆ GetSceneManager()

SceneManager & rendering_engine::Scene::GetSceneManager ( )

Gets a reference to the SceneManager that owns this scene.

Returns
Reference to the owning SceneManager.

Definition at line 129 of file scene.cpp.

130{
131 return mSceneManager;
132}

◆ Initialize()

void rendering_engine::Scene::Initialize ( )
virtual

Initializes scene resources and drawables.

Called once when the scene becomes active. Derived classes should override this to load assets and create objects.

Definition at line 25 of file scene.cpp.

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
33 AppConfig appConfig = Utility::ReadConfigFile();
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}
void SetPosition(const glm::vec2 &position)
Sets the actor's position in world space.
Definition: actor_2d.cpp:24
virtual ScreenSettings GetScreenSettings() const =0
Retrieves the current screen or window settings.
IApplication * GetApplication()
Returns the application associated with this SceneManager.
static AppConfig ReadConfigFile()
Reads application settings from the JSON config file.
Definition: utility.cpp:34
unsigned int width
Screen or window width in pixels.
unsigned int height
Screen or window height in pixels.

◆ LoadScene()

void rendering_engine::Scene::LoadScene ( std::string  newSceneName)

Requests to load another scene.

Parameters
newSceneNameThe name of the registered scene to load next.

This function delegates to SceneManager::LoadScene().

Definition at line 134 of file scene.cpp.

135{
136 mSceneManager.LoadScene(newSceneName);
137}
void LoadScene(std::string sceneName)
Loads a new scene by name.

◆ SetBackgroundColor()

void rendering_engine::Scene::SetBackgroundColor ( glm::vec3  color)
protected

Sets the scene background clear color.

Parameters
colorLinear-space RGB color.

Definition at line 220 of file scene.cpp.

221{
222 mSceneManager.GetRenderResourceContext().renderer->SetDefaultColor(color.r, color.g, color.b);
223}
virtual void SetDefaultColor(float r, float g, float b)=0
Sets the renderer clear color.
RenderResourceContext GetRenderResourceContext() const
Retrieves the current RenderResourceContext.

◆ Shutdown()

void rendering_engine::Scene::Shutdown ( )
virtual

Releases scene-specific resources and prepares for shutdown.

Called when the scene is being unloaded or replaced.

Definition at line 104 of file scene.cpp.

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}

◆ Spawn() [1/3]

template<>
StaticMesh * rendering_engine::Scene::Spawn ( StaticMeshParams  param)

◆ Spawn() [2/3]

template<>
StaticMesh * rendering_engine::Scene::Spawn ( StaticMeshParams  param)

Definition at line 41 of file spawn_drawables.hpp.

42{
43 mDrawables3D.push_back(new StaticMesh(mSceneManager.GetRenderResourceContext(), *this, param));
44 StaticMesh* staticMesh = static_cast<StaticMesh*>(mDrawables3D.back());
45 staticMesh->Initialize();
46 return staticMesh;
47}

◆ Spawn() [3/3]

template<typename T , typename V >
T * rendering_engine::Scene::Spawn ( arg)

Spawns and registers a drawable of type T.

All drawables must be created via this function to ensure correct Scene-level ownership and lifecycle management. Creation logic is provided by explicit template specializations.

Template Parameters
TDrawable type.
VConstruction argument type.
Parameters
argConstruction argument.
Returns
Pointer to the created drawable.

◆ SpawnActor()

template<typename T >
T * rendering_engine::Scene::SpawnActor
inline

Spawns and registers a 3D actor of type T.

The actor is:

  • Allocated and owned by the Scene.
  • Stored internally in the 3D actor list.
  • Automatically initialized via Initialize().

The Scene manages the full lifecycle of the actor, including deferred destruction and Shutdown() invocation.

Template Parameters
TType deriving from Actor.
Returns
Pointer to the created actor instance.
Note
The returned pointer is owned by the Scene. Do not manually delete the actor.
See also
DestroyActor(), SpawnActor2D()

Definition at line 262 of file scene.hpp.

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}

◆ SpawnActor2D()

template<typename T >
T * rendering_engine::Scene::SpawnActor2D
inline

Spawns and registers a 2D actor of type T.

The actor is:

  • Allocated and owned by the Scene.
  • Stored internally in the 2D actor list.
  • Automatically initialized via Initialize().

This function mirrors SpawnActor() but operates in the 2D domain. 2D actors use SceneComponent2D for hierarchical transformations.

Template Parameters
TType deriving from Actor2D.
Returns
Pointer to the created 2D actor instance.
Note
The returned pointer is owned by the Scene. Do not manually delete the actor.
See also
SpawnActor(), DestroyActor(Actor2D*)

Definition at line 275 of file scene.hpp.

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}

◆ Update()

void rendering_engine::Scene::Update ( float  deltaTime)
virtual

Updates all objects within the scene.

Parameters
deltaTimeTime elapsed since the last frame (in milliseconds).

Called once per frame. Override to implement per-frame logic such as animation, physics updates, or scene transitions.

Definition at line 45 of file scene.cpp.

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}

Friends And Related Function Documentation

◆ Actor

friend class Actor
friend

Definition at line 47 of file scene.hpp.

◆ Actor2D

friend class Actor2D
friend

Definition at line 48 of file scene.hpp.

◆ Drawable2D

friend class Drawable2D
friend

Definition at line 46 of file scene.hpp.

◆ Drawable3D

friend class Drawable3D
friend

Definition at line 45 of file scene.hpp.


The documentation for this class was generated from the following files: