Engine
Raylib based game framework
Loading...
Searching...
No Matches
SystemManager.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "NonCopyable.hpp"
4#include "Types.hpp"
5
6#include <algorithm>
7#include <memory>
8#include <mutex>
9#include <shared_mutex>
10#include <type_traits>
11#include <typeindex>
12#include <unordered_map>
13#include <utility>
14#include <vector>
15
20
26class System : public NonCopyable<>
27{
28public:
29
30 virtual ~System() = default;
31
40 virtual void Update(const float deltaT) = 0;
41
48 virtual void Draw() const;
49};
50
58class SystemManager
59{
60public:
61
62 SystemManager() = default;
63
64 SystemManager(const SystemManager&) = delete;
65 SystemManager& operator=(const SystemManager&) = delete;
66
84 template <typename SystemT, typename... Args>
85 requires std::is_base_of_v<System, SystemT>
86 std::shared_ptr<SystemT> AddSystem(const u32 priority = 1, Args&&... args)
87 {
88 std::unique_lock lock(m_mutex);
89
90 auto ptr = std::make_shared<SystemT>(std::forward<Args>(args)...);
91
92 m_systems.push_back(std::make_pair(priority, ptr));
93 m_systemsMap.emplace(typeid(SystemT), ptr);
94
95 std::sort(m_systems.begin(), m_systems.end(), [](const auto& a, const auto& b)
96 {
97 return a.first < b.first;
98 });
99
100 return ptr;
101 }
102
111 template <typename SystemT>
112 requires std::is_base_of_v<System, SystemT>
114 {
115 std::unique_lock lock(m_mutex);
116
117 auto it = m_systemsMap.find(typeid(SystemT));
118 if (it == m_systemsMap.end())
119 {
120 return;
121 }
122
123 std::shared_ptr<System> ptr = it->second;
124 i32 index = -1;
125
126 for (u32 i = 0; i < m_systems.size(); ++i)
127 {
128 if (m_systems[i].second == ptr)
129 {
130 index = i;
131 break;
132 }
133 }
134
135 if (index != -1)
136 {
137 m_systems.erase(m_systems.begin() + index);
138 }
139 }
140
149 template <typename SystemT>
150 requires std::is_base_of_v<System, SystemT>
151 std::shared_ptr<SystemT> GetSystem()
152 {
153 std::shared_lock lock(m_mutex);
154
155 auto it = m_systemsMap.find(typeid(SystemT));
156 if (it != m_systemsMap.end())
157 {
158 return std::dynamic_pointer_cast<SystemT>(it->second);
159 }
160
161 return nullptr;
162 }
163
169 void ClearSystems();
170
171private:
172
178 void Update(const float deltaT);
179
183 void Draw();
184
185 std::shared_mutex m_mutex;
186
187 std::vector<std::pair<u32, std::shared_ptr<System>>> m_systems;
188 std::unordered_map<std::type_index, std::shared_ptr<System>> m_systemsMap;
189
190 friend class Engine;
191};
Core engine.
Definition Engine.hpp:90
std::shared_ptr< SystemT > AddSystem(const u32 priority=1, Args &&... args)
Constructs a system and adds it to the manager.
Definition SystemManager.hpp:86
std::shared_ptr< SystemT > GetSystem()
Returns a shared pointer to a managed system.
Definition SystemManager.hpp:151
void RemoveSystem()
Removes a system from the manager.
Definition SystemManager.hpp:113
void ClearSystems()
Removes and destroys all managed systems.
Definition SystemManager.cpp:27
Base class for all systems.
Definition SystemManager.hpp:27
virtual void Update(const float deltaT)=0
Updates the system.
virtual void Draw() const
Renders the system.
Definition SystemManager.cpp:3