diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-11-13 12:12:26 +0100 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-11-13 12:12:26 +0100 |
commit | ec3601fbc17a38a44608a04f53d4e36c1bff8c96 (patch) | |
tree | 202731b9922d8a2082abdadab3f21a50c99e22fa /src/crepe/api | |
parent | 455bb50a5007daf46b8719fff2a6292da6a294bf (diff) |
re-enable loop manager + remove more singletons
Diffstat (limited to 'src/crepe/api')
-rw-r--r-- | src/crepe/api/CMakeLists.txt | 4 | ||||
-rw-r--r-- | src/crepe/api/LoopManager.cpp | 24 | ||||
-rw-r--r-- | src/crepe/api/LoopManager.h | 29 | ||||
-rw-r--r-- | src/crepe/api/LoopManager.hpp | 38 |
4 files changed, 80 insertions, 15 deletions
diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt index ee77947..85696c4 100644 --- a/src/crepe/api/CMakeLists.txt +++ b/src/crepe/api/CMakeLists.txt @@ -18,7 +18,7 @@ target_sources(crepe PUBLIC Vector2.cpp Camera.cpp Animator.cpp - # LoopManager.cpp + LoopManager.cpp LoopTimer.cpp ) @@ -44,6 +44,6 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES SceneManager.hpp Camera.h Animator.h - # LoopManager.h + LoopManager.h LoopTimer.h ) diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp index 2e9823f..f0788ab 100644 --- a/src/crepe/api/LoopManager.cpp +++ b/src/crepe/api/LoopManager.cpp @@ -1,5 +1,9 @@ - #include "../facade/SDLContext.h" + +#include "../system/AnimatorSystem.h" +#include "../system/CollisionSystem.h" +#include "../system/ParticleSystem.h" +#include "../system/PhysicsSystem.h" #include "../system/RenderSystem.h" #include "../system/ScriptSystem.h" @@ -7,11 +11,25 @@ #include "LoopTimer.h" using namespace crepe; +using namespace std; + +LoopManager::LoopManager() { + this->load_system<AnimatorSystem>(); + this->load_system<CollisionSystem>(); + this->load_system<ParticleSystem>(); + this->load_system<PhysicsSystem>(); + this->load_system<RenderSystem>(); + this->load_system<ScriptSystem>(); +} + +ComponentManager & LoopManager::get_component_manager() { + return this->component_manager; +} -LoopManager::LoopManager() {} void LoopManager::process_input() { SDLContext::get_instance().handle_events(this->game_running); } + void LoopManager::start() { this->setup(); this->loop(); @@ -48,7 +66,7 @@ void LoopManager::setup() { void LoopManager::render() { if (this->game_running) { - RenderSystem::get_instance().update(); + this->get_system<RenderSystem>().update(); } } diff --git a/src/crepe/api/LoopManager.h b/src/crepe/api/LoopManager.h index 2f03193..ef1c14f 100644 --- a/src/crepe/api/LoopManager.h +++ b/src/crepe/api/LoopManager.h @@ -2,15 +2,9 @@ #include <memory> -class RenderSystem; -class SDLContext; -class LoopTimer; -class ScriptSystem; -class SoundSystem; -class ParticleSystem; -class PhysicsSystem; -class AnimatorSystem; -class CollisionSystem; +#include "../system/System.h" +#include "../ComponentManager.h" + namespace crepe { class LoopManager { @@ -73,7 +67,22 @@ private: void render(); bool game_running = false; - //#TODO add system instances + +protected: + ComponentManager & get_component_manager(); + template <class T> + T & get_system(); + +private: + ComponentManager component_manager; + std::unordered_map<std::type_index, std::unique_ptr<System>> systems; + +private: + template <class T> + void load_system(); }; } // namespace crepe + +#include "LoopManager.hpp" + diff --git a/src/crepe/api/LoopManager.hpp b/src/crepe/api/LoopManager.hpp new file mode 100644 index 0000000..20e8d1c --- /dev/null +++ b/src/crepe/api/LoopManager.hpp @@ -0,0 +1,38 @@ +#pragma once + +#include <cassert> +#include <memory> + +#include "../system/System.h" +#include "../Exception.h" + +#include "LoopManager.h" + +namespace crepe { + +template <class T> +T & LoopManager::get_system() { + using namespace std; + static_assert(is_base_of<System, T>::value, "get_system must recieve a derivative class of System"); + + const type_info & type = typeid(T); + if (!this->systems.contains(type)) + throw Exception("LoopManager: %s is not initialized", type.name()); + + System * system = this->systems.at(type).get(); + T * concrete_system = dynamic_cast<T *>(system); + assert(concrete_system != nullptr); + + return *concrete_system; +} + +template <class T> +void LoopManager::load_system() { + using namespace std; + static_assert(is_base_of<System, T>::value, "load_system must recieve a derivative class of System"); + + System * system = new T(this->component_manager); + this->systems[typeid(T)] = unique_ptr<System>(system); +} + +} // namespace crepe |