DEADSOFTWARE

sdl: fix videomode flags on fullscreen toggle
[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 static int quit = 0;
46 static SDL_Surface *surf = NULL;
48 /* --- error.h --- */
50 void logo (const char *s, ...) {
51 va_list ap;
52 va_start(ap, s);
53 vprintf(s, ap);
54 va_end(ap);
55 fflush(stdout);
56 }
58 void logo_gas (int cur, int all) {
59 // stub
60 }
62 void ERR_failinit (char *s, ...) {
63 va_list ap;
64 va_start(ap, s);
65 vprintf(s, ap);
66 va_end(ap);
67 puts("");
68 exit(1);
69 }
71 void ERR_fatal (char *s, ...) {
72 va_list ap;
73 R_done();
74 S_done();
75 S_donemusic();
76 M_shutdown();
77 SDL_Quit();
78 puts("\nКРИТИЧЕСКАЯ ОШИБКА:");
79 va_start(ap, s);
80 vprintf(s, ap);
81 va_end(ap);
82 puts("");
83 exit(1);
84 }
86 void ERR_quit (void) {
87 puts("Спасибо за то, что вы играли в Операцию \"Смятка\"!");
88 //F_loadres(F_getresid("ENDOOM"),p,0,4000);
89 quit = 1;
90 }
92 /* --- system.h --- */
94 int Y_set_videomode (int w, int h, int flags) {
95 SDL_Surface *s;
96 int colors;
97 Uint32 f;
98 assert(w > 0);
99 assert(h > 0);
100 f = SDL_DOUBLEBUF;
101 if (flags & SYSTEM_USE_FULLSCREEN) {
102 f = flags | SDL_FULLSCREEN;
104 if (flags & SYSTEM_USE_OPENGL) {
105 f = flags | SDL_OPENGL;
106 colors = 0;
107 } else {
108 f = flags | SDL_SWSURFACE | SDL_HWPALETTE;
109 colors = 8;
111 s = SDL_SetVideoMode(w, h, colors, f);
112 if (s != NULL) {
113 surf = s;
115 return s != NULL;
118 void Y_get_videomode (int *w, int *h) {
119 if (surf != NULL) {
120 *w = surf->w;
121 *h = surf->h;
122 } else {
123 *w = 0;
124 *h = 0;
128 int Y_videomode_setted (void) {
129 return surf != NULL;
132 void Y_unset_videomode (void) {
133 surf = NULL;
134 SDL_QuitSubSystem(SDL_INIT_VIDEO);
135 SDL_InitSubSystem(SDL_INIT_VIDEO);
138 void Y_set_fullscreen (int yes) {
139 assert(surf != NULL);
140 int flags = 0;
141 if ((surf->flags & SDL_FULLSCREEN) == 0) {
142 flags |= SYSTEM_USE_FULLSCREEN;
144 if (surf->flags & SDL_OPENGL) {
145 flags |= SDL_OPENGL;
147 Y_set_videomode(surf->w, surf->h, flags);
150 int Y_get_fullscreen (void) {
151 return (surf != NULL) && ((surf->flags & SDL_FULLSCREEN) != 0);
154 void Y_swap_buffers (void) {
155 assert(surf != NULL);
156 assert(surf->flags & SDL_OPENGL);
157 SDL_GL_SwapBuffers();
160 void Y_get_buffer (byte **buf, int *w, int *h, int *pitch) {
161 assert(surf != NULL);
162 assert((surf->flags & SDL_OPENGL) == 0);
163 *buf = surf->pixels;
164 *w = surf->w;
165 *h = surf->h;
166 *pitch = surf->pitch;
169 void Y_set_vga_palette (byte *vgapal) {
170 int i;
171 byte *p = vgapal;
172 assert(vgapal != NULL);
173 assert(surf != NULL);
174 assert((surf->flags & SDL_OPENGL) == 0);
175 SDL_Color colors[256];
176 for (i = 0; i < 256; i++) {
177 colors[i] = (SDL_Color) {
178 .r = p[0] * 255 / 63,
179 .g = p[1] * 255 / 63,
180 .b = p[2] * 255 / 63
181 };
182 p += 3;
184 SDL_SetColors(surf, colors, 0, 256);
187 void Y_repaint_rect (int x, int y, int w, int h) {
188 assert(surf != NULL);
189 assert((surf->flags & SDL_OPENGL) == 0);
190 SDL_UpdateRect(surf, x, y, w, h);
193 void Y_repaint (void) {
194 assert(surf != NULL);
195 assert((surf->flags & SDL_OPENGL) == 0);
196 SDL_Flip(surf);
199 /* --- main --- */
201 static int sdl_to_key (int code) {
202 switch (code) {
203 case SDLK_0: return KEY_0;
204 case SDLK_1: return KEY_1;
205 case SDLK_2: return KEY_2;
206 case SDLK_3: return KEY_3;
207 case SDLK_4: return KEY_4;
208 case SDLK_5: return KEY_5;
209 case SDLK_6: return KEY_6;
210 case SDLK_7: return KEY_7;
211 case SDLK_8: return KEY_8;
212 case SDLK_9: return KEY_9;
213 case SDLK_a: return KEY_A;
214 case SDLK_b: return KEY_B;
215 case SDLK_c: return KEY_C;
216 case SDLK_d: return KEY_D;
217 case SDLK_e: return KEY_E;
218 case SDLK_f: return KEY_F;
219 case SDLK_g: return KEY_G;
220 case SDLK_h: return KEY_H;
221 case SDLK_i: return KEY_I;
222 case SDLK_j: return KEY_J;
223 case SDLK_k: return KEY_K;
224 case SDLK_l: return KEY_L;
225 case SDLK_m: return KEY_M;
226 case SDLK_n: return KEY_N;
227 case SDLK_o: return KEY_O;
228 case SDLK_p: return KEY_P;
229 case SDLK_q: return KEY_Q;
230 case SDLK_r: return KEY_R;
231 case SDLK_s: return KEY_S;
232 case SDLK_t: return KEY_T;
233 case SDLK_u: return KEY_U;
234 case SDLK_v: return KEY_V;
235 case SDLK_w: return KEY_W;
236 case SDLK_x: return KEY_X;
237 case SDLK_y: return KEY_Y;
238 case SDLK_z: return KEY_Z;
239 case SDLK_RETURN: return KEY_RETURN;
240 case SDLK_ESCAPE: return KEY_ESCAPE;
241 case SDLK_BACKSPACE: return KEY_BACKSPACE;
242 case SDLK_TAB: return KEY_TAB;
243 case SDLK_SPACE: return KEY_SPACE;
244 case SDLK_MINUS: return KEY_MINUS;
245 case SDLK_EQUALS: return KEY_EQUALS;
246 case SDLK_LEFTBRACKET: return KEY_LEFTBRACKET;
247 case SDLK_RIGHTBRACKET: return KEY_RIGHTBRACKET;
248 case SDLK_BACKSLASH: return KEY_BACKSLASH;
249 case SDLK_SEMICOLON: return KEY_SEMICOLON;
250 case SDLK_QUOTE: return KEY_APOSTROPHE;
251 case SDLK_BACKQUOTE: return KEY_GRAVE;
252 case SDLK_COMMA: return KEY_COMMA;
253 case SDLK_PERIOD: return KEY_PERIOD;
254 case SDLK_SLASH: return KEY_SLASH;
255 case SDLK_CAPSLOCK: return KEY_CAPSLOCK;
256 case SDLK_F1: return KEY_F1;
257 case SDLK_F2: return KEY_F2;
258 case SDLK_F3: return KEY_F3;
259 case SDLK_F4: return KEY_F4;
260 case SDLK_F5: return KEY_F5;
261 case SDLK_F6: return KEY_F6;
262 case SDLK_F7: return KEY_F7;
263 case SDLK_F8: return KEY_F8;
264 case SDLK_F9: return KEY_F9;
265 case SDLK_F10: return KEY_F10;
266 case SDLK_F11: return KEY_F11;
267 case SDLK_F12: return KEY_F12;
268 case SDLK_PRINT: return KEY_PRINTSCREEN;
269 case SDLK_SCROLLOCK: return KEY_SCROLLLOCK;
270 case SDLK_PAUSE: return KEY_PAUSE;
271 case SDLK_INSERT: return KEY_INSERT;
272 case SDLK_HOME: return KEY_HOME;
273 case SDLK_PAGEUP: return KEY_PAGEUP;
274 case SDLK_DELETE: return KEY_DELETE;
275 case SDLK_END: return KEY_END;
276 case SDLK_PAGEDOWN: return KEY_PAGEDOWN;
277 case SDLK_RIGHT: return KEY_RIGHT;
278 case SDLK_LEFT: return KEY_LEFT;
279 case SDLK_DOWN: return KEY_DOWN;
280 case SDLK_UP: return KEY_UP;
281 case SDLK_NUMLOCK: return KEY_NUMLOCK;
282 case SDLK_KP_DIVIDE: return KEY_KP_DIVIDE;
283 case SDLK_KP_MULTIPLY: return KEY_KP_MULTIPLY;
284 case SDLK_KP_MINUS: return KEY_KP_MINUS;
285 case SDLK_KP_PLUS: return KEY_KP_PLUS;
286 case SDLK_KP_ENTER: return KEY_KP_ENTER;
287 case SDLK_KP0: return KEY_KP_0;
288 case SDLK_KP1: return KEY_KP_1;
289 case SDLK_KP2: return KEY_KP_2;
290 case SDLK_KP3: return KEY_KP_3;
291 case SDLK_KP4: return KEY_KP_4;
292 case SDLK_KP5: return KEY_KP_5;
293 case SDLK_KP6: return KEY_KP_6;
294 case SDLK_KP7: return KEY_KP_7;
295 case SDLK_KP8: return KEY_KP_8;
296 case SDLK_KP9: return KEY_KP_9;
297 case SDLK_KP_PERIOD: return KEY_KP_PERIOD;
298 case SDLK_SYSREQ: return KEY_SYSREQ;
299 case SDLK_LCTRL: return KEY_LCTRL;
300 case SDLK_LSHIFT: return KEY_LSHIFT;
301 case SDLK_LALT: return KEY_LALT;
302 case SDLK_LSUPER: return KEY_LSUPER;
303 case SDLK_RCTRL: return KEY_RCTRL;
304 case SDLK_RSHIFT: return KEY_RSHIFT;
305 case SDLK_RALT: return KEY_RALT;
306 case SDLK_RSUPER: return KEY_RSUPER;
307 default: return KEY_UNKNOWN;
311 static void poll_events (void (*h)(int key, int down)) {
312 int key;
313 SDL_Event ev;
314 while (SDL_PollEvent(&ev)) {
315 switch (ev.type) {
316 case SDL_QUIT:
317 ERR_quit();
318 break;
319 case SDL_KEYDOWN:
320 case SDL_KEYUP:
321 key = sdl_to_key(ev.key.keysym.sym);
322 I_press(key, ev.type == SDL_KEYDOWN);
323 if (h != NULL) {
324 (*h)(key, ev.type == SDL_KEYDOWN);
326 break;
331 int main (int argc, char *argv[]) {
332 char *pw;
333 Uint32 t, ticks;
334 logo("main: initialize SDL\n");
335 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1) {
336 logo("main: failed to init SDL: %s\n", SDL_GetError());
337 return 1;
339 SDL_WM_SetCaption("Doom 2D v1.351", "Doom 2D");
340 // Player 1 defaults
341 pl1.ku = KEY_KP_8;
342 pl1.kd = KEY_KP_5;
343 pl1.kl = KEY_KP_4;
344 pl1.kr = KEY_KP_6;
345 pl1.kf = KEY_PAGEDOWN;
346 pl1.kj = KEY_DELETE;
347 pl1.kwl = KEY_HOME;
348 pl1.kwr = KEY_END;
349 pl1.kp = KEY_KP_8;
350 // Player 2 defaults
351 pl2.ku = KEY_E;
352 pl2.kd = KEY_D;
353 pl2.kl = KEY_S;
354 pl2.kr = KEY_F;
355 pl2.kf = KEY_A;
356 pl2.kj = KEY_Q;
357 pl2.kwl = KEY_1;
358 pl2.kwr = KEY_2;
359 pl2.kp = KEY_E;
360 srand(SDL_GetTicks());
361 F_startup();
362 #ifndef WIN32
363 pw = "/usr/share/doom2d-rembo/doom2d.wad";
364 #else
365 pw = "doom2d.wad";
366 #endif
367 if (fexists(pw)) {
368 F_addwad(pw);
369 } else {
370 F_addwad("doom2d.wad");
372 CFG_args(argc, argv);
373 CFG_load();
374 F_initwads();
375 M_startup();
376 F_allocres();
377 S_init();
378 S_initmusic();
379 R_init();
380 G_init();
381 ticks = SDL_GetTicks();
382 while (!quit) {
383 poll_events(&G_keyf);
384 S_updatemusic();
385 t = SDL_GetTicks();
386 if (t - ticks > DELAY) {
387 ticks = t;
388 G_act();
390 R_draw();
392 CFG_save();
393 R_done();
394 S_donemusic();
395 S_done();
396 M_shutdown();
397 SDL_Quit();
398 return 0;