-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmonitorsim.cpp
109 lines (90 loc) · 3.18 KB
/
monitorsim.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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <iostream>
#include <cmath>
#include <random>
std::mt19937 rng(0);
std::uniform_real_distribution<double> randrange(-1, 1);
double monitor_refresh_rate = 60;
bool vsync = true;
//these are functions so you can add randomness to them if you want to simulate "a slow frame every now and then" and stuff
double game_update_time() {
return .00001;
}
double game_render_time() {
return .005;
}
double game_display_time() {
return .000001;
}
double busy_time() {
return .000001;
}
//system time at 0 helps find rounding issues when the system timer is "in sync" with the game timer
//randomizing it is "more accurate"
double system_time = 0;//randrange(rng) * 10000;
double timing_fuzziness = 1.0 / 60.0 * .005;
double fuzzy(){
std::uniform_real_distribution<double> dist(-timing_fuzziness, timing_fuzziness);
return dist(rng);
}
//measurements
int frame_updates = 0;
double total_updates = 0;
int last_vsync = 0;
int missed_updates = 0;
int double_updates = 0;
void simulate_update(){
system_time += std::max(0.0, game_update_time() + fuzzy() * .01);
total_updates++;
frame_updates++;
}
void simulate_render(){
system_time += std::max(0.0, game_render_time() + fuzzy() * .01);
}
void simulate_display(){
if(vsync){
system_time += std::max(0.0, (ceil(system_time * monitor_refresh_rate) / monitor_refresh_rate) - system_time + fuzzy());
} else {
system_time += std::max(0.0, game_display_time() + fuzzy());
}
int current_vsync = round(system_time * monitor_refresh_rate);
if(last_vsync != current_vsync){
for(int i = last_vsync; i<current_vsync-1; i++){
std::cout << 0;
missed_updates++;
}
std::cout << frame_updates;
if(frame_updates > 1) double_updates++;
last_vsync = current_vsync;
frame_updates = 0;
}
}
void simulate_busy(){
system_time += std::max(0.0, busy_time() + fuzzy() * .00001);
}
//this is where you test your game loop
int main() {
double prev_frame_time = system_time;
last_vsync = round(system_time * monitor_refresh_rate);
int first_vsync = last_vsync;
double accumulator = 0;
while(total_updates < 10000){
double current_frame_time = system_time;
double delta_frame_time = current_frame_time - prev_frame_time;
accumulator += delta_frame_time;
prev_frame_time = current_frame_time;
while(accumulator >= 1.0 / 60.0){
simulate_update();
accumulator -= 1.0 / 60.0;
}
simulate_render();
simulate_display();
simulate_busy();
}
std::cout << std::endl << std::endl;
std::cout << "TOTAL UPDATES: " << total_updates << std::endl;
std::cout << "TOTAL VSYNCS: " << last_vsync-first_vsync << std::endl;
std::cout << "TOTAL DOUBLE UPDATES: " << double_updates << std::endl;
std::cout << "TOTAL SKIPPED RENDERS: " << missed_updates << std::endl;
std::cout << "GAME TIME: " << total_updates*(1.0/60.0) << std::endl;
std::cout << "SYSTEM TIME: " << (last_vsync-first_vsync)/monitor_refresh_rate << std::endl;
}