DEADSOFTWARE

ports: ported to emscripten
[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 <assert.h>
33 #include "system.h"
34 #include "input.h"
36 #include "my.h" // fexists
37 #include "player.h" // pl1 pl2
38 #include "menu.h" // G_keyf
39 #include "error.h" // logo
41 #include "files.h" // F_startup F_addwad F_initwads F_allocres
42 #include "config.h" // CFG_args CFG_load CFG_save
43 #include "memory.h" // M_startup
44 #include "game.h" // G_init G_act
45 #include "sound.h" // S_init S_done
46 #include "music.h" // S_initmusic S_updatemusic S_donemusic
47 #include "render.h" // R_init R_draw R_done
49 #define MODE_NONE 0
50 #define MODE_OPENGL 1
51 #define MODE_SOFTWARE 2
53 static Uint32 ticks;
54 static int quit = 0;
55 static SDL_Surface *surf = NULL;
56 static int mode = MODE_NONE;
58 /* --- error.h --- */
60 void logo (const char *s, ...) {
61 va_list ap;
62 va_start(ap, s);
63 vprintf(s, ap);
64 va_end(ap);
65 fflush(stdout);
66 }
68 void logo_gas (int cur, int all) {
69 // stub
70 }
72 void ERR_failinit (char *s, ...) {
73 va_list ap;
74 va_start(ap, s);
75 vprintf(s, ap);
76 va_end(ap);
77 puts("");
78 exit(1);
79 }
81 void ERR_fatal (char *s, ...) {
82 va_list ap;
83 R_done();
84 S_done();
85 S_donemusic();
86 M_shutdown();
87 SDL_Quit();
88 puts("\nКРИТИЧЕСКАЯ ОШИБКА:");
89 va_start(ap, s);
90 vprintf(s, ap);
91 va_end(ap);
92 puts("");
93 exit(1);
94 }
96 void ERR_quit (void) {
97 quit = 1;
98 }
100 /* --- system.h --- */
102 int Y_set_videomode_opengl (int w, int h, int fullscreen) {
103 assert(w > 0);
104 assert(h > 0);
105 Uint32 flags;
106 SDL_Surface *s;
107 if (mode == MODE_OPENGL && surf->w == w && surf->h == h && Y_get_fullscreen() == fullscreen) {
108 s = surf;
109 } else {
110 flags = SDL_DOUBLEBUF | SDL_OPENGL;
111 if (fullscreen) {
112 flags = flags | SDL_FULLSCREEN;
114 # ifdef WIN32
115 flags = flags | SDL_RESIZABLE;
116 # endif
117 s = SDL_SetVideoMode(w, h, 0, flags);
118 if (s != NULL) {
119 mode = MODE_OPENGL;
120 surf = s;
121 } else {
122 logo("Y_set_videomode_opengl: error: %s\n", SDL_GetError());
125 return s != NULL;
128 int Y_set_videomode_software (int w, int h, int fullscreen) {
129 assert(w > 0);
130 assert(h > 0);
131 Uint32 flags;
132 SDL_Surface *s;
133 if (mode == MODE_OPENGL && surf->w == w && surf->h == h && Y_get_fullscreen() == fullscreen) {
134 s = surf;
135 } else {
136 flags = SDL_DOUBLEBUF | SDL_SWSURFACE | SDL_HWPALETTE;
137 if (fullscreen) {
138 flags = flags | SDL_FULLSCREEN;
140 # ifdef WIN32
141 flags = flags | SDL_RESIZABLE;
142 # endif
143 s = SDL_SetVideoMode(w, h, 8, flags);
144 if (s != NULL) {
145 mode = MODE_SOFTWARE;
146 surf = s;
149 return s != NULL;
152 void Y_get_videomode (int *w, int *h) {
153 if (mode != MODE_NONE) {
154 *w = surf->w;
155 *h = surf->h;
156 } else {
157 *w = 0;
158 *h = 0;
162 int Y_videomode_setted (void) {
163 return mode != MODE_NONE;
166 void Y_unset_videomode (void) {
167 surf = NULL;
168 mode = MODE_NONE;
169 #ifndef __EMSCRIPTEN__
170 SDL_QuitSubSystem(SDL_INIT_VIDEO);
171 SDL_InitSubSystem(SDL_INIT_VIDEO);
172 #endif
175 void Y_set_fullscreen (int fullscreen) {
176 int fs = Y_get_fullscreen();
177 if (mode != MODE_NONE && fs != fullscreen) {
178 if (SDL_WM_ToggleFullScreen(surf) == 0) {
179 switch (mode) {
180 case MODE_OPENGL:
181 Y_set_videomode_opengl(surf->w, surf->h, fullscreen);
182 break;
183 case MODE_SOFTWARE:
184 Y_set_videomode_software(surf->w, surf->h, fullscreen);
185 break;
191 int Y_get_fullscreen (void) {
192 return (mode != MODE_NONE) && ((surf->flags & SDL_FULLSCREEN) != 0);
195 void Y_swap_buffers (void) {
196 assert(mode == MODE_OPENGL);
197 SDL_GL_SwapBuffers();
200 void Y_get_buffer (byte **buf, int *w, int *h, int *pitch) {
201 assert(mode == MODE_SOFTWARE);
202 *buf = surf->pixels;
203 *w = surf->w;
204 *h = surf->h;
205 *pitch = surf->pitch;
208 void Y_set_vga_palette (byte *vgapal) {
209 int i;
210 byte *p = vgapal;
211 assert(vgapal != NULL);
212 assert(mode == MODE_SOFTWARE);
213 SDL_Color colors[256];
214 for (i = 0; i < 256; i++) {
215 colors[i] = (SDL_Color) {
216 .r = p[0] * 255 / 63,
217 .g = p[1] * 255 / 63,
218 .b = p[2] * 255 / 63
219 };
220 p += 3;
222 SDL_SetColors(surf, colors, 0, 256);
225 void Y_repaint_rect (int x, int y, int w, int h) {
226 assert(mode == MODE_SOFTWARE);
227 SDL_UpdateRect(surf, x, y, w, h);
230 void Y_repaint (void) {
231 assert(mode == MODE_SOFTWARE);
232 SDL_Flip(surf);
235 /* --- main --- */
237 static int sdl_to_key (int code) {
238 switch (code) {
239 case SDLK_0: return KEY_0;
240 case SDLK_1: return KEY_1;
241 case SDLK_2: return KEY_2;
242 case SDLK_3: return KEY_3;
243 case SDLK_4: return KEY_4;
244 case SDLK_5: return KEY_5;
245 case SDLK_6: return KEY_6;
246 case SDLK_7: return KEY_7;
247 case SDLK_8: return KEY_8;
248 case SDLK_9: return KEY_9;
249 case SDLK_a: return KEY_A;
250 case SDLK_b: return KEY_B;
251 case SDLK_c: return KEY_C;
252 case SDLK_d: return KEY_D;
253 case SDLK_e: return KEY_E;
254 case SDLK_f: return KEY_F;
255 case SDLK_g: return KEY_G;
256 case SDLK_h: return KEY_H;
257 case SDLK_i: return KEY_I;
258 case SDLK_j: return KEY_J;
259 case SDLK_k: return KEY_K;
260 case SDLK_l: return KEY_L;
261 case SDLK_m: return KEY_M;
262 case SDLK_n: return KEY_N;
263 case SDLK_o: return KEY_O;
264 case SDLK_p: return KEY_P;
265 case SDLK_q: return KEY_Q;
266 case SDLK_r: return KEY_R;
267 case SDLK_s: return KEY_S;
268 case SDLK_t: return KEY_T;
269 case SDLK_u: return KEY_U;
270 case SDLK_v: return KEY_V;
271 case SDLK_w: return KEY_W;
272 case SDLK_x: return KEY_X;
273 case SDLK_y: return KEY_Y;
274 case SDLK_z: return KEY_Z;
275 case SDLK_RETURN: return KEY_RETURN;
276 case SDLK_ESCAPE: return KEY_ESCAPE;
277 case SDLK_BACKSPACE: return KEY_BACKSPACE;
278 case SDLK_TAB: return KEY_TAB;
279 case SDLK_SPACE: return KEY_SPACE;
280 case SDLK_MINUS: return KEY_MINUS;
281 case SDLK_EQUALS: return KEY_EQUALS;
282 case SDLK_LEFTBRACKET: return KEY_LEFTBRACKET;
283 case SDLK_RIGHTBRACKET: return KEY_RIGHTBRACKET;
284 case SDLK_BACKSLASH: return KEY_BACKSLASH;
285 case SDLK_SEMICOLON: return KEY_SEMICOLON;
286 case SDLK_QUOTE: return KEY_APOSTROPHE;
287 case SDLK_BACKQUOTE: return KEY_GRAVE;
288 case SDLK_COMMA: return KEY_COMMA;
289 case SDLK_PERIOD: return KEY_PERIOD;
290 case SDLK_SLASH: return KEY_SLASH;
291 case SDLK_CAPSLOCK: return KEY_CAPSLOCK;
292 case SDLK_F1: return KEY_F1;
293 case SDLK_F2: return KEY_F2;
294 case SDLK_F3: return KEY_F3;
295 case SDLK_F4: return KEY_F4;
296 case SDLK_F5: return KEY_F5;
297 case SDLK_F6: return KEY_F6;
298 case SDLK_F7: return KEY_F7;
299 case SDLK_F8: return KEY_F8;
300 case SDLK_F9: return KEY_F9;
301 case SDLK_F10: return KEY_F10;
302 case SDLK_F11: return KEY_F11;
303 case SDLK_F12: return KEY_F12;
304 case SDLK_PRINT: return KEY_PRINTSCREEN;
305 case SDLK_SCROLLOCK: return KEY_SCROLLLOCK;
306 case SDLK_PAUSE: return KEY_PAUSE;
307 case SDLK_INSERT: return KEY_INSERT;
308 case SDLK_HOME: return KEY_HOME;
309 case SDLK_PAGEUP: return KEY_PAGEUP;
310 case SDLK_DELETE: return KEY_DELETE;
311 case SDLK_END: return KEY_END;
312 case SDLK_PAGEDOWN: return KEY_PAGEDOWN;
313 case SDLK_RIGHT: return KEY_RIGHT;
314 case SDLK_LEFT: return KEY_LEFT;
315 case SDLK_DOWN: return KEY_DOWN;
316 case SDLK_UP: return KEY_UP;
317 case SDLK_NUMLOCK: return KEY_NUMLOCK;
318 case SDLK_KP_DIVIDE: return KEY_KP_DIVIDE;
319 case SDLK_KP_MULTIPLY: return KEY_KP_MULTIPLY;
320 case SDLK_KP_MINUS: return KEY_KP_MINUS;
321 case SDLK_KP_PLUS: return KEY_KP_PLUS;
322 case SDLK_KP_ENTER: return KEY_KP_ENTER;
323 case SDLK_KP0: return KEY_KP_0;
324 case SDLK_KP1: return KEY_KP_1;
325 case SDLK_KP2: return KEY_KP_2;
326 case SDLK_KP3: return KEY_KP_3;
327 case SDLK_KP4: return KEY_KP_4;
328 case SDLK_KP5: return KEY_KP_5;
329 case SDLK_KP6: return KEY_KP_6;
330 case SDLK_KP7: return KEY_KP_7;
331 case SDLK_KP8: return KEY_KP_8;
332 case SDLK_KP9: return KEY_KP_9;
333 case SDLK_KP_PERIOD: return KEY_KP_PERIOD;
334 case SDLK_SYSREQ: return KEY_SYSREQ;
335 case SDLK_LCTRL: return KEY_LCTRL;
336 case SDLK_LSHIFT: return KEY_LSHIFT;
337 case SDLK_LALT: return KEY_LALT;
338 case SDLK_LSUPER: return KEY_LSUPER;
339 case SDLK_RCTRL: return KEY_RCTRL;
340 case SDLK_RSHIFT: return KEY_RSHIFT;
341 case SDLK_RALT: return KEY_RALT;
342 case SDLK_RSUPER: return KEY_RSUPER;
343 default: return KEY_UNKNOWN;
347 static void poll_events (void (*h)(int key, int down)) {
348 int key;
349 SDL_Event ev;
350 while (SDL_PollEvent(&ev)) {
351 switch (ev.type) {
352 case SDL_QUIT:
353 ERR_quit();
354 break;
355 case SDL_VIDEORESIZE:
356 R_set_videomode(ev.resize.w, ev.resize.h, Y_get_fullscreen());
357 break;
358 case SDL_KEYDOWN:
359 case SDL_KEYUP:
360 key = sdl_to_key(ev.key.keysym.sym);
361 I_press(key, ev.type == SDL_KEYDOWN);
362 if (h != NULL) {
363 (*h)(key, ev.type == SDL_KEYDOWN);
365 break;
370 static void step (void) {
371 poll_events(&G_keyf);
372 S_updatemusic();
373 Uint32 t = SDL_GetTicks();
374 if (t - ticks > DELAY) {
375 ticks = t;
376 G_act();
378 R_draw();
381 int main (int argc, char *argv[]) {
382 char *pw;
383 logo("main: initialize SDL\n");
384 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1) {
385 logo("main: failed to init SDL: %s\n", SDL_GetError());
386 return 1;
388 SDL_WM_SetCaption("Doom 2D v1.351", "Doom 2D");
389 // Player 1 defaults
390 pl1.ku = KEY_KP_8;
391 pl1.kd = KEY_KP_5;
392 pl1.kl = KEY_KP_4;
393 pl1.kr = KEY_KP_6;
394 pl1.kf = KEY_PAGEDOWN;
395 pl1.kj = KEY_DELETE;
396 pl1.kwl = KEY_HOME;
397 pl1.kwr = KEY_END;
398 pl1.kp = KEY_KP_8;
399 // Player 2 defaults
400 pl2.ku = KEY_E;
401 pl2.kd = KEY_D;
402 pl2.kl = KEY_S;
403 pl2.kr = KEY_F;
404 pl2.kf = KEY_A;
405 pl2.kj = KEY_Q;
406 pl2.kwl = KEY_1;
407 pl2.kwr = KEY_2;
408 pl2.kp = KEY_E;
409 srand(SDL_GetTicks());
410 F_startup();
411 #ifndef WIN32
412 pw = "/usr/share/doom2d-rembo/doom2d.wad";
413 #else
414 pw = "doom2d.wad";
415 #endif
416 if (fexists(pw)) {
417 F_addwad(pw);
418 } else {
419 F_addwad("doom2d.wad");
421 CFG_args(argc, argv);
422 CFG_load();
423 F_initwads();
424 M_startup();
425 F_allocres();
426 S_init();
427 S_initmusic();
428 R_init();
429 G_init();
430 ticks = SDL_GetTicks();
431 #ifdef __EMSCRIPTEN__
432 emscripten_set_main_loop(step, 0, 1);
433 #else
434 while (!quit) {
435 step();
437 #endif
438 CFG_save();
439 R_done();
440 S_donemusic();
441 S_done();
442 M_shutdown();
443 SDL_Quit();
444 return 0;