DEADSOFTWARE

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