SleakEngine 1.0.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
Scene.hpp
Go to the documentation of this file.
1#ifndef _SCENE_HPP_
2#define _SCENE_HPP_
3
4#include "SceneBase.hpp"
5
6namespace Sleak {
7 /// Concrete scene type games subclass. Adds an opt-out switch for the
8 /// fixed-timestep update on top of SceneBase's lifecycle.
9 ///
10 /// This is the class your levels, menus, and screens derive from.
11 /// Override the hooks you need and call the base implementation from
12 /// each one: OnLoad() and OnUnload() for assets, OnActivate() and
13 /// OnDeactivate() for entering and leaving, Begin() to build the object
14 /// graph, and Update() / FixedUpdate() / LateUpdate() for per-frame
15 /// work.
16 ///
17 /// The scene owns every object handed to AddObject(), including lights
18 /// and cameras, and destroys all of them on unload. It also owns a
19 /// LightManager and a Physics::PhysicsWorld, reachable through
20 /// GetLightManager() and GetPhysicsWorld(), and registers added objects
21 /// with both automatically.
22 ///
23 /// Call SetFixedUpdateEnabled(false) on scenes that do not simulate
24 /// anything, such as menus, to skip the fixed-timestep pass entirely.
25 ///
26 /// @code{.cpp}
27 /// class WorldScene : public Sleak::Scene {
28 /// public:
29 /// WorldScene() : Sleak::Scene("WorldScene") {}
30 ///
31 /// void Begin() override {
32 /// auto* cube = Sleak::GameObject::CreateCube(
33 /// Sleak::Math::Vector3D(0.0f, 0.0f, 0.0f));
34 /// AddObject(cube);
35 ///
36 /// auto* sun = new Sleak::DirectionalLight("Sun");
37 /// sun->SetDirection(Sleak::Math::Vector3D(-0.5f, -0.8f, -0.3f));
38 /// sun->SetIntensity(4.0f);
39 /// AddObject(sun);
40 ///
41 /// auto* cam = new Sleak::Camera("MainCamera");
42 /// AddObject(cam);
43 /// SetActiveCamera(cam);
44 ///
45 /// Sleak::Scene::Begin(); // activates everything added above
46 /// }
47 ///
48 /// void Update(float deltaTime) override {
49 /// Sleak::Scene::Update(deltaTime);
50 /// }
51 /// };
52 /// @endcode
53 ///
54 /// @see SceneBase, GameBase, GameObject, LightManager
55 /// @ingroup core
56 class ENGINE_API Scene : public SceneBase {
57 public:
58 explicit Scene(const std::string& name)
59 : SceneBase(name), bEnableFixedUpdate(true) {}
60 ~Scene() override = default;
61
62 // Resource lifecycle
63 void OnLoad() override;
64 void OnUnload() override;
65
66 // Activation lifecycle
67 void OnActivate() override;
68 void OnDeactivate() override;
69
70 // Initialization
71 bool Initialize() override;
72 void Begin() override;
73
74 // Update loops
75 void Update(float deltaTime) override;
76 void FixedUpdate(float fixedDeltaTime) override;
77 void LateUpdate(float deltaTime) override;
78
79 void SetFixedUpdateEnabled(bool enabled) { bEnableFixedUpdate = enabled; }
80 bool IsFixedUpdateEnabled() const { return bEnableFixedUpdate; }
81
82 private:
83 bool bEnableFixedUpdate;
84 };
85}
86
87#endif // _SCENE_HPP_
SceneBase(const std::string &name)
Definition SceneBase.hpp:72
std::string name
void SetFixedUpdateEnabled(bool enabled)
Definition Scene.hpp:79
void Begin() override
Activates every owned object; runs once after Initialize().
Definition Scene.cpp:26
void OnDeactivate() override
Override for logic that should run when the scene stops being active.
Definition Scene.cpp:18
void FixedUpdate(float fixedDeltaTime) override
Advances all active, root-level objects on the fixed timestep.
Definition Scene.cpp:34
void OnLoad() override
Override to load scene-specific assets; called once from Load().
Definition Scene.cpp:6
bool IsFixedUpdateEnabled() const
Definition Scene.hpp:80
Scene(const std::string &name)
Definition Scene.hpp:58
void Update(float deltaTime) override
Advances all active, root-level objects by deltaTime, then steps lighting and physics.
Definition Scene.cpp:30
void OnUnload() override
Override to release scene-specific assets; called from Unload().
Definition Scene.cpp:10
~Scene() override=default
void OnActivate() override
Override for logic that should run when the scene becomes active.
Definition Scene.cpp:14
bool Initialize() override
Runs once before the scene's first Begin()/Update().
Definition Scene.cpp:22
void LateUpdate(float deltaTime) override
Advances all active, root-level objects after the main Update pass.
Definition Scene.cpp:39
Root namespace for everything the engine exposes.
Definition Camera.hpp:10