DEADSOFTWARE

2cc83f6409b848e56fc3d92ee72ed87142dbc610
[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 puts("Спасибо за то, что вы играли в Операцию \"Смятка\"!");
93 //F_loadres(F_getresid("ENDOOM"),p,0,4000);
94 quit = 1;
95 }
97 /* --- system.h --- */
99 int Y_set_videomode_opengl (int w, int h, int fullscreen) {
100 assert(w > 0);
101 assert(h > 0);
102 Uint32 flags;
103 SDL_Surface *s;
104 if (mode == MODE_OPENGL && surf->w == w && surf->h == h && Y_get_fullscreen() == fullscreen) {
105 s = surf;
106 } else {
107 flags = SDL_DOUBLEBUF | SDL_OPENGL;
108 if (fullscreen) {
109 flags = flags | SDL_FULLSCREEN;
111 # ifdef WIN32
112 flags = flags | SDL_RESIZABLE;
113 # endif
114 s = SDL_SetVideoMode(w, h, 0, flags);
115 if (s != NULL) {
116 mode = MODE_OPENGL;
117 surf = s;
120 return s != NULL;
123 int Y_set_videomode_software (int w, int h, int fullscreen) {
124 assert(w > 0);
125 assert(h > 0);
126 Uint32 flags;
127 SDL_Surface *s;
128 if (mode == MODE_OPENGL && surf->w == w && surf->h == h && Y_get_fullscreen() == fullscreen) {
129 s = surf;
130 } else {
131 flags = SDL_DOUBLEBUF | SDL_SWSURFACE | SDL_HWPALETTE;
132 if (fullscreen) {
133 flags = flags | SDL_FULLSCREEN;
135 # ifdef WIN32
136 flags = flags | SDL_RESIZABLE;
137 # endif
138 s = SDL_SetVideoMode(w, h, 8, flags);
139 if (s != NULL) {
140 mode = MODE_SOFTWARE;
141 surf = s;
144 return s != NULL;
147 void Y_get_videomode (int *w, int *h) {
148 if (mode != MODE_NONE) {
149 *w = surf->w;
150 *h = surf->h;
151 } else {
152 *w = 0;
153 *h = 0;
157 int Y_videomode_setted (void) {
158 return mode != MODE_NONE;
161 void Y_unset_videomode (void) {
162 surf = NULL;
163 mode = MODE_NONE;
164 SDL_QuitSubSystem(SDL_INIT_VIDEO);
165 SDL_InitSubSystem(SDL_INIT_VIDEO);
168 void Y_set_fullscreen (int fullscreen) {
169 int fs = Y_get_fullscreen();
170 if (mode != MODE_NONE && fs != fullscreen) {
171 if (SDL_WM_ToggleFullScreen(surf) == 0) {
172 switch (mode) {
173 case MODE_OPENGL:
174 Y_set_videomode_opengl(surf->w, surf->h, fullscreen);
175 break;
176 case MODE_SOFTWARE:
177 Y_set_videomode_software(surf->w, surf->h, fullscreen);
178 break;
184 int Y_get_fullscreen (void) {
185 return (mode != MODE_NONE) && ((surf->flags & SDL_FULLSCREEN) != 0);
188 void Y_swap_buffers (void) {
189 assert(mode == MODE_OPENGL);
190 SDL_GL_SwapBuffers();
193 void Y_get_buffer (byte **buf, int *w, int *h, int *pitch) {
194 assert(mode == MODE_SOFTWARE);
195 *buf = surf->pixels;
196 *w = surf->w;
197 *h = surf->h;
198 *pitch = surf->pitch;
201 void Y_set_vga_palette (byte *vgapal) {
202 int i;
203 byte *p = vgapal;
204 assert(vgapal != NULL);
205 assert(mode == MODE_SOFTWARE);
206 SDL_Color colors[256];
207 for (i = 0; i < 256; i++) {
208 colors[i] = (SDL_Color) {
209 .r = p[0] * 255 / 63,
210 .g = p[1] * 255 / 63,
211 .b = p[2] * 255 / 63
212 };
213 p += 3;
215 SDL_SetColors(surf, colors, 0, 256);
218 void Y_repaint_rect (int x, int y, int w, int h) {
219 assert(mode == MODE_SOFTWARE);
220 SDL_UpdateRect(surf, x, y, w, h);
223 void Y_repaint (void) {
224 assert(mode == MODE_SOFTWARE);
225 SDL_Flip(surf);
228 /* --- main --- */
230 static int sdl_to_key (int code) {
231 switch (code) {
232 case SDLK_0: return KEY_0;
233 case SDLK_1: return KEY_1;
234 case SDLK_2: return KEY_2;
235 case SDLK_3: return KEY_3;
236 case SDLK_4: return KEY_4;
237 case SDLK_5: return KEY_5;
238 case SDLK_6: return KEY_6;
239 case SDLK_7: return KEY_7;
240 case SDLK_8: return KEY_8;
241 case SDLK_9: return KEY_9;
242 case SDLK_a: return KEY_A;
243 case SDLK_b: return KEY_B;
244 case SDLK_c: return KEY_C;
245 case SDLK_d: return KEY_D;
246 case SDLK_e: return KEY_E;
247 case SDLK_f: return KEY_F;
248 case SDLK_g: return KEY_G;
249 case SDLK_h: return KEY_H;
250 case SDLK_i: return KEY_I;
251 case SDLK_j: return KEY_J;
252 case SDLK_k: return KEY_K;
253 case SDLK_l: return KEY_L;
254 case SDLK_m: return KEY_M;
255 case SDLK_n: return KEY_N;
256 case SDLK_o: return KEY_O;
257 case SDLK_p: return KEY_P;
258 case SDLK_q: return KEY_Q;
259 case SDLK_r: return KEY_R;
260 case SDLK_s: return KEY_S;
261 case SDLK_t: return KEY_T;
262 case SDLK_u: return KEY_U;
263 case SDLK_v: return KEY_V;
264 case SDLK_w: return KEY_W;
265 case SDLK_x: return KEY_X;
266 case SDLK_y: return KEY_Y;
267 case SDLK_z: return KEY_Z;
268 case SDLK_RETURN: return KEY_RETURN;
269 case SDLK_ESCAPE: return KEY_ESCAPE;
270 case SDLK_BACKSPACE: return KEY_BACKSPACE;
271 case SDLK_TAB: return KEY_TAB;
272 case SDLK_SPACE: return KEY_SPACE;
273 case SDLK_MINUS: return KEY_MINUS;
274 case SDLK_EQUALS: return KEY_EQUALS;
275 case SDLK_LEFTBRACKET: return KEY_LEFTBRACKET;
276 case SDLK_RIGHTBRACKET: return KEY_RIGHTBRACKET;
277 case SDLK_BACKSLASH: return KEY_BACKSLASH;
278 case SDLK_SEMICOLON: return KEY_SEMICOLON;
279 case SDLK_QUOTE: return KEY_APOSTROPHE;
280 case SDLK_BACKQUOTE: return KEY_GRAVE;
281 case SDLK_COMMA: return KEY_COMMA;
282 case SDLK_PERIOD: return KEY_PERIOD;
283 case SDLK_SLASH: return KEY_SLASH;
284 case SDLK_CAPSLOCK: return KEY_CAPSLOCK;
285 case SDLK_F1: return KEY_F1;
286 case SDLK_F2: return KEY_F2;
287 case SDLK_F3: return KEY_F3;
288 case SDLK_F4: return KEY_F4;
289 case SDLK_F5: return KEY_F5;
290 case SDLK_F6: return KEY_F6;
291 case SDLK_F7: return KEY_F7;
292 case SDLK_F8: return KEY_F8;
293 case SDLK_F9: return KEY_F9;
294 case SDLK_F10: return KEY_F10;
295 case SDLK_F11: return KEY_F11;
296 case SDLK_F12: return KEY_F12;
297 case SDLK_PRINT: return KEY_PRINTSCREEN;
298 case SDLK_SCROLLOCK: return KEY_SCROLLLOCK;
299 case SDLK_PAUSE: return KEY_PAUSE;
300 case SDLK_INSERT: return KEY_INSERT;
301 case SDLK_HOME: return KEY_HOME;
302 case SDLK_PAGEUP: return KEY_PAGEUP;
303 case SDLK_DELETE: return KEY_DELETE;
304 case SDLK_END: return KEY_END;
305 case SDLK_PAGEDOWN: return KEY_PAGEDOWN;
306 case SDLK_RIGHT: return KEY_RIGHT;
307 case SDLK_LEFT: return KEY_LEFT;
308 case SDLK_DOWN: return KEY_DOWN;
309 case SDLK_UP: return KEY_UP;
310 case SDLK_NUMLOCK: return KEY_NUMLOCK;
311 case SDLK_KP_DIVIDE: return KEY_KP_DIVIDE;
312 case SDLK_KP_MULTIPLY: return KEY_KP_MULTIPLY;
313 case SDLK_KP_MINUS: return KEY_KP_MINUS;
314 case SDLK_KP_PLUS: return KEY_KP_PLUS;
315 case SDLK_KP_ENTER: return KEY_KP_ENTER;
316 case SDLK_KP0: return KEY_KP_0;
317 case SDLK_KP1: return KEY_KP_1;
318 case SDLK_KP2: return KEY_KP_2;
319 case SDLK_KP3: return KEY_KP_3;
320 case SDLK_KP4: return KEY_KP_4;
321 case SDLK_KP5: return KEY_KP_5;
322 case SDLK_KP6: return KEY_KP_6;
323 case SDLK_KP7: return KEY_KP_7;
324 case SDLK_KP8: return KEY_KP_8;
325 case SDLK_KP9: return KEY_KP_9;
326 case SDLK_KP_PERIOD: return KEY_KP_PERIOD;
327 case SDLK_SYSREQ: return KEY_SYSREQ;
328 case SDLK_LCTRL: return KEY_LCTRL;
329 case SDLK_LSHIFT: return KEY_LSHIFT;
330 case SDLK_LALT: return KEY_LALT;
331 case SDLK_LSUPER: return KEY_LSUPER;
332 case SDLK_RCTRL: return KEY_RCTRL;
333 case SDLK_RSHIFT: return KEY_RSHIFT;
334 case SDLK_RALT: return KEY_RALT;
335 case SDLK_RSUPER: return KEY_RSUPER;
336 default: return KEY_UNKNOWN;
340 static void poll_events (void (*h)(int key, int down)) {
341 int key;
342 SDL_Event ev;
343 while (SDL_PollEvent(&ev)) {
344 switch (ev.type) {
345 case SDL_QUIT:
346 ERR_quit();
347 break;
348 case SDL_VIDEORESIZE:
349 R_set_videomode(ev.resize.w, ev.resize.h, Y_get_fullscreen());
350 break;
351 case SDL_KEYDOWN:
352 case SDL_KEYUP:
353 key = sdl_to_key(ev.key.keysym.sym);
354 I_press(key, ev.type == SDL_KEYDOWN);
355 if (h != NULL) {
356 (*h)(key, ev.type == SDL_KEYDOWN);
358 break;
363 int main (int argc, char *argv[]) {
364 char *pw;
365 Uint32 t, ticks;
366 logo("main: initialize SDL\n");
367 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1) {
368 logo("main: failed to init SDL: %s\n", SDL_GetError());
369 return 1;
371 SDL_WM_SetCaption("Doom 2D v1.351", "Doom 2D");
372 // Player 1 defaults
373 pl1.ku = KEY_KP_8;
374 pl1.kd = KEY_KP_5;
375 pl1.kl = KEY_KP_4;
376 pl1.kr = KEY_KP_6;
377 pl1.kf = KEY_PAGEDOWN;
378 pl1.kj = KEY_DELETE;
379 pl1.kwl = KEY_HOME;
380 pl1.kwr = KEY_END;
381 pl1.kp = KEY_KP_8;
382 // Player 2 defaults
383 pl2.ku = KEY_E;
384 pl2.kd = KEY_D;
385 pl2.kl = KEY_S;
386 pl2.kr = KEY_F;
387 pl2.kf = KEY_A;
388 pl2.kj = KEY_Q;
389 pl2.kwl = KEY_1;
390 pl2.kwr = KEY_2;
391 pl2.kp = KEY_E;
392 srand(SDL_GetTicks());
393 F_startup();
394 #ifndef WIN32
395 pw = "/usr/share/doom2d-rembo/doom2d.wad";
396 #else
397 pw = "doom2d.wad";
398 #endif
399 if (fexists(pw)) {
400 F_addwad(pw);
401 } else {
402 F_addwad("doom2d.wad");
404 CFG_args(argc, argv);
405 CFG_load();
406 F_initwads();
407 M_startup();
408 F_allocres();
409 S_init();
410 S_initmusic();
411 R_init();
412 G_init();
413 ticks = SDL_GetTicks();
414 while (!quit) {
415 poll_events(&G_keyf);
416 S_updatemusic();
417 t = SDL_GetTicks();
418 if (t - ticks > DELAY) {
419 ticks = t;
420 G_act();
422 R_draw();
424 CFG_save();
425 R_done();
426 S_donemusic();
427 S_done();
428 M_shutdown();
429 SDL_Quit();
430 return 0;