DEADSOFTWARE

menu: change videomode at runtime
[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 <string.h> // strcasecmp
32 #include <ctype.h>
33 #include <assert.h>
34 #include "system.h"
35 #include "input.h"
37 #include "my.h" // fexists
38 #include "player.h" // pl1 pl2
39 #include "menu.h" // G_keyf
40 #include "error.h" // logo
42 #include "files.h" // F_startup F_addwad F_initwads F_allocres
43 #include "config.h" // CFG_args CFG_load CFG_save
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 /* --- error.h --- */
63 void logo (const char *s, ...) {
64 va_list ap;
65 va_start(ap, s);
66 vprintf(s, ap);
67 va_end(ap);
68 fflush(stdout);
69 }
71 void logo_gas (int cur, int all) {
72 // stub
73 }
75 void ERR_failinit (char *s, ...) {
76 va_list ap;
77 va_start(ap, s);
78 vprintf(s, ap);
79 va_end(ap);
80 puts("");
81 exit(1);
82 }
84 void ERR_fatal (char *s, ...) {
85 va_list ap;
86 R_done();
87 S_done();
88 S_donemusic();
89 M_shutdown();
90 SDL_Quit();
91 puts("\nКРИТИЧЕСКАЯ ОШИБКА:");
92 va_start(ap, s);
93 vprintf(s, ap);
94 va_end(ap);
95 puts("");
96 exit(1);
97 }
99 void ERR_quit (void) {
100 quit = 1;
103 /* --- system.h --- */
105 int Y_set_videomode_opengl (int w, int h, int fullscreen) {
106 assert(w > 0);
107 assert(h > 0);
108 Uint32 flags;
109 SDL_Surface *s;
110 if (mode == MODE_OPENGL && surf->w == w && surf->h == h && Y_get_fullscreen() == fullscreen) {
111 s = surf;
112 } else {
113 flags = SDL_DOUBLEBUF | SDL_OPENGL;
114 if (fullscreen) {
115 flags = flags | SDL_FULLSCREEN;
117 # ifdef WIN32
118 flags = flags | SDL_RESIZABLE;
119 # endif
120 s = SDL_SetVideoMode(w, h, 0, flags);
121 if (s != NULL) {
122 mode = MODE_OPENGL;
123 surf = s;
124 } else {
125 logo("Y_set_videomode_opengl: error: %s\n", SDL_GetError());
128 return s != NULL;
131 int Y_set_videomode_software (int w, int h, int fullscreen) {
132 assert(w > 0);
133 assert(h > 0);
134 Uint32 flags;
135 SDL_Surface *s;
136 if (mode == MODE_OPENGL && surf->w == w && surf->h == h && Y_get_fullscreen() == fullscreen) {
137 s = surf;
138 } else {
139 flags = SDL_DOUBLEBUF | SDL_SWSURFACE | SDL_HWPALETTE;
140 if (fullscreen) {
141 flags = flags | SDL_FULLSCREEN;
143 # ifdef WIN32
144 flags = flags | SDL_RESIZABLE;
145 # endif
146 s = SDL_SetVideoMode(w, h, 8, flags);
147 if (s != NULL) {
148 mode = MODE_SOFTWARE;
149 surf = s;
152 return s != NULL;
155 static void init_videomode_list (Uint32 flags) {
156 int i, n;
157 SDL_Rect **r;
158 if (vlist.modes != NULL) {
159 free(vlist.modes);
160 vlist.modes = NULL;
161 vlist.n = 0;
163 r = SDL_ListModes(NULL, flags);
164 if (r == (SDL_Rect **)-1) {
165 if ((flags & SDL_FULLSCREEN) == 0) {
166 init_videomode_list(flags | SDL_FULLSCREEN);
168 } else if (r != (SDL_Rect**)0) {
169 n = 0;
170 while (r[n] != NULL) {
171 n++;
173 vlist.modes = malloc(n * sizeof(videomode_size_t));
174 if (vlist.modes != NULL) {
175 vlist.n = n;
176 for (i = 0; i < n; i++) {
177 vlist.modes[i] = (videomode_size_t) {
178 .w = r[i]->w,
179 .h = r[i]->h
180 };
186 const videomode_t *Y_get_videomode_list_opengl (int fullscreen) {
187 init_videomode_list(SDL_OPENGL | (fullscreen ? SDL_FULLSCREEN : 0));
188 return &vlist;
191 const videomode_t *Y_get_videomode_list_software (int fullscreen) {
192 init_videomode_list(SDL_SWSURFACE | SDL_HWPALETTE | (fullscreen ? SDL_FULLSCREEN : 0));
193 return &vlist;
196 void Y_get_videomode (int *w, int *h) {
197 if (mode != MODE_NONE) {
198 *w = surf->w;
199 *h = surf->h;
200 } else {
201 *w = 0;
202 *h = 0;
206 int Y_videomode_setted (void) {
207 return mode != MODE_NONE;
210 void Y_unset_videomode (void) {
211 surf = NULL;
212 mode = MODE_NONE;
213 #ifndef __EMSCRIPTEN__
214 SDL_QuitSubSystem(SDL_INIT_VIDEO);
215 SDL_InitSubSystem(SDL_INIT_VIDEO);
216 #endif
219 void Y_set_fullscreen (int fullscreen) {
220 int fs = Y_get_fullscreen();
221 if (mode != MODE_NONE && fs != fullscreen) {
222 if (SDL_WM_ToggleFullScreen(surf) == 0) {
223 switch (mode) {
224 case MODE_OPENGL:
225 Y_set_videomode_opengl(surf->w, surf->h, fullscreen);
226 break;
227 case MODE_SOFTWARE:
228 Y_set_videomode_software(surf->w, surf->h, fullscreen);
229 break;
235 int Y_get_fullscreen (void) {
236 return (mode != MODE_NONE) && ((surf->flags & SDL_FULLSCREEN) != 0);
239 void Y_swap_buffers (void) {
240 assert(mode == MODE_OPENGL);
241 SDL_GL_SwapBuffers();
244 void Y_get_buffer (byte **buf, int *w, int *h, int *pitch) {
245 assert(mode == MODE_SOFTWARE);
246 *buf = surf->pixels;
247 *w = surf->w;
248 *h = surf->h;
249 *pitch = surf->pitch;
252 void Y_set_vga_palette (byte *vgapal) {
253 int i;
254 byte *p = vgapal;
255 assert(vgapal != NULL);
256 assert(mode == MODE_SOFTWARE);
257 SDL_Color colors[256];
258 for (i = 0; i < 256; i++) {
259 colors[i] = (SDL_Color) {
260 .r = p[0] * 255 / 63,
261 .g = p[1] * 255 / 63,
262 .b = p[2] * 255 / 63
263 };
264 p += 3;
266 SDL_SetColors(surf, colors, 0, 256);
269 void Y_repaint_rect (int x, int y, int w, int h) {
270 assert(mode == MODE_SOFTWARE);
271 SDL_UpdateRect(surf, x, y, w, h);
274 void Y_repaint (void) {
275 assert(mode == MODE_SOFTWARE);
276 SDL_Flip(surf);
279 void Y_enable_text_input (void) {
280 text_input = 1;
283 void Y_disable_text_input (void) {
284 text_input = 0;
287 /* --- main --- */
289 static int sdl_to_key (int code) {
290 switch (code) {
291 case SDLK_0: return KEY_0;
292 case SDLK_1: return KEY_1;
293 case SDLK_2: return KEY_2;
294 case SDLK_3: return KEY_3;
295 case SDLK_4: return KEY_4;
296 case SDLK_5: return KEY_5;
297 case SDLK_6: return KEY_6;
298 case SDLK_7: return KEY_7;
299 case SDLK_8: return KEY_8;
300 case SDLK_9: return KEY_9;
301 case SDLK_a: return KEY_A;
302 case SDLK_b: return KEY_B;
303 case SDLK_c: return KEY_C;
304 case SDLK_d: return KEY_D;
305 case SDLK_e: return KEY_E;
306 case SDLK_f: return KEY_F;
307 case SDLK_g: return KEY_G;
308 case SDLK_h: return KEY_H;
309 case SDLK_i: return KEY_I;
310 case SDLK_j: return KEY_J;
311 case SDLK_k: return KEY_K;
312 case SDLK_l: return KEY_L;
313 case SDLK_m: return KEY_M;
314 case SDLK_n: return KEY_N;
315 case SDLK_o: return KEY_O;
316 case SDLK_p: return KEY_P;
317 case SDLK_q: return KEY_Q;
318 case SDLK_r: return KEY_R;
319 case SDLK_s: return KEY_S;
320 case SDLK_t: return KEY_T;
321 case SDLK_u: return KEY_U;
322 case SDLK_v: return KEY_V;
323 case SDLK_w: return KEY_W;
324 case SDLK_x: return KEY_X;
325 case SDLK_y: return KEY_Y;
326 case SDLK_z: return KEY_Z;
327 case SDLK_RETURN: return KEY_RETURN;
328 case SDLK_ESCAPE: return KEY_ESCAPE;
329 case SDLK_BACKSPACE: return KEY_BACKSPACE;
330 case SDLK_TAB: return KEY_TAB;
331 case SDLK_SPACE: return KEY_SPACE;
332 case SDLK_MINUS: return KEY_MINUS;
333 case SDLK_EQUALS: return KEY_EQUALS;
334 case SDLK_LEFTBRACKET: return KEY_LEFTBRACKET;
335 case SDLK_RIGHTBRACKET: return KEY_RIGHTBRACKET;
336 case SDLK_BACKSLASH: return KEY_BACKSLASH;
337 case SDLK_SEMICOLON: return KEY_SEMICOLON;
338 case SDLK_QUOTE: return KEY_APOSTROPHE;
339 case SDLK_BACKQUOTE: return KEY_GRAVE;
340 case SDLK_COMMA: return KEY_COMMA;
341 case SDLK_PERIOD: return KEY_PERIOD;
342 case SDLK_SLASH: return KEY_SLASH;
343 case SDLK_CAPSLOCK: return KEY_CAPSLOCK;
344 case SDLK_F1: return KEY_F1;
345 case SDLK_F2: return KEY_F2;
346 case SDLK_F3: return KEY_F3;
347 case SDLK_F4: return KEY_F4;
348 case SDLK_F5: return KEY_F5;
349 case SDLK_F6: return KEY_F6;
350 case SDLK_F7: return KEY_F7;
351 case SDLK_F8: return KEY_F8;
352 case SDLK_F9: return KEY_F9;
353 case SDLK_F10: return KEY_F10;
354 case SDLK_F11: return KEY_F11;
355 case SDLK_F12: return KEY_F12;
356 case SDLK_PRINT: return KEY_PRINTSCREEN;
357 case SDLK_SCROLLOCK: return KEY_SCROLLLOCK;
358 case SDLK_PAUSE: return KEY_PAUSE;
359 case SDLK_INSERT: return KEY_INSERT;
360 case SDLK_HOME: return KEY_HOME;
361 case SDLK_PAGEUP: return KEY_PAGEUP;
362 case SDLK_DELETE: return KEY_DELETE;
363 case SDLK_END: return KEY_END;
364 case SDLK_PAGEDOWN: return KEY_PAGEDOWN;
365 case SDLK_RIGHT: return KEY_RIGHT;
366 case SDLK_LEFT: return KEY_LEFT;
367 case SDLK_DOWN: return KEY_DOWN;
368 case SDLK_UP: return KEY_UP;
369 case SDLK_NUMLOCK: return KEY_NUMLOCK;
370 case SDLK_KP_DIVIDE: return KEY_KP_DIVIDE;
371 case SDLK_KP_MULTIPLY: return KEY_KP_MULTIPLY;
372 case SDLK_KP_MINUS: return KEY_KP_MINUS;
373 case SDLK_KP_PLUS: return KEY_KP_PLUS;
374 case SDLK_KP_ENTER: return KEY_KP_ENTER;
375 case SDLK_KP0: return KEY_KP_0;
376 case SDLK_KP1: return KEY_KP_1;
377 case SDLK_KP2: return KEY_KP_2;
378 case SDLK_KP3: return KEY_KP_3;
379 case SDLK_KP4: return KEY_KP_4;
380 case SDLK_KP5: return KEY_KP_5;
381 case SDLK_KP6: return KEY_KP_6;
382 case SDLK_KP7: return KEY_KP_7;
383 case SDLK_KP8: return KEY_KP_8;
384 case SDLK_KP9: return KEY_KP_9;
385 case SDLK_KP_PERIOD: return KEY_KP_PERIOD;
386 case SDLK_SYSREQ: return KEY_SYSREQ;
387 case SDLK_LCTRL: return KEY_LCTRL;
388 case SDLK_LSHIFT: return KEY_LSHIFT;
389 case SDLK_LALT: return KEY_LALT;
390 case SDLK_LSUPER: return KEY_LSUPER;
391 case SDLK_RCTRL: return KEY_RCTRL;
392 case SDLK_RSHIFT: return KEY_RSHIFT;
393 case SDLK_RALT: return KEY_RALT;
394 case SDLK_RSUPER: return KEY_RSUPER;
395 default: return KEY_UNKNOWN;
399 static void poll_events (void) {
400 int key, sym, down;
401 SDL_Event ev;
402 while (SDL_PollEvent(&ev)) {
403 switch (ev.type) {
404 case SDL_QUIT:
405 ERR_quit();
406 break;
407 case SDL_VIDEORESIZE:
408 R_set_videomode(ev.resize.w, ev.resize.h, Y_get_fullscreen());
409 break;
410 case SDL_KEYDOWN:
411 case SDL_KEYUP:
412 sym = ev.key.keysym.sym;
413 down = ev.type == SDL_KEYDOWN;
414 key = sdl_to_key(sym);
415 I_press(key, down);
416 GM_key(key, down);
417 if (down && text_input && sym >= 0x20 && sym <= 0x7e) {
418 // TODO fix this
419 GM_input(sym);
421 break;
426 static void step (void) {
427 poll_events();
428 S_updatemusic();
429 Uint32 t = SDL_GetTicks();
430 if (t - ticks > DELAY) {
431 ticks = t;
432 G_act();
434 R_draw();
437 int main (int argc, char *argv[]) {
438 char *pw;
439 logo("main: initialize SDL\n");
440 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1) {
441 logo("main: failed to init SDL: %s\n", SDL_GetError());
442 return 1;
444 SDL_WM_SetCaption("Doom 2D (SDL)", "Doom 2D");
445 // Player 1 defaults
446 pl1.ku = KEY_KP_8;
447 pl1.kd = KEY_KP_5;
448 pl1.kl = KEY_KP_4;
449 pl1.kr = KEY_KP_6;
450 pl1.kf = KEY_PAGEDOWN;
451 pl1.kj = KEY_DELETE;
452 pl1.kwl = KEY_HOME;
453 pl1.kwr = KEY_END;
454 pl1.kp = KEY_KP_8;
455 // Player 2 defaults
456 pl2.ku = KEY_E;
457 pl2.kd = KEY_D;
458 pl2.kl = KEY_S;
459 pl2.kr = KEY_F;
460 pl2.kf = KEY_A;
461 pl2.kj = KEY_Q;
462 pl2.kwl = KEY_1;
463 pl2.kwr = KEY_2;
464 pl2.kp = KEY_E;
465 srand(SDL_GetTicks());
466 F_startup();
467 #ifndef WIN32
468 pw = "/usr/share/doom2d-rembo/doom2d.wad";
469 #else
470 pw = "doom2d.wad";
471 #endif
472 if (fexists(pw)) {
473 F_addwad(pw);
474 } else {
475 F_addwad("doom2d.wad");
477 CFG_args(argc, argv);
478 CFG_load();
479 F_initwads();
480 M_startup();
481 F_allocres();
482 S_init();
483 S_initmusic();
484 R_init();
485 G_init();
486 ticks = SDL_GetTicks();
487 #ifdef __EMSCRIPTEN__
488 emscripten_set_main_loop(step, 0, 1);
489 #else
490 while (!quit) {
491 step();
493 #endif
494 CFG_save();
495 R_done();
496 S_donemusic();
497 S_done();
498 M_shutdown();
499 SDL_Quit();
500 return 0;