-
Notifications
You must be signed in to change notification settings - Fork 0
/
SysSDL.cpp
490 lines (431 loc) · 12.8 KB
/
SysSDL.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
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
478
479
480
481
482
483
484
485
486
487
488
489
490
#ifdef __EMSCRIPTEN__
#include <SDL2/SDL.h>
#include <emscripten.h>
#else
#include <SDL2/SDL.h>
#endif
#include "Sys.hpp"
#include <unistd.h>
SysU8 SysScratchMemory[SYS_MAX_SCRATCH_MEMORY];
void SysKill(SysS32 C) { exit(C); }
static const SysU32 SysAudioFreq = 44100, SysAudioFlags = SYSAUDIO_STEREO;
static SysS32 GLRGBAZD[6];
static SysS32 SYS_SCREEN_W = 1920, SYS_SCREEN_H = 1080;
SDL_AudioSpec AudioFmt;
SDL_Window *SysMainWindow;
SDL_GLContext SysMainContext;
static SysU64 XS128PS[2] = {0x2034f234a, 0xc345e342f3e5};
SysU64 SysRNG(void) {
SysU64 x = XS128PS[0];
SysU64 const y = XS128PS[1];
XS128PS[0] = y;
x ^= x << 23;
XS128PS[1] = x ^ y ^ (x >> 17) ^ (y >> 26);
return (XS128PS[1] + y);
}
static void UDPOpen(void) {}
SysU32 SysUDPBroadcastIP(void) { return 0; }
SysU32 SysUDPIP(SysC8 *IPDNSString) { return 0; }
SysU32 SysUDPSend(SysU32 IPAddress, SysU16 Port, void *Packet,
SysU32 PacketByteSize) {
return 0;
}
SysU32 SysUDPBind(SysU32 IPAddress, SysU16 Port) { return 0; }
SysU32 SysUDPRecieve(SysU32 *IPAddress, SysU16 *Port, void *Packet,
SysU32 PacketByteSize, SysU32 *BytesRecieved) {
return 0;
}
static void UDPClose(void) {}
SysF32 SysAspectRatio(SysU32 *Width, SysU32 *Height) {
if (Width)
*Width = SYS_SCREEN_W;
if (Height)
*Height = SYS_SCREEN_H;
return SysF32(SYS_SCREEN_W) / SysF32(SYS_SCREEN_H);
}
static SysS32 FlipAxis(SysS32 x) {
x -= 0x4000;
x = -x;
x += 0x4000;
return x;
}
const SysU8 *SysKeyState = 0;
SysU32 SysPause = 0, SysActive = 0;
SysS32 SysPoll(void) {
SysRNG();
SysS32 QuitHit = 0;
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.window.event) {
case SDL_WINDOWEVENT_FOCUS_GAINED:
SDL_PauseAudio(0);
SysActive |= (1 << 1);
break;
case SDL_WINDOWEVENT_FOCUS_LOST:
SDL_PauseAudio(1);
SysActive |= (1 << 0);
break;
}
SDL_MouseMotionEvent *pm = (SDL_MouseMotionEvent *)&event;
SDL_MouseButtonEvent *pb = (SDL_MouseButtonEvent *)&event;
switch (event.type) {
case SDL_MOUSEBUTTONDOWN:
if (pb->button & SDL_BUTTON(SDL_BUTTON_LEFT))
SysUserInput(SYSINPUT_TOUCHON, 1,
FlipAxis((pb->y * 0x7fff) / SYS_SCREEN_H),
(pb->x * 0x7fff) / SYS_SCREEN_W, 0, 0x7fff);
if (pb->button & SDL_BUTTON(SDL_BUTTON_MIDDLE))
SysUserInput(SYSINPUT_ACCEL, SysNSec(),
0x4000 - (pb->x * 0x7fff) / SYS_SCREEN_W,
0x4000 - (pb->y * 0x7fff) / SYS_SCREEN_H, 0, 0x7fff);
break;
case SDL_MOUSEBUTTONUP:
if (pb->button == 1)
SysUserInput(SYSINPUT_TOUCHOFF, 1, (pb->x * 0x7fff) / SYS_SCREEN_W,
(pb->y * 0x7fff) / SYS_SCREEN_H, 0, 0x7fff);
break;
case SDL_MOUSEMOTION:
if (pm->state & SDL_BUTTON(SDL_BUTTON_LEFT))
SysUserInput(SYSINPUT_TOUCHMOVE, 1,
FlipAxis((pm->y * 0x7fff) / SYS_SCREEN_H),
(pm->x * 0x7fff) / SYS_SCREEN_W, 0, 0x7fff);
if (pm->state & SDL_BUTTON(SDL_BUTTON_MIDDLE))
SysUserInput(SYSINPUT_ACCEL, SysNSec(),
0x4000 - (pm->x * 0x7fff) / SYS_SCREEN_W,
0x4000 - (pm->y * 0x7fff) / SYS_SCREEN_H, 0, 0x7fff);
break;
case SDL_QUIT:
QuitHit = 1;
break;
}
}
SysKeyState = SDL_GetKeyboardState(NULL);
QuitHit |= (SysKeyState[SDL_SCANCODE_LALT] && SysKeyState[SDL_SCANCODE_Q]);
QuitHit |= SysKeyState[SDL_SCANCODE_ESCAPE];
static SysU32 Flags = 0;
static SysS32 CurrentTurn = 0;
SysS32 t = SysKeyState[SDL_SCANCODE_Q] - SysKeyState[SDL_SCANCODE_W];
SysS32 f = SysKeyState[SDL_SCANCODE_A] - SysKeyState[SDL_SCANCODE_2];
if ((t + (f << 8)) != CurrentTurn) {
SysUserInput(SYSINPUT_ACCEL, SysNSec(), 0xff, t * 0x30, f * 0x30, 0x7fff);
CurrentTurn = (t + (f << 8));
}
if (SysPause == 0)
if (SysKeyState[SDL_SCANCODE_TAB])
SysPause = 1;
if (SysPause == 1)
if (!SysKeyState[SDL_SCANCODE_TAB])
SysPause = 4 | 1;
if (SysPause == (4 | 1))
if (SysKeyState[SDL_SCANCODE_TAB])
SysPause = 2;
if (SysPause == 2)
if (!SysKeyState[SDL_SCANCODE_TAB])
SysPause = 4 | 2;
if (SysKeyState[SDL_SCANCODE_O]) {
if (!(Flags & 4))
SysUserInput(SYSINPUT_TOUCHON, 1, 0x4000, 0x4000 - 0x2000, 0, 0x7fff);
Flags |= 4;
} else if (Flags & 4) {
SysUserInput(SYSINPUT_TOUCHOFF, 1, 0x4000, 0x4000 - 0x2000, 0, 0x7fff);
Flags &= ~4;
}
if (SysKeyState[SDL_SCANCODE_P]) {
if (!(Flags & 8))
SysUserInput(SYSINPUT_TOUCHON, 2, 0x4000, 0x4000 + 0x2000, 0, 0x7fff);
Flags |= 8;
} else if (Flags & 8) {
SysUserInput(SYSINPUT_TOUCHOFF, 2, 0x4000, 0x4000 + 0x2000, 0, 0x7fff);
Flags &= ~8;
}
if (SysKeyState[SDL_SCANCODE_F]) {
if (!(Flags & 16))
SysUserInput(SYSINPUT_TOUCHON, 1, 0x4000 + 0x2000, 0x4000, 0, 0x7fff);
Flags |= 16;
} else if (Flags & 16) {
SysUserInput(SYSINPUT_TOUCHOFF, 1, 0x4000 + 0x2000, 0x4000, 0, 0x7fff);
Flags &= ~16;
}
if (SysKeyState[SDL_SCANCODE_SPACE]) {
if (!(Flags & 32))
SysUserInput(SYSINPUT_TOUCHON, 1, 0x4000 - 0x2000, 0x4000, 0, 0x7fff);
Flags |= 32;
} else if (Flags & 32) {
SysUserInput(SYSINPUT_TOUCHOFF, 1, 0x4000 - 0x2000, 0x4000, 0, 0x7fff);
Flags &= ~32;
}
return QuitHit;
}
SysS32 SysSwap(void) {
static SysU32 FrameCount = 0, FPSFrameCount = 0,
FPSTickCount = SDL_GetTicks();
SysF32 FPS = 0;
// SDL_GL_SwapBuffers( );
SDL_GL_SwapWindow(SysMainWindow);
FrameCount++;
if ((SDL_GetTicks() - FPSTickCount) >= 1000) {
FPS = ((FrameCount - FPSFrameCount) * 1000.0f) /
(SDL_GetTicks() - FPSTickCount);
FPSFrameCount = FrameCount;
FPSTickCount = SDL_GetTicks();
char r[256];
sprintf(r, "(%3f FPS)", FPS);
SDL_SetWindowTitle(SysMainWindow, r);
}
return SysPoll();
}
void SysAudioCallBack(void *UserData, SysU8 *Stream, SysS32 Len) {
SysUserAudio(SysAudioFlags, SysAudioFreq, Stream, Len);
}
void DisplayTimeNSec(SysU64 t) {
SysF64 f = fmod(t, 1000000000.0f);
t /= 1000000000;
SysU32 s = t % 60;
t /= 60;
SysU32 m = t % 60;
t /= 60;
SysU32 h = t % 24;
t /= 24;
SysU32 d = t;
SysODS("%d:%d:%d:%d:%f", d, h, m, s, f);
}
static SysS32 SysOpened = 0;
void SysSDLExit(const char *msg) {
printf("%s: %s\n", msg, SDL_GetError());
SDL_Quit();
exit(1);
}
void SysCheckSDLError(int line = -1) {
const char *error = SDL_GetError();
if (*error != '\0') {
printf("SDL Error: %s\n", error);
if (line != -1)
printf(" + line: %i\n", line);
SDL_ClearError();
}
}
SysS32 SysOpen(SysS32 Flags) {
//SysC8 *WDir = get_current_dir_name();
SysODS("SDL Opening...\n");
if (SDL_Init(SDL_INIT_VIDEO) < 0) /* Initialize SDL's Video subsystem */
SysSDLExit("Unable to initialize SDL"); /* Or die on error */
/* Request opengl 3.2 context.
* SDL doesn't have the ability to choose which profile at this time of
* writing, but it should default to the core profile */
// SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
// SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
/* Turn on double buffering with a 24bit Z buffer.
* You may need to change this to 16 or 32 for your system */
SysODS("Checking Render Buffers...\n");
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
SysODS("Opening Window...\n");
/* Create our window centered at 512x512 resolution */
SysMainWindow = SDL_CreateWindow(
"V3D", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SYS_SCREEN_W,
SYS_SCREEN_H, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
if (!SysMainWindow) /* Die if creation failed */
SysSDLExit("Unable to create window");
SysODS("Setting Window Full Screen...\n");
SDL_SetWindowFullscreen(SysMainWindow, SDL_WINDOW_FULLSCREEN_DESKTOP);
SysODS("Checking Display Mode...\n");
SDL_DisplayMode mode;
SDL_GetWindowDisplayMode(SysMainWindow, &mode);
SYS_SCREEN_W = mode.w;
SYS_SCREEN_H = mode.h;
//SysCheckSDLError(__LINE__);
SysODS("Setting attribute sizes...\n");
/* Create our opengl context and attach it to our window */
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 6);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SysODS("Creating context...\n");
SysMainContext = SDL_GL_CreateContext(SysMainWindow);
//SysCheckSDLError(__LINE__);
/* This makes our buffer swap syncronized with the monitor's vertical refresh
*/
SysODS("Setting swap interval...\n");
SDL_GL_SetSwapInterval(1);
SysODS("Getting RGBAZD attributes...\n");
SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &GLRGBAZD[0]);
SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &GLRGBAZD[1]);
SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &GLRGBAZD[2]);
SDL_GL_GetAttribute(SDL_GL_ALPHA_SIZE, &GLRGBAZD[3]);
SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &GLRGBAZD[4]);
SDL_GL_GetAttribute(SDL_GL_DOUBLEBUFFER, &GLRGBAZD[5]);
SysODS("Video Mode Set: RGBA:%d%d%d%d Z:%d DoubleBuffer:%d\n", GLRGBAZD[0],
GLRGBAZD[1], GLRGBAZD[2], GLRGBAZD[3], GLRGBAZD[4], GLRGBAZD[5]);
/*
SDL_memset(&AudioFmt, 0, sizeof(AudioFmt));
AudioFmt.freq = SysAudioFreq;
AudioFmt.format = AUDIO_S16LSB;
AudioFmt.channels = (SysAudioFlags&SYSAUDIO_STEREO)?2:1;
AudioFmt.samples = SysAudioFreq/30;
AudioFmt.callback = SysAudioCallBack;
AudioFmt.userdata = NULL;
if (SDL_OpenAudio(&AudioFmt, NULL) != 0)
{
printf("Audio Error!\n");
exit(1);
}
*/
UDPOpen();
SysODS("Performance Counter:%d Frequency:%d(%f)\n",
SDL_GetPerformanceCounter(), SDL_GetPerformanceFrequency(),
SDL_GetPerformanceFrequency() / 1000000000.0f);
SysODS("SysOpen RealTime:");
DisplayTimeNSec(SysNSec());
SysODS(" Monotonic:");
DisplayTimeNSec(SysNSec());
SysODS("\n");
SysOpened = 1;
return 0;
}
SysS32 SysClose(SysS32 Flags) {
SysAssert(SysOpened);
UDPClose();
SDL_GL_DeleteContext(SysMainContext);
SDL_DestroyWindow(SysMainWindow);
SDL_Quit();
SysOpened = 0;
return 0;
}
SysU32 SysState(SysS32 Cmd, void *Arg) {
switch (Cmd) {
case SYSSTATE_AUDIOOFF:
SDL_PauseAudio(1);
break;
case SYSSTATE_AUDIOON:
SDL_PauseAudio(0);
break;
case SYSSTATE_UDPON:
break;
}
return 0;
}
//#define SYSIO
SysS32 SysLoad(SysU32 Flags, const SysC8 *OFileName, SysU32 Offset, SysU32 Size,
void *Buffer) {
#ifdef SYSIO
FILE *f;
SysS32 l;
SysC8 FileName[256];
#ifdef SYSLSDIR
if (Flags == SYSLOADSAVE_USERDIR) {
strcpy(FileName, "../");
strcat(FileName, OFileName);
} else
#endif
strcpy(FileName, OFileName);
f = fopen(FileName, "rb");
if (f == NULL) {
return 0;
}
if (Size && Buffer) {
fseek(f, Offset, SEEK_SET);
l = fread(Buffer, 1, Size, f);
} else {
fseek(f, 0, SEEK_END);
l = ftell(f);
}
fclose(f);
return l;
#else
return 0;
#endif
}
SysS32 SysSave(SysU32 Flags, const SysC8 *OFileName, SysU32 Size,
void *Buffer) {
#ifdef SYSIO
SysC8 FileName[256];
#ifdef SYSLSDIR
if (Flags == SYSLOADSAVE_USERDIR) {
strcpy(FileName, "../");
strcat(FileName, OFileName);
} else
#endif
strcpy(FileName, OFileName);
FILE *f = fopen(FileName, "wb");
SysAssert(f);
if (Size)
fwrite(Buffer, sizeof(char), Size, f);
fclose(f);
#endif
return 0;
}
SysU64 SysNSec(void) {
SysF64 cf = SysF64(SDL_GetPerformanceCounter())*1000000000.0f/SysF64(SDL_GetPerformanceFrequency());
return SysU64(cf);
}
static SysU8 M32Hash(void *m) {
SysU32 a = (SysU64)m;
a = ((((a ^ (a >> 8)) + (a >> 16)) ^ (a >> 24)) & 0xff);
if (!a)
return 0xff;
else
return a;
}
void *SysAllocate(SysU32 Bytes) {
SysU32 Align = 16;
SysU8 *m = (SysU8 *)malloc(Bytes + Align);
SysU8 h = M32Hash(m);
*m++ = h;
while (((SysU64)m) & (Align - 1))
*m++ = 0;
return m;
}
SysU32 SysDeAllocate(void *Mem) {
if (!Mem)
return 0;
SysU8 *m = (SysU8 *)Mem;
do {
--m;
} while (!m[0]);
SysAssert(m[0] == M32Hash(m));
free(m);
return 0;
}
#ifdef SYS_DEBUG_ODS
void SysODS(const SysC8 *DebugString, ...) {
va_list args;
va_start(args, DebugString);
vprintf(DebugString, args);
va_end(args);
}
#endif
SysS32 SysR = 0;
void SysLoop(void) {
SysU32 s = 0;
if (SysPause == (4 | 2)) {
SDL_PauseAudio(0);
s = SYSFRAME_RESUME;
SysPause = 0;
} else if (SysPause) {
s = SYSFRAME_PAUSE;
SDL_PauseAudio(1);
}
if (SysActive == 1)
s = SYSFRAME_PAUSE;
else if (SysActive) {
s = SYSFRAME_RESUME;
SysActive = 0;
}
SysR = SysUserFrame(s);
if (SysSwap())
SysR = SYSFRAME_QUIT;
}
int main(int argc, char *argv[]) {
SysR = 0;
SysOpen(0);
#ifdef __EMSCRIPTEN__
emscripten_set_main_loop(SysLoop, 0, true);
#else
while (SysR != SYSFRAME_QUIT)
SysLoop();
#endif
SysClose(0);
SysODS("Sys Finished\n");
return 0;
}