DEADSOFTWARE

cosmetix: cleanup view.h
[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 "my.h" // fexists
32 #include "player.h" // pl1 pl2
33 #include "menu.h" // G_keyf
34 #include "error.h" // logo
35 #include "monster.h" // nomon
37 #include "files.h" // F_startup F_addwad F_initwads F_allocres
38 #include "config.h" // CFG_args CFG_load CFG_save
39 #include "args.h" // ARG_parse
40 #include "memory.h" // M_startup
41 #include "game.h" // G_init G_act
42 #include "sound.h" // S_init S_done
43 #include "music.h" // S_initmusic S_updatemusic S_donemusic
44 #include "render.h" // R_init R_draw R_done
46 #define TITLE_STR "Doom 2D (SDL2)"
48 static Uint32 ticks;
49 static int quit = 0;
50 static SDL_Window *window;
51 static SDL_GLContext context;
52 static SDL_Surface *surf;
53 static videomode_t vlist;
55 static const cfg_t arg[] = {
56 {"file", NULL, Y_FILES},
57 {"cheat", &cheat, Y_SW_ON},
58 // {"vga", &shot_vga, Y_SW_ON},
59 // {"musvol", &mus_vol, Y_WORD},
60 {"mon", &nomon, Y_SW_OFF},
61 {"warp", &_warp, Y_BYTE},
62 // {"config", NULL, cfg_file, Y_STRING},
63 {NULL, NULL, 0} // end
64 };
66 static const cfg_t cfg[] = {
67 // {"screenshot", &shot_vga, Y_SW_ON},
68 // {"music_volume", &mus_vol, Y_WORD},
69 // {"music_random", &music_random, Y_SW_ON},
70 // {"music_time", &music_time, Y_DWORD},
71 // {"music_fade", &music_fade, Y_DWORD},
72 {"pl1_left", &pl1.kl, Y_KEY},
73 {"pl1_right",&pl1.kr, Y_KEY},
74 {"pl1_up", &pl1.ku, Y_KEY},
75 {"pl1_down", &pl1.kd, Y_KEY},
76 {"pl1_jump", &pl1.kj, Y_KEY},
77 {"pl1_fire", &pl1.kf, Y_KEY},
78 {"pl1_next", &pl1.kwr, Y_KEY},
79 {"pl1_prev", &pl1.kwl, Y_KEY},
80 {"pl1_use", &pl1.kp, Y_KEY},
81 {"pl2_left", &pl2.kl, Y_KEY},
82 {"pl2_right", &pl2.kr, Y_KEY},
83 {"pl2_up", &pl2.ku, Y_KEY},
84 {"pl2_down", &pl2.kd, Y_KEY},
85 {"pl2_jump", &pl2.kj, Y_KEY},
86 {"pl2_fire", &pl2.kf, Y_KEY},
87 {"pl2_next", &pl2.kwr, Y_KEY},
88 {"pl2_prev", &pl2.kwl, Y_KEY},
89 {"pl2_use", &pl2.kp, Y_KEY},
90 {NULL, NULL, 0} // end
91 };
93 static void CFG_args (int argc, char **argv) {
94 const cfg_t *list[] = { arg, R_args(), S_args(), MUS_args() };
95 ARG_parse(argc, argv, 4, list);
96 }
98 static void CFG_load (void) {
99 const cfg_t *list[] = { cfg, R_conf(), S_conf(), MUS_conf() };
100 CFG_read_config("default.cfg", 4, list);
101 CFG_read_config("doom2d.cfg", 4, list);
104 static void CFG_save (void) {
105 const cfg_t *list[] = { cfg, R_conf(), S_conf(), MUS_conf() };
106 CFG_update_config("doom2d.cfg", "doom2d.cfg", 4, list, "generated by doom2d, do not modify");
109 /* --- error.h --- */
111 void logo (const char *s, ...) {
112 va_list ap;
113 va_start(ap, s);
114 vprintf(s, ap);
115 va_end(ap);
116 fflush(stdout);
119 void logo_gas (int cur, int all) {
120 // stub
123 void ERR_failinit (char *s, ...) {
124 va_list ap;
125 va_start(ap, s);
126 vprintf(s, ap);
127 va_end(ap);
128 puts("");
129 exit(1);
132 void ERR_fatal (char *s, ...) {
133 va_list ap;
134 R_done();
135 MUS_done();
136 S_done();
137 M_shutdown();
138 SDL_Quit();
139 puts("\nCRITICAL ERROR:");
140 va_start(ap, s);
141 vprintf(s, ap);
142 va_end(ap);
143 puts("");
144 exit(1);
147 void ERR_quit (void) {
148 quit = 1;
151 /* --- system.h --- */
153 static int Y_resize_window (int w, int h, int fullscreen) {
154 assert(w > 0);
155 assert(h > 0);
156 assert(window != NULL);
157 if (surf != NULL) {
158 if (surf->w != w || surf->h != h) {
159 SDL_Surface *s = SDL_CreateRGBSurface(0, w, h, 8, 0, 0, 0, 0);
160 if (s != NULL) {
161 SDL_SetPaletteColors(s->format->palette, surf->format->palette->colors, 0, surf->format->palette->ncolors);
162 SDL_FreeSurface(surf);
163 surf = s;
167 SDL_SetWindowSize(window, w, h);
168 Y_set_fullscreen(fullscreen);
169 return 1;
172 int Y_set_videomode_opengl (int w, int h, int fullscreen) {
173 assert(w > 0);
174 assert(h > 0);
175 Uint32 flags;
176 SDL_Window *win;
177 SDL_GLContext ctx;
178 if (window != NULL && context != NULL) {
179 Y_resize_window(w, h, fullscreen);
180 win = window;
181 } else {
182 flags = SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL;
183 if (fullscreen) {
184 flags = flags | SDL_WINDOW_FULLSCREEN;
186 // TODO set context version and type
187 #ifdef __EMSCRIPTEN__
188 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
189 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
190 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
191 #else
192 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1);
193 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
194 #endif
195 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
196 win = SDL_CreateWindow(TITLE_STR, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, flags);
197 if (win != NULL) {
198 ctx = SDL_GL_CreateContext(win);
199 if (ctx != NULL) {
200 Y_unset_videomode();
201 window = win;
202 context = ctx;
203 SDL_GL_MakeCurrent(window, context);
204 } else {
205 SDL_DestroyWindow(win);
206 win = NULL;
210 if (win == NULL) {
211 logo("Y_set_videomode_opengl: error: %s\n", SDL_GetError());
213 return win != NULL;
216 int Y_set_videomode_software (int w, int h, int fullscreen) {
217 assert(w > 0);
218 assert(h > 0);
219 Uint32 flags;
220 SDL_Surface *s;
221 SDL_Window *win;
222 if (window != NULL && surf != NULL) {
223 Y_resize_window(w, h, fullscreen);
224 win = window;
225 } else {
226 flags = SDL_WINDOW_RESIZABLE;
227 if (fullscreen) {
228 flags = flags | SDL_WINDOW_FULLSCREEN;
230 win = SDL_CreateWindow(TITLE_STR, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, flags);
231 if (win != NULL) {
232 s = SDL_CreateRGBSurface(0, w, h, 8, 0, 0, 0, 0);
233 if (s != NULL) {
234 Y_unset_videomode();
235 window = win;
236 surf = s;
237 } else {
238 SDL_DestroyWindow(win);
239 win = NULL;
243 if (win == NULL) {
244 logo("Y_set_videomode_software: error: %s\n", SDL_GetError());
246 return win != NULL;
249 void Y_get_videomode (int *w, int *h) {
250 if (window != NULL) {
251 SDL_GetWindowSize(window, w, h);
252 } else {
253 *w = 0;
254 *h = 0;
258 int Y_videomode_setted (void) {
259 return window != NULL;
262 void Y_unset_videomode (void) {
263 if (window != NULL) {
264 if (context != NULL) {
265 SDL_GL_MakeCurrent(window, NULL);
266 SDL_GL_DeleteContext(context);
267 context = NULL;
269 if (surf != NULL) {
270 SDL_FreeSurface(surf);
271 surf = NULL;
273 SDL_DestroyWindow(window);
274 window = NULL;
278 static void init_videomode_list (void) {
279 int i, j, k;
280 SDL_DisplayMode m;
281 int n = SDL_GetNumDisplayModes(0);
282 if (vlist.modes != NULL) {
283 free(vlist.modes);
284 vlist.modes = NULL;
285 vlist.n = 0;
287 if (n > 0) {
288 vlist.modes = malloc(n * sizeof(videomode_size_t));
289 if (vlist.modes != NULL) {
290 j = 0;
291 for (i = 0; i < n; i++) {
292 SDL_GetDisplayMode(0, i, &m);
293 k = 0;
294 while (k < j && (m.w != vlist.modes[k].w || m.h != vlist.modes[k].h)) {
295 k++;
297 if (k >= j) {
298 vlist.modes[j] = (videomode_size_t) {
299 .w = m.w,
300 .h = m.h
301 };
302 j++;
305 vlist.n = j;
310 const videomode_t *Y_get_videomode_list_opengl (int fullscreen) {
311 init_videomode_list();
312 return &vlist;
315 const videomode_t *Y_get_videomode_list_software (int fullscreen) {
316 init_videomode_list();
317 return &vlist;
320 void Y_set_fullscreen (int yes) {
321 if (window != NULL) {
322 SDL_SetWindowFullscreen(window, yes ? SDL_WINDOW_FULLSCREEN : 0);
326 int Y_get_fullscreen (void) {
327 return (window != NULL) && (SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN);
330 void Y_swap_buffers (void) {
331 assert(window != NULL);
332 assert(context != NULL);
333 SDL_GL_SwapWindow(window);
336 void Y_get_buffer (byte **buf, int *w, int *h, int *pitch) {
337 assert(window != NULL);
338 assert(surf != NULL);
339 *buf = surf->pixels;
340 *w = surf->w;
341 *h = surf->h;
342 *pitch = surf->pitch;
345 void Y_set_vga_palette (byte *vgapal) {
346 assert(window != NULL);
347 assert(surf != NULL);
348 int i;
349 byte *p = vgapal;
350 SDL_Color colors[256];
351 for (i = 0; i < 256; i++) {
352 colors[i] = (SDL_Color) {
353 .r = p[0] * 255 / 63,
354 .g = p[1] * 255 / 63,
355 .b = p[2] * 255 / 63
356 };
357 p += 3;
359 SDL_SetPaletteColors(surf->format->palette, colors, 0, 256);
362 void Y_repaint_rect (int x, int y, int w, int h) {
363 assert(window != NULL);
364 assert(surf != NULL);
365 SDL_Surface *s = SDL_GetWindowSurface(window);
366 SDL_Rect r = (SDL_Rect) {
367 .x = x,
368 .y = y,
369 .w = w,
370 .h = h
371 };
372 SDL_BlitSurface(surf, &r, s, &r);
373 SDL_UpdateWindowSurfaceRects(window, &r, 1);
376 void Y_repaint (void) {
377 Y_repaint_rect(0, 0, surf->w, surf->h);
380 void Y_enable_text_input (void) {
381 SDL_StartTextInput();
384 void Y_disable_text_input (void) {
385 SDL_StopTextInput();
388 /* --- main --- */
390 static int sdl_to_key (int code) {
391 switch (code) {
392 case SDL_SCANCODE_0: return KEY_0;
393 case SDL_SCANCODE_1: return KEY_1;
394 case SDL_SCANCODE_2: return KEY_2;
395 case SDL_SCANCODE_3: return KEY_3;
396 case SDL_SCANCODE_4: return KEY_4;
397 case SDL_SCANCODE_5: return KEY_5;
398 case SDL_SCANCODE_6: return KEY_6;
399 case SDL_SCANCODE_7: return KEY_7;
400 case SDL_SCANCODE_8: return KEY_8;
401 case SDL_SCANCODE_9: return KEY_9;
402 case SDL_SCANCODE_A: return KEY_A;
403 case SDL_SCANCODE_B: return KEY_B;
404 case SDL_SCANCODE_C: return KEY_C;
405 case SDL_SCANCODE_D: return KEY_D;
406 case SDL_SCANCODE_E: return KEY_E;
407 case SDL_SCANCODE_F: return KEY_F;
408 case SDL_SCANCODE_G: return KEY_G;
409 case SDL_SCANCODE_H: return KEY_H;
410 case SDL_SCANCODE_I: return KEY_I;
411 case SDL_SCANCODE_J: return KEY_J;
412 case SDL_SCANCODE_K: return KEY_K;
413 case SDL_SCANCODE_L: return KEY_L;
414 case SDL_SCANCODE_M: return KEY_M;
415 case SDL_SCANCODE_N: return KEY_N;
416 case SDL_SCANCODE_O: return KEY_O;
417 case SDL_SCANCODE_P: return KEY_P;
418 case SDL_SCANCODE_Q: return KEY_Q;
419 case SDL_SCANCODE_R: return KEY_R;
420 case SDL_SCANCODE_S: return KEY_S;
421 case SDL_SCANCODE_T: return KEY_T;
422 case SDL_SCANCODE_U: return KEY_U;
423 case SDL_SCANCODE_V: return KEY_V;
424 case SDL_SCANCODE_W: return KEY_W;
425 case SDL_SCANCODE_X: return KEY_X;
426 case SDL_SCANCODE_Y: return KEY_Y;
427 case SDL_SCANCODE_Z: return KEY_Z;
428 case SDL_SCANCODE_RETURN: return KEY_RETURN;
429 case SDL_SCANCODE_ESCAPE: return KEY_ESCAPE;
430 case SDL_SCANCODE_BACKSPACE: return KEY_BACKSPACE;
431 case SDL_SCANCODE_TAB: return KEY_TAB;
432 case SDL_SCANCODE_SPACE: return KEY_SPACE;
433 case SDL_SCANCODE_MINUS: return KEY_MINUS;
434 case SDL_SCANCODE_EQUALS: return KEY_EQUALS;
435 case SDL_SCANCODE_LEFTBRACKET: return KEY_LEFTBRACKET;
436 case SDL_SCANCODE_RIGHTBRACKET: return KEY_RIGHTBRACKET;
437 case SDL_SCANCODE_BACKSLASH: return KEY_BACKSLASH;
438 case SDL_SCANCODE_SEMICOLON: return KEY_SEMICOLON;
439 case SDL_SCANCODE_APOSTROPHE: return KEY_APOSTROPHE;
440 case SDL_SCANCODE_GRAVE: return KEY_GRAVE;
441 case SDL_SCANCODE_COMMA: return KEY_COMMA;
442 case SDL_SCANCODE_PERIOD: return KEY_PERIOD;
443 case SDL_SCANCODE_SLASH: return KEY_SLASH;
444 case SDL_SCANCODE_CAPSLOCK: return KEY_CAPSLOCK;
445 case SDL_SCANCODE_F1: return KEY_F1;
446 case SDL_SCANCODE_F2: return KEY_F2;
447 case SDL_SCANCODE_F3: return KEY_F3;
448 case SDL_SCANCODE_F4: return KEY_F4;
449 case SDL_SCANCODE_F5: return KEY_F5;
450 case SDL_SCANCODE_F6: return KEY_F6;
451 case SDL_SCANCODE_F7: return KEY_F7;
452 case SDL_SCANCODE_F8: return KEY_F8;
453 case SDL_SCANCODE_F9: return KEY_F9;
454 case SDL_SCANCODE_F10: return KEY_F10;
455 case SDL_SCANCODE_F11: return KEY_F11;
456 case SDL_SCANCODE_F12: return KEY_F12;
457 case SDL_SCANCODE_PRINTSCREEN: return KEY_PRINTSCREEN;
458 case SDL_SCANCODE_SCROLLLOCK: return KEY_SCROLLLOCK;
459 case SDL_SCANCODE_PAUSE: return KEY_PAUSE;
460 case SDL_SCANCODE_INSERT: return KEY_INSERT;
461 case SDL_SCANCODE_HOME: return KEY_HOME;
462 case SDL_SCANCODE_PAGEUP: return KEY_PAGEUP;
463 case SDL_SCANCODE_DELETE: return KEY_DELETE;
464 case SDL_SCANCODE_END: return KEY_END;
465 case SDL_SCANCODE_PAGEDOWN: return KEY_PAGEDOWN;
466 case SDL_SCANCODE_RIGHT: return KEY_RIGHT;
467 case SDL_SCANCODE_LEFT: return KEY_LEFT;
468 case SDL_SCANCODE_DOWN: return KEY_DOWN;
469 case SDL_SCANCODE_UP: return KEY_UP;
470 case SDL_SCANCODE_NUMLOCKCLEAR: return KEY_NUMLOCK;
471 case SDL_SCANCODE_KP_DIVIDE: return KEY_KP_DIVIDE;
472 case SDL_SCANCODE_KP_MULTIPLY: return KEY_KP_MULTIPLY;
473 case SDL_SCANCODE_KP_MINUS: return KEY_KP_MINUS;
474 case SDL_SCANCODE_KP_PLUS: return KEY_KP_PLUS;
475 case SDL_SCANCODE_KP_ENTER: return KEY_KP_ENTER;
476 case SDL_SCANCODE_KP_0: return KEY_KP_0;
477 case SDL_SCANCODE_KP_1: return KEY_KP_1;
478 case SDL_SCANCODE_KP_2: return KEY_KP_2;
479 case SDL_SCANCODE_KP_3: return KEY_KP_3;
480 case SDL_SCANCODE_KP_4: return KEY_KP_4;
481 case SDL_SCANCODE_KP_5: return KEY_KP_5;
482 case SDL_SCANCODE_KP_6: return KEY_KP_6;
483 case SDL_SCANCODE_KP_7: return KEY_KP_7;
484 case SDL_SCANCODE_KP_8: return KEY_KP_8;
485 case SDL_SCANCODE_KP_9: return KEY_KP_9;
486 case SDL_SCANCODE_KP_PERIOD: return KEY_KP_PERIOD;
487 case SDL_SCANCODE_SYSREQ: return KEY_SYSREQ;
488 case SDL_SCANCODE_LCTRL: return KEY_LCTRL;
489 case SDL_SCANCODE_LSHIFT: return KEY_LSHIFT;
490 case SDL_SCANCODE_LALT: return KEY_LALT;
491 case SDL_SCANCODE_LGUI: return KEY_LSUPER;
492 case SDL_SCANCODE_RCTRL: return KEY_RCTRL;
493 case SDL_SCANCODE_RSHIFT: return KEY_RSHIFT;
494 case SDL_SCANCODE_RALT: return KEY_RALT;
495 case SDL_SCANCODE_RGUI: return KEY_RSUPER;
496 default: return KEY_UNKNOWN;
500 static void window_event_handler (SDL_WindowEvent *ev) {
501 switch (ev->event) {
502 case SDL_WINDOWEVENT_RESIZED:
503 R_set_videomode(ev->data1, ev->data2, Y_get_fullscreen());
504 break;
505 case SDL_WINDOWEVENT_CLOSE:
506 ERR_quit();
507 break;
511 static int utf8_to_wchar (char *x) {
512 int i = 0;
513 byte *s = (byte*)x;
514 if (s[0] < 0x80) {
515 return s[0];
516 } else if (s[0] < 0xE0) {
517 if (s[0] - 192 >= 0 && s[1] >= 0x80 && s[1] < 0xE0) {
518 i = (s[0] - 192) * 64 + s[1] - 128;
520 } else if (s[0] < 0xF0) {
521 if (s[1] >= 0x80 && s[1] < 0xE0 && s[2] >= 0x80 && s[2] < 0xE0) {
522 i = ((s[0] - 224) * 64 + s[1] - 128) * 64 + s[2] - 128;
525 return i;
528 static void poll_events (void) {
529 int key, down, uch, ch;
530 SDL_Event ev;
531 while (SDL_PollEvent(&ev)) {
532 switch (ev.type) {
533 case SDL_QUIT:
534 ERR_quit();
535 break;
536 case SDL_WINDOWEVENT:
537 if (ev.window.windowID == SDL_GetWindowID(window)) {
538 window_event_handler(&ev.window);
540 break;
541 case SDL_KEYDOWN:
542 case SDL_KEYUP:
543 down = ev.type == SDL_KEYDOWN;
544 key = sdl_to_key(ev.key.keysym.scancode);
545 I_press(key, down);
546 GM_key(key, down);
547 break;
548 case SDL_TEXTINPUT:
549 uch = utf8_to_wchar(ev.text.text);
550 ch = cp866_utoc(uch);
551 if (ch >= 0) {
552 GM_input(ch);
554 break;
559 static void step (void) {
560 poll_events();
561 MUS_update();
562 Uint32 t = SDL_GetTicks();
563 if (t - ticks > DELAY) {
564 ticks = t;
565 G_act();
567 R_draw();
570 int main (int argc, char **argv) {
571 char *pw;
572 CFG_args(argc, argv);
573 logo("system: initialize SDL2\n");
574 if (SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO | SDL_INIT_EVENTS) == -1) {
575 logo("system: failed to init SDL2: %s\n", SDL_GetError());
576 return 1;
578 // Player 1 defaults
579 pl1.ku = KEY_KP_8;
580 pl1.kd = KEY_KP_5;
581 pl1.kl = KEY_KP_4;
582 pl1.kr = KEY_KP_6;
583 pl1.kf = KEY_PAGEDOWN;
584 pl1.kj = KEY_DELETE;
585 pl1.kwl = KEY_HOME;
586 pl1.kwr = KEY_END;
587 pl1.kp = KEY_KP_8;
588 // Player 2 defaults
589 pl2.ku = KEY_E;
590 pl2.kd = KEY_D;
591 pl2.kl = KEY_S;
592 pl2.kr = KEY_F;
593 pl2.kf = KEY_A;
594 pl2.kj = KEY_Q;
595 pl2.kwl = KEY_1;
596 pl2.kwr = KEY_2;
597 pl2.kp = KEY_E;
598 srand(SDL_GetTicks());
599 F_startup();
600 CFG_load();
601 #ifndef WIN32
602 pw = "/usr/share/doom2d-rembo/doom2d.wad";
603 #else
604 pw = "doom2d.wad";
605 #endif
606 if (fexists(pw)) {
607 F_addwad(pw);
608 } else {
609 F_addwad("doom2d.wad");
611 F_initwads();
612 M_startup();
613 F_allocres();
614 S_init();
615 MUS_init();
616 R_init();
617 G_init();
618 ticks = SDL_GetTicks();
619 #ifdef __EMSCRIPTEN__
620 emscripten_set_main_loop(step, 0, 1);
621 #else
622 while (!quit) {
623 step();
625 #endif
626 CFG_save();
627 R_done();
628 MUS_done();
629 S_done();
630 M_shutdown();
631 SDL_Quit();
632 return 0;