From 149b3b5dbc8b18a142bf35869e54ab05e8f26d54 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Mon, 11 Nov 2024 20:21:31 +0100 Subject: added chrono::duration --- src/crepe/api/LoopTimer.cpp | 90 +++++++++++++++++++++++++++------------------ 1 file changed, 55 insertions(+), 35 deletions(-) (limited to 'src/crepe/api/LoopTimer.cpp') diff --git a/src/crepe/api/LoopTimer.cpp b/src/crepe/api/LoopTimer.cpp index 16d813f..039e35e 100644 --- a/src/crepe/api/LoopTimer.cpp +++ b/src/crepe/api/LoopTimer.cpp @@ -1,3 +1,4 @@ +#include #include "../facade/SDLContext.h" #include "../util/log.h" @@ -6,64 +7,83 @@ using namespace crepe; -LoopTimer::LoopTimer() { dbg_trace(); } +LoopTimer::LoopTimer() { + dbg_trace(); +} LoopTimer & LoopTimer::get_instance() { - static LoopTimer instance; - return instance; + static LoopTimer instance; + return instance; } void LoopTimer::start() { - this->last_frame_time = SDLContext::get_instance().get_ticks(); - this->elapsed_time = 0; - this->elapsed_fixed_time = 0; - this->delta_time = 0; + this->last_frame_time = std::chrono::steady_clock::now(); + this->elapsed_time = std::chrono::milliseconds(0); + this->elapsed_fixed_time = std::chrono::milliseconds(0); + this->delta_time = std::chrono::milliseconds(0); } void LoopTimer::update() { - uint64_t current_frame_time = SDLContext::get_instance().get_ticks(); - this->delta_time = (current_frame_time - last_frame_time); - - if (this->delta_time > this->maximum_delta_time) { - this->delta_time = this->maximum_delta_time; - } - this->delta_time *= this->game_scale; - this->elapsed_time += this->delta_time; - this->last_frame_time = current_frame_time; + auto current_frame_time = std::chrono::steady_clock::now(); + // Convert to duration in seconds for delta time + this->delta_time = std::chrono::duration_cast>(current_frame_time - last_frame_time); + + if (this->delta_time > this->maximum_delta_time) { + this->delta_time = this->maximum_delta_time; + } + + this->delta_time *= this->game_scale; + this->elapsed_time += this->delta_time; + this->last_frame_time = current_frame_time; +} + +double LoopTimer::get_delta_time() const { + return this->delta_time.count(); +} + +double LoopTimer::get_current_time() const { + return this->elapsed_time.count(); } -double LoopTimer::get_delta_time() const { return this->delta_time; } -double LoopTimer::get_current_time() const { return this->elapsed_time; } void LoopTimer::advance_fixed_update() { - this->elapsed_fixed_time += this->fixed_delta_time; + this->elapsed_fixed_time += this->fixed_delta_time; } + double LoopTimer::get_fixed_delta_time() const { - return this->fixed_delta_time; + return this->fixed_delta_time.count(); } void LoopTimer::set_fps(int fps) { - this->fps = fps; - //! use fps to calculate frame_target_time in ms - this->frame_target_time = 1000.0 / this->fps; + this->fps = fps; + // target time per frame in seconds + this->frame_target_time = std::chrono::seconds(1) / fps; +} + +int LoopTimer::get_fps() const { + return this->fps; } -int LoopTimer::get_fps() const { return this->fps; } -void LoopTimer::set_game_scale(double value) { this->game_scale = value; } -double LoopTimer::get_game_scale() const { return this->game_scale; } +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() { - uint64_t current_frame_time = SDLContext::get_instance().get_ticks(); - double frame_duration = (current_frame_time - this->last_frame_time); + std::chrono::steady_clock::time_point current_frame_time = std::chrono::steady_clock::now(); + std::chrono::milliseconds frame_duration = std::chrono::duration_cast(current_frame_time - this->last_frame_time); - if (frame_duration < this->frame_target_time) { - uint32_t delay_time - = static_cast(this->frame_target_time - frame_duration); - SDLContext::get_instance().delay(delay_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()); + } + } - this->last_frame_time = current_frame_time; + this->last_frame_time = current_frame_time; } double LoopTimer::get_lag() const { - return this->elapsed_time - this->elapsed_fixed_time; + return (this->elapsed_time - this->elapsed_fixed_time).count(); } -- cgit v1.2.3