From 000062b462a3af86db4dac4d8c9e5ef32feb2996 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 11 Dec 2024 21:19:57 +0100 Subject: split up loopmanager into SystemManager and Engine --- src/crepe/api/CMakeLists.txt | 5 +- src/crepe/api/Engine.cpp | 37 ++++++++++++++ src/crepe/api/Engine.h | 73 ++++++++++++++++++++++++++++ src/crepe/api/Engine.hpp | 12 +++++ src/crepe/api/LoopManager.cpp | 75 ----------------------------- src/crepe/api/LoopManager.h | 109 ------------------------------------------ src/crepe/api/LoopManager.hpp | 46 ------------------ src/crepe/api/LoopTimer.h | 2 +- 8 files changed, 126 insertions(+), 233 deletions(-) create mode 100644 src/crepe/api/Engine.cpp create mode 100644 src/crepe/api/Engine.h create mode 100644 src/crepe/api/Engine.hpp delete mode 100644 src/crepe/api/LoopManager.cpp delete mode 100644 src/crepe/api/LoopManager.h delete mode 100644 src/crepe/api/LoopManager.hpp (limited to 'src/crepe/api') diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt index 46deb67..6e062f9 100644 --- a/src/crepe/api/CMakeLists.txt +++ b/src/crepe/api/CMakeLists.txt @@ -15,7 +15,7 @@ target_sources(crepe PUBLIC CircleCollider.cpp IKeyListener.cpp IMouseListener.cpp - LoopManager.cpp + Engine.cpp LoopTimer.cpp Asset.cpp EventHandler.cpp @@ -49,7 +49,8 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES Event.h IKeyListener.h IMouseListener.h - LoopManager.h + Engine.h + Engine.hpp LoopTimer.h Asset.h Button.h diff --git a/src/crepe/api/Engine.cpp b/src/crepe/api/Engine.cpp new file mode 100644 index 0000000..8ed55fa --- /dev/null +++ b/src/crepe/api/Engine.cpp @@ -0,0 +1,37 @@ +#include "Engine.h" + +using namespace crepe; +using namespace std; + +void Engine::start() { + this->setup(); + this->loop(); +} + +void Engine::setup() { + LoopTimer & timer = this->loop_timer; + this->game_running = true; + this->scene_manager.load_next_scene(); + timer.start(); + timer.set_fps(200); +} + +void Engine::loop() { + LoopTimer & timer = this->loop_timer; + SystemManager & systems = this->system_manager; + + timer.start(); + + while (game_running) { + timer.update(); + + while (timer.get_lag() >= timer.get_fixed_delta_time()) { + systems.fixed_update(); + timer.advance_fixed_update(); + } + + systems.frame_update(); + timer.enforce_frame_rate(); + } +} + diff --git a/src/crepe/api/Engine.h b/src/crepe/api/Engine.h new file mode 100644 index 0000000..7601015 --- /dev/null +++ b/src/crepe/api/Engine.h @@ -0,0 +1,73 @@ +#pragma once + +#include "../facade/SDLContext.h" +#include "../manager/ComponentManager.h" +#include "../manager/ReplayManager.h" +#include "../manager/ResourceManager.h" +#include "../manager/ResourceManager.h" +#include "../manager/SaveManager.h" +#include "../manager/SceneManager.h" +#include "../manager/SystemManager.h" + +#include "LoopTimer.h" + +namespace crepe { + +/** + * \brief Main game entrypoint + * + * This class is responsible for managing the game loop, including initialization and updating. + */ +class Engine { +public: + void start(); + + /** + * \brief Add a new concrete scene to the scene manager + * + * \tparam T Type of concrete scene + */ + template + void add_scene(); + +private: + /** + * \brief Setup function for one-time initialization. + * + * This function initializes necessary components for the game. + */ + void setup(); + /** + * \brief Main game loop function. + * + * This function runs the main loop, handling game updates and rendering. + */ + void loop(); + + bool game_running = false; + +private: + //! Global context + Mediator mediator; + + //! Component manager instance + ComponentManager component_manager{mediator}; + //! Scene manager instance + SceneManager scene_manager{mediator}; + //! Resource manager instance + ResourceManager resource_manager{mediator}; + //! Save manager instance + SaveManager save_manager{mediator}; + //! SDLContext instance + SDLContext sdl_context{mediator}; + //! LoopTimer instance + LoopTimer loop_timer{mediator}; + //! ReplayManager instance + ReplayManager replay_manager{mediator}; + //! SystemManager + SystemManager system_manager{mediator}; +}; + +} // namespace crepe + +#include "Engine.hpp" diff --git a/src/crepe/api/Engine.hpp b/src/crepe/api/Engine.hpp new file mode 100644 index 0000000..f2fdc0a --- /dev/null +++ b/src/crepe/api/Engine.hpp @@ -0,0 +1,12 @@ +#pragma once + +#include "Engine.h" + +namespace crepe { + +template +void Engine::add_scene() { + this->scene_manager.add_scene(); +} + +} // namespace crepe diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp deleted file mode 100644 index 2855455..0000000 --- a/src/crepe/api/LoopManager.cpp +++ /dev/null @@ -1,75 +0,0 @@ -#include "../system/AISystem.h" -#include "../system/AnimatorSystem.h" -#include "../system/AudioSystem.h" -#include "../system/CollisionSystem.h" -#include "../system/InputSystem.h" -#include "../system/ParticleSystem.h" -#include "../system/PhysicsSystem.h" -#include "../system/RenderSystem.h" -#include "../system/ScriptSystem.h" -#include "../system/EventSystem.h" - -#include "LoopManager.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(); - this->load_system(); - this->load_system(); - this->load_system(); - this->load_system(); - - this->mediator.loop_manager = *this; -} - -void LoopManager::start() { - this->setup(); - this->loop(); -} - -void LoopManager::fixed_update() { - for (auto & [type, system] : this->systems) { - if (!system->active) continue; - system->fixed_update(); - } -} - -void LoopManager::frame_update() { - for (auto & [type, system] : this->systems) { - if (!system->active) continue; - system->frame_update(); - } -} - -void LoopManager::setup() { - LoopTimer & timer = this->loop_timer; - this->game_running = true; - this->scene_manager.load_next_scene(); - timer.start(); - timer.set_fps(200); -} - -void LoopManager::loop() { - LoopTimer & timer = this->loop_timer; - timer.start(); - - while (game_running) { - timer.update(); - - while (timer.get_lag() >= timer.get_fixed_delta_time()) { - this->fixed_update(); - timer.advance_fixed_update(); - } - - this->frame_update(); - timer.enforce_frame_rate(); - } -} - diff --git a/src/crepe/api/LoopManager.h b/src/crepe/api/LoopManager.h deleted file mode 100644 index f34a5a0..0000000 --- a/src/crepe/api/LoopManager.h +++ /dev/null @@ -1,109 +0,0 @@ -#pragma once - -#include "../facade/SDLContext.h" -#include "../manager/ComponentManager.h" -#include "../manager/ReplayManager.h" -#include "../manager/ResourceManager.h" -#include "../manager/ResourceManager.h" -#include "../manager/SaveManager.h" -#include "../manager/SceneManager.h" -#include "../system/System.h" - -#include "LoopTimer.h" - -namespace crepe { - -/** - * \brief Main game loop manager - * - * This class is responsible for managing the game loop, including initialization and updating. - */ -class LoopManager { -public: - void start(); - LoopManager(); - - /** - * \brief Add a new concrete scene to the scene manager - * - * \tparam T Type of concrete scene - */ - template - void add_scene(); - -private: - /** - * \brief Setup function for one-time initialization. - * - * This function initializes necessary components for the game. - */ - void setup(); - /** - * \brief Main game loop function. - * - * This function runs the main loop, handling game updates and rendering. - */ - void loop(); - - /** - * \brief Per-frame update. - * - * Updates the game state based on the elapsed time since the last frame. - */ - void frame_update(); - - /** - * \brief Fixed update executed at a fixed rate. - * - * This function updates physics and game logic based on LoopTimer's fixed_delta_time. - */ - void fixed_update(); - - bool game_running = false; - -private: - //! Global context - Mediator mediator; - - //! Component manager instance - ComponentManager component_manager{mediator}; - //! Scene manager instance - SceneManager scene_manager{mediator}; - //! Resource manager instance - ResourceManager resource_manager{mediator}; - //! Save manager instance - SaveManager save_manager{mediator}; - //! SDLContext instance - SDLContext sdl_context{mediator}; - //! LoopTimer instance - LoopTimer loop_timer{mediator}; - //! ReplayManager instance - ReplayManager replay_manager{mediator}; - -private: - /** - * \brief Collection of System instances - * - * This map holds System instances indexed by the system's class typeid. It is filled in the - * constructor of \c LoopManager using LoopManager::load_system. - */ - std::unordered_map> systems; - /** - * \brief Initialize a system - * \tparam T System type (must be derivative of \c System) - */ - template - void load_system(); - /** - * \brief Retrieve a reference to ECS system - * \tparam T System type - * \returns Reference to system instance - * \throws std::runtime_error if the System is not initialized - */ - template - T & get_system(); -}; - -} // namespace crepe - -#include "LoopManager.hpp" diff --git a/src/crepe/api/LoopManager.hpp b/src/crepe/api/LoopManager.hpp deleted file mode 100644 index 627b281..0000000 --- a/src/crepe/api/LoopManager.hpp +++ /dev/null @@ -1,46 +0,0 @@ -#pragma once - -#include -#include -#include - -#include "../system/System.h" -#include "LoopManager.h" - -namespace crepe { - -template -void LoopManager::add_scene() { - this->scene_manager.add_scene(); -} - -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 runtime_error(format("LoopManager: {} 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"); - - const type_info & type = typeid(T); - if (this->systems.contains(type)) - throw runtime_error(format("LoopManager: {} is already initialized", type.name())); - System * system = new T(this->mediator); - this->systems[type] = unique_ptr(system); -} - -} // namespace crepe diff --git a/src/crepe/api/LoopTimer.h b/src/crepe/api/LoopTimer.h index 0a73a4c..fb52a10 100644 --- a/src/crepe/api/LoopTimer.h +++ b/src/crepe/api/LoopTimer.h @@ -54,7 +54,7 @@ public: void set_game_scale(double game_scale); private: - friend class LoopManager; + friend class Engine; /** * \brief Start the loop timer. -- cgit v1.2.3