SleakEngine 1.0.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 directly.
33 ///
34 /// SceneBase defines what every scene can do regardless of subclass:
35 /// hold objects, move through the SceneState machine, and answer
36 /// queries. GameBase stores scenes by `SceneBase*`, so this is the type
37 /// you see in the scene registry API.
38 ///
39 /// Ownership is strict. AddObject() transfers ownership to the scene.
40 /// RemoveObject() unregisters and deletes immediately, which is unsafe
41 /// from inside that object's own update. DestroyObject() queues the
42 /// object (and its children) for deletion at the end of the frame,
43 /// which is the safe choice while iterating.
44 ///
45 /// Objects added to the scene are registered with the LightManager if
46 /// they report IsLight(), and their colliders are registered with the
47 /// PhysicsWorld, hierarchy included.
48 ///
49 /// @code{.cpp}
50 /// // Destroy safely from inside an update
51 /// if (auto* target = FindObjectByName("Crate")) {
52 /// DestroyObject(target);
53 /// }
54 ///
55 /// // Act on a group
56 /// Sleak::List<Sleak::GameObject*> enemies = FindObjectsByTag("Enemy");
57 /// for (size_t i = 0; i < enemies.GetSize(); ++i) {
58 /// enemies[i]->SetActive(false);
59 /// }
60 ///
61 /// // Scene-wide services
62 /// if (auto* lm = GetLightManager()) lm->SetAmbientIntensity(0.6f);
63 /// if (auto* pw = GetPhysicsWorld()) {
64 /// auto hit = pw->Raycast(origin, direction, 100.0f);
65 /// }
66 /// @endcode
67 ///
68 /// @see Scene, GameBase, GameObject, SceneState, Physics::PhysicsWorld
69 /// @ingroup core
70 class ENGINE_API SceneBase {
71 public:
72 explicit SceneBase(const std::string& name)
74 bInitialized(false), bActive(false) {}
75
76 virtual ~SceneBase();
77
78 // Resource lifecycle hooks
79 /// Override to load scene-specific assets; called once from Load().
80 virtual void OnLoad() {}
81 /// Override to release scene-specific assets; called from Unload().
82 virtual void OnUnload() {}
83
84 // Activation lifecycle hooks
85 /// Override for logic that should run when the scene becomes active.
86 virtual void OnActivate() {}
87 /// Override for logic that should run when the scene stops being active.
88 virtual void OnDeactivate() {}
89
90 // Initialization
91 /// Runs once before the scene's first Begin()/Update().
92 virtual bool Initialize();
93 /// Activates every owned object; runs once after Initialize().
94 virtual void Begin() = 0;
95
96 // Update loops
97 /// Advances all active, root-level objects by deltaTime, then steps lighting and physics.
98 virtual void Update(float deltaTime) = 0;
99 /// Advances all active, root-level objects on the fixed timestep.
100 virtual void FixedUpdate(float fixedDeltaTime);
101 /// Advances all active, root-level objects after the main Update pass.
102 virtual void LateUpdate(float deltaTime);
103
104 // State
105 const std::string& GetName() const { return name; }
106 SceneState GetState() const { return state; }
107 bool IsActive() const { return bActive; }
108 bool IsLoaded() const { return state != SceneState::Unloaded; }
109
110 // Scene state transitions
111 /// Moves Unloaded -> Loading -> Active, calling OnLoad() and Initialize().
112 void Load();
113 /// Deactivates if active, calls OnUnload(), and destroys all owned objects.
114 void Unload();
115 /// Marks the scene active and calls OnActivate().
116 void Activate();
117 /// Marks the scene inactive and calls OnDeactivate().
118 void Deactivate();
119 /// Freezes the scene without tearing it down; leaves it in the Paused state.
120 void Pause();
121 /// Reactivates a paused scene without re-running OnActivate().
122 void Resume();
123
124 // Object management — scene takes ownership of added objects
125 /// Takes ownership of object, registering it with lighting and physics as needed.
126 virtual void AddObject(GameObject* object);
127 /// Unregisters and deletes object immediately.
128 virtual void RemoveObject(GameObject* object);
129 /// Queues an object for destruction; actually freed on the next ProcessPendingDestroy().
130 void DestroyObject(GameObject* object);
131 const List<GameObject*>& GetObjects() const { return Objects; }
132
133 // Object queries
134 /// Linear search for the first object with a matching name.
135 GameObject* FindObjectByName(const std::string& name);
136 /// Linear search for the object with a matching unique ID.
137 GameObject* FindObjectByID(uint64_t id);
138 /// Collects every object whose tag matches.
139 List<GameObject*> FindObjectsByTag(const std::string& tag);
140 size_t GetObjectCount() const { return Objects.GetSize(); }
141
144
146 return m_lightManager;
147 }
148
152
153 /// Replaces the scene's skybox, deleting the previous one.
154 void SetSkybox(Skybox* skybox);
155 Skybox* GetSkybox() const { return m_skybox; }
156
157 protected:
158 std::string name;
162
165
167
170 Skybox* m_skybox = nullptr;
171
172 /// Actually deletes objects queued by DestroyObject().
174 /// Destroys every object still owned by the scene, e.g. during Unload().
175 void DestroyAllObjects();
176 };
177
178} // namespace Sleak
179
180#endif // _SCENE_BASE_HPP_
181
182
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:72
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:80
SceneState state
Camera * m_activeCamera
virtual void OnUnload()
Override to release scene-specific assets; called from Unload().
Definition SceneBase.hpp:82
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:86
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:88
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