From f64b793ad8e796458c8e175f298e8d13eb3b3459 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Wed, 11 Dec 2024 14:16:40 +0100 Subject: feedback --- src/crepe/api/LoopManager.cpp | 45 ++++++++++++++++++++++--------------------- src/crepe/api/LoopManager.h | 18 +++-------------- 2 files changed, 26 insertions(+), 37 deletions(-) (limited to 'src/crepe/api') diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp index 46a7635..a1da8be 100644 --- a/src/crepe/api/LoopManager.cpp +++ b/src/crepe/api/LoopManager.cpp @@ -7,7 +7,8 @@ #include "../system/PhysicsSystem.h" #include "../system/RenderSystem.h" #include "../system/ScriptSystem.h" -#include "manager/EventManager.h" +#include "../manager/EventManager.h" +#include "../util/Log.h" #include "LoopManager.h" @@ -25,56 +26,56 @@ LoopManager::LoopManager() { this->event_manager.subscribe( [this](const ShutDownEvent & event) { return this->on_shutdown(event); }); } - -void LoopManager::process_input() { - this->get_system().update(); - this->event_manager.dispatch_events(); -} - void LoopManager::start() { this->setup(); this->loop(); } -void LoopManager::fixed_update() { - this->get_system().update(); - this->get_system().update(); - this->get_system().update(); +void LoopManager::setup() { + this->game_running = true; + this->loop_timer.start(); + this->scene_manager.load_next_scene(); } void LoopManager::loop() { + try { while (game_running) { this->loop_timer.update(); while (this->loop_timer.get_lag() >= this->loop_timer.get_fixed_delta_time()) { - this->process_input(); this->fixed_update(); this->loop_timer.advance_fixed_elapsed_time(); } this->frame_update(); - this->render(); this->loop_timer.enforce_frame_rate(); } + }catch(const exception & e){ + Log::logf(Log::Level::ERROR, "Exception caught in main loop: %s", e.what()); + this->event_manager.trigger_event(ShutDownEvent{}); + } } -void LoopManager::setup() { - this->game_running = true; - this->loop_timer.start(); - this->scene_manager.load_next_scene(); +// will be called at a fixed interval +void LoopManager::fixed_update() { + this->get_system().update(); + this->event_manager.dispatch_events(); + this->get_system().update(); + this->get_system().update(); + this->get_system().update(); } -void LoopManager::render() { - if (!this->game_running) return; - +// will be called every frame +void LoopManager::frame_update() { + this->scene_manager.load_next_scene(); this->get_system().update(); + //render this->get_system().update(); } bool LoopManager::on_shutdown(const ShutDownEvent & e) { this->game_running = false; + // propagate to possible user ShutDownEvent listeners return false; } - -void LoopManager::frame_update() { this->scene_manager.load_next_scene(); } diff --git a/src/crepe/api/LoopManager.h b/src/crepe/api/LoopManager.h index f94cea1..2319d65 100644 --- a/src/crepe/api/LoopManager.h +++ b/src/crepe/api/LoopManager.h @@ -25,7 +25,7 @@ public: * \brief Start the gameloop * * This is the start of the engine where the setup is called and then the loop keeps running until the game stops running. - * Developers need to call this function to run the game. + * The Game programmer needs to call this function to run the game. This should be done after creating and adding all scenes. */ void start(); @@ -51,13 +51,6 @@ private: */ void loop(); - /** - * \brief Function for handling input-related system calls. - * - * Processes user inputs from keyboard and mouse. - */ - void process_input(); - /** * \brief Per-frame update. * @@ -71,15 +64,9 @@ private: * This function updates physics and game logic based on LoopTimer's fixed_delta_time. */ virtual void fixed_update(); - /** - * \brief Function for executing render-related systems. - * - * Renders the current state of the game to the screen. - */ - virtual void render(); + //! Indicates whether the game is running. bool game_running = false; - private: //! Global context Mediator mediator; @@ -97,6 +84,7 @@ private: SDLContext & sdl_context = SDLContext::get_instance(); private: + /** * \brief Callback function for ShutDownEvent * -- cgit v1.2.3