-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiau.c
More file actions
195 lines (160 loc) · 3.41 KB
/
Copy pathmiau.c
File metadata and controls
195 lines (160 loc) · 3.41 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
#include <SDL.h>
#include <setjmp.h>
#include <assert.h>
#ifdef EMSCRIPTEN
#include <emscripten.h>
#endif
#include "game.h"
#include "controller.h"
#include "renderer.h"
#include "mixer.h"
#include "miau.h"
static jmp_buf quit_jmp_buf;
static int exit_status = 0;
static bool paused;
static unsigned long t1, t2, millis;
static Uint64 frame_time, throttling_time;
void quit(int status)
{
exit_status = status;
longjmp(quit_jmp_buf, 1);
}
void pause()
{
info("pausing");
paused = true;
}
void unpause()
{
info("unpausing");
t2 = SDL_GetTicks();
paused = false;
}
#if LOG_RUSAGE
#include <sys/time.h>
#include <sys/resource.h>
Uint32 log_rusage_timer_id;
Uint32 log_rusage(Uint32 interval, void* param)
{
static struct rusage ru1 = {0};
struct rusage ru2;
double utime, stime, rtime;
long memory;
getrusage(RUSAGE_SELF, &ru2);
rtime = (double) interval / 1000;
utime = (double) (ru2.ru_utime.tv_usec - ru1.ru_utime.tv_usec) / 1000000;
utime += (double) (ru2.ru_utime.tv_sec - ru1.ru_utime.tv_sec);
stime = (double) (ru2.ru_stime.tv_usec - ru1.ru_stime.tv_usec) / 1000000;
stime += (double) (ru2.ru_stime.tv_sec - ru1.ru_stime.tv_sec);
memory = ru2.ru_maxrss;
info("rusage: %8.3f s user %8.3f s system %8.3f s real %8ld kb mem",
utime, stime, rtime, memory);
ru1 = ru2;
return LOG_RUSAGE;
}
#endif
#if LOG_FRAME_STATS
static FILE* fp_frame_stats;
static inline void FRAME_STATS_PUT()
{
#define toms(x) ((double)((x)*1000) / SDL_GetPerformanceFrequency())
fprintf(fp_frame_stats,
"%f\t"
"%f\t"
"%f\t"
"%f\t"
"%f\t"
"%f\t"
"%f\t"
"%f\n",
toms(frame_time),
toms(throttling_time),
(double) millis,
toms(c_frame_time),
toms(g_frame_time),
toms(r_frame_time),
sqrt(v_nrm2(g_player->v_walk)),
sqrt(v_nrm2(g_player->b.v))
);
#undef toms
}
static inline void FRAME_STATS_OPEN()
{
fp_frame_stats = fopen("frame_stats.log", "w");
assert (fp_frame_stats);
fprintf(fp_frame_stats,
"frame_time\t"
"throttling_time\t"
"millis\t"
"c_frame_time\t"
"g_frame_time\t"
"r_frame_time\t"
"|g_player->v_walk|\t"
"|g_player->b.v|\n"
);
}
static inline void FRAME_STATS_CLOSE()
{
fclose(fp_frame_stats);
}
#else
#define FRAME_STATS_PUT() ((void) 0)
#define FRAME_STATS_OPEN() ((void) 0)
#define FRAME_STATS_CLOSE() ((void) 0)
#endif
static void frame()
{
frame_time = SDL_GetPerformanceCounter();
/* frame rate throttling */
while ((millis = ((t2 = SDL_GetTicks()) - t1)) < 10)
SDL_Delay(10 - millis);
throttling_time = SDL_GetPerformanceCounter() - frame_time;
if (50 < millis)
{
erorr("Frame time spike: %ld ms! (capped to 50 ms)", millis);
millis = 50;
}
c_frame(millis);
if (!paused)
g_frame(millis);
r_frame();
mix_frame();
t1 = t2;
frame_time = SDL_GetPerformanceCounter() - frame_time;
FRAME_STATS_PUT();
}
int main(int argc, char* argv[])
{
info("MIAU!!1!ONE!");
SDL_Init(0);
#if LOG_RUSAGE
SDL_Init(SDL_INIT_TIMER);
log_rusage_timer_id = SDL_AddTimer(LOG_RUSAGE, log_rusage, NULL);
#endif
FRAME_STATS_OPEN();
if (setjmp(quit_jmp_buf) == 0)
{
g_init();
r_init();
c_init();
mix_init();
t1 = SDL_GetTicks();
#ifdef EMSCRIPTEN
emscripten_set_main_loop(frame, 0, 1);
#else
while (true)
frame();
#endif
}
FRAME_STATS_CLOSE();
mix_fini();
c_fini();
r_fini();
g_fini();
if (exit_status)
info("Exiting with erorr %d!\n", exit_status);
else
info("Exiting successfuly.");
SDL_Quit();
return exit_status;
}