DEADSOFTWARE

portability: avoid errors on some compilers
[flatwaifu.git] / src / sdl2 / main.c
1 /* Copyright (C) 2020 SovietPony
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, version 3 of the License ONLY.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 */
16 #ifdef __EMSCRIPTEN__
17 # include <emscripten.h>
18 #endif
20 #include <SDL2/SDL.h>
21 #include <stdio.h>
22 #include <stdarg.h>
23 #include <stdlib.h> // srand exit
24 #include <string.h> // strcasecmp
25 #include <assert.h>
26 #include "system.h"
27 #include "input.h"
29 #include "player.h" // pl1 pl2
30 #include "menu.h" // G_keyf
31 #include "error.h" // logo
32 #include "monster.h" // nomon
34 #include "files.h" // F_startup F_addwad F_initwads F_allocres
35 #include "config.h" // CFG_args CFG_load CFG_save
36 #include "args.h" // ARG_parse
37 #include "memory.h" // M_startup
38 #include "game.h" // G_init G_act
39 #include "sound.h" // S_init S_done
40 #include "music.h" // S_initmusic S_updatemusic S_donemusic
41 #include "render.h" // R_init R_draw R_done
43 #include "common/cp866.h"
45 #define TITLE_STR "Doom 2D (SDL2)"
47 static Uint32 ticks;
48 static int quit = 0;
49 static SDL_Window *window;
50 static SDL_GLContext context;
51 static SDL_Surface *surf;
52 static videomode_t vlist;
54 static const cfg_t arg[] = {
55 {"file", NULL, Y_FILES},
56 {"cheat", &cheat, Y_SW_ON},
57 // {"vga", &shot_vga, Y_SW_ON},
58 // {"musvol", &mus_vol, Y_WORD},
59 {"mon", &nomon, Y_SW_OFF},
60 {"warp", &_warp, Y_BYTE},
61 // {"config", NULL, cfg_file, Y_STRING},
62 {NULL, NULL, 0} // end
63 };
65 static const cfg_t cfg[] = {
66 // {"screenshot", &shot_vga, Y_SW_ON},
67 // {"music_volume", &mus_vol, Y_WORD},
68 // {"music_random", &music_random, Y_SW_ON},
69 // {"music_time", &music_time, Y_DWORD},
70 // {"music_fade", &music_fade, Y_DWORD},
71 {"pl1_left", &pl1.kl, Y_KEY},
72 {"pl1_right",&pl1.kr, Y_KEY},
73 {"pl1_up", &pl1.ku, Y_KEY},
74 {"pl1_down", &pl1.kd, Y_KEY},
75 {"pl1_jump", &pl1.kj, Y_KEY},
76 {"pl1_fire", &pl1.kf, Y_KEY},
77 {"pl1_next", &pl1.kwr, Y_KEY},
78 {"pl1_prev", &pl1.kwl, Y_KEY},
79 {"pl1_use", &pl1.kp, Y_KEY},
80 {"pl2_left", &pl2.kl, Y_KEY},
81 {"pl2_right", &pl2.kr, Y_KEY},
82 {"pl2_up", &pl2.ku, Y_KEY},
83 {"pl2_down", &pl2.kd, Y_KEY},
84 {"pl2_jump", &pl2.kj, Y_KEY},
85 {"pl2_fire", &pl2.kf, Y_KEY},
86 {"pl2_next", &pl2.kwr, Y_KEY},
87 {"pl2_prev", &pl2.kwl, Y_KEY},
88 {"pl2_use", &pl2.kp, Y_KEY},
89 {NULL, NULL, 0} // end
90 };
92 static void CFG_args (int argc, char **argv) {
93 const cfg_t *list[] = { arg, R_args(), S_args(), MUS_args() };
94 ARG_parse(argc, argv, 4, list);
95 }
97 static void CFG_load (void) {
98 const cfg_t *list[] = { cfg, R_conf(), S_conf(), MUS_conf() };
99 CFG_read_config("default.cfg", 4, list);
100 CFG_read_config("doom2d.cfg", 4, list);
103 static void CFG_save (void) {
104 const cfg_t *list[] = { cfg, R_conf(), S_conf(), MUS_conf() };
105 CFG_update_config("doom2d.cfg", "doom2d.cfg", 4, list, "generated by doom2d, do not modify");
108 /* --- error.h --- */
110 void logo (const char *s, ...) {
111 va_list ap;
112 va_start(ap, s);
113 vprintf(s, ap);
114 va_end(ap);
115 fflush(stdout);
118 void logo_gas (int cur, int all) {
119 // stub
122 void ERR_failinit (char *s, ...) {
123 va_list ap;
124 va_start(ap, s);
125 vprintf(s, ap);
126 va_end(ap);
127 puts("");
128 abort();
131 void ERR_fatal (char *s, ...) {
132 va_list ap;
133 R_done();
134 MUS_done();
135 S_done();
136 SDL_Quit();
137 puts("\nCRITICAL ERROR:");
138 va_start(ap, s);
139 vprintf(s, ap);
140 va_end(ap);
141 puts("");
142 abort();
145 void ERR_quit (void) {
146 quit = 1;
149 /* --- system.h --- */
151 static int Y_resize_window (int w, int h, int fullscreen) {
152 assert(w > 0);
153 assert(h > 0);
154 assert(window != NULL);
155 if (surf != NULL) {
156 if (surf->w != w || surf->h != h) {
157 SDL_Surface *s = SDL_CreateRGBSurface(0, w, h, 8, 0, 0, 0, 0);
158 if (s != NULL) {
159 SDL_SetPaletteColors(s->format->palette, surf->format->palette->colors, 0, surf->format->palette->ncolors);
160 SDL_FreeSurface(surf);
161 surf = s;
165 SDL_SetWindowSize(window, w, h);
166 Y_set_fullscreen(fullscreen);
167 return 1;
170 int Y_set_videomode_opengl (int w, int h, int fullscreen) {
171 assert(w > 0);
172 assert(h > 0);
173 Uint32 flags;
174 SDL_Window *win;
175 SDL_GLContext ctx;
176 if (window != NULL && context != NULL) {
177 Y_resize_window(w, h, fullscreen);
178 win = window;
179 } else {
180 flags = SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL;
181 if (fullscreen) {
182 flags = flags | SDL_WINDOW_FULLSCREEN;
184 // TODO set context version and type
185 #ifdef __EMSCRIPTEN__
186 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
187 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
188 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
189 #else
190 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1);
191 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
192 #endif
193 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
194 win = SDL_CreateWindow(TITLE_STR, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, flags);
195 if (win != NULL) {
196 ctx = SDL_GL_CreateContext(win);
197 if (ctx != NULL) {
198 Y_unset_videomode();
199 window = win;
200 context = ctx;
201 SDL_GL_MakeCurrent(window, context);
202 } else {
203 SDL_DestroyWindow(win);
204 win = NULL;
208 if (win == NULL) {
209 logo("Y_set_videomode_opengl: error: %s\n", SDL_GetError());
211 return win != NULL;
214 int Y_set_videomode_software (int w, int h, int fullscreen) {
215 assert(w > 0);
216 assert(h > 0);
217 Uint32 flags;
218 SDL_Surface *s;
219 SDL_Window *win;
220 if (window != NULL && surf != NULL) {
221 Y_resize_window(w, h, fullscreen);
222 win = window;
223 } else {
224 flags = SDL_WINDOW_RESIZABLE;
225 if (fullscreen) {
226 flags = flags | SDL_WINDOW_FULLSCREEN;
228 win = SDL_CreateWindow(TITLE_STR, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, flags);
229 if (win != NULL) {
230 s = SDL_CreateRGBSurface(0, w, h, 8, 0, 0, 0, 0);
231 if (s != NULL) {
232 Y_unset_videomode();
233 window = win;
234 surf = s;
235 } else {
236 SDL_DestroyWindow(win);
237 win = NULL;
241 if (win == NULL) {
242 logo("Y_set_videomode_software: error: %s\n", SDL_GetError());
244 return win != NULL;
247 void Y_get_videomode (int *w, int *h) {
248 if (window != NULL) {
249 SDL_GetWindowSize(window, w, h);
250 } else {
251 *w = 0;
252 *h = 0;
256 int Y_videomode_setted (void) {
257 return window != NULL;
260 void Y_unset_videomode (void) {
261 if (window != NULL) {
262 if (context != NULL) {
263 SDL_GL_MakeCurrent(window, NULL);
264 SDL_GL_DeleteContext(context);
265 context = NULL;
267 if (surf != NULL) {
268 SDL_FreeSurface(surf);
269 surf = NULL;
271 SDL_DestroyWindow(window);
272 window = NULL;
276 static void init_videomode_list (void) {
277 int i, j, k;
278 SDL_DisplayMode m;
279 int n = SDL_GetNumDisplayModes(0);
280 if (vlist.modes != NULL) {
281 free(vlist.modes);
282 vlist.modes = NULL;
283 vlist.n = 0;
285 if (n > 0) {
286 vlist.modes = malloc(n * sizeof(videomode_size_t));
287 if (vlist.modes != NULL) {
288 j = 0;
289 for (i = 0; i < n; i++) {
290 SDL_GetDisplayMode(0, i, &m);
291 k = 0;
292 while (k < j && (m.w != vlist.modes[k].w || m.h != vlist.modes[k].h)) {
293 k++;
295 if (k >= j) {
296 vlist.modes[j] = (videomode_size_t) {
297 .w = m.w,
298 .h = m.h
299 };
300 j++;
303 vlist.n = j;
308 const videomode_t *Y_get_videomode_list_opengl (int fullscreen) {
309 init_videomode_list();
310 return &vlist;
313 const videomode_t *Y_get_videomode_list_software (int fullscreen) {
314 init_videomode_list();
315 return &vlist;
318 void Y_set_fullscreen (int yes) {
319 if (window != NULL) {
320 SDL_SetWindowFullscreen(window, yes ? SDL_WINDOW_FULLSCREEN : 0);
324 int Y_get_fullscreen (void) {
325 return (window != NULL) && (SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN);
328 void Y_swap_buffers (void) {
329 assert(window != NULL);
330 assert(context != NULL);
331 SDL_GL_SwapWindow(window);
334 void Y_get_buffer (byte **buf, int *w, int *h, int *pitch) {
335 assert(window != NULL);
336 assert(surf != NULL);
337 *buf = surf->pixels;
338 *w = surf->w;
339 *h = surf->h;
340 *pitch = surf->pitch;
343 void Y_set_vga_palette (byte *vgapal) {
344 assert(window != NULL);
345 assert(surf != NULL);
346 int i;
347 byte *p = vgapal;
348 SDL_Color colors[256];
349 for (i = 0; i < 256; i++) {
350 colors[i] = (SDL_Color) {
351 .r = p[0] * 255 / 63,
352 .g = p[1] * 255 / 63,
353 .b = p[2] * 255 / 63
354 };
355 p += 3;
357 SDL_SetPaletteColors(surf->format->palette, colors, 0, 256);
360 void Y_repaint_rect (int x, int y, int w, int h) {
361 assert(window != NULL);
362 assert(surf != NULL);
363 SDL_Surface *s = SDL_GetWindowSurface(window);
364 SDL_Rect r = (SDL_Rect) {
365 .x = x,
366 .y = y,
367 .w = w,
368 .h = h
369 };
370 SDL_BlitSurface(surf, &r, s, &r);
371 SDL_UpdateWindowSurfaceRects(window, &r, 1);
374 void Y_repaint (void) {
375 Y_repaint_rect(0, 0, surf->w, surf->h);
378 void Y_enable_text_input (void) {
379 SDL_StartTextInput();
382 void Y_disable_text_input (void) {
383 SDL_StopTextInput();
386 /* --- main --- */
388 static int sdl_to_key (int code) {
389 switch (code) {
390 case SDL_SCANCODE_0: return KEY_0;
391 case SDL_SCANCODE_1: return KEY_1;
392 case SDL_SCANCODE_2: return KEY_2;
393 case SDL_SCANCODE_3: return KEY_3;
394 case SDL_SCANCODE_4: return KEY_4;
395 case SDL_SCANCODE_5: return KEY_5;
396 case SDL_SCANCODE_6: return KEY_6;
397 case SDL_SCANCODE_7: return KEY_7;
398 case SDL_SCANCODE_8: return KEY_8;
399 case SDL_SCANCODE_9: return KEY_9;
400 case SDL_SCANCODE_A: return KEY_A;
401 case SDL_SCANCODE_B: return KEY_B;
402 case SDL_SCANCODE_C: return KEY_C;
403 case SDL_SCANCODE_D: return KEY_D;
404 case SDL_SCANCODE_E: return KEY_E;
405 case SDL_SCANCODE_F: return KEY_F;
406 case SDL_SCANCODE_G: return KEY_G;
407 case SDL_SCANCODE_H: return KEY_H;
408 case SDL_SCANCODE_I: return KEY_I;
409 case SDL_SCANCODE_J: return KEY_J;
410 case SDL_SCANCODE_K: return KEY_K;
411 case SDL_SCANCODE_L: return KEY_L;
412 case SDL_SCANCODE_M: return KEY_M;
413 case SDL_SCANCODE_N: return KEY_N;
414 case SDL_SCANCODE_O: return KEY_O;
415 case SDL_SCANCODE_P: return KEY_P;
416 case SDL_SCANCODE_Q: return KEY_Q;
417 case SDL_SCANCODE_R: return KEY_R;
418 case SDL_SCANCODE_S: return KEY_S;
419 case SDL_SCANCODE_T: return KEY_T;
420 case SDL_SCANCODE_U: return KEY_U;
421 case SDL_SCANCODE_V: return KEY_V;
422 case SDL_SCANCODE_W: return KEY_W;
423 case SDL_SCANCODE_X: return KEY_X;
424 case SDL_SCANCODE_Y: return KEY_Y;
425 case SDL_SCANCODE_Z: return KEY_Z;
426 case SDL_SCANCODE_RETURN: return KEY_RETURN;
427 case SDL_SCANCODE_ESCAPE: return KEY_ESCAPE;
428 case SDL_SCANCODE_BACKSPACE: return KEY_BACKSPACE;
429 case SDL_SCANCODE_TAB: return KEY_TAB;
430 case SDL_SCANCODE_SPACE: return KEY_SPACE;
431 case SDL_SCANCODE_MINUS: return KEY_MINUS;
432 case SDL_SCANCODE_EQUALS: return KEY_EQUALS;
433 case SDL_SCANCODE_LEFTBRACKET: return KEY_LEFTBRACKET;
434 case SDL_SCANCODE_RIGHTBRACKET: return KEY_RIGHTBRACKET;
435 case SDL_SCANCODE_BACKSLASH: return KEY_BACKSLASH;
436 case SDL_SCANCODE_SEMICOLON: return KEY_SEMICOLON;
437 case SDL_SCANCODE_APOSTROPHE: return KEY_APOSTROPHE;
438 case SDL_SCANCODE_GRAVE: return KEY_GRAVE;
439 case SDL_SCANCODE_COMMA: return KEY_COMMA;
440 case SDL_SCANCODE_PERIOD: return KEY_PERIOD;
441 case SDL_SCANCODE_SLASH: return KEY_SLASH;
442 case SDL_SCANCODE_CAPSLOCK: return KEY_CAPSLOCK;
443 case SDL_SCANCODE_F1: return KEY_F1;
444 case SDL_SCANCODE_F2: return KEY_F2;
445 case SDL_SCANCODE_F3: return KEY_F3;
446 case SDL_SCANCODE_F4: return KEY_F4;
447 case SDL_SCANCODE_F5: return KEY_F5;
448 case SDL_SCANCODE_F6: return KEY_F6;
449 case SDL_SCANCODE_F7: return KEY_F7;
450 case SDL_SCANCODE_F8: return KEY_F8;
451 case SDL_SCANCODE_F9: return KEY_F9;
452 case SDL_SCANCODE_F10: return KEY_F10;
453 case SDL_SCANCODE_F11: return KEY_F11;
454 case SDL_SCANCODE_F12: return KEY_F12;
455 case SDL_SCANCODE_PRINTSCREEN: return KEY_PRINTSCREEN;
456 case SDL_SCANCODE_SCROLLLOCK: return KEY_SCROLLLOCK;
457 case SDL_SCANCODE_PAUSE: return KEY_PAUSE;
458 case SDL_SCANCODE_INSERT: return KEY_INSERT;
459 case SDL_SCANCODE_HOME: return KEY_HOME;
460 case SDL_SCANCODE_PAGEUP: return KEY_PAGEUP;
461 case SDL_SCANCODE_DELETE: return KEY_DELETE;
462 case SDL_SCANCODE_END: return KEY_END;
463 case SDL_SCANCODE_PAGEDOWN: return KEY_PAGEDOWN;
464 case SDL_SCANCODE_RIGHT: return KEY_RIGHT;
465 case SDL_SCANCODE_LEFT: return KEY_LEFT;
466 case SDL_SCANCODE_DOWN: return KEY_DOWN;
467 case SDL_SCANCODE_UP: return KEY_UP;
468 case SDL_SCANCODE_NUMLOCKCLEAR: return KEY_NUMLOCK;
469 case SDL_SCANCODE_KP_DIVIDE: return KEY_KP_DIVIDE;
470 case SDL_SCANCODE_KP_MULTIPLY: return KEY_KP_MULTIPLY;
471 case SDL_SCANCODE_KP_MINUS: return KEY_KP_MINUS;
472 case SDL_SCANCODE_KP_PLUS: return KEY_KP_PLUS;
473 case SDL_SCANCODE_KP_ENTER: return KEY_KP_ENTER;
474 case SDL_SCANCODE_KP_0: return KEY_KP_0;
475 case SDL_SCANCODE_KP_1: return KEY_KP_1;
476 case SDL_SCANCODE_KP_2: return KEY_KP_2;
477 case SDL_SCANCODE_KP_3: return KEY_KP_3;
478 case SDL_SCANCODE_KP_4: return KEY_KP_4;
479 case SDL_SCANCODE_KP_5: return KEY_KP_5;
480 case SDL_SCANCODE_KP_6: return KEY_KP_6;
481 case SDL_SCANCODE_KP_7: return KEY_KP_7;
482 case SDL_SCANCODE_KP_8: return KEY_KP_8;
483 case SDL_SCANCODE_KP_9: return KEY_KP_9;
484 case SDL_SCANCODE_KP_PERIOD: return KEY_KP_PERIOD;
485 case SDL_SCANCODE_SYSREQ: return KEY_SYSREQ;
486 case SDL_SCANCODE_LCTRL: return KEY_LCTRL;
487 case SDL_SCANCODE_LSHIFT: return KEY_LSHIFT;
488 case SDL_SCANCODE_LALT: return KEY_LALT;
489 case SDL_SCANCODE_LGUI: return KEY_LSUPER;
490 case SDL_SCANCODE_RCTRL: return KEY_RCTRL;
491 case SDL_SCANCODE_RSHIFT: return KEY_RSHIFT;
492 case SDL_SCANCODE_RALT: return KEY_RALT;
493 case SDL_SCANCODE_RGUI: return KEY_RSUPER;
494 default: return KEY_UNKNOWN;
498 static void window_event_handler (SDL_WindowEvent *ev) {
499 switch (ev->event) {
500 case SDL_WINDOWEVENT_RESIZED:
501 R_set_videomode(ev->data1, ev->data2, Y_get_fullscreen());
502 break;
503 case SDL_WINDOWEVENT_CLOSE:
504 ERR_quit();
505 break;
509 static int utf8_to_wchar (char *x) {
510 int i = 0;
511 byte *s = (byte*)x;
512 if (s[0] < 0x80) {
513 return s[0];
514 } else if (s[0] < 0xE0) {
515 if (s[0] - 192 >= 0 && s[1] >= 0x80 && s[1] < 0xE0) {
516 i = (s[0] - 192) * 64 + s[1] - 128;
518 } else if (s[0] < 0xF0) {
519 if (s[1] >= 0x80 && s[1] < 0xE0 && s[2] >= 0x80 && s[2] < 0xE0) {
520 i = ((s[0] - 224) * 64 + s[1] - 128) * 64 + s[2] - 128;
523 return i;
526 static void poll_events (void) {
527 int key, down, uch, ch;
528 SDL_Event ev;
529 while (SDL_PollEvent(&ev)) {
530 switch (ev.type) {
531 case SDL_QUIT:
532 ERR_quit();
533 break;
534 case SDL_WINDOWEVENT:
535 if (ev.window.windowID == SDL_GetWindowID(window)) {
536 window_event_handler(&ev.window);
538 break;
539 case SDL_KEYDOWN:
540 case SDL_KEYUP:
541 down = ev.type == SDL_KEYDOWN;
542 key = sdl_to_key(ev.key.keysym.scancode);
543 I_press(key, down);
544 GM_key(key, down);
545 break;
546 case SDL_TEXTINPUT:
547 uch = utf8_to_wchar(ev.text.text);
548 ch = cp866_utoc(uch);
549 if (ch >= 0) {
550 GM_input(ch);
552 break;
557 static void step (void) {
558 poll_events();
559 MUS_update();
560 Uint32 t = SDL_GetTicks();
561 if (t - ticks > DELAY) {
562 ticks = t;
563 G_act();
565 R_draw();
568 int main (int argc, char **argv) {
569 CFG_args(argc, argv);
570 logo("system: initialize SDL2\n");
571 if (SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO | SDL_INIT_EVENTS) == -1) {
572 logo("system: failed to init SDL2: %s\n", SDL_GetError());
573 return 1;
575 // Player 1 defaults
576 pl1.ku = KEY_KP_8;
577 pl1.kd = KEY_KP_5;
578 pl1.kl = KEY_KP_4;
579 pl1.kr = KEY_KP_6;
580 pl1.kf = KEY_PAGEDOWN;
581 pl1.kj = KEY_DELETE;
582 pl1.kwl = KEY_HOME;
583 pl1.kwr = KEY_END;
584 pl1.kp = KEY_KP_8;
585 // Player 2 defaults
586 pl2.ku = KEY_E;
587 pl2.kd = KEY_D;
588 pl2.kl = KEY_S;
589 pl2.kr = KEY_F;
590 pl2.kf = KEY_A;
591 pl2.kj = KEY_Q;
592 pl2.kwl = KEY_1;
593 pl2.kwr = KEY_2;
594 pl2.kp = KEY_E;
595 srand(SDL_GetTicks());
596 CFG_load();
597 F_addwad("doom2d.wad");
598 F_initwads();
599 S_init();
600 MUS_init();
601 R_init();
602 G_init();
603 ticks = SDL_GetTicks();
604 #ifdef __EMSCRIPTEN__
605 emscripten_set_main_loop(step, 0, 1);
606 #else
607 while (!quit) {
608 step();
610 #endif
611 CFG_save();
612 R_done();
613 MUS_done();
614 S_done();
615 SDL_Quit();
616 return 0;