Engine
Raylib based game framework
Loading...
Searching...
No Matches
SceneManager.h
1#pragma once
2
3#include "Assert.h"
4#include "Lua/MyLua.h"
5
6#include <memory>
7#include <typeindex>
8#include <unordered_map>
9
17class Scene
18{
19public:
20
21 virtual ~Scene()
22 {
23 }
24
33 virtual void Update(const float deltaT) = 0;
34
41 virtual void Draw() = 0;
42
47 virtual void OnEnter() = 0;
48
53 virtual void OnExit() = 0;
54};
55
64{
65public:
66
73 void Update(const float deltaT);
74
79 void Draw();
80
97 template <typename T, typename... Args>
98 requires std::is_base_of_v<Scene, T>
99 void AddScene(Args&&... args)
100 {
101 auto ptr = std::make_unique<T>(std::forward<Args>(args)...);
102
103 m_scenes.emplace(typeid(T), std::move(ptr));
104 }
105
112 template <typename T>
113 requires std::is_base_of_v<Scene, T>
115 {
116 auto it = m_scenes.find(typeid(T));
117 if (it != m_scenes.end())
118 {
119 Assert(it->second.get() != m_currentScene, "Cannot delete the current scene");
120
121 m_scenes.erase(it);
122 }
123 }
124
134 template <typename T>
135 requires std::is_base_of_v<Scene, T>
137 {
138 auto it = m_scenes.find(typeid(T));
139 Assert(it != m_scenes.end(), "Scene ", Demangle<T>().c_str(), " does not exist");
140 Assert(it->second.get() != m_currentScene, "Cannot change to the current scene");
141
142 m_nextSceneType = typeid(T);
143 m_changeScene = true;
144 }
145
150 void ClearScenes();
151
152private:
153
154 void CheckForChange();
155
156 Scene* m_currentScene = nullptr;
157
158 bool m_changeScene = false;
159 std::type_index m_nextSceneType = typeid(void);
160
161 std::unordered_map<std::type_index, std::unique_ptr<Scene>> m_scenes;
162};
Manages scenes and the the execution of the current scene.
Definition SceneManager.h:64
void ClearScenes()
Removes all scenes.
Definition SceneManager.cpp:23
void Draw()
Renders the current scene.
Definition SceneManager.cpp:13
void RemoveScene()
Removes a scene from the manager.
Definition SceneManager.h:114
void AddScene(Args &&... args)
Adds a scene to the manager.
Definition SceneManager.h:99
void Update(const float deltaT)
Updates current scene.
Definition SceneManager.cpp:5
void ChangeScene()
Queues a scene change at the end of the frame.
Definition SceneManager.h:136
Base class for all scenes.
Definition SceneManager.h:18
virtual void Draw()=0
Renders the scene.
virtual void OnExit()=0
Called by the engine when a scene is left.
virtual void OnEnter()=0
Called by the engine when a scene is entered.
virtual void Update(const float deltaT)=0
Updates the scene.