DEADSOFTWARE

1c16dc33307100048c40af58c3a38ee2060b7319
[flatwaifu.git] / src / sdl / main.c
1 /*
2 Copyright (C) Prikol Software 1996-1997
3 Copyright (C) Aleksey Volynskov 1996-1997
4 Copyright (C) <ARembo@gmail.com> 2011
6 This file is part of the Doom2D:Rembo project.
8 Doom2D:Rembo is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License version 2 as
10 published by the Free Software Foundation.
12 Doom2D:Rembo is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, see <http://www.gnu.org/licenses/> or
19 write to the Free Software Foundation, Inc.,
20 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 */
23 #ifdef __EMSCRIPTEN__
24 # include <emscripten.h>
25 #endif
27 #include <SDL/SDL.h>
28 #include <stdio.h>
29 #include <stdarg.h>
30 #include <stdlib.h> // srand exit
31 #include <assert.h>
32 #include "system.h"
33 #include "input.h"
35 #include "my.h" // fexists
36 #include "player.h" // pl1 pl2
37 #include "menu.h" // G_keyf
38 #include "error.h" // logo
39 #include "monster.h" // nomon
41 #include "files.h" // F_startup F_addwad F_initwads F_allocres
42 #include "config.h" // CFG_args CFG_load CFG_save
43 #include "args.h" // ARG_parse
44 #include "memory.h" // M_startup
45 #include "game.h" // G_init G_act
46 #include "sound.h" // S_init S_done
47 #include "music.h" // S_initmusic S_updatemusic S_donemusic
48 #include "render.h" // R_init R_draw R_done
50 #define MODE_NONE 0
51 #define MODE_OPENGL 1
52 #define MODE_SOFTWARE 2
54 static Uint32 ticks;
55 static int quit = 0;
56 static SDL_Surface *surf = NULL;
57 static int mode = MODE_NONE;
58 static int text_input;
59 static videomode_t vlist;
61 static const cfg_t arg[] = {
62 {"file", NULL, Y_FILES},
63 {"cheat", &cheat, Y_SW_ON},
64 // {"vga", &shot_vga, Y_SW_ON},
65 {"sndvol", &snd_vol, Y_WORD},
66 {"musvol", &mus_vol, Y_WORD},
67 {"mon", &nomon, Y_SW_OFF},
68 {"warp", &_warp, Y_BYTE},
69 // {"config", NULL, cfg_file, Y_STRING},
70 {NULL, NULL, 0} // end
71 };
73 static const cfg_t cfg[] = {
74 // {"screenshot", &shot_vga, Y_SW_ON},
75 {"sound_volume", &snd_vol, Y_WORD},
76 {"music_volume", &mus_vol, Y_WORD},
77 {"music_random", &music_random, Y_SW_ON},
78 {"music_time", &music_time, Y_DWORD},
79 {"music_fade", &music_fade, Y_DWORD},
80 {"pl1_left", &pl1.kl, Y_KEY},
81 {"pl1_right",&pl1.kr, Y_KEY},
82 {"pl1_up", &pl1.ku, Y_KEY},
83 {"pl1_down", &pl1.kd, Y_KEY},
84 {"pl1_jump", &pl1.kj, Y_KEY},
85 {"pl1_fire", &pl1.kf, Y_KEY},
86 {"pl1_next", &pl1.kwr, Y_KEY},
87 {"pl1_prev", &pl1.kwl, Y_KEY},
88 {"pl1_use", &pl1.kp, Y_KEY},
89 {"pl2_left", &pl2.kl, Y_KEY},
90 {"pl2_right", &pl2.kr, Y_KEY},
91 {"pl2_up", &pl2.ku, Y_KEY},
92 {"pl2_down", &pl2.kd, Y_KEY},
93 {"pl2_jump", &pl2.kj, Y_KEY},
94 {"pl2_fire", &pl2.kf, Y_KEY},
95 {"pl2_next", &pl2.kwr, Y_KEY},
96 {"pl2_prev", &pl2.kwl, Y_KEY},
97 {"pl2_use", &pl2.kp, Y_KEY},
98 {NULL, NULL, 0} // end
99 };
101 static void CFG_args (int argc, char **argv) {
102 const cfg_t *list[] = { arg, R_args() };
103 ARG_parse(argc, argv, 2, list);
106 static void CFG_load (void) {
107 const cfg_t *list[] = { cfg, R_conf() };
108 CFG_read_config("default.cfg", 2, list);
109 CFG_read_config("doom2d.cfg", 2, list);
112 static void CFG_save (void) {
113 const cfg_t *list[] = { cfg, R_conf() };
114 CFG_update_config("doom2d.cfg", "doom2d.cfg", 2, list, "generated by doom2d, do not modify");
117 /* --- error.h --- */
119 void logo (const char *s, ...) {
120 va_list ap;
121 va_start(ap, s);
122 vprintf(s, ap);
123 va_end(ap);
124 fflush(stdout);
127 void logo_gas (int cur, int all) {
128 // stub
131 void ERR_failinit (char *s, ...) {
132 va_list ap;
133 va_start(ap, s);
134 vprintf(s, ap);
135 va_end(ap);
136 puts("");
137 exit(1);
140 void ERR_fatal (char *s, ...) {
141 va_list ap;
142 R_done();
143 S_done();
144 S_donemusic();
145 M_shutdown();
146 SDL_Quit();
147 puts("\nКРИТИЧЕСКАЯ ОШИБКА:");
148 va_start(ap, s);
149 vprintf(s, ap);
150 va_end(ap);
151 puts("");
152 exit(1);
155 void ERR_quit (void) {
156 quit = 1;
159 /* --- system.h --- */
161 int Y_set_videomode_opengl (int w, int h, int fullscreen) {
162 assert(w > 0);
163 assert(h > 0);
164 Uint32 flags;
165 SDL_Surface *s;
166 if (mode == MODE_OPENGL && surf->w == w && surf->h == h && Y_get_fullscreen() == fullscreen) {
167 s = surf;
168 } else {
169 flags = SDL_DOUBLEBUF | SDL_OPENGL;
170 if (fullscreen) {
171 flags = flags | SDL_FULLSCREEN;
173 # ifdef WIN32
174 flags = flags | SDL_RESIZABLE;
175 # endif
176 s = SDL_SetVideoMode(w, h, 0, flags);
177 if (s != NULL) {
178 mode = MODE_OPENGL;
179 surf = s;
180 } else {
181 logo("Y_set_videomode_opengl: error: %s\n", SDL_GetError());
184 return s != NULL;
187 int Y_set_videomode_software (int w, int h, int fullscreen) {
188 assert(w > 0);
189 assert(h > 0);
190 Uint32 flags;
191 SDL_Surface *s;
192 if (mode == MODE_OPENGL && surf->w == w && surf->h == h && Y_get_fullscreen() == fullscreen) {
193 s = surf;
194 } else {
195 flags = SDL_DOUBLEBUF | SDL_SWSURFACE | SDL_HWPALETTE;
196 if (fullscreen) {
197 flags = flags | SDL_FULLSCREEN;
199 # ifdef WIN32
200 flags = flags | SDL_RESIZABLE;
201 # endif
202 s = SDL_SetVideoMode(w, h, 8, flags);
203 if (s != NULL) {
204 mode = MODE_SOFTWARE;
205 surf = s;
208 return s != NULL;
211 static void init_videomode_list (Uint32 flags) {
212 int i, n;
213 SDL_Rect **r;
214 if (vlist.modes != NULL) {
215 free(vlist.modes);
216 vlist.modes = NULL;
217 vlist.n = 0;
219 r = SDL_ListModes(NULL, flags);
220 if (r == (SDL_Rect **)-1) {
221 if ((flags & SDL_FULLSCREEN) == 0) {
222 init_videomode_list(flags | SDL_FULLSCREEN);
224 } else if (r != (SDL_Rect**)0) {
225 n = 0;
226 while (r[n] != NULL) {
227 n++;
229 vlist.modes = malloc(n * sizeof(videomode_size_t));
230 if (vlist.modes != NULL) {
231 vlist.n = n;
232 for (i = 0; i < n; i++) {
233 vlist.modes[i] = (videomode_size_t) {
234 .w = r[i]->w,
235 .h = r[i]->h
236 };
242 const videomode_t *Y_get_videomode_list_opengl (int fullscreen) {
243 init_videomode_list(SDL_OPENGL | (fullscreen ? SDL_FULLSCREEN : 0));
244 return &vlist;
247 const videomode_t *Y_get_videomode_list_software (int fullscreen) {
248 init_videomode_list(SDL_SWSURFACE | SDL_HWPALETTE | (fullscreen ? SDL_FULLSCREEN : 0));
249 return &vlist;
252 void Y_get_videomode (int *w, int *h) {
253 if (mode != MODE_NONE) {
254 *w = surf->w;
255 *h = surf->h;
256 } else {
257 *w = 0;
258 *h = 0;
262 int Y_videomode_setted (void) {
263 return mode != MODE_NONE;
266 void Y_unset_videomode (void) {
267 surf = NULL;
268 mode = MODE_NONE;
269 #ifndef __EMSCRIPTEN__
270 SDL_QuitSubSystem(SDL_INIT_VIDEO);
271 SDL_InitSubSystem(SDL_INIT_VIDEO);
272 #endif
275 void Y_set_fullscreen (int fullscreen) {
276 int fs = Y_get_fullscreen();
277 if (mode != MODE_NONE && fs != fullscreen) {
278 if (SDL_WM_ToggleFullScreen(surf) == 0) {
279 switch (mode) {
280 case MODE_OPENGL:
281 Y_set_videomode_opengl(surf->w, surf->h, fullscreen);
282 break;
283 case MODE_SOFTWARE:
284 Y_set_videomode_software(surf->w, surf->h, fullscreen);
285 break;
291 int Y_get_fullscreen (void) {
292 return (mode != MODE_NONE) && ((surf->flags & SDL_FULLSCREEN) != 0);
295 void Y_swap_buffers (void) {
296 assert(mode == MODE_OPENGL);
297 SDL_GL_SwapBuffers();
300 void Y_get_buffer (byte **buf, int *w, int *h, int *pitch) {
301 assert(mode == MODE_SOFTWARE);
302 *buf = surf->pixels;
303 *w = surf->w;
304 *h = surf->h;
305 *pitch = surf->pitch;
308 void Y_set_vga_palette (byte *vgapal) {
309 int i;
310 byte *p = vgapal;
311 assert(vgapal != NULL);
312 assert(mode == MODE_SOFTWARE);
313 SDL_Color colors[256];
314 for (i = 0; i < 256; i++) {
315 colors[i] = (SDL_Color) {
316 .r = p[0] * 255 / 63,
317 .g = p[1] * 255 / 63,
318 .b = p[2] * 255 / 63
319 };
320 p += 3;
322 SDL_SetColors(surf, colors, 0, 256);
325 void Y_repaint_rect (int x, int y, int w, int h) {
326 assert(mode == MODE_SOFTWARE);
327 SDL_UpdateRect(surf, x, y, w, h);
330 void Y_repaint (void) {
331 assert(mode == MODE_SOFTWARE);
332 SDL_Flip(surf);
335 void Y_enable_text_input (void) {
336 text_input = 1;
339 void Y_disable_text_input (void) {
340 text_input = 0;
343 /* --- main --- */
345 static int sdl_to_key (int code) {
346 switch (code) {
347 case SDLK_0: return KEY_0;
348 case SDLK_1: return KEY_1;
349 case SDLK_2: return KEY_2;
350 case SDLK_3: return KEY_3;
351 case SDLK_4: return KEY_4;
352 case SDLK_5: return KEY_5;
353 case SDLK_6: return KEY_6;
354 case SDLK_7: return KEY_7;
355 case SDLK_8: return KEY_8;
356 case SDLK_9: return KEY_9;
357 case SDLK_a: return KEY_A;
358 case SDLK_b: return KEY_B;
359 case SDLK_c: return KEY_C;
360 case SDLK_d: return KEY_D;
361 case SDLK_e: return KEY_E;
362 case SDLK_f: return KEY_F;
363 case SDLK_g: return KEY_G;
364 case SDLK_h: return KEY_H;
365 case SDLK_i: return KEY_I;
366 case SDLK_j: return KEY_J;
367 case SDLK_k: return KEY_K;
368 case SDLK_l: return KEY_L;
369 case SDLK_m: return KEY_M;
370 case SDLK_n: return KEY_N;
371 case SDLK_o: return KEY_O;
372 case SDLK_p: return KEY_P;
373 case SDLK_q: return KEY_Q;
374 case SDLK_r: return KEY_R;
375 case SDLK_s: return KEY_S;
376 case SDLK_t: return KEY_T;
377 case SDLK_u: return KEY_U;
378 case SDLK_v: return KEY_V;
379 case SDLK_w: return KEY_W;
380 case SDLK_x: return KEY_X;
381 case SDLK_y: return KEY_Y;
382 case SDLK_z: return KEY_Z;
383 case SDLK_RETURN: return KEY_RETURN;
384 case SDLK_ESCAPE: return KEY_ESCAPE;
385 case SDLK_BACKSPACE: return KEY_BACKSPACE;
386 case SDLK_TAB: return KEY_TAB;
387 case SDLK_SPACE: return KEY_SPACE;
388 case SDLK_MINUS: return KEY_MINUS;
389 case SDLK_EQUALS: return KEY_EQUALS;
390 case SDLK_LEFTBRACKET: return KEY_LEFTBRACKET;
391 case SDLK_RIGHTBRACKET: return KEY_RIGHTBRACKET;
392 case SDLK_BACKSLASH: return KEY_BACKSLASH;
393 case SDLK_SEMICOLON: return KEY_SEMICOLON;
394 case SDLK_QUOTE: return KEY_APOSTROPHE;
395 case SDLK_BACKQUOTE: return KEY_GRAVE;
396 case SDLK_COMMA: return KEY_COMMA;
397 case SDLK_PERIOD: return KEY_PERIOD;
398 case SDLK_SLASH: return KEY_SLASH;
399 case SDLK_CAPSLOCK: return KEY_CAPSLOCK;
400 case SDLK_F1: return KEY_F1;
401 case SDLK_F2: return KEY_F2;
402 case SDLK_F3: return KEY_F3;
403 case SDLK_F4: return KEY_F4;
404 case SDLK_F5: return KEY_F5;
405 case SDLK_F6: return KEY_F6;
406 case SDLK_F7: return KEY_F7;
407 case SDLK_F8: return KEY_F8;
408 case SDLK_F9: return KEY_F9;
409 case SDLK_F10: return KEY_F10;
410 case SDLK_F11: return KEY_F11;
411 case SDLK_F12: return KEY_F12;
412 case SDLK_PRINT: return KEY_PRINTSCREEN;
413 case SDLK_SCROLLOCK: return KEY_SCROLLLOCK;
414 case SDLK_PAUSE: return KEY_PAUSE;
415 case SDLK_INSERT: return KEY_INSERT;
416 case SDLK_HOME: return KEY_HOME;
417 case SDLK_PAGEUP: return KEY_PAGEUP;
418 case SDLK_DELETE: return KEY_DELETE;
419 case SDLK_END: return KEY_END;
420 case SDLK_PAGEDOWN: return KEY_PAGEDOWN;
421 case SDLK_RIGHT: return KEY_RIGHT;
422 case SDLK_LEFT: return KEY_LEFT;
423 case SDLK_DOWN: return KEY_DOWN;
424 case SDLK_UP: return KEY_UP;
425 case SDLK_NUMLOCK: return KEY_NUMLOCK;
426 case SDLK_KP_DIVIDE: return KEY_KP_DIVIDE;
427 case SDLK_KP_MULTIPLY: return KEY_KP_MULTIPLY;
428 case SDLK_KP_MINUS: return KEY_KP_MINUS;
429 case SDLK_KP_PLUS: return KEY_KP_PLUS;
430 case SDLK_KP_ENTER: return KEY_KP_ENTER;
431 case SDLK_KP0: return KEY_KP_0;
432 case SDLK_KP1: return KEY_KP_1;
433 case SDLK_KP2: return KEY_KP_2;
434 case SDLK_KP3: return KEY_KP_3;
435 case SDLK_KP4: return KEY_KP_4;
436 case SDLK_KP5: return KEY_KP_5;
437 case SDLK_KP6: return KEY_KP_6;
438 case SDLK_KP7: return KEY_KP_7;
439 case SDLK_KP8: return KEY_KP_8;
440 case SDLK_KP9: return KEY_KP_9;
441 case SDLK_KP_PERIOD: return KEY_KP_PERIOD;
442 case SDLK_SYSREQ: return KEY_SYSREQ;
443 case SDLK_LCTRL: return KEY_LCTRL;
444 case SDLK_LSHIFT: return KEY_LSHIFT;
445 case SDLK_LALT: return KEY_LALT;
446 case SDLK_LSUPER: return KEY_LSUPER;
447 case SDLK_RCTRL: return KEY_RCTRL;
448 case SDLK_RSHIFT: return KEY_RSHIFT;
449 case SDLK_RALT: return KEY_RALT;
450 case SDLK_RSUPER: return KEY_RSUPER;
451 default: return KEY_UNKNOWN;
455 static void poll_events (void) {
456 int key, sym, down;
457 SDL_Event ev;
458 while (SDL_PollEvent(&ev)) {
459 switch (ev.type) {
460 case SDL_QUIT:
461 ERR_quit();
462 break;
463 case SDL_VIDEORESIZE:
464 R_set_videomode(ev.resize.w, ev.resize.h, Y_get_fullscreen());
465 break;
466 case SDL_KEYDOWN:
467 case SDL_KEYUP:
468 sym = ev.key.keysym.sym;
469 down = ev.type == SDL_KEYDOWN;
470 key = sdl_to_key(sym);
471 I_press(key, down);
472 GM_key(key, down);
473 if (down && text_input && sym >= 0x20 && sym <= 0x7e) {
474 // TODO fix this
475 GM_input(sym);
477 break;
482 static void step (void) {
483 poll_events();
484 S_updatemusic();
485 Uint32 t = SDL_GetTicks();
486 if (t - ticks > DELAY) {
487 ticks = t;
488 G_act();
490 R_draw();
493 int main (int argc, char *argv[]) {
494 char *pw;
495 logo("main: initialize SDL\n");
496 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1) {
497 logo("main: failed to init SDL: %s\n", SDL_GetError());
498 return 1;
500 SDL_WM_SetCaption("Doom 2D (SDL)", "Doom 2D");
501 // Player 1 defaults
502 pl1.ku = KEY_KP_8;
503 pl1.kd = KEY_KP_5;
504 pl1.kl = KEY_KP_4;
505 pl1.kr = KEY_KP_6;
506 pl1.kf = KEY_PAGEDOWN;
507 pl1.kj = KEY_DELETE;
508 pl1.kwl = KEY_HOME;
509 pl1.kwr = KEY_END;
510 pl1.kp = KEY_KP_8;
511 // Player 2 defaults
512 pl2.ku = KEY_E;
513 pl2.kd = KEY_D;
514 pl2.kl = KEY_S;
515 pl2.kr = KEY_F;
516 pl2.kf = KEY_A;
517 pl2.kj = KEY_Q;
518 pl2.kwl = KEY_1;
519 pl2.kwr = KEY_2;
520 pl2.kp = KEY_E;
521 srand(SDL_GetTicks());
522 F_startup();
523 #ifndef WIN32
524 pw = "/usr/share/doom2d-rembo/doom2d.wad";
525 #else
526 pw = "doom2d.wad";
527 #endif
528 if (fexists(pw)) {
529 F_addwad(pw);
530 } else {
531 F_addwad("doom2d.wad");
533 CFG_args(argc, argv);
534 CFG_load();
535 F_initwads();
536 M_startup();
537 F_allocres();
538 S_init();
539 S_initmusic();
540 R_init();
541 G_init();
542 ticks = SDL_GetTicks();
543 #ifdef __EMSCRIPTEN__
544 emscripten_set_main_loop(step, 0, 1);
545 #else
546 while (!quit) {
547 step();
549 #endif
550 CFG_save();
551 R_done();
552 S_donemusic();
553 S_done();
554 M_shutdown();
555 SDL_Quit();
556 return 0;