DEADSOFTWARE

67387dba6b6d8bf2cabfc2e1382ceeb00628e2fb
[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 "cp866.h"
16 #include "my.h" // fexists
17 #include "player.h" // pl1 pl2
18 #include "menu.h" // G_keyf
19 #include "error.h" // logo
21 #include "files.h" // F_startup F_addwad F_initwads F_allocres
22 #include "config.h" // CFG_args CFG_load CFG_save
23 #include "memory.h" // M_startup
24 #include "game.h" // G_init G_act
25 #include "sound.h" // S_init S_done
26 #include "music.h" // S_initmusic S_updatemusic S_donemusic
27 #include "render.h" // R_init R_draw R_done
29 #define TITLE_STR "Doom 2D (SDL2)"
31 static Uint32 ticks;
32 static int quit = 0;
33 static SDL_Window *window;
34 static SDL_GLContext context;
35 static SDL_Surface *surf;
36 static videomode_t vlist;
38 /* --- error.h --- */
40 void logo (const char *s, ...) {
41 va_list ap;
42 va_start(ap, s);
43 vprintf(s, ap);
44 va_end(ap);
45 fflush(stdout);
46 }
48 void logo_gas (int cur, int all) {
49 // stub
50 }
52 void ERR_failinit (char *s, ...) {
53 va_list ap;
54 va_start(ap, s);
55 vprintf(s, ap);
56 va_end(ap);
57 puts("");
58 exit(1);
59 }
61 void ERR_fatal (char *s, ...) {
62 va_list ap;
63 R_done();
64 S_done();
65 S_donemusic();
66 M_shutdown();
67 SDL_Quit();
68 puts("\nКРИТИЧЕСКАЯ ОШИБКА:");
69 va_start(ap, s);
70 vprintf(s, ap);
71 va_end(ap);
72 puts("");
73 exit(1);
74 }
76 void ERR_quit (void) {
77 quit = 1;
78 }
80 /* --- system.h --- */
82 static int Y_resize_window (int w, int h, int fullscreen) {
83 assert(w > 0);
84 assert(h > 0);
85 assert(window != NULL);
86 if (surf != NULL) {
87 if (surf->w != w || surf->h != h) {
88 SDL_Surface *s = SDL_CreateRGBSurface(0, w, h, 8, 0, 0, 0, 0);
89 if (s != NULL) {
90 SDL_SetPaletteColors(s->format->palette, surf->format->palette->colors, 0, surf->format->palette->ncolors);
91 SDL_FreeSurface(surf);
92 surf = s;
93 }
94 }
95 }
96 SDL_SetWindowSize(window, w, h);
97 Y_set_fullscreen(fullscreen);
98 return 1;
99 }
101 int Y_set_videomode_opengl (int w, int h, int fullscreen) {
102 assert(w > 0);
103 assert(h > 0);
104 Uint32 flags;
105 SDL_Window *win;
106 SDL_GLContext ctx;
107 if (window != NULL && context != NULL) {
108 Y_resize_window(w, h, fullscreen);
109 win = window;
110 } else {
111 flags = SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL;
112 if (fullscreen) {
113 flags = flags | SDL_WINDOW_FULLSCREEN;
115 // TODO set context version and type
116 #ifdef __EMSCRIPTEN__
117 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
118 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
119 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
120 #else
121 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1);
122 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
123 #endif
124 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
125 win = SDL_CreateWindow(TITLE_STR, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, flags);
126 if (win != NULL) {
127 ctx = SDL_GL_CreateContext(win);
128 if (ctx != NULL) {
129 Y_unset_videomode();
130 window = win;
131 context = ctx;
132 SDL_GL_MakeCurrent(window, context);
133 } else {
134 SDL_DestroyWindow(win);
135 win = NULL;
139 if (win == NULL) {
140 logo("Y_set_videomode_opengl: error: %s\n", SDL_GetError());
142 return win != NULL;
145 int Y_set_videomode_software (int w, int h, int fullscreen) {
146 assert(w > 0);
147 assert(h > 0);
148 Uint32 flags;
149 SDL_Surface *s;
150 SDL_Window *win;
151 if (window != NULL && surf != NULL) {
152 Y_resize_window(w, h, fullscreen);
153 win = window;
154 } else {
155 flags = SDL_WINDOW_RESIZABLE;
156 if (fullscreen) {
157 flags = flags | SDL_WINDOW_FULLSCREEN;
159 win = SDL_CreateWindow(TITLE_STR, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, flags);
160 if (win != NULL) {
161 s = SDL_CreateRGBSurface(0, w, h, 8, 0, 0, 0, 0);
162 if (s != NULL) {
163 Y_unset_videomode();
164 window = win;
165 surf = s;
166 } else {
167 SDL_DestroyWindow(win);
168 win = NULL;
172 if (win == NULL) {
173 logo("Y_set_videomode_software: error: %s\n", SDL_GetError());
175 return win != NULL;
178 void Y_get_videomode (int *w, int *h) {
179 if (window != NULL) {
180 SDL_GetWindowSize(window, w, h);
181 } else {
182 *w = 0;
183 *h = 0;
187 int Y_videomode_setted (void) {
188 return window != NULL;
191 void Y_unset_videomode (void) {
192 if (window != NULL) {
193 if (context != NULL) {
194 SDL_GL_MakeCurrent(window, NULL);
195 SDL_GL_DeleteContext(context);
196 context = NULL;
198 if (surf != NULL) {
199 SDL_FreeSurface(surf);
200 surf = NULL;
202 SDL_DestroyWindow(window);
203 window = NULL;
207 static void init_videomode_list (void) {
208 int i, j, k;
209 SDL_DisplayMode m;
210 int n = SDL_GetNumDisplayModes(0);
211 if (vlist.modes != NULL) {
212 free(vlist.modes);
213 vlist.modes = NULL;
214 vlist.n = 0;
216 if (n > 0) {
217 vlist.modes = malloc(n * sizeof(videomode_size_t));
218 if (vlist.modes != NULL) {
219 j = 0;
220 for (i = 0; i < n; i++) {
221 SDL_GetDisplayMode(0, i, &m);
222 k = 0;
223 while (k < j && (m.w != vlist.modes[k].w || m.h != vlist.modes[k].h)) {
224 k++;
226 if (k >= j) {
227 vlist.modes[j] = (videomode_size_t) {
228 .w = m.w,
229 .h = m.h
230 };
231 j++;
234 vlist.n = j;
239 const videomode_t *Y_get_videomode_list_opengl (int fullscreen) {
240 init_videomode_list();
241 return &vlist;
244 const videomode_t *Y_get_videomode_list_software (int fullscreen) {
245 init_videomode_list();
246 return &vlist;
249 void Y_set_fullscreen (int yes) {
250 if (window != NULL) {
251 SDL_SetWindowFullscreen(window, yes ? SDL_WINDOW_FULLSCREEN : 0);
255 int Y_get_fullscreen (void) {
256 return (window != NULL) && (SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN);
259 void Y_swap_buffers (void) {
260 assert(window != NULL);
261 assert(context != NULL);
262 SDL_GL_SwapWindow(window);
265 void Y_get_buffer (byte **buf, int *w, int *h, int *pitch) {
266 assert(window != NULL);
267 assert(surf != NULL);
268 *buf = surf->pixels;
269 *w = surf->w;
270 *h = surf->h;
271 *pitch = surf->pitch;
274 void Y_set_vga_palette (byte *vgapal) {
275 assert(window != NULL);
276 assert(surf != NULL);
277 int i;
278 byte *p = vgapal;
279 SDL_Color colors[256];
280 for (i = 0; i < 256; i++) {
281 colors[i] = (SDL_Color) {
282 .r = p[0] * 255 / 63,
283 .g = p[1] * 255 / 63,
284 .b = p[2] * 255 / 63
285 };
286 p += 3;
288 SDL_SetPaletteColors(surf->format->palette, colors, 0, 256);
291 void Y_repaint_rect (int x, int y, int w, int h) {
292 assert(window != NULL);
293 assert(surf != NULL);
294 SDL_Surface *s = SDL_GetWindowSurface(window);
295 SDL_Rect r = (SDL_Rect) {
296 .x = x,
297 .y = y,
298 .w = w,
299 .h = h
300 };
301 SDL_BlitSurface(surf, &r, s, &r);
302 SDL_UpdateWindowSurfaceRects(window, &r, 1);
305 void Y_repaint (void) {
306 Y_repaint_rect(0, 0, surf->w, surf->h);
309 void Y_enable_text_input (void) {
310 SDL_StartTextInput();
313 void Y_disable_text_input (void) {
314 SDL_StopTextInput();
317 /* --- main --- */
319 static int sdl_to_key (int code) {
320 switch (code) {
321 case SDL_SCANCODE_0: return KEY_0;
322 case SDL_SCANCODE_1: return KEY_1;
323 case SDL_SCANCODE_2: return KEY_2;
324 case SDL_SCANCODE_3: return KEY_3;
325 case SDL_SCANCODE_4: return KEY_4;
326 case SDL_SCANCODE_5: return KEY_5;
327 case SDL_SCANCODE_6: return KEY_6;
328 case SDL_SCANCODE_7: return KEY_7;
329 case SDL_SCANCODE_8: return KEY_8;
330 case SDL_SCANCODE_9: return KEY_9;
331 case SDL_SCANCODE_A: return KEY_A;
332 case SDL_SCANCODE_B: return KEY_B;
333 case SDL_SCANCODE_C: return KEY_C;
334 case SDL_SCANCODE_D: return KEY_D;
335 case SDL_SCANCODE_E: return KEY_E;
336 case SDL_SCANCODE_F: return KEY_F;
337 case SDL_SCANCODE_G: return KEY_G;
338 case SDL_SCANCODE_H: return KEY_H;
339 case SDL_SCANCODE_I: return KEY_I;
340 case SDL_SCANCODE_J: return KEY_J;
341 case SDL_SCANCODE_K: return KEY_K;
342 case SDL_SCANCODE_L: return KEY_L;
343 case SDL_SCANCODE_M: return KEY_M;
344 case SDL_SCANCODE_N: return KEY_N;
345 case SDL_SCANCODE_O: return KEY_O;
346 case SDL_SCANCODE_P: return KEY_P;
347 case SDL_SCANCODE_Q: return KEY_Q;
348 case SDL_SCANCODE_R: return KEY_R;
349 case SDL_SCANCODE_S: return KEY_S;
350 case SDL_SCANCODE_T: return KEY_T;
351 case SDL_SCANCODE_U: return KEY_U;
352 case SDL_SCANCODE_V: return KEY_V;
353 case SDL_SCANCODE_W: return KEY_W;
354 case SDL_SCANCODE_X: return KEY_X;
355 case SDL_SCANCODE_Y: return KEY_Y;
356 case SDL_SCANCODE_Z: return KEY_Z;
357 case SDL_SCANCODE_RETURN: return KEY_RETURN;
358 case SDL_SCANCODE_ESCAPE: return KEY_ESCAPE;
359 case SDL_SCANCODE_BACKSPACE: return KEY_BACKSPACE;
360 case SDL_SCANCODE_TAB: return KEY_TAB;
361 case SDL_SCANCODE_SPACE: return KEY_SPACE;
362 case SDL_SCANCODE_MINUS: return KEY_MINUS;
363 case SDL_SCANCODE_EQUALS: return KEY_EQUALS;
364 case SDL_SCANCODE_LEFTBRACKET: return KEY_LEFTBRACKET;
365 case SDL_SCANCODE_RIGHTBRACKET: return KEY_RIGHTBRACKET;
366 case SDL_SCANCODE_BACKSLASH: return KEY_BACKSLASH;
367 case SDL_SCANCODE_SEMICOLON: return KEY_SEMICOLON;
368 case SDL_SCANCODE_APOSTROPHE: return KEY_APOSTROPHE;
369 case SDL_SCANCODE_GRAVE: return KEY_GRAVE;
370 case SDL_SCANCODE_COMMA: return KEY_COMMA;
371 case SDL_SCANCODE_PERIOD: return KEY_PERIOD;
372 case SDL_SCANCODE_SLASH: return KEY_SLASH;
373 case SDL_SCANCODE_CAPSLOCK: return KEY_CAPSLOCK;
374 case SDL_SCANCODE_F1: return KEY_F1;
375 case SDL_SCANCODE_F2: return KEY_F2;
376 case SDL_SCANCODE_F3: return KEY_F3;
377 case SDL_SCANCODE_F4: return KEY_F4;
378 case SDL_SCANCODE_F5: return KEY_F5;
379 case SDL_SCANCODE_F6: return KEY_F6;
380 case SDL_SCANCODE_F7: return KEY_F7;
381 case SDL_SCANCODE_F8: return KEY_F8;
382 case SDL_SCANCODE_F9: return KEY_F9;
383 case SDL_SCANCODE_F10: return KEY_F10;
384 case SDL_SCANCODE_F11: return KEY_F11;
385 case SDL_SCANCODE_F12: return KEY_F12;
386 case SDL_SCANCODE_PRINTSCREEN: return KEY_PRINTSCREEN;
387 case SDL_SCANCODE_SCROLLLOCK: return KEY_SCROLLLOCK;
388 case SDL_SCANCODE_PAUSE: return KEY_PAUSE;
389 case SDL_SCANCODE_INSERT: return KEY_INSERT;
390 case SDL_SCANCODE_HOME: return KEY_HOME;
391 case SDL_SCANCODE_PAGEUP: return KEY_PAGEUP;
392 case SDL_SCANCODE_DELETE: return KEY_DELETE;
393 case SDL_SCANCODE_END: return KEY_END;
394 case SDL_SCANCODE_PAGEDOWN: return KEY_PAGEDOWN;
395 case SDL_SCANCODE_RIGHT: return KEY_RIGHT;
396 case SDL_SCANCODE_LEFT: return KEY_LEFT;
397 case SDL_SCANCODE_DOWN: return KEY_DOWN;
398 case SDL_SCANCODE_UP: return KEY_UP;
399 case SDL_SCANCODE_NUMLOCKCLEAR: return KEY_NUMLOCK;
400 case SDL_SCANCODE_KP_DIVIDE: return KEY_KP_DIVIDE;
401 case SDL_SCANCODE_KP_MULTIPLY: return KEY_KP_MULTIPLY;
402 case SDL_SCANCODE_KP_MINUS: return KEY_KP_MINUS;
403 case SDL_SCANCODE_KP_PLUS: return KEY_KP_PLUS;
404 case SDL_SCANCODE_KP_ENTER: return KEY_KP_ENTER;
405 case SDL_SCANCODE_KP_0: return KEY_KP_0;
406 case SDL_SCANCODE_KP_1: return KEY_KP_1;
407 case SDL_SCANCODE_KP_2: return KEY_KP_2;
408 case SDL_SCANCODE_KP_3: return KEY_KP_3;
409 case SDL_SCANCODE_KP_4: return KEY_KP_4;
410 case SDL_SCANCODE_KP_5: return KEY_KP_5;
411 case SDL_SCANCODE_KP_6: return KEY_KP_6;
412 case SDL_SCANCODE_KP_7: return KEY_KP_7;
413 case SDL_SCANCODE_KP_8: return KEY_KP_8;
414 case SDL_SCANCODE_KP_9: return KEY_KP_9;
415 case SDL_SCANCODE_KP_PERIOD: return KEY_KP_PERIOD;
416 case SDL_SCANCODE_SYSREQ: return KEY_SYSREQ;
417 case SDL_SCANCODE_LCTRL: return KEY_LCTRL;
418 case SDL_SCANCODE_LSHIFT: return KEY_LSHIFT;
419 case SDL_SCANCODE_LALT: return KEY_LALT;
420 case SDL_SCANCODE_LGUI: return KEY_LSUPER;
421 case SDL_SCANCODE_RCTRL: return KEY_RCTRL;
422 case SDL_SCANCODE_RSHIFT: return KEY_RSHIFT;
423 case SDL_SCANCODE_RALT: return KEY_RALT;
424 case SDL_SCANCODE_RGUI: return KEY_RSUPER;
425 default: return KEY_UNKNOWN;
429 static void window_event_handler (SDL_WindowEvent *ev) {
430 switch (ev->event) {
431 case SDL_WINDOWEVENT_RESIZED:
432 R_set_videomode(ev->data1, ev->data2, Y_get_fullscreen());
433 break;
434 case SDL_WINDOWEVENT_CLOSE:
435 ERR_quit();
436 break;
440 static int utf8_to_wchar (char *x) {
441 int i = 0;
442 byte *s = (byte*)x;
443 if (s[0] < 0x80) {
444 return s[0];
445 } else if (s[0] < 0xE0) {
446 if (s[0] - 192 >= 0 && s[1] >= 0x80 && s[1] < 0xE0) {
447 i = (s[0] - 192) * 64 + s[1] - 128;
449 } else if (s[0] < 0xF0) {
450 if (s[1] >= 0x80 && s[1] < 0xE0 && s[2] >= 0x80 && s[2] < 0xE0) {
451 i = ((s[0] - 224) * 64 + s[1] - 128) * 64 + s[2] - 128;
454 return i;
457 static void poll_events (void) {
458 int key, down, uch, ch;
459 SDL_Event ev;
460 while (SDL_PollEvent(&ev)) {
461 switch (ev.type) {
462 case SDL_QUIT:
463 ERR_quit();
464 break;
465 case SDL_WINDOWEVENT:
466 if (ev.window.windowID == SDL_GetWindowID(window)) {
467 window_event_handler(&ev.window);
469 break;
470 case SDL_KEYDOWN:
471 case SDL_KEYUP:
472 down = ev.type == SDL_KEYDOWN;
473 key = sdl_to_key(ev.key.keysym.scancode);
474 I_press(key, down);
475 GM_key(key, down);
476 break;
477 case SDL_TEXTINPUT:
478 uch = utf8_to_wchar(ev.text.text);
479 ch = cp866_utoc(uch);
480 if (ch >= 0) {
481 GM_input(ch);
483 break;
488 static void step (void) {
489 poll_events();
490 S_updatemusic();
491 Uint32 t = SDL_GetTicks();
492 if (t - ticks > DELAY) {
493 ticks = t;
494 G_act();
496 R_draw();
499 int main (int argc, char **argv) {
500 char *pw;
501 logo("system: initialize SDL2\n");
502 if (SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO | SDL_INIT_EVENTS) == -1) {
503 logo("system: failed to init SDL2: %s\n", SDL_GetError());
504 return 1;
506 // Player 1 defaults
507 pl1.ku = KEY_KP_8;
508 pl1.kd = KEY_KP_5;
509 pl1.kl = KEY_KP_4;
510 pl1.kr = KEY_KP_6;
511 pl1.kf = KEY_PAGEDOWN;
512 pl1.kj = KEY_DELETE;
513 pl1.kwl = KEY_HOME;
514 pl1.kwr = KEY_END;
515 pl1.kp = KEY_KP_8;
516 // Player 2 defaults
517 pl2.ku = KEY_E;
518 pl2.kd = KEY_D;
519 pl2.kl = KEY_S;
520 pl2.kr = KEY_F;
521 pl2.kf = KEY_A;
522 pl2.kj = KEY_Q;
523 pl2.kwl = KEY_1;
524 pl2.kwr = KEY_2;
525 pl2.kp = KEY_E;
526 srand(SDL_GetTicks());
527 F_startup();
528 #ifndef WIN32
529 pw = "/usr/share/doom2d-rembo/doom2d.wad";
530 #else
531 pw = "doom2d.wad";
532 #endif
533 if (fexists(pw)) {
534 F_addwad(pw);
535 } else {
536 F_addwad("doom2d.wad");
538 CFG_args(argc, argv);
539 CFG_load();
540 F_initwads();
541 M_startup();
542 F_allocres();
543 S_init();
544 S_initmusic();
545 R_init();
546 G_init();
547 ticks = SDL_GetTicks();
548 #ifdef __EMSCRIPTEN__
549 emscripten_set_main_loop(step, 0, 1);
550 #else
551 while (!quit) {
552 step();
554 #endif
555 CFG_save();
556 R_done();
557 S_donemusic();
558 S_done();
559 M_shutdown();
560 SDL_Quit();
561 return 0;