DEADSOFTWARE

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