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 | |
parent | 455bb50a5007daf46b8719fff2a6292da6a294bf (diff) |
re-enable loop manager + remove more singletons
-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 | ||||
-rw-r--r-- | src/crepe/system/AnimatorSystem.cpp | 12 | ||||
-rw-r--r-- | src/crepe/system/AnimatorSystem.h | 17 | ||||
-rw-r--r-- | src/crepe/system/CMakeLists.txt | 12 | ||||
-rw-r--r-- | src/crepe/system/CollisionSystem.cpp | 3 | ||||
-rw-r--r-- | src/crepe/system/CollisionSystem.h | 8 | ||||
-rw-r--r-- | src/crepe/system/RenderSystem.cpp | 14 | ||||
-rw-r--r-- | src/crepe/system/RenderSystem.h | 13 | ||||
-rw-r--r-- | src/crepe/system/System.cpp | 6 |
12 files changed, 104 insertions, 76 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 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 <cstdint> #include <functional> #include <vector> #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<std::reference_wrapper<Animator>> animations = mgr.get_components_by_type<Animator>(); 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<std::reference_wrapper<Camera>> cameras = mgr.get_components_by_type<Camera>(); @@ -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<std::reference_wrapper<Sprite>> sprites = mgr.get_components_by_type<Sprite>(); 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(); +} |