1 /* Copyright (C) 2020 SovietPony
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, version 3 of the License ONLY.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 # include <emscripten.h>
23 #include <stdlib.h> // srand exit
24 #include <string.h> // strcasecmp
31 #include "my.h" // fexists
32 #include "player.h" // pl1 pl2
33 #include "menu.h" // G_keyf
34 #include "error.h" // logo
35 #include "monster.h" // nomon
37 #include "files.h" // F_startup F_addwad F_initwads F_allocres
38 #include "config.h" // CFG_args CFG_load CFG_save
39 #include "args.h" // ARG_parse
40 #include "memory.h" // M_startup
41 #include "game.h" // G_init G_act
42 #include "sound.h" // S_init S_done
43 #include "music.h" // S_initmusic S_updatemusic S_donemusic
44 #include "render.h" // R_init R_draw R_done
46 #define TITLE_STR "Doom 2D (SDL2)"
50 static SDL_Window
*window
;
51 static SDL_GLContext context
;
52 static SDL_Surface
*surf
;
53 static videomode_t vlist
;
55 static const cfg_t arg
[] = {
56 {"file", NULL
, Y_FILES
},
57 {"cheat", &cheat
, Y_SW_ON
},
58 // {"vga", &shot_vga, Y_SW_ON},
59 // {"musvol", &mus_vol, Y_WORD},
60 {"mon", &nomon
, Y_SW_OFF
},
61 {"warp", &_warp
, Y_BYTE
},
62 // {"config", NULL, cfg_file, Y_STRING},
63 {NULL
, NULL
, 0} // end
66 static const cfg_t cfg
[] = {
67 // {"screenshot", &shot_vga, Y_SW_ON},
68 // {"music_volume", &mus_vol, Y_WORD},
69 // {"music_random", &music_random, Y_SW_ON},
70 // {"music_time", &music_time, Y_DWORD},
71 // {"music_fade", &music_fade, Y_DWORD},
72 {"pl1_left", &pl1
.kl
, Y_KEY
},
73 {"pl1_right",&pl1
.kr
, Y_KEY
},
74 {"pl1_up", &pl1
.ku
, Y_KEY
},
75 {"pl1_down", &pl1
.kd
, Y_KEY
},
76 {"pl1_jump", &pl1
.kj
, Y_KEY
},
77 {"pl1_fire", &pl1
.kf
, Y_KEY
},
78 {"pl1_next", &pl1
.kwr
, Y_KEY
},
79 {"pl1_prev", &pl1
.kwl
, Y_KEY
},
80 {"pl1_use", &pl1
.kp
, Y_KEY
},
81 {"pl2_left", &pl2
.kl
, Y_KEY
},
82 {"pl2_right", &pl2
.kr
, Y_KEY
},
83 {"pl2_up", &pl2
.ku
, Y_KEY
},
84 {"pl2_down", &pl2
.kd
, Y_KEY
},
85 {"pl2_jump", &pl2
.kj
, Y_KEY
},
86 {"pl2_fire", &pl2
.kf
, Y_KEY
},
87 {"pl2_next", &pl2
.kwr
, Y_KEY
},
88 {"pl2_prev", &pl2
.kwl
, Y_KEY
},
89 {"pl2_use", &pl2
.kp
, Y_KEY
},
90 {NULL
, NULL
, 0} // end
93 static void CFG_args (int argc
, char **argv
) {
94 const cfg_t
*list
[] = { arg
, R_args(), S_args(), MUS_args() };
95 ARG_parse(argc
, argv
, 4, list
);
98 static void CFG_load (void) {
99 const cfg_t
*list
[] = { cfg
, R_conf(), S_conf(), MUS_conf() };
100 CFG_read_config("default.cfg", 4, list
);
101 CFG_read_config("doom2d.cfg", 4, list
);
104 static void CFG_save (void) {
105 const cfg_t
*list
[] = { cfg
, R_conf(), S_conf(), MUS_conf() };
106 CFG_update_config("doom2d.cfg", "doom2d.cfg", 4, list
, "generated by doom2d, do not modify");
109 /* --- error.h --- */
111 void logo (const char *s
, ...) {
119 void logo_gas (int cur
, int all
) {
123 void ERR_failinit (char *s
, ...) {
132 void ERR_fatal (char *s
, ...) {
139 puts("\nCRITICAL ERROR:");
147 void ERR_quit (void) {
151 /* --- system.h --- */
153 static int Y_resize_window (int w
, int h
, int fullscreen
) {
156 assert(window
!= NULL
);
158 if (surf
->w
!= w
|| surf
->h
!= h
) {
159 SDL_Surface
*s
= SDL_CreateRGBSurface(0, w
, h
, 8, 0, 0, 0, 0);
161 SDL_SetPaletteColors(s
->format
->palette
, surf
->format
->palette
->colors
, 0, surf
->format
->palette
->ncolors
);
162 SDL_FreeSurface(surf
);
167 SDL_SetWindowSize(window
, w
, h
);
168 Y_set_fullscreen(fullscreen
);
172 int Y_set_videomode_opengl (int w
, int h
, int fullscreen
) {
178 if (window
!= NULL
&& context
!= NULL
) {
179 Y_resize_window(w
, h
, fullscreen
);
182 flags
= SDL_WINDOW_RESIZABLE
| SDL_WINDOW_OPENGL
;
184 flags
= flags
| SDL_WINDOW_FULLSCREEN
;
186 // TODO set context version and type
187 #ifdef __EMSCRIPTEN__
188 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK
, SDL_GL_CONTEXT_PROFILE_ES
);
189 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION
, 2);
190 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION
, 0);
192 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION
, 1);
193 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION
, 1);
195 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER
, 1);
196 win
= SDL_CreateWindow(TITLE_STR
, SDL_WINDOWPOS_CENTERED
, SDL_WINDOWPOS_CENTERED
, w
, h
, flags
);
198 ctx
= SDL_GL_CreateContext(win
);
203 SDL_GL_MakeCurrent(window
, context
);
205 SDL_DestroyWindow(win
);
211 logo("Y_set_videomode_opengl: error: %s\n", SDL_GetError());
216 int Y_set_videomode_software (int w
, int h
, int fullscreen
) {
222 if (window
!= NULL
&& surf
!= NULL
) {
223 Y_resize_window(w
, h
, fullscreen
);
226 flags
= SDL_WINDOW_RESIZABLE
;
228 flags
= flags
| SDL_WINDOW_FULLSCREEN
;
230 win
= SDL_CreateWindow(TITLE_STR
, SDL_WINDOWPOS_CENTERED
, SDL_WINDOWPOS_CENTERED
, w
, h
, flags
);
232 s
= SDL_CreateRGBSurface(0, w
, h
, 8, 0, 0, 0, 0);
238 SDL_DestroyWindow(win
);
244 logo("Y_set_videomode_software: error: %s\n", SDL_GetError());
249 void Y_get_videomode (int *w
, int *h
) {
250 if (window
!= NULL
) {
251 SDL_GetWindowSize(window
, w
, h
);
258 int Y_videomode_setted (void) {
259 return window
!= NULL
;
262 void Y_unset_videomode (void) {
263 if (window
!= NULL
) {
264 if (context
!= NULL
) {
265 SDL_GL_MakeCurrent(window
, NULL
);
266 SDL_GL_DeleteContext(context
);
270 SDL_FreeSurface(surf
);
273 SDL_DestroyWindow(window
);
278 static void init_videomode_list (void) {
281 int n
= SDL_GetNumDisplayModes(0);
282 if (vlist
.modes
!= NULL
) {
288 vlist
.modes
= malloc(n
* sizeof(videomode_size_t
));
289 if (vlist
.modes
!= NULL
) {
291 for (i
= 0; i
< n
; i
++) {
292 SDL_GetDisplayMode(0, i
, &m
);
294 while (k
< j
&& (m
.w
!= vlist
.modes
[k
].w
|| m
.h
!= vlist
.modes
[k
].h
)) {
298 vlist
.modes
[j
] = (videomode_size_t
) {
310 const videomode_t
*Y_get_videomode_list_opengl (int fullscreen
) {
311 init_videomode_list();
315 const videomode_t
*Y_get_videomode_list_software (int fullscreen
) {
316 init_videomode_list();
320 void Y_set_fullscreen (int yes
) {
321 if (window
!= NULL
) {
322 SDL_SetWindowFullscreen(window
, yes
? SDL_WINDOW_FULLSCREEN
: 0);
326 int Y_get_fullscreen (void) {
327 return (window
!= NULL
) && (SDL_GetWindowFlags(window
) & SDL_WINDOW_FULLSCREEN
);
330 void Y_swap_buffers (void) {
331 assert(window
!= NULL
);
332 assert(context
!= NULL
);
333 SDL_GL_SwapWindow(window
);
336 void Y_get_buffer (byte
**buf
, int *w
, int *h
, int *pitch
) {
337 assert(window
!= NULL
);
338 assert(surf
!= NULL
);
342 *pitch
= surf
->pitch
;
345 void Y_set_vga_palette (byte
*vgapal
) {
346 assert(window
!= NULL
);
347 assert(surf
!= NULL
);
350 SDL_Color colors
[256];
351 for (i
= 0; i
< 256; i
++) {
352 colors
[i
] = (SDL_Color
) {
353 .r
= p
[0] * 255 / 63,
354 .g
= p
[1] * 255 / 63,
359 SDL_SetPaletteColors(surf
->format
->palette
, colors
, 0, 256);
362 void Y_repaint_rect (int x
, int y
, int w
, int h
) {
363 assert(window
!= NULL
);
364 assert(surf
!= NULL
);
365 SDL_Surface
*s
= SDL_GetWindowSurface(window
);
366 SDL_Rect r
= (SDL_Rect
) {
372 SDL_BlitSurface(surf
, &r
, s
, &r
);
373 SDL_UpdateWindowSurfaceRects(window
, &r
, 1);
376 void Y_repaint (void) {
377 Y_repaint_rect(0, 0, surf
->w
, surf
->h
);
380 void Y_enable_text_input (void) {
381 SDL_StartTextInput();
384 void Y_disable_text_input (void) {
390 static int sdl_to_key (int code
) {
392 case SDL_SCANCODE_0
: return KEY_0
;
393 case SDL_SCANCODE_1
: return KEY_1
;
394 case SDL_SCANCODE_2
: return KEY_2
;
395 case SDL_SCANCODE_3
: return KEY_3
;
396 case SDL_SCANCODE_4
: return KEY_4
;
397 case SDL_SCANCODE_5
: return KEY_5
;
398 case SDL_SCANCODE_6
: return KEY_6
;
399 case SDL_SCANCODE_7
: return KEY_7
;
400 case SDL_SCANCODE_8
: return KEY_8
;
401 case SDL_SCANCODE_9
: return KEY_9
;
402 case SDL_SCANCODE_A
: return KEY_A
;
403 case SDL_SCANCODE_B
: return KEY_B
;
404 case SDL_SCANCODE_C
: return KEY_C
;
405 case SDL_SCANCODE_D
: return KEY_D
;
406 case SDL_SCANCODE_E
: return KEY_E
;
407 case SDL_SCANCODE_F
: return KEY_F
;
408 case SDL_SCANCODE_G
: return KEY_G
;
409 case SDL_SCANCODE_H
: return KEY_H
;
410 case SDL_SCANCODE_I
: return KEY_I
;
411 case SDL_SCANCODE_J
: return KEY_J
;
412 case SDL_SCANCODE_K
: return KEY_K
;
413 case SDL_SCANCODE_L
: return KEY_L
;
414 case SDL_SCANCODE_M
: return KEY_M
;
415 case SDL_SCANCODE_N
: return KEY_N
;
416 case SDL_SCANCODE_O
: return KEY_O
;
417 case SDL_SCANCODE_P
: return KEY_P
;
418 case SDL_SCANCODE_Q
: return KEY_Q
;
419 case SDL_SCANCODE_R
: return KEY_R
;
420 case SDL_SCANCODE_S
: return KEY_S
;
421 case SDL_SCANCODE_T
: return KEY_T
;
422 case SDL_SCANCODE_U
: return KEY_U
;
423 case SDL_SCANCODE_V
: return KEY_V
;
424 case SDL_SCANCODE_W
: return KEY_W
;
425 case SDL_SCANCODE_X
: return KEY_X
;
426 case SDL_SCANCODE_Y
: return KEY_Y
;
427 case SDL_SCANCODE_Z
: return KEY_Z
;
428 case SDL_SCANCODE_RETURN
: return KEY_RETURN
;
429 case SDL_SCANCODE_ESCAPE
: return KEY_ESCAPE
;
430 case SDL_SCANCODE_BACKSPACE
: return KEY_BACKSPACE
;
431 case SDL_SCANCODE_TAB
: return KEY_TAB
;
432 case SDL_SCANCODE_SPACE
: return KEY_SPACE
;
433 case SDL_SCANCODE_MINUS
: return KEY_MINUS
;
434 case SDL_SCANCODE_EQUALS
: return KEY_EQUALS
;
435 case SDL_SCANCODE_LEFTBRACKET
: return KEY_LEFTBRACKET
;
436 case SDL_SCANCODE_RIGHTBRACKET
: return KEY_RIGHTBRACKET
;
437 case SDL_SCANCODE_BACKSLASH
: return KEY_BACKSLASH
;
438 case SDL_SCANCODE_SEMICOLON
: return KEY_SEMICOLON
;
439 case SDL_SCANCODE_APOSTROPHE
: return KEY_APOSTROPHE
;
440 case SDL_SCANCODE_GRAVE
: return KEY_GRAVE
;
441 case SDL_SCANCODE_COMMA
: return KEY_COMMA
;
442 case SDL_SCANCODE_PERIOD
: return KEY_PERIOD
;
443 case SDL_SCANCODE_SLASH
: return KEY_SLASH
;
444 case SDL_SCANCODE_CAPSLOCK
: return KEY_CAPSLOCK
;
445 case SDL_SCANCODE_F1
: return KEY_F1
;
446 case SDL_SCANCODE_F2
: return KEY_F2
;
447 case SDL_SCANCODE_F3
: return KEY_F3
;
448 case SDL_SCANCODE_F4
: return KEY_F4
;
449 case SDL_SCANCODE_F5
: return KEY_F5
;
450 case SDL_SCANCODE_F6
: return KEY_F6
;
451 case SDL_SCANCODE_F7
: return KEY_F7
;
452 case SDL_SCANCODE_F8
: return KEY_F8
;
453 case SDL_SCANCODE_F9
: return KEY_F9
;
454 case SDL_SCANCODE_F10
: return KEY_F10
;
455 case SDL_SCANCODE_F11
: return KEY_F11
;
456 case SDL_SCANCODE_F12
: return KEY_F12
;
457 case SDL_SCANCODE_PRINTSCREEN
: return KEY_PRINTSCREEN
;
458 case SDL_SCANCODE_SCROLLLOCK
: return KEY_SCROLLLOCK
;
459 case SDL_SCANCODE_PAUSE
: return KEY_PAUSE
;
460 case SDL_SCANCODE_INSERT
: return KEY_INSERT
;
461 case SDL_SCANCODE_HOME
: return KEY_HOME
;
462 case SDL_SCANCODE_PAGEUP
: return KEY_PAGEUP
;
463 case SDL_SCANCODE_DELETE
: return KEY_DELETE
;
464 case SDL_SCANCODE_END
: return KEY_END
;
465 case SDL_SCANCODE_PAGEDOWN
: return KEY_PAGEDOWN
;
466 case SDL_SCANCODE_RIGHT
: return KEY_RIGHT
;
467 case SDL_SCANCODE_LEFT
: return KEY_LEFT
;
468 case SDL_SCANCODE_DOWN
: return KEY_DOWN
;
469 case SDL_SCANCODE_UP
: return KEY_UP
;
470 case SDL_SCANCODE_NUMLOCKCLEAR
: return KEY_NUMLOCK
;
471 case SDL_SCANCODE_KP_DIVIDE
: return KEY_KP_DIVIDE
;
472 case SDL_SCANCODE_KP_MULTIPLY
: return KEY_KP_MULTIPLY
;
473 case SDL_SCANCODE_KP_MINUS
: return KEY_KP_MINUS
;
474 case SDL_SCANCODE_KP_PLUS
: return KEY_KP_PLUS
;
475 case SDL_SCANCODE_KP_ENTER
: return KEY_KP_ENTER
;
476 case SDL_SCANCODE_KP_0
: return KEY_KP_0
;
477 case SDL_SCANCODE_KP_1
: return KEY_KP_1
;
478 case SDL_SCANCODE_KP_2
: return KEY_KP_2
;
479 case SDL_SCANCODE_KP_3
: return KEY_KP_3
;
480 case SDL_SCANCODE_KP_4
: return KEY_KP_4
;
481 case SDL_SCANCODE_KP_5
: return KEY_KP_5
;
482 case SDL_SCANCODE_KP_6
: return KEY_KP_6
;
483 case SDL_SCANCODE_KP_7
: return KEY_KP_7
;
484 case SDL_SCANCODE_KP_8
: return KEY_KP_8
;
485 case SDL_SCANCODE_KP_9
: return KEY_KP_9
;
486 case SDL_SCANCODE_KP_PERIOD
: return KEY_KP_PERIOD
;
487 case SDL_SCANCODE_SYSREQ
: return KEY_SYSREQ
;
488 case SDL_SCANCODE_LCTRL
: return KEY_LCTRL
;
489 case SDL_SCANCODE_LSHIFT
: return KEY_LSHIFT
;
490 case SDL_SCANCODE_LALT
: return KEY_LALT
;
491 case SDL_SCANCODE_LGUI
: return KEY_LSUPER
;
492 case SDL_SCANCODE_RCTRL
: return KEY_RCTRL
;
493 case SDL_SCANCODE_RSHIFT
: return KEY_RSHIFT
;
494 case SDL_SCANCODE_RALT
: return KEY_RALT
;
495 case SDL_SCANCODE_RGUI
: return KEY_RSUPER
;
496 default: return KEY_UNKNOWN
;
500 static void window_event_handler (SDL_WindowEvent
*ev
) {
502 case SDL_WINDOWEVENT_RESIZED
:
503 R_set_videomode(ev
->data1
, ev
->data2
, Y_get_fullscreen());
505 case SDL_WINDOWEVENT_CLOSE
:
511 static int utf8_to_wchar (char *x
) {
516 } else if (s
[0] < 0xE0) {
517 if (s
[0] - 192 >= 0 && s
[1] >= 0x80 && s
[1] < 0xE0) {
518 i
= (s
[0] - 192) * 64 + s
[1] - 128;
520 } else if (s
[0] < 0xF0) {
521 if (s
[1] >= 0x80 && s
[1] < 0xE0 && s
[2] >= 0x80 && s
[2] < 0xE0) {
522 i
= ((s
[0] - 224) * 64 + s
[1] - 128) * 64 + s
[2] - 128;
528 static void poll_events (void) {
529 int key
, down
, uch
, ch
;
531 while (SDL_PollEvent(&ev
)) {
536 case SDL_WINDOWEVENT
:
537 if (ev
.window
.windowID
== SDL_GetWindowID(window
)) {
538 window_event_handler(&ev
.window
);
543 down
= ev
.type
== SDL_KEYDOWN
;
544 key
= sdl_to_key(ev
.key
.keysym
.scancode
);
549 uch
= utf8_to_wchar(ev
.text
.text
);
550 ch
= cp866_utoc(uch
);
559 static void step (void) {
562 Uint32 t
= SDL_GetTicks();
563 if (t
- ticks
> DELAY
) {
570 int main (int argc
, char **argv
) {
572 CFG_args(argc
, argv
);
573 logo("system: initialize SDL2\n");
574 if (SDL_Init(SDL_INIT_TIMER
| SDL_INIT_VIDEO
| SDL_INIT_EVENTS
) == -1) {
575 logo("system: failed to init SDL2: %s\n", SDL_GetError());
583 pl1
.kf
= KEY_PAGEDOWN
;
598 srand(SDL_GetTicks());
602 pw
= "/usr/share/doom2d-rembo/doom2d.wad";
609 F_addwad("doom2d.wad");
618 ticks
= SDL_GetTicks();
619 #ifdef __EMSCRIPTEN__
620 emscripten_set_main_loop(step
, 0, 1);