forked from ceu-lang/ceu-sdl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
478 lines (441 loc) · 11.6 KB
/
main.c
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
#include <stdio.h>
/*
#ifndef DEBUG
#define DEBUG
#endif
*/
#ifdef CEU_TIMEMACHINE
int CEU_TIMEMACHINE_ON = 0;
#else
#define CEU_TIMEMACHINE_ON 0
#endif
#define CEU_FPS 50
// definitely lost: 2,478 bytes in 17 blocks
#ifdef __ANDROID__
#include "SDL.h"
#include "SDL_image.h"
//#include "SDL_mixer.h"
#include "SDL_ttf.h"
//#include "SDL_net.h"
//#include "SDL_opengles.h"
#ifdef DEBUG
#define printf(args...) __android_log_print(4, "SDL", ## args);
#define fprintf(x, args...) __android_log_print(4, "SDL", ## args);
#else
#define printf(args...)
#define fprintf(x, args...)
#endif
#else
#include "SDL2/SDL.h"
#include "SDL2/SDL_image.h"
#include "SDL2/SDL_mixer.h"
#include "SDL2/SDL_ttf.h"
#include "SDL2/SDL_opengl.h"
#endif
#include <assert.h>
#include "_ceu_app.h"
s32 WCLOCK_nxt;
#define ceu_out_wclock_set(us) WCLOCK_nxt = us;
#include "_ceu_app.c"
static char CEU_DATA[sizeof(CEU_Main)];
#ifdef __ANDROID__
int SDL_main (int argc, char *argv[])
#else
int main (int argc, char *argv[])
#endif
{
int err = SDL_Init(SDL_INIT_EVERYTHING);
if (err != 0) {
printf("SDL_Init failed: %s\n", SDL_GetError());
return err;
}
WCLOCK_nxt = CEU_WCLOCK_INACTIVE;
u32 old = SDL_GetTicks();
#ifdef CEU_FPS
int fps_next = (1000/CEU_FPS);
#endif
#ifdef CEU_THREADS
// just before executing CEU code
CEU_THREADS_MUTEX_LOCK(&CEU.threads_mutex);
#endif
tceu_app app;
app.data = (tceu_org*) &CEU_DATA;
app.init = &ceu_app_init;
app.init(&app); /* calls CEU_THREADS_MUTEX_LOCK() */
#ifdef CEU_RET
if (! app.isAlive)
goto END;
#endif
#ifdef CEU_IN_OS_START_
ceu_sys_go(&app, CEU_IN_OS_START_, NULL);
#ifdef CEU_RET
if (! app.isAlive)
goto END;
#endif
#endif
#ifdef CEU_IN_OS_START
if (!CEU_TIMEMACHINE_ON) {
ceu_sys_go(&app, CEU_IN_OS_START, NULL);
#ifdef CEU_RET
if (! app.isAlive)
goto END;
#endif
}
#endif
#ifdef CEU_IN_SDL_REDRAW_
ceu_sys_go(&app, CEU_IN_SDL_REDRAW_, NULL);
#ifdef CEU_RET
if (! app.isAlive)
goto END;
#endif
#endif
#ifdef CEU_IN_SDL_REDRAW
if (!CEU_TIMEMACHINE_ON) {
ceu_sys_go(&app, CEU_IN_SDL_REDRAW, NULL);
#ifdef CEU_RET
if (! app.isAlive)
goto END;
#endif
}
#endif
SDL_Event evt;
#ifdef __ANDROID__
int isPaused = 0;
#endif
for (;;)
{
#ifdef CEU_THREADS
// unlock from INIT->START->REDRAW or last loop iteration
CEU_THREADS_MUTEX_UNLOCK(&CEU.threads_mutex);
#endif
/*
* With SDL_DT, 'tm=0' (update as fast as possible).
* Without SDL_DT, 'tm=?' respects the timers.
*/
#if defined(CEU_IN_SDL_DT) || defined(CEU_IN_SDL_DT_) || defined(CEU_FPS)
#ifdef CEU_FPS
s32 tm = (CEU_TIMEMACHINE_ON ? 0 : fps_next);
#else
s32 tm = 0; // as fast as possible
#endif
#else /* !(defined(CEU_IN_SDL_DT) || defined(CEU_IN_SDL_DT_) || defined(CEU_FPS)) */
s32 tm = -1;
#ifdef CEU_WCLOCKS
if (WCLOCK_nxt != CEU_WCLOCK_INACTIVE)
tm = WCLOCK_nxt / 1000;
#endif
#ifdef CEU_ASYNCS
if (app.pendingAsyncs) {
tm = 0;
}
#endif
#endif /* defined(CEU_IN_SDL_DT) || defined(CEU_IN_SDL_DT_) || defined(CEU_FPS) */
//SDL_EventState(SDL_FINGERMOTION, SDL_IGNORE);
int has;
#ifdef __ANDROID__
if (isPaused) {
has = SDL_WaitEvent(&evt);
} else
#endif
{
has = SDL_WaitEventTimeout(&evt, tm);
}
u32 now = SDL_GetTicks();
s32 dt_ms = (now - old);
assert(dt_ms >= 0);
old = now;
#ifdef CEU_FPS
/* force dt_ms=(1000/CEU_FPS) */
int fps_ok = 0;
int togo = (fps_next - dt_ms);
if (togo <= 0) {
fps_ok = 1;
dt_ms = (1000/CEU_FPS);
fps_next = (dt_ms + togo);
if (fps_next < 0) {
printf("[TODO: main.c] delayed %d\n", -fps_next);
fps_next = 0;
}
} else {
fps_next = togo;
}
assert(fps_next >= 0);
#else
int fps_ok = 1;
#endif
s32 dt_us = dt_ms*1000;
#ifdef CEU_THREADS
// just before executing CEU code
CEU_THREADS_MUTEX_LOCK(&CEU.threads_mutex);
#endif
#ifdef __ANDROID__
if (!isPaused)
#endif
if (fps_ok) {
#ifdef CEU_WCLOCKS
#if ! (defined(CEU_IN_SDL_DT) || defined(CEU_IN_SDL_DT_))
if (WCLOCK_nxt != CEU_WCLOCK_INACTIVE)
{
//redraw = WCLOCK_nxt <= dt_us;
#endif
#ifdef CEU_TIMEMACHINE
//#ifdef CEU_IN__WCLOCK_ (TODO: always defined)
ceu_sys_go(&app, CEU_IN__WCLOCK_, &dt_us);
#ifdef CEU_RET
if (! app.isAlive)
goto END;
#endif
while (WCLOCK_nxt <= 0) {
s32 dt_us = 0;
ceu_sys_go(&app, CEU_IN__WCLOCK_, &dt_us);
#ifdef CEU_RET
if (! app.isAlive)
goto END;
#endif
}
#endif
if (!CEU_TIMEMACHINE_ON) {
ceu_sys_go(&app, CEU_IN__WCLOCK, &dt_us);
#ifdef CEU_RET
if (! app.isAlive)
goto END;
#endif
while (WCLOCK_nxt <= 0) {
s32 dt_us = 0;
ceu_sys_go(&app, CEU_IN__WCLOCK, &dt_us);
#ifdef CEU_RET
if (! app.isAlive)
goto END;
#endif
}
}
#if ! (defined(CEU_IN_SDL_DT) || defined(CEU_IN_SDL_DT_))
}
#endif
#endif
#ifdef CEU_IN_SDL_DT_
if (fps_ok) {
ceu_sys_go(&app, CEU_IN_SDL_DT_, &dt_ms);
}
#ifdef CEU_RET
if (! app.isAlive)
goto END;
#endif
//redraw = 1;
#endif
#ifdef CEU_IN_SDL_DT
if (!CEU_TIMEMACHINE_ON) {
if (fps_ok) {
ceu_sys_go(&app, CEU_IN_SDL_DT, &dt_ms);
}
#ifdef CEU_RET
if (! app.isAlive)
goto END;
#endif
//redraw = 1;
}
#endif
}
// OTHER EVENTS
if (has)
{
int handled = 1; // =1 for defined events
SDL_Event* evtp = &evt;
switch (evt.type) {
case SDL_QUIT:
#ifdef CEU_IN_SDL_QUIT_
ceu_sys_go(&app, CEU_IN_SDL_QUIT_, &evtp);
#endif
#ifdef CEU_IN_SDL_QUIT
if (!CEU_TIMEMACHINE_ON) {
ceu_sys_go(&app, CEU_IN_SDL_QUIT, &evtp);
}
#endif
break;
case SDL_WINDOWEVENT:
#ifdef CEU_IN_SDL_WINDOWEVENT_
ceu_sys_go(&app, CEU_IN_SDL_WINDOWEVENT_, &evtp);
#endif
#ifdef CEU_IN_SDL_WINDOWEVENT
if (!CEU_TIMEMACHINE_ON) {
ceu_sys_go(&app, CEU_IN_SDL_WINDOWEVENT, &evtp);
}
#endif
break;
case SDL_KEYDOWN:
#ifdef CEU_IN_SDL_KEYDOWN_
ceu_sys_go(&app, CEU_IN_SDL_KEYDOWN_, &evtp);
#endif
#ifdef CEU_IN_SDL_KEYDOWN
if (!CEU_TIMEMACHINE_ON) {
ceu_sys_go(&app, CEU_IN_SDL_KEYDOWN, &evtp);
}
#endif
break;
case SDL_KEYUP:
#ifdef CEU_IN_SDL_KEYUP_
ceu_sys_go(&app, CEU_IN_SDL_KEYUP_, &evtp);
#endif
#ifdef CEU_IN_SDL_KEYUP
if (!CEU_TIMEMACHINE_ON) {
ceu_sys_go(&app, CEU_IN_SDL_KEYUP, &evtp);
}
#endif
break;
case SDL_TEXTINPUT:
#ifdef CEU_IN_SDL_TEXTINPUT_
ceu_sys_go(&app, CEU_IN_SDL_TEXTINPUT_, &evtp);
#endif
#ifdef CEU_IN_SDL_TEXTINPUT
if (!CEU_TIMEMACHINE_ON) {
ceu_sys_go(&app, CEU_IN_SDL_TEXTINPUT, &evtp);
}
#endif
break;
case SDL_TEXTEDITING:
#ifdef CEU_IN_SDL_TEXTEDITING_
ceu_sys_go(&app, CEU_IN_SDL_TEXTEDITING_, &evtp);
#endif
#ifdef CEU_IN_SDL_TEXTEDITING
if (!CEU_TIMEMACHINE_ON) {
ceu_sys_go(&app, CEU_IN_SDL_TEXTEDITING, &evtp);
}
#endif
break;
case SDL_MOUSEMOTION:
#ifdef CEU_IN_SDL_MOUSEMOTION_
ceu_sys_go(&app, CEU_IN_SDL_MOUSEMOTION_, &evtp);
#endif
#ifdef CEU_IN_SDL_MOUSEMOTION
if (!CEU_TIMEMACHINE_ON) {
ceu_sys_go(&app, CEU_IN_SDL_MOUSEMOTION, &evtp);
}
#endif
break;
case SDL_MOUSEBUTTONDOWN:
#ifdef CEU_IN_SDL_MOUSEBUTTONDOWN_
ceu_sys_go(&app, CEU_IN_SDL_MOUSEBUTTONDOWN_, &evtp);
#endif
#ifdef CEU_IN_SDL_MOUSEBUTTONDOWN
if (!CEU_TIMEMACHINE_ON) {
ceu_sys_go(&app, CEU_IN_SDL_MOUSEBUTTONDOWN, &evtp);
}
#endif
break;
case SDL_MOUSEBUTTONUP:
#ifdef CEU_IN_SDL_MOUSEBUTTONUP_
ceu_sys_go(&app, CEU_IN_SDL_MOUSEBUTTONUP_, &evtp);
#endif
#ifdef CEU_IN_SDL_MOUSEBUTTONUP
if (!CEU_TIMEMACHINE_ON) {
ceu_sys_go(&app, CEU_IN_SDL_MOUSEBUTTONUP, &evtp);
}
#endif
break;
case SDL_FINGERDOWN:
#ifdef CEU_IN_SDL_FINGERDOWN_
ceu_sys_go(&app, CEU_IN_SDL_FINGERDOWN_, &evtp);
#endif
#ifdef CEU_IN_SDL_FINGERDOWN
if (!CEU_TIMEMACHINE_ON) {
ceu_sys_go(&app, CEU_IN_SDL_FINGERDOWN, &evtp);
}
#endif
break;
case SDL_FINGERUP:
#ifdef CEU_IN_SDL_FINGERUP_
ceu_sys_go(&app, CEU_IN_SDL_FINGERUP_, &evtp);
#endif
#ifdef CEU_IN_SDL_FINGERUP
if (!CEU_TIMEMACHINE_ON) {
ceu_sys_go(&app, CEU_IN_SDL_FINGERUP, &evtp);
}
#endif
break;
case SDL_FINGERMOTION:
#ifdef CEU_IN_SDL_FINGERMOTION_
ceu_sys_go(&app, CEU_IN_SDL_FINGERMOTION_, &evtp);
#endif
#ifdef CEU_IN_SDL_FINGERMOTION
if (!CEU_TIMEMACHINE_ON) {
ceu_sys_go(&app, CEU_IN_SDL_FINGERMOTION, &evtp);
}
#endif
break;
/* TODO: "_" events */
#if defined(CEU_IN_SDL_APP_WILLENTERBACKGROUND) || defined(__ANDROID__)
case SDL_APP_WILLENTERBACKGROUND:
#ifdef __ANDROID__
// handle onPause/onResume
isPaused = 1;
#endif
#ifdef CEU_IN_SDL_APP_WILLENTERBACKGROUND
ceu_sys_go(&app, CEU_IN_SDL_APP_WILLENTERBACKGROUND, &evtp);
#endif
break;
#endif
#if defined(CEU_IN_SDL_APP_WILLENTERFOREGROUND) || defined(__ANDROID__)
case SDL_APP_WILLENTERFOREGROUND:
#ifdef __ANDROID__
// handle onPause/onResume
isPaused = 0;
old = SDL_GetTicks(); // ignores previous 'old' on resume
#endif
#ifdef CEU_IN_SDL_APP_WILLENTERFOREGROUND
ceu_sys_go(&app, CEU_IN_SDL_APP_WILLENTERFOREGROUND, &evtp);
#endif
break;
#endif
default:
handled = 0; // undefined event
}
#ifdef CEU_RET
if (! app.isAlive) goto END;
#endif
//redraw = redraw || handled;
}
#ifdef CEU_IN_SDL_REDRAW_
//if (redraw && !SDL_PollEvent(NULL))
if (fps_ok) {
ceu_sys_go(&app, CEU_IN_SDL_REDRAW_, NULL);
#ifdef CEU_RET
if (! app.isAlive)
goto END;
#endif
}
#endif
#ifdef CEU_IN_SDL_REDRAW
if (!CEU_TIMEMACHINE_ON) {
//if (redraw && !SDL_PollEvent(NULL))
if (fps_ok) {
ceu_sys_go(&app, CEU_IN_SDL_REDRAW, NULL);
#ifdef CEU_RET
if (! app.isAlive)
goto END;
#endif
}
}
#endif
/* TODO: "_" events */
#ifdef CEU_ASYNCS
if (app.pendingAsyncs) {
ceu_sys_go(&app, CEU_IN__ASYNC, NULL);
#ifdef CEU_RET
if (! app.isAlive)
goto END;
#endif
}
#endif
}
END:
#ifdef CEU_THREADS
// only reachable if LOCKED
CEU_THREADS_MUTEX_UNLOCK(&CEU.threads_mutex);
#endif
SDL_Quit(); // TODO: slow
#ifdef CEU_RET
return app.ret;
#else
return 0;
#endif
}