SleakEngine
0.1.0
C++23 multi-backend game engine
Toggle main menu visibility
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
>
7
#include <
Utility/Container/List.hpp
>
8
9
10
namespace
Sleak
{
11
12
/// Lifecycle state a SceneBase moves through via Load/Unload/Activate/Deactivate.
13
/// @ingroup core
14
enum class
SceneState
{
15
Unloaded
,
16
Loading
,
17
Active
,
18
Paused
,
19
Unloading
20
};
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
)
75
:
name
(
name
),
state
(
SceneState
::
Unloaded
),
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
145
Camera
*
GetActiveCamera
()
const
{
return
m_activeCamera
; }
146
void
SetActiveCamera
(
Camera
* cam) {
m_activeCamera
= cam; }
147
148
LightManager
*
GetLightManager
()
const
{
149
return
m_lightManager
;
150
}
151
152
Physics::PhysicsWorld
*
GetPhysicsWorld
()
const
{
153
return
m_physicsWorld
;
154
}
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
;
162
SceneState
state
;
163
bool
bInitialized
;
164
bool
bActive
;
165
bool
bHasBegun
=
false
;
166
167
List<GameObject*>
Objects
;
168
List<GameObject*>
m_pendingDestroy
;
169
170
Camera
*
m_activeCamera
=
nullptr
;
171
172
LightManager
*
m_lightManager
=
nullptr
;
173
Physics::PhysicsWorld
*
m_physicsWorld
=
nullptr
;
174
Skybox
*
m_skybox
=
nullptr
;
175
176
/// Actually deletes objects queued by DestroyObject().
177
void
ProcessPendingDestroy
();
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
List.hpp
OSDef.hpp
RefPtr.hpp
Sleak::Camera
Definition
Camera.hpp:59
Sleak::GameObject
Definition
GameObject.hpp:62
Sleak::LightManager
Definition
LightManager.hpp:57
Sleak::List
Implements a dynamic array-like list for storing and managing a collection of elements.
Definition
List.hpp:20
Sleak::Physics::PhysicsWorld
Definition
PhysicsWorld.hpp:86
Sleak::SceneBase
Definition
SceneBase.hpp:72
Sleak::SceneBase::GetLightManager
LightManager * GetLightManager() const
Definition
SceneBase.hpp:148
Sleak::SceneBase::GetPhysicsWorld
Physics::PhysicsWorld * GetPhysicsWorld() const
Definition
SceneBase.hpp:152
Sleak::SceneBase::GetState
SceneState GetState() const
Definition
SceneBase.hpp:108
Sleak::SceneBase::SceneBase
SceneBase(const std::string &name)
Definition
SceneBase.hpp:74
Sleak::SceneBase::Objects
List< GameObject * > Objects
Definition
SceneBase.hpp:167
Sleak::SceneBase::GetSkybox
Skybox * GetSkybox() const
Definition
SceneBase.hpp:158
Sleak::SceneBase::IsLoaded
bool IsLoaded() const
Definition
SceneBase.hpp:110
Sleak::SceneBase::ProcessPendingDestroy
void ProcessPendingDestroy()
Actually deletes objects queued by DestroyObject().
Definition
SceneBase.cpp:358
Sleak::SceneBase::OnLoad
virtual void OnLoad()
Override to load scene-specific assets; called once from Load().
Definition
SceneBase.hpp:82
Sleak::SceneBase::state
SceneState state
Definition
SceneBase.hpp:162
Sleak::SceneBase::m_activeCamera
Camera * m_activeCamera
Definition
SceneBase.hpp:170
Sleak::SceneBase::OnUnload
virtual void OnUnload()
Override to release scene-specific assets; called from Unload().
Definition
SceneBase.hpp:84
Sleak::SceneBase::DestroyAllObjects
void DestroyAllObjects()
Destroys every object still owned by the scene, e.g. during Unload().
Definition
SceneBase.cpp:379
Sleak::SceneBase::m_pendingDestroy
List< GameObject * > m_pendingDestroy
Definition
SceneBase.hpp:168
Sleak::SceneBase::bInitialized
bool bInitialized
Definition
SceneBase.hpp:163
Sleak::SceneBase::GetObjects
const List< GameObject * > & GetObjects() const
Definition
SceneBase.hpp:134
Sleak::SceneBase::OnActivate
virtual void OnActivate()
Override for logic that should run when the scene becomes active.
Definition
SceneBase.hpp:88
Sleak::SceneBase::bActive
bool bActive
Definition
SceneBase.hpp:164
Sleak::SceneBase::GetObjectCount
size_t GetObjectCount() const
Definition
SceneBase.hpp:143
Sleak::SceneBase::m_lightManager
LightManager * m_lightManager
Definition
SceneBase.hpp:172
Sleak::SceneBase::GetActiveCamera
Camera * GetActiveCamera() const
Definition
SceneBase.hpp:145
Sleak::SceneBase::OnDeactivate
virtual void OnDeactivate()
Override for logic that should run when the scene stops being active.
Definition
SceneBase.hpp:90
Sleak::SceneBase::GetName
const std::string & GetName() const
Definition
SceneBase.hpp:107
Sleak::SceneBase::SetActiveCamera
void SetActiveCamera(Camera *cam)
Definition
SceneBase.hpp:146
Sleak::SceneBase::name
std::string name
Definition
SceneBase.hpp:161
Sleak::SceneBase::m_physicsWorld
Physics::PhysicsWorld * m_physicsWorld
Definition
SceneBase.hpp:173
Sleak::SceneBase::IsActive
bool IsActive() const
Definition
SceneBase.hpp:109
Sleak::SceneBase::m_skybox
Skybox * m_skybox
Definition
SceneBase.hpp:174
Sleak::SceneBase::bHasBegun
bool bHasBegun
Definition
SceneBase.hpp:165
Sleak::Skybox
Definition
Skybox.hpp:19
Sleak::SceneState
SceneState
Definition
SceneBase.hpp:14
Sleak::SceneState::Loading
@ Loading
Definition
SceneBase.hpp:16
Sleak::SceneState::Active
@ Active
Definition
SceneBase.hpp:17
Sleak::SceneState::Unloading
@ Unloading
Definition
SceneBase.hpp:19
Sleak::SceneState::Unloaded
@ Unloaded
Definition
SceneBase.hpp:15
Sleak::SceneState::Paused
@ Paused
Definition
SceneBase.hpp:18
Sleak::EventType::Update
@ Update
Definition
Event.hpp:26
Sleak::Physics
Collision shapes, the broadphase tree, and the world that steps them.
Definition
SceneBase.hpp:29
Sleak
Root namespace for everything the engine exposes.
Definition
Camera.hpp:10
include
public
Core
SceneBase.hpp
Generated on
for SleakEngine by
1.18.0