DEADSOFTWARE

menu: change videomode at runtime
[flatwaifu.git] / src / sdl2 / main.c
1 #ifdef __EMSCRIPTEN__
2 # include <emscripten.h>
3 #endif
5 #include <SDL2/SDL.h>
6 #include <stdio.h>
7 #include <stdarg.h>
8 #include <stdlib.h> // srand exit
9 #include <string.h> // strcasecmp
10 #include <assert.h>
11 #include "system.h"
12 #include "input.h"
14 #include "my.h" // fexists
15 #include "player.h" // pl1 pl2
16 #include "menu.h" // G_keyf
17 #include "error.h" // logo
19 #include "files.h" // F_startup F_addwad F_initwads F_allocres
20 #include "config.h" // CFG_args CFG_load CFG_save
21 #include "memory.h" // M_startup
22 #include "game.h" // G_init G_act
23 #include "sound.h" // S_init S_done
24 #include "music.h" // S_initmusic S_updatemusic S_donemusic
25 #include "render.h" // R_init R_draw R_done
27 #define TITLE_STR "Doom 2D (SDL2)"
29 static Uint32 ticks;
30 static int quit = 0;
31 static SDL_Window *window;
32 static SDL_GLContext context;
33 static SDL_Surface *surf;
34 static videomode_t vlist;
36 /* --- error.h --- */
38 void logo (const char *s, ...) {
39 va_list ap;
40 va_start(ap, s);
41 vprintf(s, ap);
42 va_end(ap);
43 fflush(stdout);
44 }
46 void logo_gas (int cur, int all) {
47 // stub
48 }
50 void ERR_failinit (char *s, ...) {
51 va_list ap;
52 va_start(ap, s);
53 vprintf(s, ap);
54 va_end(ap);
55 puts("");
56 exit(1);
57 }
59 void ERR_fatal (char *s, ...) {
60 va_list ap;
61 R_done();
62 S_done();
63 S_donemusic();
64 M_shutdown();
65 SDL_Quit();
66 puts("\nКРИТИЧЕСКАЯ ОШИБКА:");
67 va_start(ap, s);
68 vprintf(s, ap);
69 va_end(ap);
70 puts("");
71 exit(1);
72 }
74 void ERR_quit (void) {
75 quit = 1;
76 }
78 /* --- system.h --- */
80 static int Y_resize_window (int w, int h, int fullscreen) {
81 assert(w > 0);
82 assert(h > 0);
83 assert(window != NULL);
84 if (surf != NULL) {
85 if (surf->w != w || surf->h != h) {
86 SDL_Surface *s = SDL_CreateRGBSurface(0, w, h, 8, 0, 0, 0, 0);
87 if (s != NULL) {
88 SDL_SetPaletteColors(s->format->palette, surf->format->palette->colors, 0, surf->format->palette->ncolors);
89 SDL_FreeSurface(surf);
90 surf = s;
91 }
92 }
93 }
94 SDL_SetWindowSize(window, w, h);
95 Y_set_fullscreen(fullscreen);
96 return 1;
97 }
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_Window *win;
104 SDL_GLContext ctx;
105 if (window != NULL && context != NULL) {
106 Y_resize_window(w, h, fullscreen);
107 win = window;
108 } else {
109 flags = SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL;
110 if (fullscreen) {
111 flags = flags | SDL_WINDOW_FULLSCREEN;
113 // TODO set context version and type
114 #ifdef __EMSCRIPTEN__
115 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
116 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
117 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
118 #else
119 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1);
120 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
121 #endif
122 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
123 win = SDL_CreateWindow(TITLE_STR, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, flags);
124 if (win != NULL) {
125 ctx = SDL_GL_CreateContext(win);
126 if (ctx != NULL) {
127 Y_unset_videomode();
128 window = win;
129 context = ctx;
130 SDL_GL_MakeCurrent(window, context);
131 } else {
132 SDL_DestroyWindow(win);
133 win = NULL;
137 if (win == NULL) {
138 logo("Y_set_videomode_opengl: error: %s\n", SDL_GetError());
140 return win != NULL;
143 int Y_set_videomode_software (int w, int h, int fullscreen) {
144 assert(w > 0);
145 assert(h > 0);
146 Uint32 flags;
147 SDL_Surface *s;
148 SDL_Window *win;
149 if (window != NULL && surf != NULL) {
150 Y_resize_window(w, h, fullscreen);
151 win = window;
152 } else {
153 flags = SDL_WINDOW_RESIZABLE;
154 if (fullscreen) {
155 flags = flags | SDL_WINDOW_FULLSCREEN;
157 win = SDL_CreateWindow(TITLE_STR, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, flags);
158 if (win != NULL) {
159 s = SDL_CreateRGBSurface(0, w, h, 8, 0, 0, 0, 0);
160 if (s != NULL) {
161 Y_unset_videomode();
162 window = win;
163 surf = s;
164 } else {
165 SDL_DestroyWindow(win);
166 win = NULL;
170 if (win == NULL) {
171 logo("Y_set_videomode_software: error: %s\n", SDL_GetError());
173 return win != NULL;
176 void Y_get_videomode (int *w, int *h) {
177 if (window != NULL) {
178 SDL_GetWindowSize(window, w, h);
179 } else {
180 *w = 0;
181 *h = 0;
185 int Y_videomode_setted (void) {
186 return window != NULL;
189 void Y_unset_videomode (void) {
190 if (window != NULL) {
191 if (context != NULL) {
192 SDL_GL_MakeCurrent(window, NULL);
193 SDL_GL_DeleteContext(context);
194 context = NULL;
196 if (surf != NULL) {
197 SDL_FreeSurface(surf);
198 surf = NULL;
200 SDL_DestroyWindow(window);
201 window = NULL;
205 static void init_videomode_list (void) {
206 int i, j, k;
207 SDL_DisplayMode m;
208 int n = SDL_GetNumDisplayModes(0);
209 if (vlist.modes != NULL) {
210 free(vlist.modes);
211 vlist.modes = NULL;
212 vlist.n = 0;
214 if (n > 0) {
215 vlist.modes = malloc(n * sizeof(videomode_size_t));
216 if (vlist.modes != NULL) {
217 j = 0;
218 for (i = 0; i < n; i++) {
219 SDL_GetDisplayMode(0, i, &m);
220 k = 0;
221 while (k < j && (m.w != vlist.modes[k].w || m.h != vlist.modes[k].h)) {
222 k++;
224 if (k >= j) {
225 vlist.modes[j] = (videomode_size_t) {
226 .w = m.w,
227 .h = m.h
228 };
229 j++;
232 vlist.n = j;
237 const videomode_t *Y_get_videomode_list_opengl (int fullscreen) {
238 init_videomode_list();
239 return &vlist;
242 const videomode_t *Y_get_videomode_list_software (int fullscreen) {
243 init_videomode_list();
244 return &vlist;
247 void Y_set_fullscreen (int yes) {
248 if (window != NULL) {
249 SDL_SetWindowFullscreen(window, yes ? SDL_WINDOW_FULLSCREEN : 0);
253 int Y_get_fullscreen (void) {
254 return (window != NULL) && (SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN);
257 void Y_swap_buffers (void) {
258 assert(window != NULL);
259 assert(context != NULL);
260 SDL_GL_SwapWindow(window);
263 void Y_get_buffer (byte **buf, int *w, int *h, int *pitch) {
264 assert(window != NULL);
265 assert(surf != NULL);
266 *buf = surf->pixels;
267 *w = surf->w;
268 *h = surf->h;
269 *pitch = surf->pitch;
272 void Y_set_vga_palette (byte *vgapal) {
273 assert(window != NULL);
274 assert(surf != NULL);
275 int i;
276 byte *p = vgapal;
277 SDL_Color colors[256];
278 for (i = 0; i < 256; i++) {
279 colors[i] = (SDL_Color) {
280 .r = p[0] * 255 / 63,
281 .g = p[1] * 255 / 63,
282 .b = p[2] * 255 / 63
283 };
284 p += 3;
286 SDL_SetPaletteColors(surf->format->palette, colors, 0, 256);
289 void Y_repaint_rect (int x, int y, int w, int h) {
290 assert(window != NULL);
291 assert(surf != NULL);
292 SDL_Surface *s = SDL_GetWindowSurface(window);
293 SDL_Rect r = (SDL_Rect) {
294 .x = x,
295 .y = y,
296 .w = w,
297 .h = h
298 };
299 SDL_BlitSurface(surf, &r, s, &r);
300 SDL_UpdateWindowSurfaceRects(window, &r, 1);
303 void Y_repaint (void) {
304 Y_repaint_rect(0, 0, surf->w, surf->h);
307 void Y_enable_text_input (void) {
308 SDL_StartTextInput();
311 void Y_disable_text_input (void) {
312 SDL_StopTextInput();
315 /* --- main --- */
317 static int sdl_to_key (int code) {
318 switch (code) {
319 case SDL_SCANCODE_0: return KEY_0;
320 case SDL_SCANCODE_1: return KEY_1;
321 case SDL_SCANCODE_2: return KEY_2;
322 case SDL_SCANCODE_3: return KEY_3;
323 case SDL_SCANCODE_4: return KEY_4;
324 case SDL_SCANCODE_5: return KEY_5;
325 case SDL_SCANCODE_6: return KEY_6;
326 case SDL_SCANCODE_7: return KEY_7;
327 case SDL_SCANCODE_8: return KEY_8;
328 case SDL_SCANCODE_9: return KEY_9;
329 case SDL_SCANCODE_A: return KEY_A;
330 case SDL_SCANCODE_B: return KEY_B;
331 case SDL_SCANCODE_C: return KEY_C;
332 case SDL_SCANCODE_D: return KEY_D;
333 case SDL_SCANCODE_E: return KEY_E;
334 case SDL_SCANCODE_F: return KEY_F;
335 case SDL_SCANCODE_G: return KEY_G;
336 case SDL_SCANCODE_H: return KEY_H;
337 case SDL_SCANCODE_I: return KEY_I;
338 case SDL_SCANCODE_J: return KEY_J;
339 case SDL_SCANCODE_K: return KEY_K;
340 case SDL_SCANCODE_L: return KEY_L;
341 case SDL_SCANCODE_M: return KEY_M;
342 case SDL_SCANCODE_N: return KEY_N;
343 case SDL_SCANCODE_O: return KEY_O;
344 case SDL_SCANCODE_P: return KEY_P;
345 case SDL_SCANCODE_Q: return KEY_Q;
346 case SDL_SCANCODE_R: return KEY_R;
347 case SDL_SCANCODE_S: return KEY_S;
348 case SDL_SCANCODE_T: return KEY_T;
349 case SDL_SCANCODE_U: return KEY_U;
350 case SDL_SCANCODE_V: return KEY_V;
351 case SDL_SCANCODE_W: return KEY_W;
352 case SDL_SCANCODE_X: return KEY_X;
353 case SDL_SCANCODE_Y: return KEY_Y;
354 case SDL_SCANCODE_Z: return KEY_Z;
355 case SDL_SCANCODE_RETURN: return KEY_RETURN;
356 case SDL_SCANCODE_ESCAPE: return KEY_ESCAPE;
357 case SDL_SCANCODE_BACKSPACE: return KEY_BACKSPACE;
358 case SDL_SCANCODE_TAB: return KEY_TAB;
359 case SDL_SCANCODE_SPACE: return KEY_SPACE;
360 case SDL_SCANCODE_MINUS: return KEY_MINUS;
361 case SDL_SCANCODE_EQUALS: return KEY_EQUALS;
362 case SDL_SCANCODE_LEFTBRACKET: return KEY_LEFTBRACKET;
363 case SDL_SCANCODE_RIGHTBRACKET: return KEY_RIGHTBRACKET;
364 case SDL_SCANCODE_BACKSLASH: return KEY_BACKSLASH;
365 case SDL_SCANCODE_SEMICOLON: return KEY_SEMICOLON;
366 case SDL_SCANCODE_APOSTROPHE: return KEY_APOSTROPHE;
367 case SDL_SCANCODE_GRAVE: return KEY_GRAVE;
368 case SDL_SCANCODE_COMMA: return KEY_COMMA;
369 case SDL_SCANCODE_PERIOD: return KEY_PERIOD;
370 case SDL_SCANCODE_SLASH: return KEY_SLASH;
371 case SDL_SCANCODE_CAPSLOCK: return KEY_CAPSLOCK;
372 case SDL_SCANCODE_F1: return KEY_F1;
373 case SDL_SCANCODE_F2: return KEY_F2;
374 case SDL_SCANCODE_F3: return KEY_F3;
375 case SDL_SCANCODE_F4: return KEY_F4;
376 case SDL_SCANCODE_F5: return KEY_F5;
377 case SDL_SCANCODE_F6: return KEY_F6;
378 case SDL_SCANCODE_F7: return KEY_F7;
379 case SDL_SCANCODE_F8: return KEY_F8;
380 case SDL_SCANCODE_F9: return KEY_F9;
381 case SDL_SCANCODE_F10: return KEY_F10;
382 case SDL_SCANCODE_F11: return KEY_F11;
383 case SDL_SCANCODE_F12: return KEY_F12;
384 case SDL_SCANCODE_PRINTSCREEN: return KEY_PRINTSCREEN;
385 case SDL_SCANCODE_SCROLLLOCK: return KEY_SCROLLLOCK;
386 case SDL_SCANCODE_PAUSE: return KEY_PAUSE;
387 case SDL_SCANCODE_INSERT: return KEY_INSERT;
388 case SDL_SCANCODE_HOME: return KEY_HOME;
389 case SDL_SCANCODE_PAGEUP: return KEY_PAGEUP;
390 case SDL_SCANCODE_DELETE: return KEY_DELETE;
391 case SDL_SCANCODE_END: return KEY_END;
392 case SDL_SCANCODE_PAGEDOWN: return KEY_PAGEDOWN;
393 case SDL_SCANCODE_RIGHT: return KEY_RIGHT;
394 case SDL_SCANCODE_LEFT: return KEY_LEFT;
395 case SDL_SCANCODE_DOWN: return KEY_DOWN;
396 case SDL_SCANCODE_UP: return KEY_UP;
397 case SDL_SCANCODE_NUMLOCKCLEAR: return KEY_NUMLOCK;
398 case SDL_SCANCODE_KP_DIVIDE: return KEY_KP_DIVIDE;
399 case SDL_SCANCODE_KP_MULTIPLY: return KEY_KP_MULTIPLY;
400 case SDL_SCANCODE_KP_MINUS: return KEY_KP_MINUS;
401 case SDL_SCANCODE_KP_PLUS: return KEY_KP_PLUS;
402 case SDL_SCANCODE_KP_ENTER: return KEY_KP_ENTER;
403 case SDL_SCANCODE_KP_0: return KEY_KP_0;
404 case SDL_SCANCODE_KP_1: return KEY_KP_1;
405 case SDL_SCANCODE_KP_2: return KEY_KP_2;
406 case SDL_SCANCODE_KP_3: return KEY_KP_3;
407 case SDL_SCANCODE_KP_4: return KEY_KP_4;
408 case SDL_SCANCODE_KP_5: return KEY_KP_5;
409 case SDL_SCANCODE_KP_6: return KEY_KP_6;
410 case SDL_SCANCODE_KP_7: return KEY_KP_7;
411 case SDL_SCANCODE_KP_8: return KEY_KP_8;
412 case SDL_SCANCODE_KP_9: return KEY_KP_9;
413 case SDL_SCANCODE_KP_PERIOD: return KEY_KP_PERIOD;
414 case SDL_SCANCODE_SYSREQ: return KEY_SYSREQ;
415 case SDL_SCANCODE_LCTRL: return KEY_LCTRL;
416 case SDL_SCANCODE_LSHIFT: return KEY_LSHIFT;
417 case SDL_SCANCODE_LALT: return KEY_LALT;
418 case SDL_SCANCODE_LGUI: return KEY_LSUPER;
419 case SDL_SCANCODE_RCTRL: return KEY_RCTRL;
420 case SDL_SCANCODE_RSHIFT: return KEY_RSHIFT;
421 case SDL_SCANCODE_RALT: return KEY_RALT;
422 case SDL_SCANCODE_RGUI: return KEY_RSUPER;
423 default: return KEY_UNKNOWN;
427 static void window_event_handler (SDL_WindowEvent *ev) {
428 switch (ev->event) {
429 case SDL_WINDOWEVENT_RESIZED:
430 R_set_videomode(ev->data1, ev->data2, Y_get_fullscreen());
431 break;
432 case SDL_WINDOWEVENT_CLOSE:
433 ERR_quit();
434 break;
438 static int utf8_to_wchar (char *x) {
439 int i = 0;
440 byte *s = (byte*)x;
441 if (s[0] < 0x80) {
442 return s[0];
443 } else if (s[0] < 0xE0) {
444 if (s[0] - 192 >= 0 && s[1] >= 0x80 && s[1] < 0xE0) {
445 i = (s[0] - 192) * 64 + s[1] - 128;
447 } else if (s[0] < 0xF0) {
448 if (s[1] >= 0x80 && s[1] < 0xE0 && s[2] >= 0x80 && s[2] < 0xE0) {
449 i = ((s[0] - 224) * 64 + s[1] - 128) * 64 + s[2] - 128;
452 return i;
455 static int wchar_to_cp866 (int uch) {
456 if (uch <= 0x7f) {
457 return uch;
458 } else if (uch >= 0x410 && uch <= 0x43f) {
459 return uch - 0x410 + 0x80;
460 } else if (uch >= 0x440 && uch <= 0x44f) {
461 return uch - 0x440 + 0xe0;
462 } else {
463 switch (uch) {
464 // TODO graphics from 0xb0..0xdf
465 case 0x401: return 0xf0;
466 case 0x451: return 0xf1;
467 case 0x404: return 0xf2;
468 case 0x454: return 0xf3;
469 case 0x407: return 0xf4;
470 case 0x457: return 0xf5;
471 case 0x40e: return 0xf6;
472 case 0x45e: return 0xf7;
473 case 0xb0: return 0xf8;
474 case 0x2219: return 0xf9;
475 case 0xb7: return 0xfa;
476 case 0x221a: return 0xfb;
477 case 0x2116: return 0xfc;
478 case 0xa4: return 0xfd;
479 case 0x25a0: return 0xfe;
480 case 0xa0: return 0xff;
481 default: return 0; // unknown
486 static void poll_events (void) {
487 int key, down, uch, ch;
488 SDL_Event ev;
489 while (SDL_PollEvent(&ev)) {
490 switch (ev.type) {
491 case SDL_QUIT:
492 ERR_quit();
493 break;
494 case SDL_WINDOWEVENT:
495 if (ev.window.windowID == SDL_GetWindowID(window)) {
496 window_event_handler(&ev.window);
498 break;
499 case SDL_KEYDOWN:
500 case SDL_KEYUP:
501 down = ev.type == SDL_KEYDOWN;
502 key = sdl_to_key(ev.key.keysym.scancode);
503 I_press(key, down);
504 GM_key(key, down);
505 break;
506 case SDL_TEXTINPUT:
507 uch = utf8_to_wchar(ev.text.text);
508 ch = wchar_to_cp866(uch);
509 GM_input(ch);
510 break;
515 static void step (void) {
516 poll_events();
517 S_updatemusic();
518 Uint32 t = SDL_GetTicks();
519 if (t - ticks > DELAY) {
520 ticks = t;
521 G_act();
523 R_draw();
526 int main (int argc, char **argv) {
527 char *pw;
528 logo("system: initialize SDL2\n");
529 if (SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO | SDL_INIT_EVENTS) == -1) {
530 logo("system: failed to init SDL2: %s\n", SDL_GetError());
531 return 1;
533 // Player 1 defaults
534 pl1.ku = KEY_KP_8;
535 pl1.kd = KEY_KP_5;
536 pl1.kl = KEY_KP_4;
537 pl1.kr = KEY_KP_6;
538 pl1.kf = KEY_PAGEDOWN;
539 pl1.kj = KEY_DELETE;
540 pl1.kwl = KEY_HOME;
541 pl1.kwr = KEY_END;
542 pl1.kp = KEY_KP_8;
543 // Player 2 defaults
544 pl2.ku = KEY_E;
545 pl2.kd = KEY_D;
546 pl2.kl = KEY_S;
547 pl2.kr = KEY_F;
548 pl2.kf = KEY_A;
549 pl2.kj = KEY_Q;
550 pl2.kwl = KEY_1;
551 pl2.kwr = KEY_2;
552 pl2.kp = KEY_E;
553 srand(SDL_GetTicks());
554 F_startup();
555 #ifndef WIN32
556 pw = "/usr/share/doom2d-rembo/doom2d.wad";
557 #else
558 pw = "doom2d.wad";
559 #endif
560 if (fexists(pw)) {
561 F_addwad(pw);
562 } else {
563 F_addwad("doom2d.wad");
565 CFG_args(argc, argv);
566 CFG_load();
567 F_initwads();
568 M_startup();
569 F_allocres();
570 S_init();
571 S_initmusic();
572 R_init();
573 G_init();
574 ticks = SDL_GetTicks();
575 #ifdef __EMSCRIPTEN__
576 emscripten_set_main_loop(step, 0, 1);
577 #else
578 while (!quit) {
579 step();
581 #endif
582 CFG_save();
583 R_done();
584 S_donemusic();
585 S_done();
586 M_shutdown();
587 SDL_Quit();
588 return 0;