DEADSOFTWARE

1c20e9ea746035dd0a8a49bec7beed072f86b99b
[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 "cp866.h"
31 #include "player.h" // pl1 pl2
32 #include "menu.h" // G_keyf
33 #include "error.h" // logo
34 #include "monster.h" // nomon
36 #include "files.h" // F_startup F_addwad F_initwads F_allocres
37 #include "config.h" // CFG_args CFG_load CFG_save
38 #include "args.h" // ARG_parse
39 #include "memory.h" // M_startup
40 #include "game.h" // G_init G_act
41 #include "sound.h" // S_init S_done
42 #include "music.h" // S_initmusic S_updatemusic S_donemusic
43 #include "render.h" // R_init R_draw R_done
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 M_shutdown();
137 SDL_Quit();
138 puts("\nCRITICAL ERROR:");
139 va_start(ap, s);
140 vprintf(s, ap);
141 va_end(ap);
142 puts("");
143 abort();
146 void ERR_quit (void) {
147 quit = 1;
150 /* --- system.h --- */
152 static int Y_resize_window (int w, int h, int fullscreen) {
153 assert(w > 0);
154 assert(h > 0);
155 assert(window != NULL);
156 if (surf != NULL) {
157 if (surf->w != w || surf->h != h) {
158 SDL_Surface *s = SDL_CreateRGBSurface(0, w, h, 8, 0, 0, 0, 0);
159 if (s != NULL) {
160 SDL_SetPaletteColors(s->format->palette, surf->format->palette->colors, 0, surf->format->palette->ncolors);
161 SDL_FreeSurface(surf);
162 surf = s;
166 SDL_SetWindowSize(window, w, h);
167 Y_set_fullscreen(fullscreen);
168 return 1;
171 int Y_set_videomode_opengl (int w, int h, int fullscreen) {
172 assert(w > 0);
173 assert(h > 0);
174 Uint32 flags;
175 SDL_Window *win;
176 SDL_GLContext ctx;
177 if (window != NULL && context != NULL) {
178 Y_resize_window(w, h, fullscreen);
179 win = window;
180 } else {
181 flags = SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL;
182 if (fullscreen) {
183 flags = flags | SDL_WINDOW_FULLSCREEN;
185 // TODO set context version and type
186 #ifdef __EMSCRIPTEN__
187 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
188 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
189 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
190 #else
191 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1);
192 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
193 #endif
194 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
195 win = SDL_CreateWindow(TITLE_STR, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, flags);
196 if (win != NULL) {
197 ctx = SDL_GL_CreateContext(win);
198 if (ctx != NULL) {
199 Y_unset_videomode();
200 window = win;
201 context = ctx;
202 SDL_GL_MakeCurrent(window, context);
203 } else {
204 SDL_DestroyWindow(win);
205 win = NULL;
209 if (win == NULL) {
210 logo("Y_set_videomode_opengl: error: %s\n", SDL_GetError());
212 return win != NULL;
215 int Y_set_videomode_software (int w, int h, int fullscreen) {
216 assert(w > 0);
217 assert(h > 0);
218 Uint32 flags;
219 SDL_Surface *s;
220 SDL_Window *win;
221 if (window != NULL && surf != NULL) {
222 Y_resize_window(w, h, fullscreen);
223 win = window;
224 } else {
225 flags = SDL_WINDOW_RESIZABLE;
226 if (fullscreen) {
227 flags = flags | SDL_WINDOW_FULLSCREEN;
229 win = SDL_CreateWindow(TITLE_STR, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, flags);
230 if (win != NULL) {
231 s = SDL_CreateRGBSurface(0, w, h, 8, 0, 0, 0, 0);
232 if (s != NULL) {
233 Y_unset_videomode();
234 window = win;
235 surf = s;
236 } else {
237 SDL_DestroyWindow(win);
238 win = NULL;
242 if (win == NULL) {
243 logo("Y_set_videomode_software: error: %s\n", SDL_GetError());
245 return win != NULL;
248 void Y_get_videomode (int *w, int *h) {
249 if (window != NULL) {
250 SDL_GetWindowSize(window, w, h);
251 } else {
252 *w = 0;
253 *h = 0;
257 int Y_videomode_setted (void) {
258 return window != NULL;
261 void Y_unset_videomode (void) {
262 if (window != NULL) {
263 if (context != NULL) {
264 SDL_GL_MakeCurrent(window, NULL);
265 SDL_GL_DeleteContext(context);
266 context = NULL;
268 if (surf != NULL) {
269 SDL_FreeSurface(surf);
270 surf = NULL;
272 SDL_DestroyWindow(window);
273 window = NULL;
277 static void init_videomode_list (void) {
278 int i, j, k;
279 SDL_DisplayMode m;
280 int n = SDL_GetNumDisplayModes(0);
281 if (vlist.modes != NULL) {
282 free(vlist.modes);
283 vlist.modes = NULL;
284 vlist.n = 0;
286 if (n > 0) {
287 vlist.modes = malloc(n * sizeof(videomode_size_t));
288 if (vlist.modes != NULL) {
289 j = 0;
290 for (i = 0; i < n; i++) {
291 SDL_GetDisplayMode(0, i, &m);
292 k = 0;
293 while (k < j && (m.w != vlist.modes[k].w || m.h != vlist.modes[k].h)) {
294 k++;
296 if (k >= j) {
297 vlist.modes[j] = (videomode_size_t) {
298 .w = m.w,
299 .h = m.h
300 };
301 j++;
304 vlist.n = j;
309 const videomode_t *Y_get_videomode_list_opengl (int fullscreen) {
310 init_videomode_list();
311 return &vlist;
314 const videomode_t *Y_get_videomode_list_software (int fullscreen) {
315 init_videomode_list();
316 return &vlist;
319 void Y_set_fullscreen (int yes) {
320 if (window != NULL) {
321 SDL_SetWindowFullscreen(window, yes ? SDL_WINDOW_FULLSCREEN : 0);
325 int Y_get_fullscreen (void) {
326 return (window != NULL) && (SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN);
329 void Y_swap_buffers (void) {
330 assert(window != NULL);
331 assert(context != NULL);
332 SDL_GL_SwapWindow(window);
335 void Y_get_buffer (byte **buf, int *w, int *h, int *pitch) {
336 assert(window != NULL);
337 assert(surf != NULL);
338 *buf = surf->pixels;
339 *w = surf->w;
340 *h = surf->h;
341 *pitch = surf->pitch;
344 void Y_set_vga_palette (byte *vgapal) {
345 assert(window != NULL);
346 assert(surf != NULL);
347 int i;
348 byte *p = vgapal;
349 SDL_Color colors[256];
350 for (i = 0; i < 256; i++) {
351 colors[i] = (SDL_Color) {
352 .r = p[0] * 255 / 63,
353 .g = p[1] * 255 / 63,
354 .b = p[2] * 255 / 63
355 };
356 p += 3;
358 SDL_SetPaletteColors(surf->format->palette, colors, 0, 256);
361 void Y_repaint_rect (int x, int y, int w, int h) {
362 assert(window != NULL);
363 assert(surf != NULL);
364 SDL_Surface *s = SDL_GetWindowSurface(window);
365 SDL_Rect r = (SDL_Rect) {
366 .x = x,
367 .y = y,
368 .w = w,
369 .h = h
370 };
371 SDL_BlitSurface(surf, &r, s, &r);
372 SDL_UpdateWindowSurfaceRects(window, &r, 1);
375 void Y_repaint (void) {
376 Y_repaint_rect(0, 0, surf->w, surf->h);
379 void Y_enable_text_input (void) {
380 SDL_StartTextInput();
383 void Y_disable_text_input (void) {
384 SDL_StopTextInput();
387 /* --- main --- */
389 static int sdl_to_key (int code) {
390 switch (code) {
391 case SDL_SCANCODE_0: return KEY_0;
392 case SDL_SCANCODE_1: return KEY_1;
393 case SDL_SCANCODE_2: return KEY_2;
394 case SDL_SCANCODE_3: return KEY_3;
395 case SDL_SCANCODE_4: return KEY_4;
396 case SDL_SCANCODE_5: return KEY_5;
397 case SDL_SCANCODE_6: return KEY_6;
398 case SDL_SCANCODE_7: return KEY_7;
399 case SDL_SCANCODE_8: return KEY_8;
400 case SDL_SCANCODE_9: return KEY_9;
401 case SDL_SCANCODE_A: return KEY_A;
402 case SDL_SCANCODE_B: return KEY_B;
403 case SDL_SCANCODE_C: return KEY_C;
404 case SDL_SCANCODE_D: return KEY_D;
405 case SDL_SCANCODE_E: return KEY_E;
406 case SDL_SCANCODE_F: return KEY_F;
407 case SDL_SCANCODE_G: return KEY_G;
408 case SDL_SCANCODE_H: return KEY_H;
409 case SDL_SCANCODE_I: return KEY_I;
410 case SDL_SCANCODE_J: return KEY_J;
411 case SDL_SCANCODE_K: return KEY_K;
412 case SDL_SCANCODE_L: return KEY_L;
413 case SDL_SCANCODE_M: return KEY_M;
414 case SDL_SCANCODE_N: return KEY_N;
415 case SDL_SCANCODE_O: return KEY_O;
416 case SDL_SCANCODE_P: return KEY_P;
417 case SDL_SCANCODE_Q: return KEY_Q;
418 case SDL_SCANCODE_R: return KEY_R;
419 case SDL_SCANCODE_S: return KEY_S;
420 case SDL_SCANCODE_T: return KEY_T;
421 case SDL_SCANCODE_U: return KEY_U;
422 case SDL_SCANCODE_V: return KEY_V;
423 case SDL_SCANCODE_W: return KEY_W;
424 case SDL_SCANCODE_X: return KEY_X;
425 case SDL_SCANCODE_Y: return KEY_Y;
426 case SDL_SCANCODE_Z: return KEY_Z;
427 case SDL_SCANCODE_RETURN: return KEY_RETURN;
428 case SDL_SCANCODE_ESCAPE: return KEY_ESCAPE;
429 case SDL_SCANCODE_BACKSPACE: return KEY_BACKSPACE;
430 case SDL_SCANCODE_TAB: return KEY_TAB;
431 case SDL_SCANCODE_SPACE: return KEY_SPACE;
432 case SDL_SCANCODE_MINUS: return KEY_MINUS;
433 case SDL_SCANCODE_EQUALS: return KEY_EQUALS;
434 case SDL_SCANCODE_LEFTBRACKET: return KEY_LEFTBRACKET;
435 case SDL_SCANCODE_RIGHTBRACKET: return KEY_RIGHTBRACKET;
436 case SDL_SCANCODE_BACKSLASH: return KEY_BACKSLASH;
437 case SDL_SCANCODE_SEMICOLON: return KEY_SEMICOLON;
438 case SDL_SCANCODE_APOSTROPHE: return KEY_APOSTROPHE;
439 case SDL_SCANCODE_GRAVE: return KEY_GRAVE;
440 case SDL_SCANCODE_COMMA: return KEY_COMMA;
441 case SDL_SCANCODE_PERIOD: return KEY_PERIOD;
442 case SDL_SCANCODE_SLASH: return KEY_SLASH;
443 case SDL_SCANCODE_CAPSLOCK: return KEY_CAPSLOCK;
444 case SDL_SCANCODE_F1: return KEY_F1;
445 case SDL_SCANCODE_F2: return KEY_F2;
446 case SDL_SCANCODE_F3: return KEY_F3;
447 case SDL_SCANCODE_F4: return KEY_F4;
448 case SDL_SCANCODE_F5: return KEY_F5;
449 case SDL_SCANCODE_F6: return KEY_F6;
450 case SDL_SCANCODE_F7: return KEY_F7;
451 case SDL_SCANCODE_F8: return KEY_F8;
452 case SDL_SCANCODE_F9: return KEY_F9;
453 case SDL_SCANCODE_F10: return KEY_F10;
454 case SDL_SCANCODE_F11: return KEY_F11;
455 case SDL_SCANCODE_F12: return KEY_F12;
456 case SDL_SCANCODE_PRINTSCREEN: return KEY_PRINTSCREEN;
457 case SDL_SCANCODE_SCROLLLOCK: return KEY_SCROLLLOCK;
458 case SDL_SCANCODE_PAUSE: return KEY_PAUSE;
459 case SDL_SCANCODE_INSERT: return KEY_INSERT;
460 case SDL_SCANCODE_HOME: return KEY_HOME;
461 case SDL_SCANCODE_PAGEUP: return KEY_PAGEUP;
462 case SDL_SCANCODE_DELETE: return KEY_DELETE;
463 case SDL_SCANCODE_END: return KEY_END;
464 case SDL_SCANCODE_PAGEDOWN: return KEY_PAGEDOWN;
465 case SDL_SCANCODE_RIGHT: return KEY_RIGHT;
466 case SDL_SCANCODE_LEFT: return KEY_LEFT;
467 case SDL_SCANCODE_DOWN: return KEY_DOWN;
468 case SDL_SCANCODE_UP: return KEY_UP;
469 case SDL_SCANCODE_NUMLOCKCLEAR: return KEY_NUMLOCK;
470 case SDL_SCANCODE_KP_DIVIDE: return KEY_KP_DIVIDE;
471 case SDL_SCANCODE_KP_MULTIPLY: return KEY_KP_MULTIPLY;
472 case SDL_SCANCODE_KP_MINUS: return KEY_KP_MINUS;
473 case SDL_SCANCODE_KP_PLUS: return KEY_KP_PLUS;
474 case SDL_SCANCODE_KP_ENTER: return KEY_KP_ENTER;
475 case SDL_SCANCODE_KP_0: return KEY_KP_0;
476 case SDL_SCANCODE_KP_1: return KEY_KP_1;
477 case SDL_SCANCODE_KP_2: return KEY_KP_2;
478 case SDL_SCANCODE_KP_3: return KEY_KP_3;
479 case SDL_SCANCODE_KP_4: return KEY_KP_4;
480 case SDL_SCANCODE_KP_5: return KEY_KP_5;
481 case SDL_SCANCODE_KP_6: return KEY_KP_6;
482 case SDL_SCANCODE_KP_7: return KEY_KP_7;
483 case SDL_SCANCODE_KP_8: return KEY_KP_8;
484 case SDL_SCANCODE_KP_9: return KEY_KP_9;
485 case SDL_SCANCODE_KP_PERIOD: return KEY_KP_PERIOD;
486 case SDL_SCANCODE_SYSREQ: return KEY_SYSREQ;
487 case SDL_SCANCODE_LCTRL: return KEY_LCTRL;
488 case SDL_SCANCODE_LSHIFT: return KEY_LSHIFT;
489 case SDL_SCANCODE_LALT: return KEY_LALT;
490 case SDL_SCANCODE_LGUI: return KEY_LSUPER;
491 case SDL_SCANCODE_RCTRL: return KEY_RCTRL;
492 case SDL_SCANCODE_RSHIFT: return KEY_RSHIFT;
493 case SDL_SCANCODE_RALT: return KEY_RALT;
494 case SDL_SCANCODE_RGUI: return KEY_RSUPER;
495 default: return KEY_UNKNOWN;
499 static void window_event_handler (SDL_WindowEvent *ev) {
500 switch (ev->event) {
501 case SDL_WINDOWEVENT_RESIZED:
502 R_set_videomode(ev->data1, ev->data2, Y_get_fullscreen());
503 break;
504 case SDL_WINDOWEVENT_CLOSE:
505 ERR_quit();
506 break;
510 static int utf8_to_wchar (char *x) {
511 int i = 0;
512 byte *s = (byte*)x;
513 if (s[0] < 0x80) {
514 return s[0];
515 } else if (s[0] < 0xE0) {
516 if (s[0] - 192 >= 0 && s[1] >= 0x80 && s[1] < 0xE0) {
517 i = (s[0] - 192) * 64 + s[1] - 128;
519 } else if (s[0] < 0xF0) {
520 if (s[1] >= 0x80 && s[1] < 0xE0 && s[2] >= 0x80 && s[2] < 0xE0) {
521 i = ((s[0] - 224) * 64 + s[1] - 128) * 64 + s[2] - 128;
524 return i;
527 static void poll_events (void) {
528 int key, down, uch, ch;
529 SDL_Event ev;
530 while (SDL_PollEvent(&ev)) {
531 switch (ev.type) {
532 case SDL_QUIT:
533 ERR_quit();
534 break;
535 case SDL_WINDOWEVENT:
536 if (ev.window.windowID == SDL_GetWindowID(window)) {
537 window_event_handler(&ev.window);
539 break;
540 case SDL_KEYDOWN:
541 case SDL_KEYUP:
542 down = ev.type == SDL_KEYDOWN;
543 key = sdl_to_key(ev.key.keysym.scancode);
544 I_press(key, down);
545 GM_key(key, down);
546 break;
547 case SDL_TEXTINPUT:
548 uch = utf8_to_wchar(ev.text.text);
549 ch = cp866_utoc(uch);
550 if (ch >= 0) {
551 GM_input(ch);
553 break;
558 static void step (void) {
559 poll_events();
560 MUS_update();
561 Uint32 t = SDL_GetTicks();
562 if (t - ticks > DELAY) {
563 ticks = t;
564 G_act();
566 R_draw();
569 int main (int argc, char **argv) {
570 CFG_args(argc, argv);
571 logo("system: initialize SDL2\n");
572 if (SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO | SDL_INIT_EVENTS) == -1) {
573 logo("system: failed to init SDL2: %s\n", SDL_GetError());
574 return 1;
576 // Player 1 defaults
577 pl1.ku = KEY_KP_8;
578 pl1.kd = KEY_KP_5;
579 pl1.kl = KEY_KP_4;
580 pl1.kr = KEY_KP_6;
581 pl1.kf = KEY_PAGEDOWN;
582 pl1.kj = KEY_DELETE;
583 pl1.kwl = KEY_HOME;
584 pl1.kwr = KEY_END;
585 pl1.kp = KEY_KP_8;
586 // Player 2 defaults
587 pl2.ku = KEY_E;
588 pl2.kd = KEY_D;
589 pl2.kl = KEY_S;
590 pl2.kr = KEY_F;
591 pl2.kf = KEY_A;
592 pl2.kj = KEY_Q;
593 pl2.kwl = KEY_1;
594 pl2.kwr = KEY_2;
595 pl2.kp = KEY_E;
596 srand(SDL_GetTicks());
597 F_startup();
598 CFG_load();
599 F_addwad("doom2d.wad");
600 F_initwads();
601 M_startup();
602 F_allocres();
603 S_init();
604 MUS_init();
605 R_init();
606 G_init();
607 ticks = SDL_GetTicks();
608 #ifdef __EMSCRIPTEN__
609 emscripten_set_main_loop(step, 0, 1);
610 #else
611 while (!quit) {
612 step();
614 #endif
615 CFG_save();
616 R_done();
617 MUS_done();
618 S_done();
619 M_shutdown();
620 SDL_Quit();
621 return 0;