Engine
Raylib based game framework
Loading...
Searching...
No Matches
SceneManager.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "Assert.hpp"
4#include "Log/Log.hpp"
5#include "NonCopyable.hpp"
6
7#include <memory>
8#include <mutex>
9#include <typeindex>
10#include <unordered_map>
11
16
22class Scene : public NonCopyable<>
23{
24public:
25
26 virtual ~Scene() = default;
27
35 virtual void Update(const float deltaT) = 0;
36
42 virtual void Draw() const;
43
50 virtual void OnEnter();
51
58 virtual void OnExit();
59};
60
71{
72public:
73
89 template <typename SceneT, typename... Args>
90 requires std::is_base_of_v<Scene, SceneT>
91 void AddScene(Args&&... args)
92 {
93 auto ptr = std::make_unique<SceneT>(std::forward<Args>(args)...);
94
95 std::unique_lock lock(m_mutex);
96
97 m_scenes.emplace(typeid(SceneT), std::move(ptr));
98 }
99
107 template <typename SceneT>
108 requires std::is_base_of_v<Scene, SceneT>
110 {
111 std::unique_lock lock(m_mutex);
112
113 auto it = m_scenes.find(typeid(SceneT));
114 if (it != m_scenes.end())
115 {
116 Assert(it->second.get() != m_currentScene, "Cannot delete the current scene");
117
118 m_scenes.erase(it);
119 }
120 }
121
131 template <typename SceneT>
132 requires std::is_base_of_v<Scene, SceneT>
134 {
135 std::unique_lock lock(m_mutex);
136
137 auto it = m_scenes.find(typeid(SceneT));
138 Assert(it != m_scenes.end(), "Scene ", Demangle<SceneT>().c_str(), " does not exist");
139 Assert(it->second.get() != m_currentScene, "Cannot change to the current scene");
140
141 m_nextSceneType = typeid(SceneT);
142 m_changeScene = true;
143 }
144
150 void ClearScenes();
151
152private:
153
159 void Update(const float deltaT);
160
164 void Draw();
165
171 void CheckForChange();
172
173 Scene* m_currentScene = nullptr;
174
175 bool m_changeScene = false;
176 std::type_index m_nextSceneType = typeid(void);
177
178 std::mutex m_mutex;
179
180 std::unordered_map<std::type_index, std::unique_ptr<Scene>> m_scenes;
181
182 friend class Engine;
183};
Core engine.
Definition Engine.hpp:90
Manages scenes and the execution of the current active scene.
Definition SceneManager.hpp:71
void ClearScenes()
Removes all scenes and clears the active scene pointer.
Definition SceneManager.cpp:35
void RemoveScene()
Removes a scene from the manager.
Definition SceneManager.hpp:109
void ChangeScene()
Queues a scene transition to be applied at the end of the current frame.
Definition SceneManager.hpp:133
void AddScene(Args &&... args)
Adds a scene to the manager.
Definition SceneManager.hpp:91
Base class for all scenes.
Definition SceneManager.hpp:23
virtual void Draw() const
Renders the scene.
Definition SceneManager.cpp:5
virtual void OnExit()
Called by the engine just before this scene is replaced by another.
Definition SceneManager.cpp:13
virtual void OnEnter()
Called by the engine when this scene becomes the active scene.
Definition SceneManager.cpp:9
virtual void Update(const float deltaT)=0
Updates the scene.