From 020cdfddcd06e162515deee4941ce91f3a945ee6 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Sun, 24 Nov 2024 16:38:58 +0100 Subject: fps counter functionality --- src/crepe/api/LoopManager.cpp | 2 +- src/crepe/api/LoopTimer.cpp | 13 ++++++------- src/crepe/api/LoopTimer.h | 11 +++++++---- 3 files changed, 14 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp index 7edf4d1..df09f7e 100644 --- a/src/crepe/api/LoopManager.cpp +++ b/src/crepe/api/LoopManager.cpp @@ -57,7 +57,7 @@ void LoopManager::loop() { void LoopManager::setup() { this->game_running = true; LoopTimer::get_instance().start(); - LoopTimer::get_instance().set_fps(200); + LoopTimer::get_instance().set_target_fps(200); } void LoopManager::render() { diff --git a/src/crepe/api/LoopTimer.cpp b/src/crepe/api/LoopTimer.cpp index 15a0e3a..f94e24b 100644 --- a/src/crepe/api/LoopTimer.cpp +++ b/src/crepe/api/LoopTimer.cpp @@ -30,7 +30,8 @@ void LoopTimer::update() { if (this->delta_time > this->maximum_delta_time) { 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; @@ -44,13 +45,13 @@ void LoopTimer::advance_fixed_update() { this->elapsed_fixed_time += this->fixed double LoopTimer::get_fixed_delta_time() const { return this->fixed_delta_time.count(); } -void LoopTimer::set_fps(int fps) { - this->fps = fps; +void LoopTimer::set_target_fps(int fps) { + this->target_fps = fps; // target time per frame in seconds - this->frame_target_time = std::chrono::duration(1.0) / fps; + this->frame_target_time = std::chrono::duration(1.0) / target_fps; } -int LoopTimer::get_fps() const { return this->fps; } +int LoopTimer::get_fps() const { return this->actual_fps; } void LoopTimer::set_game_scale(double value) { this->game_scale = value; } @@ -70,8 +71,6 @@ void LoopTimer::enforce_frame_rate() { SDLContext::get_instance().delay(delay_time.count()); } } - - this->last_frame_time = current_frame_time; } double LoopTimer::get_lag() const { diff --git a/src/crepe/api/LoopTimer.h b/src/crepe/api/LoopTimer.h index 9393439..eea60bb 100644 --- a/src/crepe/api/LoopTimer.h +++ b/src/crepe/api/LoopTimer.h @@ -35,7 +35,7 @@ public: * * \param fps The desired frames rendered per second. */ - void set_fps(int fps); + void set_target_fps(int fps); /** * \brief Get the current frames per second (FPS). @@ -121,8 +121,10 @@ private: void advance_fixed_update(); private: - //! Current frames per second - int fps = 50; + //! Target frames per second + int target_fps = 50; + //! Actual frames per second + int actual_fps = 0; //! Current game scale double game_scale = 1; //! Maximum delta time in seconds to avoid large jumps @@ -130,7 +132,7 @@ private: //! Delta time for the current frame in seconds std::chrono::duration delta_time{0.0}; //! Target time per frame in seconds - std::chrono::duration frame_target_time = std::chrono::duration(1.0) / fps; + std::chrono::duration frame_target_time = std::chrono::duration(1.0) / target_fps; //! Fixed delta time for fixed updates in seconds std::chrono::duration fixed_delta_time = std::chrono::duration(1.0) / 50.0; //! Total elapsed game time in seconds @@ -139,6 +141,7 @@ private: std::chrono::duration elapsed_fixed_time{0.0}; //! Time of the last frame std::chrono::steady_clock::time_point last_frame_time; + }; } // namespace crepe -- cgit v1.2.3