DEADSOFTWARE

ports: ported to emscripten
[flatwaifu.git] / src / sdl2 / main.c
1 #ifdef __EMSCRIPTEN__
2 # include <emscripten.h>
3 #endif
5 #include <SDL2/SDL.h>
6 #include <stdio.h>
7 #include <stdarg.h>
8 #include <stdlib.h> // srand exit
9 #include <string.h> // strcasecmp
10 #include <assert.h>
11 #include "system.h"
12 #include "input.h"
14 #include "my.h" // fexists
15 #include "player.h" // pl1 pl2
16 #include "menu.h" // G_keyf
17 #include "error.h" // logo
19 #include "files.h" // F_startup F_addwad F_initwads F_allocres
20 #include "config.h" // CFG_args CFG_load CFG_save
21 #include "memory.h" // M_startup
22 #include "game.h" // G_init G_act
23 #include "sound.h" // S_init S_done
24 #include "music.h" // S_initmusic S_updatemusic S_donemusic
25 #include "render.h" // R_init R_draw R_done
27 #define TITLE_STR "DooM 2D (SDL2)"
29 static Uint32 ticks;
30 static int quit = 0;
31 static SDL_Window *window;
32 static SDL_GLContext context;
33 static SDL_Surface *surf;
35 /* --- error.h --- */
37 void logo (const char *s, ...) {
38 va_list ap;
39 va_start(ap, s);
40 vprintf(s, ap);
41 va_end(ap);
42 fflush(stdout);
43 }
45 void logo_gas (int cur, int all) {
46 // stub
47 }
49 void ERR_failinit (char *s, ...) {
50 va_list ap;
51 va_start(ap, s);
52 vprintf(s, ap);
53 va_end(ap);
54 puts("");
55 exit(1);
56 }
58 void ERR_fatal (char *s, ...) {
59 va_list ap;
60 R_done();
61 S_done();
62 S_donemusic();
63 M_shutdown();
64 SDL_Quit();
65 puts("\nКРИТИЧЕСКАЯ ОШИБКА:");
66 va_start(ap, s);
67 vprintf(s, ap);
68 va_end(ap);
69 puts("");
70 exit(1);
71 }
73 void ERR_quit (void) {
74 quit = 1;
75 }
77 /* --- system.h --- */
79 static int Y_resize_window (int w, int h, int fullscreen) {
80 assert(w > 0);
81 assert(h > 0);
82 assert(window != NULL);
83 if (surf != NULL) {
84 if (surf->w != w || surf->h != h) {
85 SDL_Surface *s = SDL_CreateRGBSurface(0, w, h, 8, 0, 0, 0, 0);
86 if (s != NULL) {
87 SDL_SetPaletteColors(s->format->palette, surf->format->palette->colors, 0, surf->format->palette->ncolors);
88 SDL_FreeSurface(surf);
89 surf = s;
90 }
91 }
92 }
93 SDL_SetWindowSize(window, w, h);
94 Y_set_fullscreen(fullscreen);
95 return 1;
96 }
98 int Y_set_videomode_opengl (int w, int h, int fullscreen) {
99 assert(w > 0);
100 assert(h > 0);
101 Uint32 flags;
102 SDL_Window *win;
103 SDL_GLContext ctx;
104 if (window != NULL && context != NULL) {
105 Y_resize_window(w, h, fullscreen);
106 win = window;
107 } else {
108 flags = SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL;
109 if (fullscreen) {
110 flags = flags | SDL_WINDOW_FULLSCREEN;
112 // TODO set context version and type
113 #ifdef __EMSCRIPTEN__
114 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
115 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
116 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
117 #else
118 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1);
119 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
120 #endif
121 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
122 win = SDL_CreateWindow(TITLE_STR, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, flags);
123 if (win != NULL) {
124 ctx = SDL_GL_CreateContext(win);
125 if (ctx != NULL) {
126 Y_unset_videomode();
127 window = win;
128 context = ctx;
129 SDL_GL_MakeCurrent(window, context);
130 } else {
131 SDL_DestroyWindow(win);
132 win = NULL;
136 if (win == NULL) {
137 logo("Y_set_videomode_opengl: error: %s\n", SDL_GetError());
139 return win != NULL;
142 int Y_set_videomode_software (int w, int h, int fullscreen) {
143 assert(w > 0);
144 assert(h > 0);
145 Uint32 flags;
146 SDL_Surface *s;
147 SDL_Window *win;
148 if (window != NULL && surf != NULL) {
149 Y_resize_window(w, h, fullscreen);
150 win = window;
151 } else {
152 flags = SDL_WINDOW_RESIZABLE;
153 if (fullscreen) {
154 flags = flags | SDL_WINDOW_FULLSCREEN;
156 win = SDL_CreateWindow(TITLE_STR, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, flags);
157 if (win != NULL) {
158 s = SDL_CreateRGBSurface(0, w, h, 8, 0, 0, 0, 0);
159 if (s != NULL) {
160 Y_unset_videomode();
161 window = win;
162 surf = s;
163 } else {
164 SDL_DestroyWindow(win);
165 win = NULL;
169 if (win == NULL) {
170 logo("Y_set_videomode_software: error: %s\n", SDL_GetError());
172 return win != NULL;
175 void Y_get_videomode (int *w, int *h) {
176 if (window != NULL) {
177 SDL_GetWindowSize(window, w, h);
178 } else {
179 *w = 0;
180 *h = 0;
184 int Y_videomode_setted (void) {
185 return window != NULL;
188 void Y_unset_videomode (void) {
189 if (window != NULL) {
190 if (context != NULL) {
191 SDL_GL_MakeCurrent(window, NULL);
192 SDL_GL_DeleteContext(context);
193 context = NULL;
195 if (surf != NULL) {
196 SDL_FreeSurface(surf);
197 surf = NULL;
199 SDL_DestroyWindow(window);
200 window = NULL;
204 void Y_set_fullscreen (int yes) {
205 if (window != NULL) {
206 SDL_SetWindowFullscreen(window, yes ? SDL_WINDOW_FULLSCREEN : 0);
210 int Y_get_fullscreen (void) {
211 return (window != NULL) && (SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN);
214 void Y_swap_buffers (void) {
215 assert(window != NULL);
216 assert(context != NULL);
217 SDL_GL_SwapWindow(window);
220 void Y_get_buffer (byte **buf, int *w, int *h, int *pitch) {
221 assert(window != NULL);
222 assert(surf != NULL);
223 *buf = surf->pixels;
224 *w = surf->w;
225 *h = surf->h;
226 *pitch = surf->pitch;
229 void Y_set_vga_palette (byte *vgapal) {
230 assert(window != NULL);
231 assert(surf != NULL);
232 int i;
233 byte *p = vgapal;
234 SDL_Color colors[256];
235 for (i = 0; i < 256; i++) {
236 colors[i] = (SDL_Color) {
237 .r = p[0] * 255 / 63,
238 .g = p[1] * 255 / 63,
239 .b = p[2] * 255 / 63
240 };
241 p += 3;
243 SDL_SetPaletteColors(surf->format->palette, colors, 0, 256);
246 void Y_repaint_rect (int x, int y, int w, int h) {
247 assert(window != NULL);
248 assert(surf != NULL);
249 SDL_Surface *s = SDL_GetWindowSurface(window);
250 SDL_Rect r = (SDL_Rect) {
251 .x = x,
252 .y = y,
253 .w = w,
254 .h = h
255 };
256 SDL_BlitSurface(surf, &r, s, &r);
257 SDL_UpdateWindowSurfaceRects(window, &r, 1);
260 void Y_repaint (void) {
261 Y_repaint_rect(0, 0, surf->w, surf->h);
264 /* --- main --- */
266 static int sdl_to_key (int code) {
267 switch (code) {
268 case SDL_SCANCODE_0: return KEY_0;
269 case SDL_SCANCODE_1: return KEY_1;
270 case SDL_SCANCODE_2: return KEY_2;
271 case SDL_SCANCODE_3: return KEY_3;
272 case SDL_SCANCODE_4: return KEY_4;
273 case SDL_SCANCODE_5: return KEY_5;
274 case SDL_SCANCODE_6: return KEY_6;
275 case SDL_SCANCODE_7: return KEY_7;
276 case SDL_SCANCODE_8: return KEY_8;
277 case SDL_SCANCODE_9: return KEY_9;
278 case SDL_SCANCODE_A: return KEY_A;
279 case SDL_SCANCODE_B: return KEY_B;
280 case SDL_SCANCODE_C: return KEY_C;
281 case SDL_SCANCODE_D: return KEY_D;
282 case SDL_SCANCODE_E: return KEY_E;
283 case SDL_SCANCODE_F: return KEY_F;
284 case SDL_SCANCODE_G: return KEY_G;
285 case SDL_SCANCODE_H: return KEY_H;
286 case SDL_SCANCODE_I: return KEY_I;
287 case SDL_SCANCODE_J: return KEY_J;
288 case SDL_SCANCODE_K: return KEY_K;
289 case SDL_SCANCODE_L: return KEY_L;
290 case SDL_SCANCODE_M: return KEY_M;
291 case SDL_SCANCODE_N: return KEY_N;
292 case SDL_SCANCODE_O: return KEY_O;
293 case SDL_SCANCODE_P: return KEY_P;
294 case SDL_SCANCODE_Q: return KEY_Q;
295 case SDL_SCANCODE_R: return KEY_R;
296 case SDL_SCANCODE_S: return KEY_S;
297 case SDL_SCANCODE_T: return KEY_T;
298 case SDL_SCANCODE_U: return KEY_U;
299 case SDL_SCANCODE_V: return KEY_V;
300 case SDL_SCANCODE_W: return KEY_W;
301 case SDL_SCANCODE_X: return KEY_X;
302 case SDL_SCANCODE_Y: return KEY_Y;
303 case SDL_SCANCODE_Z: return KEY_Z;
304 case SDL_SCANCODE_RETURN: return KEY_RETURN;
305 case SDL_SCANCODE_ESCAPE: return KEY_ESCAPE;
306 case SDL_SCANCODE_BACKSPACE: return KEY_BACKSPACE;
307 case SDL_SCANCODE_TAB: return KEY_TAB;
308 case SDL_SCANCODE_SPACE: return KEY_SPACE;
309 case SDL_SCANCODE_MINUS: return KEY_MINUS;
310 case SDL_SCANCODE_EQUALS: return KEY_EQUALS;
311 case SDL_SCANCODE_LEFTBRACKET: return KEY_LEFTBRACKET;
312 case SDL_SCANCODE_RIGHTBRACKET: return KEY_RIGHTBRACKET;
313 case SDL_SCANCODE_BACKSLASH: return KEY_BACKSLASH;
314 case SDL_SCANCODE_SEMICOLON: return KEY_SEMICOLON;
315 case SDL_SCANCODE_APOSTROPHE: return KEY_APOSTROPHE;
316 case SDL_SCANCODE_GRAVE: return KEY_GRAVE;
317 case SDL_SCANCODE_COMMA: return KEY_COMMA;
318 case SDL_SCANCODE_PERIOD: return KEY_PERIOD;
319 case SDL_SCANCODE_SLASH: return KEY_SLASH;
320 case SDL_SCANCODE_CAPSLOCK: return KEY_CAPSLOCK;
321 case SDL_SCANCODE_F1: return KEY_F1;
322 case SDL_SCANCODE_F2: return KEY_F2;
323 case SDL_SCANCODE_F3: return KEY_F3;
324 case SDL_SCANCODE_F4: return KEY_F4;
325 case SDL_SCANCODE_F5: return KEY_F5;
326 case SDL_SCANCODE_F6: return KEY_F6;
327 case SDL_SCANCODE_F7: return KEY_F7;
328 case SDL_SCANCODE_F8: return KEY_F8;
329 case SDL_SCANCODE_F9: return KEY_F9;
330 case SDL_SCANCODE_F10: return KEY_F10;
331 case SDL_SCANCODE_F11: return KEY_F11;
332 case SDL_SCANCODE_F12: return KEY_F12;
333 case SDL_SCANCODE_PRINTSCREEN: return KEY_PRINTSCREEN;
334 case SDL_SCANCODE_SCROLLLOCK: return KEY_SCROLLLOCK;
335 case SDL_SCANCODE_PAUSE: return KEY_PAUSE;
336 case SDL_SCANCODE_INSERT: return KEY_INSERT;
337 case SDL_SCANCODE_HOME: return KEY_HOME;
338 case SDL_SCANCODE_PAGEUP: return KEY_PAGEUP;
339 case SDL_SCANCODE_DELETE: return KEY_DELETE;
340 case SDL_SCANCODE_END: return KEY_END;
341 case SDL_SCANCODE_PAGEDOWN: return KEY_PAGEDOWN;
342 case SDL_SCANCODE_RIGHT: return KEY_RIGHT;
343 case SDL_SCANCODE_LEFT: return KEY_LEFT;
344 case SDL_SCANCODE_DOWN: return KEY_DOWN;
345 case SDL_SCANCODE_UP: return KEY_UP;
346 case SDL_SCANCODE_NUMLOCKCLEAR: return KEY_NUMLOCK;
347 case SDL_SCANCODE_KP_DIVIDE: return KEY_KP_DIVIDE;
348 case SDL_SCANCODE_KP_MULTIPLY: return KEY_KP_MULTIPLY;
349 case SDL_SCANCODE_KP_MINUS: return KEY_KP_MINUS;
350 case SDL_SCANCODE_KP_PLUS: return KEY_KP_PLUS;
351 case SDL_SCANCODE_KP_ENTER: return KEY_KP_ENTER;
352 case SDL_SCANCODE_KP_0: return KEY_KP_0;
353 case SDL_SCANCODE_KP_1: return KEY_KP_1;
354 case SDL_SCANCODE_KP_2: return KEY_KP_2;
355 case SDL_SCANCODE_KP_3: return KEY_KP_3;
356 case SDL_SCANCODE_KP_4: return KEY_KP_4;
357 case SDL_SCANCODE_KP_5: return KEY_KP_5;
358 case SDL_SCANCODE_KP_6: return KEY_KP_6;
359 case SDL_SCANCODE_KP_7: return KEY_KP_7;
360 case SDL_SCANCODE_KP_8: return KEY_KP_8;
361 case SDL_SCANCODE_KP_9: return KEY_KP_9;
362 case SDL_SCANCODE_KP_PERIOD: return KEY_KP_PERIOD;
363 case SDL_SCANCODE_SYSREQ: return KEY_SYSREQ;
364 case SDL_SCANCODE_LCTRL: return KEY_LCTRL;
365 case SDL_SCANCODE_LSHIFT: return KEY_LSHIFT;
366 case SDL_SCANCODE_LALT: return KEY_LALT;
367 case SDL_SCANCODE_LGUI: return KEY_LSUPER;
368 case SDL_SCANCODE_RCTRL: return KEY_RCTRL;
369 case SDL_SCANCODE_RSHIFT: return KEY_RSHIFT;
370 case SDL_SCANCODE_RALT: return KEY_RALT;
371 case SDL_SCANCODE_RGUI: return KEY_RSUPER;
372 default: return KEY_UNKNOWN;
376 static void window_event_handler (SDL_WindowEvent *ev) {
377 switch (ev->event) {
378 case SDL_WINDOWEVENT_RESIZED:
379 R_set_videomode(ev->data1, ev->data2, Y_get_fullscreen());
380 break;
381 case SDL_WINDOWEVENT_CLOSE:
382 ERR_quit();
383 break;
387 static void poll_events (void (*h)(int key, int down)) {
388 int key;
389 SDL_Event ev;
390 while (SDL_PollEvent(&ev)) {
391 switch (ev.type) {
392 case SDL_QUIT:
393 ERR_quit();
394 break;
395 case SDL_WINDOWEVENT:
396 if (ev.window.windowID == SDL_GetWindowID(window)) {
397 window_event_handler(&ev.window);
399 break;
400 case SDL_KEYDOWN:
401 case SDL_KEYUP:
402 key = sdl_to_key(ev.key.keysym.scancode);
403 I_press(key, ev.type == SDL_KEYDOWN);
404 if (h != NULL) {
405 (*h)(key, ev.type == SDL_KEYDOWN);
407 break;
412 static void step (void) {
413 poll_events(&G_keyf);
414 S_updatemusic();
415 Uint32 t = SDL_GetTicks();
416 if (t - ticks > DELAY) {
417 ticks = t;
418 G_act();
420 R_draw();
423 int main (int argc, char **argv) {
424 char *pw;
425 logo("system: initialize SDL2\n");
426 if (SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO | SDL_INIT_EVENTS) == -1) {
427 logo("system: failed to init SDL2: %s\n", SDL_GetError());
428 return 1;
430 // Player 1 defaults
431 pl1.ku = KEY_KP_8;
432 pl1.kd = KEY_KP_5;
433 pl1.kl = KEY_KP_4;
434 pl1.kr = KEY_KP_6;
435 pl1.kf = KEY_PAGEDOWN;
436 pl1.kj = KEY_DELETE;
437 pl1.kwl = KEY_HOME;
438 pl1.kwr = KEY_END;
439 pl1.kp = KEY_KP_8;
440 // Player 2 defaults
441 pl2.ku = KEY_E;
442 pl2.kd = KEY_D;
443 pl2.kl = KEY_S;
444 pl2.kr = KEY_F;
445 pl2.kf = KEY_A;
446 pl2.kj = KEY_Q;
447 pl2.kwl = KEY_1;
448 pl2.kwr = KEY_2;
449 pl2.kp = KEY_E;
450 srand(SDL_GetTicks());
451 F_startup();
452 #ifndef WIN32
453 pw = "/usr/share/doom2d-rembo/doom2d.wad";
454 #else
455 pw = "doom2d.wad";
456 #endif
457 if (fexists(pw)) {
458 F_addwad(pw);
459 } else {
460 F_addwad("doom2d.wad");
462 CFG_args(argc, argv);
463 CFG_load();
464 F_initwads();
465 M_startup();
466 F_allocres();
467 S_init();
468 S_initmusic();
469 R_init();
470 G_init();
471 ticks = SDL_GetTicks();
472 #ifdef __EMSCRIPTEN__
473 emscripten_set_main_loop(step, 0, 1);
474 #else
475 while (!quit) {
476 step();
478 #endif
479 CFG_save();
480 R_done();
481 S_donemusic();
482 S_done();
483 M_shutdown();
484 SDL_Quit();
485 return 0;