galaxy 1.0.0
Real-Time C++23 Game Programming Framework. Built on data-driven design principles and agile software engineering.
Loading...
Searching...
No Matches
SceneManager.hpp
Go to the documentation of this file.
1
7
8#ifndef GALAXY_STATE_SCENEMANAGER_HPP_
9#define GALAXY_STATE_SCENEMANAGER_HPP_
10
11#include <ankerl/unordered_dense.h>
12
13#include "galaxy/math/FNV1a.hpp"
15
16namespace galaxy
17{
18 namespace scene
19 {
23 class SceneManager final : public fs::Serializable
24 {
25 using Map = ankerl::unordered_dense::map<std::uint64_t, std::unique_ptr<Scene>>;
26 using SystemContainer = meta::vector<std::unique_ptr<systems::System>>;
27
28 public:
33
37 virtual ~SceneManager();
38
46 [[maybe_unused]] Scene* add(const std::string& name);
47
55 template<std::derived_from<Scene> Custom>
56 [[maybe_unused]] Custom* add_custom(const std::string& name);
57
65 [[nodiscard]] Scene* get(const std::string& name);
66
74 void remove(const std::string& name);
75
83 [[nodiscard]] bool has(const std::string& name);
84
90 void set_scene(const std::string& name);
91
102 template<meta::is_system System, typename... Args>
103 void create_system(Args&&... args);
104
112 void load_app(const std::string& appdata_file);
113
119 void save_app(const std::string& file);
120
124 void update();
125
130
134 void render();
135
139 void clear();
140
146 [[nodiscard]] scene::Scene* current() const;
147
153 [[nodiscard]] const Map& map() const;
154
160 [[nodiscard]] bool empty() const;
161
167 [[nodiscard]] nlohmann::json serialize() override;
168
174 void deserialize(const nlohmann::json& json) override;
175
176 private:
181
186
191
196 };
197
198 template<std::derived_from<Scene> Custom>
199 inline Custom* SceneManager::add_custom(const std::string& name)
200 {
201 const auto hash = math::fnv1a_64(name.c_str());
202
203 if (!m_scenes.contains(hash))
204 {
205 m_scenes[hash] = std::make_unique<Custom>(name);
206 }
207 else
208 {
209 GALAXY_LOG(GALAXY_WARNING, "Tried to add a duplicate scene '{0}'.", name);
210 }
211
212 return static_cast<Custom*>(m_scenes[hash].get());
213 }
214
215 template<meta::is_system System, typename... Args>
216 inline void SceneManager::create_system(Args&&... args)
217 {
218 auto ptr = std::make_unique<System>(std::forward<Args>(args)...);
219 m_systems.emplace_back(std::move(ptr));
220
221 if constexpr (std::is_same<System, systems::RenderSystem>::value)
222 {
223 m_rendersystem_index = m_systems.size() - 1;
224 }
225 }
226 } // namespace scene
227} // namespace galaxy
228
229#endif
#define GALAXY_LOG(level, msg,...)
Definition Log.hpp:28
void create_system(Args &&... args)
Add a system to the manager.
void only_update_rendering()
Only update rendering.
void save_app(const std::string &file)
Save all active scenes and sub data within those scenes.
Custom * add_custom(const std::string &name)
Add a custom scene.
bool has(const std::string &name)
Does a scene exist.
scene::Scene * current() const
Get current scene.
virtual ~SceneManager()
Destructor.
std::size_t m_rendersystem_index
Rendersystem index.
SystemContainer m_systems
Stores systems.
void clear()
Deletes all scene data.
void deserialize(const nlohmann::json &json) override
Deserializes from object.
void update()
Handle events and update logic.
void render()
Handle rendering.
nlohmann::json serialize() override
Serializes object.
void load_app(const std::string &appdata_file)
Load app data file into scene manager.
Scene * get(const std::string &name)
Get a specific scene.
Scene * add(const std::string &name)
Add a new scene.
ankerl::unordered_dense::map< std::uint64_t, std::unique_ptr< Scene > > Map
void set_scene(const std::string &name)
Scene to set to currently active.
void remove(const std::string &name)
Remove a specific scene.
meta::vector< std::unique_ptr< systems::System > > SystemContainer
bool empty() const
Are there any scenes.
const Map & map() const
Get all scenes.
scene::Scene * m_current
Current scene.
Represents a scene in a game. Like the menu, game, etc. Does not share resources.
Definition Scene.hpp:29
Concept to ensure a system is actually derived from a System.
Definition System.hpp:76
constexpr std::uint64_t fnv1a_64(const char *const str) noexcept
Convert string to 64bit hash.
Definition FNV1a.hpp:92
Animated.cpp galaxy.
Definition Animated.cpp:16