aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/manager/LoopTimerManager.cpp
blob: 71a72e29a9487250cd47e5d0b094ffac24016d89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <chrono>
#include <thread>

#include "../util/Log.h"

#include "LoopTimerManager.h"

using namespace crepe;
using namespace std::chrono_literals;
LoopTimerManager::LoopTimerManager(Mediator & mediator) : Manager(mediator) {
	this->mediator.loop_timer = *this;
	dbg_trace();
}

void LoopTimerManager::start() {
	this->last_frame_time = std::chrono::steady_clock::now();

	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<ElapsedTime_t>(this->fixed_delta_time);
	this->delta_time = 0s;
}

void LoopTimerManager::update() {
	TimePoint_t current_frame_time = std::chrono::steady_clock::now();
	// Convert to duration in seconds for delta 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 > 0s) {
		this->actual_fps = 1.0 / this->delta_time.count();
	} else {
		this->actual_fps = 0;
	}
	this->elapsed_time += std::chrono::duration_cast<ElapsedTime_t>(this->delta_time);
	this->last_frame_time = current_frame_time;
}

Duration_t LoopTimerManager::get_delta_time() const {
	return this->delta_time * this->time_scale;
}

ElapsedTime_t LoopTimerManager::get_elapsed_time() const { return this->elapsed_time; }

void LoopTimerManager::advance_fixed_elapsed_time() {
	this->elapsed_fixed_time
		+= std::chrono::duration_cast<ElapsedTime_t>(this->fixed_delta_time);
}

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 = Duration_t(1s) / this->target_fps;
}

unsigned LoopTimerManager::get_fps() const { return this->actual_fps; }

void LoopTimerManager::set_time_scale(double value) { this->time_scale = value; }

float LoopTimerManager::get_time_scale() const { return this->time_scale; }

void LoopTimerManager::enforce_frame_rate() {
	TimePoint_t current_frame_time = std::chrono::steady_clock::now();
	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
			= std::chrono::duration_cast<std::chrono::microseconds>(this->frame_target_time
																	- frame_duration);

		if (delay_time.count() > 0) {
			std::this_thread::sleep_for(delay_time);
		}
	}
}

Duration_t LoopTimerManager::get_lag() const {
	return (this->elapsed_time - this->elapsed_fixed_time);
}

Duration_t LoopTimerManager::get_scaled_fixed_delta_time() const {
	return this->fixed_delta_time * this->time_scale;
}
void LoopTimerManager::set_fixed_delta_time(float seconds) {
	this->fixed_delta_time = Duration_t(seconds);
}

Duration_t LoopTimerManager::get_fixed_delta_time() const { return this->fixed_delta_time; }