DEADSOFTWARE

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