aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/api/LoopTimer.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/crepe/api/LoopTimer.cpp')
-rw-r--r--src/crepe/api/LoopTimer.cpp35
1 files changed, 14 insertions, 21 deletions
diff --git a/src/crepe/api/LoopTimer.cpp b/src/crepe/api/LoopTimer.cpp
index d0a19d7..fe5544d 100644
--- a/src/crepe/api/LoopTimer.cpp
+++ b/src/crepe/api/LoopTimer.cpp
@@ -26,7 +26,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;
@@ -40,13 +41,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<double>(1.0) / fps;
+ this->frame_target_time = std::chrono::duration<double>(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; }
@@ -55,22 +56,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) {
- auto remaining_time = this->frame_target_time - frame_duration;
-
- // Sleep for most of the remaining time using SDLContext
- if (remaining_time > std::chrono::microseconds(2000)) { // 2ms threshold
- SDLContext::get_instance().delay(
- std::chrono::duration_cast<std::chrono::milliseconds>(remaining_time).count());
- }
-
- // Busy-wait for the last tiny remaining duration
- while (std::chrono::steady_clock::now() - current_frame_time < remaining_time) {
- // Busy wait
- }
- }
-
- this->last_frame_time = std::chrono::steady_clock::now(); // Update frame time
+ if (frame_duration < this->frame_target_time) {
+ std::chrono::milliseconds delay_time
+ = std::chrono::duration_cast<std::chrono::milliseconds>(this->frame_target_time
+ - frame_duration);
+ if (delay_time.count() > 0) {
+ SDLContext::get_instance().delay(delay_time.count());
+ }
+ }
}
double LoopTimer::get_lag() const {