SleakEngine 1.0.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
GameBase.hpp
Go to the documentation of this file.
1#ifndef _GAMEBASE_H_
2#define _GAMEBASE_H_
3
4#include <Core/OSDef.hpp>
6#include <Core/Scene.hpp>
7
8namespace Sleak {
9
10 /// Base class the game's top-level object derives from. Owns the scene
11 /// list and the currently active scene; Application drives it each frame.
12 ///
13 /// This is the root of your game. Subclass it, register your scenes in
14 /// Initialize(), and hand an instance to Application::Run(). Three
15 /// methods are pure virtual and must be implemented: Initialize() for
16 /// one-time setup (return false to abort the run), Begin() for work that
17 /// needs the first scene already active, and Loop() for per-frame logic
18 /// that is not tied to any single scene.
19 ///
20 /// GameBase owns every scene passed to AddScene() and unloads and
21 /// deletes all of them in its destructor. Never delete a registered
22 /// scene yourself; call RemoveScene() instead. When swapping scenes at
23 /// runtime, call Application::WaitGPUIdle() first so the GPU is not
24 /// still reading resources you are about to free.
25 ///
26 /// @code{.cpp}
27 /// class SLEAK_API Game : public Sleak::GameBase {
28 /// public:
29 /// bool Initialize() override {
30 /// Sleak::Application::GetInstance()->SetVSync(true);
31 ///
32 /// auto* menu = new MenuScene();
33 /// auto* world = new WorldScene();
34 /// AddScene(menu);
35 /// AddScene(world);
36 /// SetActiveScene(menu);
37 /// return true;
38 /// }
39 ///
40 /// void Begin() override {}
41 /// void Loop(float deltaTime) override {}
42 /// bool GetIsGameRunning() override { return bIsGameRunning; }
43 ///
44 /// private:
45 /// bool bIsGameRunning = true;
46 /// };
47 /// @endcode
48 ///
49 /// @see Application, Scene, SceneBase
50 /// @ingroup core
51 class ENGINE_API GameBase {
52 public:
53 virtual ~GameBase() {
54 ActiveScene = nullptr;
55 for (size_t i = 0; i < Scenes.GetSize(); ++i) {
56 if (Scenes[i]) {
57 Scenes[i]->Unload();
58 delete Scenes[i];
59 }
60 }
61 Scenes.clear();
62 }
63 /// One-time setup before the loop starts. Return false to abort the run.
64 virtual bool Initialize() = 0;
65 /// Called once after Initialize(), before the first Loop().
66 virtual void Begin() = 0;
67 /// Per-frame update, called after the active scene's own update.
68 virtual void Loop(float DeltaTime) = 0;
69
70 virtual bool GetIsGameRunning() = 0;
71
72 // Scene management
73 /// Registers a scene; the game takes ownership.
74 virtual void AddScene(SceneBase* scene) {
75 if (scene) Scenes.add(scene);
76 }
77
78 /// Unloads, destroys, and drops a scene from the registry.
79 virtual void RemoveScene(SceneBase* scene) {
80 int index = Scenes.indexOf(scene);
81 if (index != -1) {
82 if (ActiveScene == scene) {
83 ActiveScene->Deactivate();
84 ActiveScene = nullptr;
85 }
86 Scenes[index]->Unload();
87 delete Scenes[index];
88 Scenes.erase(index);
89 }
90 }
91
92 /// Deactivates the current scene and activates the given one.
93 virtual void SetActiveScene(SceneBase* scene) {
94 if (!scene || ActiveScene == scene) return;
95 if (ActiveScene) ActiveScene->Deactivate();
96 ActiveScene = scene;
97 ActiveScene->Activate();
98 }
99
100 /// Activates the scene at the given index in the registry.
101 virtual void SetActiveScene(int index) {
102 SetActiveScene(Scenes[index]);
103 }
104
105 virtual SceneBase* GetActiveScene() { return ActiveScene; }
106
107 protected:
110};
111
112}
113
114#endif
virtual SceneBase * GetActiveScene()
Definition GameBase.hpp:105
virtual ~GameBase()
Definition GameBase.hpp:53
virtual void Loop(float DeltaTime)=0
Per-frame update, called after the active scene's own update.
virtual bool GetIsGameRunning()=0
Sleak::List< Sleak::SceneBase * > Scenes
Definition GameBase.hpp:108
virtual bool Initialize()=0
One-time setup before the loop starts. Return false to abort the run.
virtual void SetActiveScene(SceneBase *scene)
Deactivates the current scene and activates the given one.
Definition GameBase.hpp:93
SceneBase * ActiveScene
Definition GameBase.hpp:109
virtual void RemoveScene(SceneBase *scene)
Unloads, destroys, and drops a scene from the registry.
Definition GameBase.hpp:79
virtual void SetActiveScene(int index)
Activates the scene at the given index in the registry.
Definition GameBase.hpp:101
virtual void AddScene(SceneBase *scene)
Registers a scene; the game takes ownership.
Definition GameBase.hpp:74
virtual void Begin()=0
Called once after Initialize(), before the first Loop().
Implements a dynamic array-like list for storing and managing a collection of elements.
Definition List.hpp:20
Root namespace for everything the engine exposes.
Definition Camera.hpp:10