SleakEngine 0.1.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
SceneBase.hpp
Go to the documentation of this file.
1#ifndef _SCENE_BASE_HPP_
2#define _SCENE_BASE_HPP_
3
4#include <string>
5#include <Memory/RefPtr.hpp>
6#include <Core/OSDef.hpp>
8
9
10namespace Sleak {
11
12 /// Lifecycle state a SceneBase moves through via Load/Unload/Activate/Deactivate.
13 /// @ingroup core
21
22 class GameObject;
23 class Camera;
24 class Light;
25 class LightManager;
26 class Skybox;
27 class ColliderComponent;
28
29 namespace Physics { class PhysicsWorld; }
30
31 /// Non-templated scene interface: object ownership, state machine, and
32 /// per-frame update hooks. Game scenes derive from Scene, not this
33 /// directly.
34 ///
35 /// SceneBase defines what every scene can do regardless of subclass:
36 /// hold objects, move through the SceneState machine, and answer
37 /// queries. GameBase stores scenes by `SceneBase*`, so this is the type
38 /// you see in the scene registry API.
39 ///
40 /// Ownership is strict. AddObject() transfers ownership to the scene.
41 /// RemoveObject() unregisters and deletes immediately, which is unsafe
42 /// from inside that object's own update. DestroyObject() queues the
43 /// object (and its children) for deletion at the end of the frame,
44 /// which is the safe choice while iterating.
45 ///
46 /// Children of an added object that were never added themselves are
47 /// owned by their parent and belong to the scene through it. Lights and
48 /// colliders anywhere in that hierarchy are registered with the
49 /// LightManager and PhysicsWorld, and the queries below search it too.
50 ///
51 /// @code{.cpp}
52 /// // Destroy safely from inside an update
53 /// if (auto* target = FindObjectByName("Crate")) {
54 /// DestroyObject(target);
55 /// }
56 ///
57 /// // Act on a group
58 /// Sleak::List<Sleak::GameObject*> enemies = FindObjectsByTag("Enemy");
59 /// for (size_t i = 0; i < enemies.GetSize(); ++i) {
60 /// enemies[i]->SetActive(false);
61 /// }
62 ///
63 /// // Scene-wide services
64 /// if (auto* lm = GetLightManager()) lm->SetAmbientIntensity(0.6f);
65 /// if (auto* pw = GetPhysicsWorld()) {
66 /// auto hit = pw->Raycast(origin, direction, 100.0f);
67 /// }
68 /// @endcode
69 ///
70 /// @see Scene, GameBase, GameObject, SceneState, Physics::PhysicsWorld
71 /// @ingroup core
72 class ENGINE_API SceneBase {
73 public:
74 explicit SceneBase(const std::string& name)
76 bInitialized(false), bActive(false) {}
77
78 virtual ~SceneBase();
79
80 // Resource lifecycle hooks
81 /// Override to load scene-specific assets; called once from Load().
82 virtual void OnLoad() {}
83 /// Override to release scene-specific assets; called from Unload().
84 virtual void OnUnload() {}
85
86 // Activation lifecycle hooks
87 /// Override for logic that should run when the scene becomes active.
88 virtual void OnActivate() {}
89 /// Override for logic that should run when the scene stops being active.
90 virtual void OnDeactivate() {}
91
92 // Initialization
93 /// Runs once before the scene's first Begin()/Update().
94 virtual bool Initialize();
95 /// Activates every owned object; runs once per Load().
96 virtual void Begin() = 0;
97
98 // Update loops
99 /// Advances all active, root-level objects by deltaTime, then steps lighting and physics.
100 virtual void Update(float deltaTime) = 0;
101 /// Advances all active, root-level objects on the fixed timestep.
102 virtual void FixedUpdate(float fixedDeltaTime);
103 /// Advances all active, root-level objects after the main Update pass.
104 virtual void LateUpdate(float deltaTime);
105
106 // State
107 const std::string& GetName() const { return name; }
108 SceneState GetState() const { return state; }
109 bool IsActive() const { return bActive; }
110 bool IsLoaded() const { return state != SceneState::Unloaded; }
111
112 // Scene state transitions
113 /// Moves Unloaded -> Loading -> Active, calling OnLoad() and Initialize().
114 void Load();
115 /// Deactivates if active, calls OnUnload(), and destroys all owned objects.
116 void Unload();
117 /// Marks the scene active, calls OnActivate(), then Begin() once.
118 void Activate();
119 /// Marks the scene inactive and calls OnDeactivate().
120 void Deactivate();
121 /// Freezes the scene without tearing it down; leaves it in the Paused state.
122 void Pause();
123 /// Reactivates a paused scene without re-running OnActivate().
124 void Resume();
125
126 // Object management — scene takes ownership of added objects
127 /// Takes ownership of object, registering it with lighting and physics as needed.
128 virtual void AddObject(GameObject* object);
129 /// Unregisters and deletes object and its whole hierarchy immediately.
130 /// Cancels a queued DestroyObject() on anything it deletes.
131 virtual void RemoveObject(GameObject* object);
132 /// Queues an object for destruction; actually freed on the next ProcessPendingDestroy().
133 void DestroyObject(GameObject* object);
134 const List<GameObject*>& GetObjects() const { return Objects; }
135
136 // Object queries
137 /// First object with a matching name, children included.
138 GameObject* FindObjectByName(const std::string& name);
139 /// Object with a matching unique ID, children included.
140 GameObject* FindObjectByID(uint64_t id);
141 /// Collects every object whose tag matches, children included.
142 List<GameObject*> FindObjectsByTag(const std::string& tag);
143 size_t GetObjectCount() const { return Objects.GetSize(); }
144
147
149 return m_lightManager;
150 }
151
155
156 /// Replaces the scene's skybox, deleting the previous one.
157 void SetSkybox(Skybox* skybox);
158 Skybox* GetSkybox() const { return m_skybox; }
159
160 protected:
161 std::string name;
165 bool bHasBegun = false;
166
169
171
174 Skybox* m_skybox = nullptr;
175
176 /// Actually deletes objects queued by DestroyObject().
178 /// Destroys every object still owned by the scene, e.g. during Unload().
179 void DestroyAllObjects();
180 };
181
182} // namespace Sleak
183
184#endif // _SCENE_BASE_HPP_
185
186
Implements a dynamic array-like list for storing and managing a collection of elements.
Definition List.hpp:20
LightManager * GetLightManager() const
Physics::PhysicsWorld * GetPhysicsWorld() const
SceneState GetState() const
SceneBase(const std::string &name)
Definition SceneBase.hpp:74
List< GameObject * > Objects
Skybox * GetSkybox() const
bool IsLoaded() const
void ProcessPendingDestroy()
Actually deletes objects queued by DestroyObject().
virtual void OnLoad()
Override to load scene-specific assets; called once from Load().
Definition SceneBase.hpp:82
SceneState state
Camera * m_activeCamera
virtual void OnUnload()
Override to release scene-specific assets; called from Unload().
Definition SceneBase.hpp:84
void DestroyAllObjects()
Destroys every object still owned by the scene, e.g. during Unload().
List< GameObject * > m_pendingDestroy
const List< GameObject * > & GetObjects() const
virtual void OnActivate()
Override for logic that should run when the scene becomes active.
Definition SceneBase.hpp:88
size_t GetObjectCount() const
LightManager * m_lightManager
Camera * GetActiveCamera() const
virtual void OnDeactivate()
Override for logic that should run when the scene stops being active.
Definition SceneBase.hpp:90
const std::string & GetName() const
void SetActiveCamera(Camera *cam)
std::string name
Physics::PhysicsWorld * m_physicsWorld
bool IsActive() const
Collision shapes, the broadphase tree, and the world that steps them.
Definition SceneBase.hpp:29
Root namespace for everything the engine exposes.
Definition Camera.hpp:10