CoriEngine
Loading...
Searching...
No Matches
SceneManager.cpp
Go to the documentation of this file.
1#include "SceneManager.hpp"
4#include "Systems/System.hpp"
5#include "Systems/Trigger.hpp"
10#include "Systems/Physics.hpp"
11
12namespace Cori {
13 namespace World {
14 SceneManager::Data* SceneManager::s_Data{ nullptr };
15
18 using is_transparent = void;
19 size_t operator()(std::string_view sv) const noexcept {
20 return std::hash<std::string_view>{}(sv);
21 }
22 };
23
25 using is_transparent = void;
26 bool operator()(std::string_view lhs, std::string_view rhs) const noexcept {
27 return lhs == rhs;
28 }
29 };
30
31 std::atomic<uint32_t> s_NextSceneID{ 1 };
32 std::unordered_map<uint32_t, SceneHandle> m_Handles;
33 std::unordered_map<std::string, std::shared_ptr<Scene>, TransparentHash, TransparentEqual> m_Scenes;
34 };
35
36 std::expected<SceneHandle, Core::CoriError<>> SceneManager::GetScene(const std::string& name) {
37 if (!s_Data->m_Scenes.contains(name)) {
38 return std::unexpected(Core::CoriError(std::format("No Scene with name '{}' exists.", name)));
39 }
40
41 return SceneHandle(s_Data->m_Scenes.at(name));
42 }
43
44 std::expected<SceneHandle, Core::CoriError<>> SceneManager::GetScene(const std::string_view name) {
45 if (!s_Data->m_Scenes.contains(name)) {
46 return std::unexpected(Core::CoriError(std::format("No Scene with name '{}' exists.", name)));
47 }
48
49 return SceneHandle(s_Data->m_Scenes.find(name)->second);
50 }
51
52 std::expected<SceneHandle, Core::CoriError<>> SceneManager::GetHandle(const uint32_t sceneID) {
53 if (s_Data->m_Handles.contains(sceneID)) {
54 return s_Data->m_Handles.at(sceneID);
55 }
56
57 return std::unexpected(Core::CoriError(std::format("No scene with ID '{}' found.", sceneID)));
58 }
59
60 void SceneManager::Init() {
61 s_Data = new Data();
62 }
63
64 void SceneManager::Shutdown() {
65 delete s_Data;
66 }
67
68 std::expected<SceneHandle, Core::CoriError<>> SceneManager::CreateScene(const std::string& name) {
69 if (name.empty()) {
70 return std::unexpected(Core::CoriError("Scene name cannot be empty!"));
71 }
72
73 if (s_Data->m_Scenes.contains(name)) {
74 return std::unexpected(Core::CoriError(std::format("Scene with name '{}' already exists.", name)));
75 }
76
78
79 std::shared_ptr<Scene> scene = Scene::Create(name);
80 s_Data->m_Scenes.insert({ name, scene });
81 const uint32_t id = s_Data->s_NextSceneID.fetch_add(1, std::memory_order_relaxed);
82 scene->m_SceneID = id;
83 SceneHandle handle = SceneHandle(scene);
84 s_Data->m_Handles.insert({ id, handle });
85 scene->RegisterSystem<Systems::Transform>();
86 scene->RegisterSystem<Systems::Animation>();
87 scene->RegisterSystem<Systems::StateMachine>();
88 scene->RegisterSystem<Systems::Hierarchy>();
89 return handle;
90 }
91
92 std::expected<void, Core::CoriError<>> SceneManager::DestroyScene(const std::string& name) {
93 if (name.empty()) {
94 return std::unexpected(Core::CoriError("Scene name cannot be empty!"));
95 }
96
97 if (!s_Data->m_Scenes.contains(name)) {
98 return std::unexpected(Core::CoriError(std::format("No Scene with name '{}' exists.", name)));
99 }
100
102
103 if (s_Data->m_Scenes.at(name).use_count() == 1) {
104 s_Data->m_Handles.erase(s_Data->m_Scenes.at(name)->m_SceneID);
105 s_Data->m_Scenes.erase(name);
106 return {};
107 }
108
109 return std::unexpected(Core::CoriError(std::format("Failed to destroy Scene '{}', this scene is active in some layer. (ref count is > 1", name)));
110 }
111 }
112}
#define CORI_CORE_INFO_TAGGED(...)
Definition Logger.hpp:1027
Custom error class mainly used in std::expected.
Definition Error.hpp:27
A handle for the scene, checks for scene validity before any call to the scene, if scene is invalid a...
static std::expected< SceneHandle, Core::CoriError<> > GetHandle(const uint32_t sceneID)
Allows you to get SceneHandle if all you know is scene id.
static std::expected< SceneHandle, Core::CoriError<> > GetScene(const std::string &name)
Retries the scene with the specified name from the cache.
static std::expected< void, Core::CoriError<> > DestroyScene(const std::string &name)
Destroys a scene with the specified name.
static std::expected< SceneHandle, Core::CoriError<> > CreateScene(const std::string &name)
Creates a scene with the specified name and adds it to the cache.
static std::shared_ptr< Scene > Create(std::string name)
Definition Scene.cpp:9
System that is responsible for Animations, every Scene has it by default.
Anything connected to WorldSystem (ECS) is in this namespace.
Global engine namespace.
static constexpr char SceneManager[]
Definition Logger.hpp:100
static constexpr char Self[]
Definition Logger.hpp:83
bool operator()(std::string_view lhs, std::string_view rhs) const noexcept
size_t operator()(std::string_view sv) const noexcept
std::atomic< uint32_t > s_NextSceneID
std::unordered_map< uint32_t, SceneHandle > m_Handles
std::unordered_map< std::string, std::shared_ptr< Scene >, TransparentHash, TransparentEqual > m_Scenes