Engine²
Open-source game engine written in C++.
Loading...
Searching...
No Matches
SceneManager.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <iostream>
4#include <optional>
5#include <string>
6#include <type_traits>
7
8#include "Engine.hpp"
9
10#include "Logger.hpp"
11
12#include "utils/AScene.hpp"
13
14namespace Scene::Resource {
16 public:
17 SceneManager() = default;
18 ~SceneManager() = default;
19
26 inline void SetNextScene(const std::string_view &name) { _nextScene = name; }
27
33 void Update(Engine::Core &core);
34
42 template <typename TScene> TScene &RegisterScene(const std::string &name)
43 {
44 static_assert(std::is_base_of_v<Utils::AScene, TScene>, "TScene must inherit from Utils::AScene");
45 if (_scenes.contains(name))
46 {
47 Log::Warning(fmt::format("Scene {} already exists", name));
48 }
49 std::shared_ptr<TScene> new_scene = std::make_shared<TScene>();
50 _scenes[name] = new_scene;
51 return *new_scene.get();
52 }
53
54 const std::optional<std::string> &GetCurrentScene() const { return _currentScene; }
55
56 private:
57 void _loadScene(Engine::Core &core, const std::string &name);
58
59 void _unloadScene(Engine::Core &core, const std::string &name);
60
61 [[nodiscard]] std::optional<std::shared_ptr<Utils::AScene>> _getScene(const std::string &name);
62
64 using is_transparent = void;
65 std::size_t operator()(const std::string &str) const noexcept { return std::hash<std::string>{}(str); }
66 std::size_t operator()(std::string_view str) const noexcept { return std::hash<std::string_view>{}(str); }
67 std::size_t operator()(const char *str) const noexcept { return std::hash<std::string_view>{}(str); }
68 };
69 std::unordered_map<std::string, std::shared_ptr<Utils::AScene>, TransparentHash, std::equal_to<>> _scenes;
70
71 std::optional<std::string> _nextScene;
72 std::optional<std::string> _currentScene;
73};
74} // namespace Scene::Resource
The core is the place where all the data of the engine is stored. It contains the registry (entities)...
Definition Core.hpp:33
void _loadScene(Engine::Core &core, const std::string &name)
Definition SceneManager.cpp:23
std::optional< std::shared_ptr< Utils::AScene > > _getScene(const std::string &name)
Definition SceneManager.cpp:43
TScene & RegisterScene(const std::string &name)
Register a scene using a name as a key.
Definition SceneManager.hpp:42
std::unordered_map< std::string, std::shared_ptr< Utils::AScene >, TransparentHash, std::equal_to<> > _scenes
Definition SceneManager.hpp:69
void Update(Engine::Core &core)
Unload the current scene and load the next scene.
Definition SceneManager.cpp:9
const std::optional< std::string > & GetCurrentScene() const
Definition SceneManager.hpp:54
void SetNextScene(const std::string_view &name)
Set the next scene to load. It will be loaded at the next call of Update.
Definition SceneManager.hpp:26
std::optional< std::string > _currentScene
Definition SceneManager.hpp:72
std::optional< std::string > _nextScene
Definition SceneManager.hpp:71
void _unloadScene(Engine::Core &core, const std::string &name)
Definition SceneManager.cpp:33
void Warning(const T &msg) noexcept
Definition Logger.hpp:49
Definition SceneManager.hpp:14
void is_transparent
Definition SceneManager.hpp:64
std::size_t operator()(const char *str) const noexcept
Definition SceneManager.hpp:67
std::size_t operator()(const std::string &str) const noexcept
Definition SceneManager.hpp:65
std::size_t operator()(std::string_view str) const noexcept
Definition SceneManager.hpp:66