diff options
Diffstat (limited to 'src/crepe/api/LoopManager.cpp')
-rw-r--r-- | src/crepe/api/LoopManager.cpp | 45 |
1 files changed, 23 insertions, 22 deletions
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<ShutDownEvent>( [this](const ShutDownEvent & event) { return this->on_shutdown(event); }); } - -void LoopManager::process_input() { - this->get_system<InputSystem>().update(); - this->event_manager.dispatch_events(); -} - void LoopManager::start() { this->setup(); this->loop(); } -void LoopManager::fixed_update() { - this->get_system<ScriptSystem>().update(); - this->get_system<PhysicsSystem>().update(); - this->get_system<CollisionSystem>().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>(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<InputSystem>().update(); + this->event_manager.dispatch_events(); + this->get_system<ScriptSystem>().update(); + this->get_system<PhysicsSystem>().update(); + this->get_system<CollisionSystem>().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<AnimatorSystem>().update(); + //render this->get_system<RenderSystem>().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(); } |