From 5a43793e247fbffec590d334b89cc34d19049f45 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Wed, 4 Dec 2024 16:38:15 +0100 Subject: gameloop tests --- src/crepe/api/LoopManager.cpp | 10 +++++++--- src/crepe/api/LoopManager.h | 5 ++++- src/crepe/api/LoopTimer.cpp | 17 +++++++++-------- 3 files changed, 20 insertions(+), 12 deletions(-) (limited to 'src/crepe') diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp index 4a6d2cd..e584ba7 100644 --- a/src/crepe/api/LoopManager.cpp +++ b/src/crepe/api/LoopManager.cpp @@ -1,3 +1,5 @@ +#include + #include "../facade/SDLContext.h" #include "../system/AnimatorSystem.h" @@ -60,12 +62,12 @@ void LoopManager::loop() { void LoopManager::setup() { this->game_running = true; this->loop_timer->start(); - this->loop_timer->set_fps(60); + this->loop_timer->set_target_fps(60); } void LoopManager::render() { if (this->game_running) { - this->get_system().update(); + //this->get_system().update(); } } bool LoopManager::on_shutdown(const ShutDownEvent & e){ @@ -73,4 +75,6 @@ bool LoopManager::on_shutdown(const ShutDownEvent & e){ return false; } -void LoopManager::update() {} +void LoopManager::update() { + std::cout << this->loop_timer->get_fps() << std::endl; +} diff --git a/src/crepe/api/LoopManager.h b/src/crepe/api/LoopManager.h index ff1ff55..3bf54b9 100644 --- a/src/crepe/api/LoopManager.h +++ b/src/crepe/api/LoopManager.h @@ -5,9 +5,10 @@ #include "../ComponentManager.h" #include "../system/System.h" #include "api/SceneManager.h" +#include "api/Event.h" +#include "api/LoopTimer.h" namespace crepe { -class LoopTimer; /** * \brief Main game loop manager * @@ -91,7 +92,9 @@ private: SceneManager scene_manager{component_manager}; private: + //! loop timer instance std::unique_ptr loop_timer; + //! callback function for shutdown event bool on_shutdown(const ShutDownEvent & e); /** * \brief Collection of System instances diff --git a/src/crepe/api/LoopTimer.cpp b/src/crepe/api/LoopTimer.cpp index fe5544d..15011ca 100644 --- a/src/crepe/api/LoopTimer.cpp +++ b/src/crepe/api/LoopTimer.cpp @@ -1,4 +1,5 @@ #include +#include #include "../facade/SDLContext.h" #include "../util/Log.h" @@ -56,14 +57,14 @@ void LoopTimer::enforce_frame_rate() { auto current_frame_time = std::chrono::steady_clock::now(); auto frame_duration = current_frame_time - this->last_frame_time; - if (frame_duration < this->frame_target_time) { - std::chrono::milliseconds delay_time - = std::chrono::duration_cast(this->frame_target_time - - frame_duration); - if (delay_time.count() > 0) { - SDLContext::get_instance().delay(delay_time.count()); - } - } + // 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(this->frame_target_time - frame_duration); + + if (delay_time.count() > 0) { + std::this_thread::sleep_for(delay_time); + } + } } double LoopTimer::get_lag() const { -- cgit v1.2.3