forked from pyland/pyland
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_time.cpp
38 lines (30 loc) · 1.2 KB
/
game_time.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
#include <chrono>
#include <functional>
#include <ratio>
#include "game_time.hpp"
GameTime::GameTime():
GameTime(1.0, duration(0), std::chrono::steady_clock::now())
{}
GameTime::GameTime(const GameTime &other):
GameTime(other.game_seconds_per_real_second, other.passed_time, other.time_at_last_tick)
{}
GameTime::GameTime(double game_seconds_per_real_second,
duration passed_time,
std::chrono::steady_clock::time_point time_at_last_tick):
game_seconds_per_real_second(game_seconds_per_real_second),
passed_time(passed_time),
time_at_last_tick(time_at_last_tick)
{}
GameTime::time_point GameTime::time() {
auto real_time = std::chrono::steady_clock::now();
auto real_time_difference = real_time - time_at_last_tick;
// Ignore the potential for inaccuracy;
// the point is that everything will be inaccurate the
// same way so actual (microsecond) jitter isn't really important.
passed_time += std::chrono::duration_cast<duration>(
real_time_difference * double(get_game_seconds_per_real_second())
);
time_at_last_tick = real_time;
// Make it seem like this is a time.
return time_point(passed_time);
}