diff options
Diffstat (limited to 'src/crepe/api')
-rw-r--r-- | src/crepe/api/LoopManager.cpp | 12 | ||||
-rw-r--r-- | src/crepe/api/LoopManager.h | 5 | ||||
-rw-r--r-- | src/crepe/api/LoopTimer.cpp | 27 | ||||
-rw-r--r-- | src/crepe/api/LoopTimer.h | 6 |
4 files changed, 22 insertions, 28 deletions
diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp index 040cb93..5879d79 100644 --- a/src/crepe/api/LoopManager.cpp +++ b/src/crepe/api/LoopManager.cpp @@ -1,12 +1,12 @@ #include "../facade/SDLContext.h" +#include "../manager/EventManager.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" -#include "../manager/EventManager.h" #include "LoopManager.h" @@ -20,9 +20,8 @@ LoopManager::LoopManager() { this->load_system<PhysicsSystem>(); this->load_system<RenderSystem>(); this->load_system<ScriptSystem>(); - EventManager::get_instance().subscribe<ShutDownEvent>([this](const ShutDownEvent& event) { - return this->on_shutdown(event); - }); + EventManager::get_instance().subscribe<ShutDownEvent>( + [this](const ShutDownEvent & event) { return this->on_shutdown(event); }); this->loop_timer = make_unique<LoopTimer>(); this->mediator.loop_timer = *loop_timer; } @@ -41,7 +40,7 @@ void LoopManager::loop() { 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(); @@ -55,7 +54,6 @@ void LoopManager::loop() { } void LoopManager::setup() { - this->game_running = true; this->loop_timer->start(); @@ -67,7 +65,7 @@ void LoopManager::render() { this->get_system<RenderSystem>().update(); } -bool LoopManager::on_shutdown(const ShutDownEvent & e){ +bool LoopManager::on_shutdown(const ShutDownEvent & e) { this->game_running = false; return false; } diff --git a/src/crepe/api/LoopManager.h b/src/crepe/api/LoopManager.h index c50f5aa..6a212eb 100644 --- a/src/crepe/api/LoopManager.h +++ b/src/crepe/api/LoopManager.h @@ -5,10 +5,8 @@ #include "../facade/SDLContext.h" #include "../manager/ComponentManager.h" #include "../manager/SceneManager.h" -#include "../manager/SceneManager.h" #include "../system/System.h" - #include "api/Event.h" #include "api/LoopTimer.h" @@ -28,7 +26,6 @@ public: * Developers need to call this function to run the game. */ void start(); - /** * \brief Add a new concrete scene to the scene manager @@ -101,8 +98,8 @@ private: SDLContext & sdl_context = SDLContext::get_instance(); //! loop timer instance std::unique_ptr<LoopTimer> loop_timer; + private: - /** * \brief Callback function for ShutDownEvent * diff --git a/src/crepe/api/LoopTimer.cpp b/src/crepe/api/LoopTimer.cpp index eedb5ee..8fb7ce8 100644 --- a/src/crepe/api/LoopTimer.cpp +++ b/src/crepe/api/LoopTimer.cpp @@ -10,13 +10,13 @@ using namespace crepe; LoopTimer::LoopTimer() { dbg_trace(); } - void LoopTimer::start() { this->last_frame_time = std::chrono::steady_clock::now(); - + this->elapsed_time = std::chrono::milliseconds(0); // by starting the elapsed_fixed_time at (0 - fixed_delta_time) in milliseconds it calls a fixed update at the start of the loop. - this->elapsed_fixed_time = -std::chrono::duration_cast<std::chrono::milliseconds>(fixed_delta_time); + this->elapsed_fixed_time + = -std::chrono::duration_cast<std::chrono::milliseconds>(fixed_delta_time); this->delta_time = std::chrono::milliseconds(0); } @@ -30,7 +30,7 @@ void LoopTimer::update() { this->delta_time = this->maximum_delta_time; } this->actual_fps = 1.0 / this->delta_time.count(); - + this->delta_time *= this->game_scale; this->elapsed_time += this->delta_time; this->last_frame_time = current_frame_time; @@ -56,17 +56,18 @@ void LoopTimer::set_game_scale(double value) { this->game_scale = value; } double LoopTimer::get_game_scale() const { return this->game_scale; } void LoopTimer::enforce_frame_rate() { - auto current_frame_time = std::chrono::steady_clock::now(); - auto frame_duration = current_frame_time - this->last_frame_time; + auto current_frame_time = std::chrono::steady_clock::now(); + auto frame_duration = current_frame_time - this->last_frame_time; - // Check if frame duration is less than the target frame time - if (frame_duration < this->frame_target_time) { - auto delay_time = std::chrono::duration_cast<std::chrono::microseconds>(this->frame_target_time - frame_duration); + // Check if frame duration is less than the target frame time + if (frame_duration < this->frame_target_time) { + auto delay_time = std::chrono::duration_cast<std::chrono::microseconds>( + this->frame_target_time - frame_duration); - if (delay_time.count() > 0) { - std::this_thread::sleep_for(delay_time); - } - } + if (delay_time.count() > 0) { + std::this_thread::sleep_for(delay_time); + } + } } double LoopTimer::get_lag() const { diff --git a/src/crepe/api/LoopTimer.h b/src/crepe/api/LoopTimer.h index e348628..c4294d7 100644 --- a/src/crepe/api/LoopTimer.h +++ b/src/crepe/api/LoopTimer.h @@ -90,8 +90,6 @@ private: */ double get_lag() const; - - /** * \brief Update the timer to the current frame. * @@ -120,7 +118,8 @@ private: //! Delta time for the current frame in seconds std::chrono::duration<double> delta_time{0.0}; //! Target time per frame in seconds - std::chrono::duration<double> frame_target_time = std::chrono::duration<double>(1.0) / target_fps; + std::chrono::duration<double> frame_target_time + = std::chrono::duration<double>(1.0) / target_fps; //! Fixed delta time for fixed updates in seconds std::chrono::duration<double> fixed_delta_time = std::chrono::duration<double>(1.0) / 50.0; //! Total elapsed game time in seconds @@ -129,7 +128,6 @@ private: std::chrono::duration<double> elapsed_fixed_time{0.0}; //! Time of the last frame std::chrono::steady_clock::time_point last_frame_time; - }; } // namespace crepe |