-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime_info.cpp
52 lines (39 loc) · 1.14 KB
/
time_info.cpp
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
#include "time_info.h"
// @Fixme: Remove time dependency from C++
#include <chrono>
Time_Info timez = {};
f64 global_time_rate = 1.0;
f64 last_time = 0.0;
f64 start_time;
bool has_init = false;
f64 time_since_epoch()
{
auto t = std::chrono::high_resolution_clock::now();
auto duration_in_seconds_since_epoch = std::chrono::duration<f64>(t.time_since_epoch());
return duration_in_seconds_since_epoch.count();
}
void init_time()
{
start_time = time_since_epoch();
has_init = true;
}
f64 get_time()
{
if (!has_init) init_time();
return time_since_epoch() - start_time;
}
void update_time(f32 dt_max)
{
f64 now = get_time();
f64 delta = now - last_time;
f64 dilated_dt = (delta * global_time_rate);
f32 clamped_dilated_dt = static_cast<f32>(dilated_dt);
if (clamped_dilated_dt > dt_max) {clamped_dilated_dt = dt_max;}
timez.current_dt = clamped_dilated_dt;
timez.current_time += clamped_dilated_dt;
timez.real_world_dt = dilated_dt;
timez.real_world_time += dilated_dt;
timez.ui_dt = static_cast<f32>(delta);
timez.ui_time += static_cast<f32>(delta);
last_time = now;
}