From ec3601fbc17a38a44608a04f53d4e36c1bff8c96 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 13 Nov 2024 12:12:26 +0100 Subject: re-enable loop manager + remove more singletons --- src/crepe/api/CMakeLists.txt | 4 ++-- src/crepe/api/LoopManager.cpp | 24 ++++++++++++++++++++--- src/crepe/api/LoopManager.h | 29 +++++++++++++++++---------- src/crepe/api/LoopManager.hpp | 38 ++++++++++++++++++++++++++++++++++++ src/crepe/system/AnimatorSystem.cpp | 12 +----------- src/crepe/system/AnimatorSystem.h | 17 ++-------------- src/crepe/system/CMakeLists.txt | 12 ++++++------ src/crepe/system/CollisionSystem.cpp | 3 +-- src/crepe/system/CollisionSystem.h | 8 +++++--- src/crepe/system/RenderSystem.cpp | 14 ++----------- src/crepe/system/RenderSystem.h | 13 ++---------- src/crepe/system/System.cpp | 6 +++++- 12 files changed, 104 insertions(+), 76 deletions(-) create mode 100644 src/crepe/api/LoopManager.hpp 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(); + this->load_system(); + this->load_system(); + this->load_system(); + this->load_system(); + this->load_system(); +} + +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().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 -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 + T & get_system(); + +private: + ComponentManager component_manager; + std::unordered_map> systems; + +private: + template + 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 +#include + +#include "../system/System.h" +#include "../Exception.h" + +#include "LoopManager.h" + +namespace crepe { + +template +T & LoopManager::get_system() { + using namespace std; + static_assert(is_base_of::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(system); + assert(concrete_system != nullptr); + + return *concrete_system; +} + +template +void LoopManager::load_system() { + using namespace std; + static_assert(is_base_of::value, "load_system must recieve a derivative class of System"); + + System * system = new T(this->component_manager); + this->systems[typeid(T)] = unique_ptr(system); +} + +} // namespace crepe diff --git a/src/crepe/system/AnimatorSystem.cpp b/src/crepe/system/AnimatorSystem.cpp index bf45362..9d18873 100644 --- a/src/crepe/system/AnimatorSystem.cpp +++ b/src/crepe/system/AnimatorSystem.cpp @@ -1,27 +1,17 @@ - #include #include #include #include "api/Animator.h" #include "facade/SDLContext.h" -#include "util/log.h" #include "AnimatorSystem.h" #include "ComponentManager.h" using namespace crepe; -AnimatorSystem::AnimatorSystem() { dbg_trace(); } -AnimatorSystem::~AnimatorSystem() { dbg_trace(); } - -AnimatorSystem & AnimatorSystem::get_instance() { - static AnimatorSystem instance; - return instance; -} - void AnimatorSystem::update() { - ComponentManager & mgr = ComponentManager::get_instance(); + ComponentManager & mgr = this->component_manager; std::vector> animations = mgr.get_components_by_type(); diff --git a/src/crepe/system/AnimatorSystem.h b/src/crepe/system/AnimatorSystem.h index 969e9d1..aa97084 100644 --- a/src/crepe/system/AnimatorSystem.h +++ b/src/crepe/system/AnimatorSystem.h @@ -17,16 +17,7 @@ namespace crepe { class AnimatorSystem : public System { public: - /** - * \brief Retrieves the singleton instance of the AnimatorSystem. - * - * \return A reference to the single instance of the AnimatorSystem. - * - * This method ensures that there is only one instance of the AnimatorSystem, following the - * singleton design pattern. It can be used to access the system globally. - */ - static AnimatorSystem & get_instance(); - + using System::System; /** * \brief Updates the Animator components. * @@ -34,11 +25,7 @@ public: * Animator components, moving the animations forward and managing their behavior (e.g., looping). */ void update() override; - -private: - // private because singleton - AnimatorSystem(); // dbg_trace - ~AnimatorSystem(); // dbg_trace + // FIXME: never say "likely" in the documentation lmao }; } // namespace crepe diff --git a/src/crepe/system/CMakeLists.txt b/src/crepe/system/CMakeLists.txt index 2fb58dc..d658b25 100644 --- a/src/crepe/system/CMakeLists.txt +++ b/src/crepe/system/CMakeLists.txt @@ -3,16 +3,16 @@ target_sources(crepe PUBLIC ParticleSystem.cpp ScriptSystem.cpp PhysicsSystem.cpp - # CollisionSystem.cpp - # RenderSystem.cpp - # AnimatorSystem.cpp + CollisionSystem.cpp + RenderSystem.cpp + AnimatorSystem.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES System.h ScriptSystem.h PhysicsSystem.h - # CollisionSystem.h - # RenderSystem.h - # AnimatorSystem.h + CollisionSystem.h + RenderSystem.h + AnimatorSystem.h ) diff --git a/src/crepe/system/CollisionSystem.cpp b/src/crepe/system/CollisionSystem.cpp index 55e0fdc..67f535a 100644 --- a/src/crepe/system/CollisionSystem.cpp +++ b/src/crepe/system/CollisionSystem.cpp @@ -2,6 +2,5 @@ using namespace crepe; -CollisionSystem::CollisionSystem() {} - void CollisionSystem::update() {} + diff --git a/src/crepe/system/CollisionSystem.h b/src/crepe/system/CollisionSystem.h index 1e9f1aa..c1a70d8 100644 --- a/src/crepe/system/CollisionSystem.h +++ b/src/crepe/system/CollisionSystem.h @@ -1,11 +1,13 @@ #pragma once +#include "System.h" + namespace crepe { -class CollisionSystem { +class CollisionSystem : public System { public: - CollisionSystem(); - void update(); + using System::System; + void update() override; }; } // namespace crepe diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 10211a3..3e6360c 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -11,15 +11,6 @@ using namespace crepe; -RenderSystem::RenderSystem() { dbg_trace(); } - -RenderSystem::~RenderSystem() { dbg_trace(); } - -RenderSystem & RenderSystem::get_instance() { - static RenderSystem instance; - return instance; -} - void RenderSystem::clear_screen() const { SDLContext::get_instance().clear_screen(); } @@ -28,7 +19,7 @@ void RenderSystem::present_screen() const { SDLContext::get_instance().present_screen(); } void RenderSystem::update_camera() { - ComponentManager & mgr = ComponentManager::get_instance(); + ComponentManager & mgr = this->component_manager; std::vector> cameras = mgr.get_components_by_type(); @@ -39,8 +30,7 @@ void RenderSystem::update_camera() { } } void RenderSystem::render_sprites() const { - - ComponentManager & mgr = ComponentManager::get_instance(); + ComponentManager & mgr = this->component_manager; std::vector> sprites = mgr.get_components_by_type(); diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index 70db21a..da4e910 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -15,14 +15,8 @@ namespace crepe { * rendering services for the application. */ class RenderSystem : public System { - public: - /** - * \brief Gets the singleton instance of RenderSystem. - * \return Reference to the RenderSystem instance. - */ - static RenderSystem & get_instance(); - + using System::System; /** * \brief Updates the RenderSystem for the current frame. * This method is called to perform all rendering operations for the current game frame. @@ -30,10 +24,6 @@ public: void update() override; private: - // Private constructor to enforce singleton pattern. - RenderSystem(); - ~RenderSystem(); - //! Clears the screen in preparation for rendering. void clear_screen() const; @@ -61,4 +51,5 @@ private: Camera * curr_cam = nullptr; // TODO: needs a better solution }; + } // namespace crepe diff --git a/src/crepe/system/System.cpp b/src/crepe/system/System.cpp index 296f1ed..31b1337 100644 --- a/src/crepe/system/System.cpp +++ b/src/crepe/system/System.cpp @@ -1,6 +1,10 @@ +#include "../util/log.h" + #include "System.h" using namespace crepe; -System::System(ComponentManager & mgr) : component_manager(mgr) {} +System::System(ComponentManager & mgr) : component_manager(mgr) { + dbg_trace(); +} -- cgit v1.2.3