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
|
#include "all.h"
void GameInitialize(game_t * game, char * window_name) {
SetConfigFlags(FLAG_WINDOW_RESIZABLE);
SetTraceLogLevel(LOG_NONE);
/* :config */
game->horizontal = 1920;
game->vertical = 1080;
InitWindow(game->horizontal, game->vertical, window_name);
game->ups = 60;
game->fps = 30;
game->font = DefaultFont("fonts/Atkinson/mono/AtkinsonHyperlegibleMono-Bold.otf");
/* :setup */
SetWindowState(FLAG_WINDOW_HIDDEN);
InitAudioDevice();
SetWindowPosition(0, 0);
GuiLoadStyleDarkSimple();
GuiSetFont(game->font);
/* --- */
}
void GameDeinitialize(game_t * game) {
SetWindowState(FLAG_WINDOW_HIDDEN);
UnloadFont(game->font);
CloseAudioDevice();
CloseWindow();
}
void GameLoop(game_t * game) {
#define StepStart(prefix) \
prefix##_delta = timespec_sub(now, prefix##_last); \
if (timespec_cmp(prefix##_delta, prefix##_interval) >= 0) { (void) 0
#define StepStop(prefix) \
prefix##s_per_second++; \
prefix##_total++; \
prefix##_last = timespec_add(prefix##_last, prefix##_interval); \
if (timespec_cmp(prefix##_last, now) < 0) { \
prefix##_last = now; \
} \
}
#define StepSimpleStart(prefix,linear) \
prefix##_delta = timespec_sub(now, prefix##_last); \
if (timespec_cmp(prefix##_delta, (timespec_t){1.,0.}) >= 0) { \
(void)0
#define StepSimpleStop(prefix) \
prefix##_last = now; \
}
timespec_t
now,
update_interval = {0, (f64) TIMESPEC_HZ / game->ups},
frame_interval = {0, (f64) TIMESPEC_HZ / game->fps},
update_last, frame_last, print_last,
update_delta, frame_delta, print_delta,
wait;
u16
updates_per_second = 0, frames_per_second = 0;
u32
frame_total = 0, update_total = 0;
f64 interpolation = 0.;
ClearWindowState(FLAG_WINDOW_HIDDEN);
clock_gettime(CLOCK_MONOTONIC, &now);
update_last = frame_last = print_last = now;
while (1) {
StepStart(update);
if (GameUpdate(game, now)) { return; }
StepStop(update);
StepStart(frame);
clock_gettime(CLOCK_MONOTONIC, &now);
interpolation =
CLAMP(
TIMESPEC_TO_F64(update_delta)
/ TIMESPEC_TO_F64(update_interval),
0.0, 1.0f);
GameRender(game, interpolation);
StepStop(frame);
clock_gettime(CLOCK_MONOTONIC, &now);
wait = timespec_sub(
timespec_min(
timespec_add(update_last, update_interval),
timespec_add(frame_last, frame_interval)),
now);
if (timespec_cmp(wait, zero_seconds) > 0) {
nanosleep(&wait, NULL);
}
StepSimpleStart(print, one_second);
GameReport(game,
round(frames_per_second / TIMESPEC_TO_F64(print_delta)),
round(updates_per_second / TIMESPEC_TO_F64(print_delta)),
frame_total,
update_total);
frames_per_second = updates_per_second = 0;
StepSimpleStop(print);
clock_gettime(CLOCK_MONOTONIC, &now);
}
}
i16 GameUpdate(game_t * game, timespec_t now) {
(void) now;
PollInputEvents();
if (IsWindowResized()) {
game->horizontal = GetScreenWidth();
game->vertical = GetScreenHeight();
}
switch (GetKeyPressed()) {
case KEY_ESCAPE: return 1;
}
return 0;
}
void GameRender(game_t * game, f64 interpolation) {
(void)interpolation;
BeginDrawing();
ClearBackground(BLACK);
/* >>> */
/* --- */
rlDrawRenderBatchActive();
SwapScreenBuffer();
}
void GameReport(game_t * game, f32 fps, f32 ups, u32 total_fps, u32 total_ups) {
printf("[FPS|UPS|Total] (%3.0f : %3.0f) | [%7u/%7u]\n",
fps, ups, total_fps, total_ups);
}
|