-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_gameloop.cpp
More file actions
300 lines (243 loc) · 7.95 KB
/
test_gameloop.cpp
File metadata and controls
300 lines (243 loc) · 7.95 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#include "catch.hpp"
#include <thread>
#include <chrono>
#include "../src/core/GameLoop.hpp"
using namespace Caffeine;
TEST_CASE("GameLoopConfig - Default values", "[gameloop][config]") {
GameLoopConfig cfg;
REQUIRE_THAT(cfg.fixedDeltaTime, Catch::Matchers::WithinAbs(1.0 / 60.0, 1e-9));
REQUIRE_THAT(cfg.maxFrameTime, Catch::Matchers::WithinAbs(0.25, 1e-9));
REQUIRE(cfg.targetFPS == 0);
REQUIRE(cfg.vsync == true);
REQUIRE(cfg.interpolation == true);
}
TEST_CASE("GameState - Init is default", "[gameloop][state]") {
GameLoop loop;
REQUIRE(loop.state() == GameState::Init);
}
TEST_CASE("GameLoop - Init transitions to Running", "[gameloop][state]") {
GameLoop loop;
loop.init();
REQUIRE(loop.state() == GameState::Running);
}
TEST_CASE("GameLoop - Pause transitions from Running", "[gameloop][state]") {
GameLoop loop;
loop.init();
loop.pause();
REQUIRE(loop.state() == GameState::Paused);
}
TEST_CASE("GameLoop - Resume transitions from Paused to Running", "[gameloop][state]") {
GameLoop loop;
loop.init();
loop.pause();
loop.resume();
REQUIRE(loop.state() == GameState::Running);
}
TEST_CASE("GameLoop - Shutdown from Running", "[gameloop][state]") {
GameLoop loop;
loop.init();
loop.shutdown();
REQUIRE(loop.state() == GameState::Shutdown);
}
TEST_CASE("GameLoop - Shutdown from Paused", "[gameloop][state]") {
GameLoop loop;
loop.init();
loop.pause();
loop.shutdown();
REQUIRE(loop.state() == GameState::Shutdown);
}
TEST_CASE("GameLoop - Pause from Init is ignored", "[gameloop][state]") {
GameLoop loop;
loop.pause();
REQUIRE(loop.state() == GameState::Init);
}
TEST_CASE("GameLoop - Resume from Init is ignored", "[gameloop][state]") {
GameLoop loop;
loop.resume();
REQUIRE(loop.state() == GameState::Init);
}
TEST_CASE("GameLoop - Tick does nothing before init", "[gameloop][state]") {
GameLoop loop;
loop.tick(1.0 / 60.0);
REQUIRE(loop.frameCount() == 0);
}
TEST_CASE("GameLoop - Tick does nothing after shutdown", "[gameloop][state]") {
GameLoop loop;
loop.init();
loop.shutdown();
loop.tick(1.0 / 60.0);
REQUIRE(loop.frameCount() == 0);
}
TEST_CASE("GameLoop - Frame count increments on tick", "[gameloop][tick]") {
GameLoop loop;
loop.init();
loop.tick(1.0 / 60.0);
REQUIRE(loop.frameCount() == 1);
loop.tick(1.0 / 60.0);
REQUIRE(loop.frameCount() == 2);
}
TEST_CASE("GameLoop - Elapsed time accumulates", "[gameloop][tick]") {
GameLoop loop;
loop.init();
loop.tick(1.0 / 60.0);
loop.tick(1.0 / 60.0);
REQUIRE(loop.elapsedTime() > 0.0);
}
TEST_CASE("GameLoop - Fixed update fires at correct rate", "[gameloop][tick]") {
GameLoopConfig cfg;
cfg.fixedDeltaTime = 1.0 / 60.0;
GameLoop loop(cfg);
loop.init();
int fixedUpdateCount = 0;
loop.onFixedUpdate = [&](f64) { fixedUpdateCount++; };
loop.tick(1.0 / 60.0);
REQUIRE(fixedUpdateCount == 1);
}
TEST_CASE("GameLoop - Multiple fixed updates per frame when dt is large", "[gameloop][tick]") {
GameLoopConfig cfg;
cfg.fixedDeltaTime = 1.0 / 60.0;
GameLoop loop(cfg);
loop.init();
int fixedUpdateCount = 0;
loop.onFixedUpdate = [&](f64) { fixedUpdateCount++; };
loop.tick(3.0 / 60.0);
REQUIRE(fixedUpdateCount == 3);
}
TEST_CASE("GameLoop - No fixed update when dt is too small", "[gameloop][tick]") {
GameLoopConfig cfg;
cfg.fixedDeltaTime = 1.0 / 60.0;
GameLoop loop(cfg);
loop.init();
int fixedUpdateCount = 0;
loop.onFixedUpdate = [&](f64) { fixedUpdateCount++; };
loop.tick(0.001);
REQUIRE(fixedUpdateCount == 0);
}
TEST_CASE("GameLoop - Spiral of death prevention clamps accumulator", "[gameloop][tick]") {
GameLoopConfig cfg;
cfg.fixedDeltaTime = 1.0 / 60.0;
cfg.maxFrameTime = 0.25;
GameLoop loop(cfg);
loop.init();
int fixedUpdateCount = 0;
loop.onFixedUpdate = [&](f64) { fixedUpdateCount++; };
loop.tick(10.0);
int maxPossibleUpdates = static_cast<int>(cfg.maxFrameTime / cfg.fixedDeltaTime);
REQUIRE(fixedUpdateCount <= maxPossibleUpdates);
}
TEST_CASE("GameLoop - Interpolation alpha is in [0, 1)", "[gameloop][tick]") {
GameLoopConfig cfg;
cfg.fixedDeltaTime = 1.0 / 60.0;
cfg.interpolation = true;
GameLoop loop(cfg);
loop.init();
loop.tick(0.5 / 60.0);
f64 alpha = loop.interpolationAlpha();
REQUIRE(alpha >= 0.0);
REQUIRE(alpha < 1.0);
}
TEST_CASE("GameLoop - Alpha is zero when interpolation disabled", "[gameloop][tick]") {
GameLoopConfig cfg;
cfg.interpolation = false;
GameLoop loop(cfg);
loop.init();
loop.tick(0.5 / 60.0);
REQUIRE(loop.interpolationAlpha() == 0.0);
}
TEST_CASE("GameLoop - Paused state skips fixed update", "[gameloop][tick]") {
GameLoop loop;
loop.init();
int fixedUpdateCount = 0;
loop.onFixedUpdate = [&](f64) { fixedUpdateCount++; };
loop.pause();
loop.tick(1.0 / 60.0);
REQUIRE(fixedUpdateCount == 0);
}
TEST_CASE("GameLoop - Paused state still calls render", "[gameloop][tick]") {
GameLoop loop;
loop.init();
int renderCount = 0;
loop.onRender = [&](f64) { renderCount++; };
loop.pause();
loop.tick(1.0 / 60.0);
REQUIRE(renderCount == 1);
}
TEST_CASE("GameLoop - onBeginFrame and onEndFrame called per tick", "[gameloop][callbacks]") {
GameLoop loop;
loop.init();
int beginCount = 0;
int endCount = 0;
loop.onBeginFrame = [&]() { beginCount++; };
loop.onEndFrame = [&]() { endCount++; };
loop.tick(1.0 / 60.0);
REQUIRE(beginCount == 1);
REQUIRE(endCount == 1);
loop.tick(1.0 / 60.0);
REQUIRE(beginCount == 2);
REQUIRE(endCount == 2);
}
TEST_CASE("GameLoop - onRender called once per tick", "[gameloop][callbacks]") {
GameLoop loop;
loop.init();
int renderCount = 0;
loop.onRender = [&](f64) { renderCount++; };
loop.tick(3.0 / 60.0);
REQUIRE(renderCount == 1);
}
TEST_CASE("GameLoop - Fixed update receives correct dt", "[gameloop][callbacks]") {
GameLoopConfig cfg;
cfg.fixedDeltaTime = 1.0 / 60.0;
GameLoop loop(cfg);
loop.init();
f64 receivedDt = 0.0;
loop.onFixedUpdate = [&](f64 dt) { receivedDt = dt; };
loop.tick(1.0 / 60.0);
REQUIRE_THAT(receivedDt, Catch::Matchers::WithinAbs(1.0 / 60.0, 1e-9));
}
TEST_CASE("GameLoop - IGameCallbacks interface works", "[gameloop][callbacks]") {
struct TestCallbacks : IGameCallbacks {
int fixedCount = 0;
int renderCount = 0;
int beginCount = 0;
int endCount = 0;
void onBeginFrame() override { beginCount++; }
void onFixedUpdate(f64) override { fixedCount++; }
void onRender(f64) override { renderCount++; }
void onEndFrame() override { endCount++; }
};
TestCallbacks cb;
GameLoop loop;
loop.setCallbacks(&cb);
loop.init();
loop.tick(1.0 / 60.0);
REQUIRE(cb.beginCount == 1);
REQUIRE(cb.fixedCount == 1);
REQUIRE(cb.renderCount == 1);
REQUIRE(cb.endCount == 1);
}
TEST_CASE("GameLoop - Deterministic accumulation over 3600 frames", "[gameloop][integration]") {
GameLoopConfig cfg;
cfg.fixedDeltaTime = 1.0 / 60.0;
GameLoop loop(cfg);
loop.init();
int fixedUpdateCount = 0;
loop.onFixedUpdate = [&](f64) { fixedUpdateCount++; };
for (int i = 0; i < 3600; ++i) {
loop.tick(1.0 / 60.0);
}
REQUIRE(fixedUpdateCount == 3600);
}
TEST_CASE("GameLoop - Custom config applies", "[gameloop][config]") {
GameLoopConfig cfg;
cfg.fixedDeltaTime = 1.0 / 30.0;
cfg.maxFrameTime = 0.5;
cfg.targetFPS = 120;
cfg.vsync = false;
cfg.interpolation = false;
GameLoop loop(cfg);
REQUIRE_THAT(loop.config().fixedDeltaTime, Catch::Matchers::WithinAbs(1.0 / 30.0, 1e-9));
REQUIRE_THAT(loop.config().maxFrameTime, Catch::Matchers::WithinAbs(0.5, 1e-9));
REQUIRE(loop.config().targetFPS == 120);
REQUIRE(loop.config().vsync == false);
REQUIRE(loop.config().interpolation == false);
}