diff options
author | WBoerenkamps <wrj.boerenkamps@student.avans.nl> | 2024-12-11 14:16:40 +0100 |
---|---|---|
committer | WBoerenkamps <wrj.boerenkamps@student.avans.nl> | 2024-12-11 14:16:40 +0100 |
commit | f64b793ad8e796458c8e175f298e8d13eb3b3459 (patch) | |
tree | c939140d97f5d3aad7c493198019cf9c1a72d8a4 /src/crepe/manager/LoopTimerManager.cpp | |
parent | 6cd6ed3eb2a14e703e97bf95fe1b73fb8d3d91f4 (diff) |
feedback
Diffstat (limited to 'src/crepe/manager/LoopTimerManager.cpp')
-rw-r--r-- | src/crepe/manager/LoopTimerManager.cpp | 56 |
1 files changed, 26 insertions, 30 deletions
diff --git a/src/crepe/manager/LoopTimerManager.cpp b/src/crepe/manager/LoopTimerManager.cpp index 8725c33..a306eb7 100644 --- a/src/crepe/manager/LoopTimerManager.cpp +++ b/src/crepe/manager/LoopTimerManager.cpp @@ -6,7 +6,7 @@ #include "LoopTimerManager.h" using namespace crepe; - +using namespace std::chrono_literals; LoopTimerManager::LoopTimerManager(Mediator & mediator) : Manager(mediator) { this->mediator.loop_timer = *this; dbg_trace(); @@ -15,62 +15,57 @@ LoopTimerManager::LoopTimerManager(Mediator & mediator) : Manager(mediator) { void LoopTimerManager::start() { this->last_frame_time = std::chrono::steady_clock::now(); - this->elapsed_time = std::chrono::milliseconds(0); + this->elapsed_time = 0s; // 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->delta_time = std::chrono::milliseconds(0); + = -std::chrono::duration_cast<ElapsedTime_t>(this->fixed_delta_time); + this->delta_time = 0s; } void LoopTimerManager::update() { - std::chrono::steady_clock::time_point current_frame_time + TimePoint_t current_frame_time = std::chrono::steady_clock::now(); // Convert to duration in seconds for delta time - this->delta_time = std::chrono::duration_cast<std::chrono::duration<double>>( - current_frame_time - last_frame_time); + this->delta_time = current_frame_time - last_frame_time; if (this->delta_time > this->maximum_delta_time) { this->delta_time = this->maximum_delta_time; } - if (this->delta_time.count() > 0.0) { + if (this->delta_time > 0s) { this->actual_fps = 1.0 / this->delta_time.count(); } else { this->actual_fps = 0; } - - this->elapsed_time += this->delta_time; + this->elapsed_time += std::chrono::duration_cast<ElapsedTime_t>(this->delta_time); this->last_frame_time = current_frame_time; } -double LoopTimerManager::get_delta_time() const { - return this->delta_time.count() * this->time_scale; -} +Duration_t LoopTimerManager::get_delta_time() const {return this->delta_time * this->time_scale;} -double LoopTimerManager::get_current_time() const { return this->elapsed_time.count(); } +ElapsedTime_t LoopTimerManager::get_elapsed_time() const { return this->elapsed_time; } void LoopTimerManager::advance_fixed_elapsed_time() { - this->elapsed_fixed_time += this->fixed_delta_time; + this->elapsed_fixed_time += std::chrono::duration_cast<ElapsedTime_t>(this->fixed_delta_time); } -void LoopTimerManager::set_target_fps(int fps) { +void LoopTimerManager::set_target_framerate(unsigned fps) { this->target_fps = fps; //check if fps is lower or equals 0 if (fps <= 0) return; // target time per frame in seconds - this->frame_target_time = std::chrono::duration<double>(1.0) / this->target_fps; + this->frame_target_time = Duration_t(1s) / this->target_fps; } -int LoopTimerManager::get_fps() const { return this->actual_fps; } +unsigned LoopTimerManager::get_fps() const { return this->actual_fps; } void LoopTimerManager::set_time_scale(double value) { this->time_scale = value; } -double LoopTimerManager::get_time_scale() const { return this->time_scale; } +float LoopTimerManager::get_time_scale() const { return this->time_scale; } void LoopTimerManager::enforce_frame_rate() { - std::chrono::steady_clock::time_point current_frame_time + TimePoint_t current_frame_time = std::chrono::steady_clock::now(); - std::chrono::duration<double> frame_duration = current_frame_time - this->last_frame_time; - + Duration_t 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) { std::chrono::microseconds delay_time @@ -83,16 +78,17 @@ void LoopTimerManager::enforce_frame_rate() { } } -double LoopTimerManager::get_lag() const { - return (this->elapsed_time - this->elapsed_fixed_time).count(); +Duration_t LoopTimerManager::get_lag() const { + return (this->elapsed_time - this->elapsed_fixed_time); } -double LoopTimerManager::get_scaled_fixed_delta_time() const { - return this->fixed_delta_time.count() * this->time_scale; + +Duration_t LoopTimerManager::get_scaled_fixed_delta_time() const { + return this->fixed_delta_time * this->time_scale; } -void LoopTimerManager::set_fixed_delta_time(double seconds) { - this->fixed_delta_time = std::chrono::duration<double>(seconds); +void LoopTimerManager::set_fixed_delta_time(float seconds) { + this->fixed_delta_time = Duration_t(seconds); } -double LoopTimerManager::get_fixed_delta_time() const { - return this->fixed_delta_time.count(); +Duration_t LoopTimerManager::get_fixed_delta_time() const { + return this->fixed_delta_time; } |