DEADSOFTWARE

streams: new method: getlen
[flatwaifu.git] / src / sdl / 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 <SDL/SDL.h>
21 #include <stdio.h>
22 #include <stdarg.h>
23 #include <stdlib.h> // srand exit
24 #include <assert.h>
25 #include "system.h"
26 #include "input.h"
28 #include "player.h" // pl1 pl2
29 #include "menu.h" // G_keyf
30 #include "error.h" // logo
31 #include "monster.h" // nomon
33 #include "files.h" // F_startup F_addwad F_initwads F_allocres
34 #include "config.h" // CFG_args CFG_load CFG_save
35 #include "args.h" // ARG_parse
36 #include "memory.h" // M_startup
37 #include "game.h" // G_init G_act
38 #include "sound.h" // S_init S_done
39 #include "music.h" // S_initmusic S_updatemusic S_donemusic
40 #include "render.h" // R_init R_draw R_done
42 #define MODE_NONE 0
43 #define MODE_OPENGL 1
44 #define MODE_SOFTWARE 2
46 static Uint32 ticks;
47 static int quit = 0;
48 static SDL_Surface *surf = NULL;
49 static int mode = MODE_NONE;
50 static int text_input;
51 static videomode_t vlist;
53 static const cfg_t arg[] = {
54 {"file", NULL, Y_FILES},
55 {"cheat", &cheat, Y_SW_ON},
56 // {"vga", &shot_vga, Y_SW_ON},
57 // {"musvol", &mus_vol, Y_WORD},
58 {"mon", &nomon, Y_SW_OFF},
59 {"warp", &_warp, Y_BYTE},
60 // {"config", NULL, cfg_file, Y_STRING},
61 {NULL, NULL, 0} // end
62 };
64 static const cfg_t cfg[] = {
65 // {"screenshot", &shot_vga, Y_SW_ON},
66 // {"music_volume", &mus_vol, Y_WORD},
67 // {"music_random", &music_random, Y_SW_ON},
68 // {"music_time", &music_time, Y_DWORD},
69 // {"music_fade", &music_fade, Y_DWORD},
70 {"pl1_left", &pl1.kl, Y_KEY},
71 {"pl1_right",&pl1.kr, Y_KEY},
72 {"pl1_up", &pl1.ku, Y_KEY},
73 {"pl1_down", &pl1.kd, Y_KEY},
74 {"pl1_jump", &pl1.kj, Y_KEY},
75 {"pl1_fire", &pl1.kf, Y_KEY},
76 {"pl1_next", &pl1.kwr, Y_KEY},
77 {"pl1_prev", &pl1.kwl, Y_KEY},
78 {"pl1_use", &pl1.kp, Y_KEY},
79 {"pl2_left", &pl2.kl, Y_KEY},
80 {"pl2_right", &pl2.kr, Y_KEY},
81 {"pl2_up", &pl2.ku, Y_KEY},
82 {"pl2_down", &pl2.kd, Y_KEY},
83 {"pl2_jump", &pl2.kj, Y_KEY},
84 {"pl2_fire", &pl2.kf, Y_KEY},
85 {"pl2_next", &pl2.kwr, Y_KEY},
86 {"pl2_prev", &pl2.kwl, Y_KEY},
87 {"pl2_use", &pl2.kp, Y_KEY},
88 {NULL, NULL, 0} // end
89 };
91 static void CFG_args (int argc, char **argv) {
92 const cfg_t *list[] = { arg, R_args(), S_args(), MUS_args() };
93 ARG_parse(argc, argv, 4, list);
94 }
96 static void CFG_load (void) {
97 const cfg_t *list[] = { cfg, R_conf(), S_conf(), MUS_conf() };
98 CFG_read_config("default.cfg", 4, list);
99 CFG_read_config("doom2d.cfg", 4, list);
102 static void CFG_save (void) {
103 const cfg_t *list[] = { cfg, R_conf(), S_conf(), MUS_conf() };
104 CFG_update_config("doom2d.cfg", "doom2d.cfg", 4, list, "generated by doom2d, do not modify");
107 /* --- error.h --- */
109 void logo (const char *s, ...) {
110 va_list ap;
111 va_start(ap, s);
112 vprintf(s, ap);
113 va_end(ap);
114 fflush(stdout);
117 void logo_gas (int cur, int all) {
118 // stub
121 void ERR_failinit (char *s, ...) {
122 va_list ap;
123 va_start(ap, s);
124 vprintf(s, ap);
125 va_end(ap);
126 puts("");
127 abort();
130 void ERR_fatal (char *s, ...) {
131 va_list ap;
132 R_done();
133 MUS_done();
134 S_done();
135 M_shutdown();
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 int Y_set_videomode_opengl (int w, int h, int fullscreen) {
152 assert(w > 0);
153 assert(h > 0);
154 Uint32 flags;
155 SDL_Surface *s;
156 if (mode == MODE_OPENGL && surf->w == w && surf->h == h && Y_get_fullscreen() == fullscreen) {
157 s = surf;
158 } else {
159 flags = SDL_DOUBLEBUF | SDL_OPENGL;
160 if (fullscreen) {
161 flags = flags | SDL_FULLSCREEN;
163 # ifdef WIN32
164 flags = flags | SDL_RESIZABLE;
165 # endif
166 s = SDL_SetVideoMode(w, h, 0, flags);
167 if (s != NULL) {
168 mode = MODE_OPENGL;
169 surf = s;
170 } else {
171 logo("Y_set_videomode_opengl: error: %s\n", SDL_GetError());
174 return s != NULL;
177 int Y_set_videomode_software (int w, int h, int fullscreen) {
178 assert(w > 0);
179 assert(h > 0);
180 Uint32 flags;
181 SDL_Surface *s;
182 if (mode == MODE_OPENGL && surf->w == w && surf->h == h && Y_get_fullscreen() == fullscreen) {
183 s = surf;
184 } else {
185 flags = SDL_DOUBLEBUF | SDL_SWSURFACE | SDL_HWPALETTE;
186 if (fullscreen) {
187 flags = flags | SDL_FULLSCREEN;
189 # ifdef WIN32
190 flags = flags | SDL_RESIZABLE;
191 # endif
192 s = SDL_SetVideoMode(w, h, 8, flags);
193 if (s != NULL) {
194 mode = MODE_SOFTWARE;
195 surf = s;
198 return s != NULL;
201 static void init_videomode_list (Uint32 flags) {
202 int i, n;
203 SDL_Rect **r;
204 if (vlist.modes != NULL) {
205 free(vlist.modes);
206 vlist.modes = NULL;
207 vlist.n = 0;
209 r = SDL_ListModes(NULL, flags);
210 if (r == (SDL_Rect **)-1) {
211 if ((flags & SDL_FULLSCREEN) == 0) {
212 init_videomode_list(flags | SDL_FULLSCREEN);
214 } else if (r != (SDL_Rect**)0) {
215 n = 0;
216 while (r[n] != NULL) {
217 n++;
219 vlist.modes = malloc(n * sizeof(videomode_size_t));
220 if (vlist.modes != NULL) {
221 vlist.n = n;
222 for (i = 0; i < n; i++) {
223 vlist.modes[i] = (videomode_size_t) {
224 .w = r[i]->w,
225 .h = r[i]->h
226 };
232 const videomode_t *Y_get_videomode_list_opengl (int fullscreen) {
233 init_videomode_list(SDL_OPENGL | (fullscreen ? SDL_FULLSCREEN : 0));
234 return &vlist;
237 const videomode_t *Y_get_videomode_list_software (int fullscreen) {
238 init_videomode_list(SDL_SWSURFACE | SDL_HWPALETTE | (fullscreen ? SDL_FULLSCREEN : 0));
239 return &vlist;
242 void Y_get_videomode (int *w, int *h) {
243 if (mode != MODE_NONE) {
244 *w = surf->w;
245 *h = surf->h;
246 } else {
247 *w = 0;
248 *h = 0;
252 int Y_videomode_setted (void) {
253 return mode != MODE_NONE;
256 void Y_unset_videomode (void) {
257 surf = NULL;
258 mode = MODE_NONE;
259 #ifndef __EMSCRIPTEN__
260 SDL_QuitSubSystem(SDL_INIT_VIDEO);
261 SDL_InitSubSystem(SDL_INIT_VIDEO);
262 #endif
265 void Y_set_fullscreen (int fullscreen) {
266 int fs = Y_get_fullscreen();
267 if (mode != MODE_NONE && fs != fullscreen) {
268 if (SDL_WM_ToggleFullScreen(surf) == 0) {
269 switch (mode) {
270 case MODE_OPENGL:
271 Y_set_videomode_opengl(surf->w, surf->h, fullscreen);
272 break;
273 case MODE_SOFTWARE:
274 Y_set_videomode_software(surf->w, surf->h, fullscreen);
275 break;
281 int Y_get_fullscreen (void) {
282 return (mode != MODE_NONE) && ((surf->flags & SDL_FULLSCREEN) != 0);
285 void Y_swap_buffers (void) {
286 assert(mode == MODE_OPENGL);
287 SDL_GL_SwapBuffers();
290 void Y_get_buffer (byte **buf, int *w, int *h, int *pitch) {
291 assert(mode == MODE_SOFTWARE);
292 *buf = surf->pixels;
293 *w = surf->w;
294 *h = surf->h;
295 *pitch = surf->pitch;
298 void Y_set_vga_palette (byte *vgapal) {
299 int i;
300 byte *p = vgapal;
301 assert(vgapal != NULL);
302 assert(mode == MODE_SOFTWARE);
303 SDL_Color colors[256];
304 for (i = 0; i < 256; i++) {
305 colors[i] = (SDL_Color) {
306 .r = p[0] * 255 / 63,
307 .g = p[1] * 255 / 63,
308 .b = p[2] * 255 / 63
309 };
310 p += 3;
312 SDL_SetColors(surf, colors, 0, 256);
315 void Y_repaint_rect (int x, int y, int w, int h) {
316 assert(mode == MODE_SOFTWARE);
317 SDL_UpdateRect(surf, x, y, w, h);
320 void Y_repaint (void) {
321 assert(mode == MODE_SOFTWARE);
322 SDL_Flip(surf);
325 void Y_enable_text_input (void) {
326 text_input = 1;
329 void Y_disable_text_input (void) {
330 text_input = 0;
333 /* --- main --- */
335 static int sdl_to_key (int code) {
336 switch (code) {
337 case SDLK_0: return KEY_0;
338 case SDLK_1: return KEY_1;
339 case SDLK_2: return KEY_2;
340 case SDLK_3: return KEY_3;
341 case SDLK_4: return KEY_4;
342 case SDLK_5: return KEY_5;
343 case SDLK_6: return KEY_6;
344 case SDLK_7: return KEY_7;
345 case SDLK_8: return KEY_8;
346 case SDLK_9: return KEY_9;
347 case SDLK_a: return KEY_A;
348 case SDLK_b: return KEY_B;
349 case SDLK_c: return KEY_C;
350 case SDLK_d: return KEY_D;
351 case SDLK_e: return KEY_E;
352 case SDLK_f: return KEY_F;
353 case SDLK_g: return KEY_G;
354 case SDLK_h: return KEY_H;
355 case SDLK_i: return KEY_I;
356 case SDLK_j: return KEY_J;
357 case SDLK_k: return KEY_K;
358 case SDLK_l: return KEY_L;
359 case SDLK_m: return KEY_M;
360 case SDLK_n: return KEY_N;
361 case SDLK_o: return KEY_O;
362 case SDLK_p: return KEY_P;
363 case SDLK_q: return KEY_Q;
364 case SDLK_r: return KEY_R;
365 case SDLK_s: return KEY_S;
366 case SDLK_t: return KEY_T;
367 case SDLK_u: return KEY_U;
368 case SDLK_v: return KEY_V;
369 case SDLK_w: return KEY_W;
370 case SDLK_x: return KEY_X;
371 case SDLK_y: return KEY_Y;
372 case SDLK_z: return KEY_Z;
373 case SDLK_RETURN: return KEY_RETURN;
374 case SDLK_ESCAPE: return KEY_ESCAPE;
375 case SDLK_BACKSPACE: return KEY_BACKSPACE;
376 case SDLK_TAB: return KEY_TAB;
377 case SDLK_SPACE: return KEY_SPACE;
378 case SDLK_MINUS: return KEY_MINUS;
379 case SDLK_EQUALS: return KEY_EQUALS;
380 case SDLK_LEFTBRACKET: return KEY_LEFTBRACKET;
381 case SDLK_RIGHTBRACKET: return KEY_RIGHTBRACKET;
382 case SDLK_BACKSLASH: return KEY_BACKSLASH;
383 case SDLK_SEMICOLON: return KEY_SEMICOLON;
384 case SDLK_QUOTE: return KEY_APOSTROPHE;
385 case SDLK_BACKQUOTE: return KEY_GRAVE;
386 case SDLK_COMMA: return KEY_COMMA;
387 case SDLK_PERIOD: return KEY_PERIOD;
388 case SDLK_SLASH: return KEY_SLASH;
389 case SDLK_CAPSLOCK: return KEY_CAPSLOCK;
390 case SDLK_F1: return KEY_F1;
391 case SDLK_F2: return KEY_F2;
392 case SDLK_F3: return KEY_F3;
393 case SDLK_F4: return KEY_F4;
394 case SDLK_F5: return KEY_F5;
395 case SDLK_F6: return KEY_F6;
396 case SDLK_F7: return KEY_F7;
397 case SDLK_F8: return KEY_F8;
398 case SDLK_F9: return KEY_F9;
399 case SDLK_F10: return KEY_F10;
400 case SDLK_F11: return KEY_F11;
401 case SDLK_F12: return KEY_F12;
402 case SDLK_PRINT: return KEY_PRINTSCREEN;
403 case SDLK_SCROLLOCK: return KEY_SCROLLLOCK;
404 case SDLK_PAUSE: return KEY_PAUSE;
405 case SDLK_INSERT: return KEY_INSERT;
406 case SDLK_HOME: return KEY_HOME;
407 case SDLK_PAGEUP: return KEY_PAGEUP;
408 case SDLK_DELETE: return KEY_DELETE;
409 case SDLK_END: return KEY_END;
410 case SDLK_PAGEDOWN: return KEY_PAGEDOWN;
411 case SDLK_RIGHT: return KEY_RIGHT;
412 case SDLK_LEFT: return KEY_LEFT;
413 case SDLK_DOWN: return KEY_DOWN;
414 case SDLK_UP: return KEY_UP;
415 case SDLK_NUMLOCK: return KEY_NUMLOCK;
416 case SDLK_KP_DIVIDE: return KEY_KP_DIVIDE;
417 case SDLK_KP_MULTIPLY: return KEY_KP_MULTIPLY;
418 case SDLK_KP_MINUS: return KEY_KP_MINUS;
419 case SDLK_KP_PLUS: return KEY_KP_PLUS;
420 case SDLK_KP_ENTER: return KEY_KP_ENTER;
421 case SDLK_KP0: return KEY_KP_0;
422 case SDLK_KP1: return KEY_KP_1;
423 case SDLK_KP2: return KEY_KP_2;
424 case SDLK_KP3: return KEY_KP_3;
425 case SDLK_KP4: return KEY_KP_4;
426 case SDLK_KP5: return KEY_KP_5;
427 case SDLK_KP6: return KEY_KP_6;
428 case SDLK_KP7: return KEY_KP_7;
429 case SDLK_KP8: return KEY_KP_8;
430 case SDLK_KP9: return KEY_KP_9;
431 case SDLK_KP_PERIOD: return KEY_KP_PERIOD;
432 case SDLK_SYSREQ: return KEY_SYSREQ;
433 case SDLK_LCTRL: return KEY_LCTRL;
434 case SDLK_LSHIFT: return KEY_LSHIFT;
435 case SDLK_LALT: return KEY_LALT;
436 case SDLK_LSUPER: return KEY_LSUPER;
437 case SDLK_RCTRL: return KEY_RCTRL;
438 case SDLK_RSHIFT: return KEY_RSHIFT;
439 case SDLK_RALT: return KEY_RALT;
440 case SDLK_RSUPER: return KEY_RSUPER;
441 default: return KEY_UNKNOWN;
445 static void poll_events (void) {
446 int key, sym, down;
447 SDL_Event ev;
448 while (SDL_PollEvent(&ev)) {
449 switch (ev.type) {
450 case SDL_QUIT:
451 ERR_quit();
452 break;
453 case SDL_VIDEORESIZE:
454 R_set_videomode(ev.resize.w, ev.resize.h, Y_get_fullscreen());
455 break;
456 case SDL_KEYDOWN:
457 case SDL_KEYUP:
458 sym = ev.key.keysym.sym;
459 down = ev.type == SDL_KEYDOWN;
460 key = sdl_to_key(sym);
461 I_press(key, down);
462 GM_key(key, down);
463 if (down && text_input && sym >= 0x20 && sym <= 0x7e) {
464 // TODO fix this
465 GM_input(sym);
467 break;
472 static void step (void) {
473 poll_events();
474 MUS_update();
475 Uint32 t = SDL_GetTicks();
476 if (t - ticks > DELAY) {
477 ticks = t;
478 G_act();
480 R_draw();
483 int main (int argc, char *argv[]) {
484 logo("main: initialize SDL\n");
485 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1) {
486 logo("main: failed to init SDL: %s\n", SDL_GetError());
487 return 1;
489 SDL_WM_SetCaption("Doom 2D (SDL)", "Doom 2D");
490 // Player 1 defaults
491 pl1.ku = KEY_KP_8;
492 pl1.kd = KEY_KP_5;
493 pl1.kl = KEY_KP_4;
494 pl1.kr = KEY_KP_6;
495 pl1.kf = KEY_PAGEDOWN;
496 pl1.kj = KEY_DELETE;
497 pl1.kwl = KEY_HOME;
498 pl1.kwr = KEY_END;
499 pl1.kp = KEY_KP_8;
500 // Player 2 defaults
501 pl2.ku = KEY_E;
502 pl2.kd = KEY_D;
503 pl2.kl = KEY_S;
504 pl2.kr = KEY_F;
505 pl2.kf = KEY_A;
506 pl2.kj = KEY_Q;
507 pl2.kwl = KEY_1;
508 pl2.kwr = KEY_2;
509 pl2.kp = KEY_E;
510 srand(SDL_GetTicks());
511 F_startup();
512 F_addwad("doom2d.wad");
513 CFG_args(argc, argv);
514 CFG_load();
515 F_initwads();
516 M_startup();
517 F_allocres();
518 S_init();
519 MUS_init();
520 R_init();
521 G_init();
522 ticks = SDL_GetTicks();
523 #ifdef __EMSCRIPTEN__
524 emscripten_set_main_loop(step, 0, 1);
525 #else
526 while (!quit) {
527 step();
529 #endif
530 CFG_save();
531 R_done();
532 MUS_done();
533 S_done();
534 M_shutdown();
535 SDL_Quit();
536 return 0;