diff options
Diffstat (limited to 'src/crepe/api/LoopManager.cpp')
-rw-r--r-- | src/crepe/api/LoopManager.cpp | 33 |
1 files changed, 19 insertions, 14 deletions
diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp index 044f096..c25e31e 100644 --- a/src/crepe/api/LoopManager.cpp +++ b/src/crepe/api/LoopManager.cpp @@ -1,3 +1,6 @@ +#include "../facade/SDLContext.h" + +#include "../manager/EventManager.h" #include "../system/AnimatorSystem.h" #include "../system/CollisionSystem.h" #include "../system/InputSystem.h" @@ -20,18 +23,20 @@ LoopManager::LoopManager() { this->load_system<RenderSystem>(); this->load_system<ScriptSystem>(); this->load_system<InputSystem>(); + this->event_manager.subscribe<ShutDownEvent>( + [this](const ShutDownEvent & event) { return this->on_shutdown(event); }); } -void LoopManager::process_input() { this->get_system<InputSystem>().update(); } +void LoopManager::process_input() { + this->get_system<InputSystem>().update(); +} void LoopManager::start() { this->setup(); this->loop(); } -void LoopManager::set_running(bool running) { this->game_running = running; } void LoopManager::fixed_update() { - // TODO: retrieve EventManager from direct member after singleton refactor EventManager & ev = this->mediator.event_manager; ev.dispatch_events(); this->get_system<ScriptSystem>().update(); @@ -40,31 +45,27 @@ void LoopManager::fixed_update() { } void LoopManager::loop() { - LoopTimer & timer = this->loop_timer; - timer.start(); + while (game_running) { - timer.update(); + this->loop_timer.update(); - while (timer.get_lag() >= timer.get_fixed_delta_time()) { + while (this->loop_timer.get_lag() >= this->loop_timer.get_fixed_delta_time()) { this->process_input(); + event_manager.dispatch_events(); this->fixed_update(); - timer.advance_fixed_update(); + this->loop_timer.advance_fixed_update(); } this->update(); this->render(); - - timer.enforce_frame_rate(); + this->loop_timer.enforce_frame_rate(); } } void LoopManager::setup() { - LoopTimer & timer = this->loop_timer; this->game_running = true; - this->scene_manager.load_next_scene(); - timer.start(); - timer.set_fps(200); + this->loop_timer.start(); this->scene_manager.load_next_scene(); } @@ -74,5 +75,9 @@ void LoopManager::render() { this->get_system<AnimatorSystem>().update(); this->get_system<RenderSystem>().update(); } +bool LoopManager::on_shutdown(const ShutDownEvent & e) { + this->game_running = false; + return false; +} void LoopManager::update() {} |