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
28 #include "player.h" // pl1 pl2
29 #include "menu.h" // G_keyf
30 #include "error.h" // logo
31 #include "monster.h" // nomon
33 #include "files.h" // F_startup F_addwad F_initwads F_allocres
34 #include "config.h" // CFG_args CFG_load CFG_save
35 #include "args.h" // ARG_parse
36 #include "memory.h" // M_startup
37 #include "game.h" // G_init G_act
38 #include "sound.h" // S_init S_done
39 #include "music.h" // S_initmusic S_updatemusic S_donemusic
40 #include "render.h" // R_init R_draw R_done
44 #define MODE_SOFTWARE 2
48 static SDL_Surface
*surf
= NULL
;
49 static int mode
= MODE_NONE
;
50 static int text_input
;
51 static videomode_t vlist
;
53 static const cfg_t arg
[] = {
54 {"file", NULL
, Y_FILES
},
55 {"cheat", &cheat
, Y_SW_ON
},
56 // {"vga", &shot_vga, Y_SW_ON},
57 // {"musvol", &mus_vol, Y_WORD},
58 {"mon", &nomon
, Y_SW_OFF
},
59 {"warp", &_warp
, Y_BYTE
},
60 // {"config", NULL, cfg_file, Y_STRING},
61 {NULL
, NULL
, 0} // end
64 static const cfg_t cfg
[] = {
65 // {"screenshot", &shot_vga, Y_SW_ON},
66 // {"music_volume", &mus_vol, Y_WORD},
67 // {"music_random", &music_random, Y_SW_ON},
68 // {"music_time", &music_time, Y_DWORD},
69 // {"music_fade", &music_fade, Y_DWORD},
70 {"pl1_left", &pl1
.kl
, Y_KEY
},
71 {"pl1_right",&pl1
.kr
, Y_KEY
},
72 {"pl1_up", &pl1
.ku
, Y_KEY
},
73 {"pl1_down", &pl1
.kd
, Y_KEY
},
74 {"pl1_jump", &pl1
.kj
, Y_KEY
},
75 {"pl1_fire", &pl1
.kf
, Y_KEY
},
76 {"pl1_next", &pl1
.kwr
, Y_KEY
},
77 {"pl1_prev", &pl1
.kwl
, Y_KEY
},
78 {"pl1_use", &pl1
.kp
, Y_KEY
},
79 {"pl2_left", &pl2
.kl
, Y_KEY
},
80 {"pl2_right", &pl2
.kr
, Y_KEY
},
81 {"pl2_up", &pl2
.ku
, Y_KEY
},
82 {"pl2_down", &pl2
.kd
, Y_KEY
},
83 {"pl2_jump", &pl2
.kj
, Y_KEY
},
84 {"pl2_fire", &pl2
.kf
, Y_KEY
},
85 {"pl2_next", &pl2
.kwr
, Y_KEY
},
86 {"pl2_prev", &pl2
.kwl
, Y_KEY
},
87 {"pl2_use", &pl2
.kp
, Y_KEY
},
88 {NULL
, NULL
, 0} // end
91 static void CFG_args (int argc
, char **argv
) {
92 const cfg_t
*list
[] = { arg
, R_args(), S_args(), MUS_args() };
93 ARG_parse(argc
, argv
, 4, list
);
96 static void CFG_load (void) {
97 const cfg_t
*list
[] = { cfg
, R_conf(), S_conf(), MUS_conf() };
98 CFG_read_config("default.cfg", 4, list
);
99 CFG_read_config("doom2d.cfg", 4, list
);
102 static void CFG_save (void) {
103 const cfg_t
*list
[] = { cfg
, R_conf(), S_conf(), MUS_conf() };
104 CFG_update_config("doom2d.cfg", "doom2d.cfg", 4, list
, "generated by doom2d, do not modify");
107 /* --- error.h --- */
109 void logo (const char *s
, ...) {
117 void logo_gas (int cur
, int all
) {
121 void ERR_failinit (char *s
, ...) {
130 void ERR_fatal (char *s
, ...) {
137 puts("\nCRITICAL ERROR:");
145 void ERR_quit (void) {
149 /* --- system.h --- */
151 int Y_set_videomode_opengl (int w
, int h
, int fullscreen
) {
156 if (mode
== MODE_OPENGL
&& surf
->w
== w
&& surf
->h
== h
&& Y_get_fullscreen() == fullscreen
) {
159 flags
= SDL_DOUBLEBUF
| SDL_OPENGL
;
161 flags
= flags
| SDL_FULLSCREEN
;
164 flags
= flags
| SDL_RESIZABLE
;
166 s
= SDL_SetVideoMode(w
, h
, 0, flags
);
171 logo("Y_set_videomode_opengl: error: %s\n", SDL_GetError());
177 int Y_set_videomode_software (int w
, int h
, int fullscreen
) {
182 if (mode
== MODE_OPENGL
&& surf
->w
== w
&& surf
->h
== h
&& Y_get_fullscreen() == fullscreen
) {
185 flags
= SDL_DOUBLEBUF
| SDL_SWSURFACE
| SDL_HWPALETTE
;
187 flags
= flags
| SDL_FULLSCREEN
;
190 flags
= flags
| SDL_RESIZABLE
;
192 s
= SDL_SetVideoMode(w
, h
, 8, flags
);
194 mode
= MODE_SOFTWARE
;
201 static void init_videomode_list (Uint32 flags
) {
204 if (vlist
.modes
!= NULL
) {
209 r
= SDL_ListModes(NULL
, flags
);
210 if (r
== (SDL_Rect
**)-1) {
211 if ((flags
& SDL_FULLSCREEN
) == 0) {
212 init_videomode_list(flags
| SDL_FULLSCREEN
);
214 } else if (r
!= (SDL_Rect
**)0) {
216 while (r
[n
] != NULL
) {
219 vlist
.modes
= malloc(n
* sizeof(videomode_size_t
));
220 if (vlist
.modes
!= NULL
) {
222 for (i
= 0; i
< n
; i
++) {
223 vlist
.modes
[i
] = (videomode_size_t
) {
232 const videomode_t
*Y_get_videomode_list_opengl (int fullscreen
) {
233 init_videomode_list(SDL_OPENGL
| (fullscreen
? SDL_FULLSCREEN
: 0));
237 const videomode_t
*Y_get_videomode_list_software (int fullscreen
) {
238 init_videomode_list(SDL_SWSURFACE
| SDL_HWPALETTE
| (fullscreen
? SDL_FULLSCREEN
: 0));
242 void Y_get_videomode (int *w
, int *h
) {
243 if (mode
!= MODE_NONE
) {
252 int Y_videomode_setted (void) {
253 return mode
!= MODE_NONE
;
256 void Y_unset_videomode (void) {
259 #ifndef __EMSCRIPTEN__
260 SDL_QuitSubSystem(SDL_INIT_VIDEO
);
261 SDL_InitSubSystem(SDL_INIT_VIDEO
);
265 void Y_set_fullscreen (int fullscreen
) {
266 int fs
= Y_get_fullscreen();
267 if (mode
!= MODE_NONE
&& fs
!= fullscreen
) {
268 if (SDL_WM_ToggleFullScreen(surf
) == 0) {
271 Y_set_videomode_opengl(surf
->w
, surf
->h
, fullscreen
);
274 Y_set_videomode_software(surf
->w
, surf
->h
, fullscreen
);
281 int Y_get_fullscreen (void) {
282 return (mode
!= MODE_NONE
) && ((surf
->flags
& SDL_FULLSCREEN
) != 0);
285 void Y_swap_buffers (void) {
286 assert(mode
== MODE_OPENGL
);
287 SDL_GL_SwapBuffers();
290 void Y_get_buffer (byte
**buf
, int *w
, int *h
, int *pitch
) {
291 assert(mode
== MODE_SOFTWARE
);
295 *pitch
= surf
->pitch
;
298 void Y_set_vga_palette (byte
*vgapal
) {
301 assert(vgapal
!= NULL
);
302 assert(mode
== MODE_SOFTWARE
);
303 SDL_Color colors
[256];
304 for (i
= 0; i
< 256; i
++) {
305 colors
[i
] = (SDL_Color
) {
306 .r
= p
[0] * 255 / 63,
307 .g
= p
[1] * 255 / 63,
312 SDL_SetColors(surf
, colors
, 0, 256);
315 void Y_repaint_rect (int x
, int y
, int w
, int h
) {
316 assert(mode
== MODE_SOFTWARE
);
317 SDL_UpdateRect(surf
, x
, y
, w
, h
);
320 void Y_repaint (void) {
321 assert(mode
== MODE_SOFTWARE
);
325 void Y_enable_text_input (void) {
329 void Y_disable_text_input (void) {
335 static int sdl_to_key (int code
) {
337 case SDLK_0
: return KEY_0
;
338 case SDLK_1
: return KEY_1
;
339 case SDLK_2
: return KEY_2
;
340 case SDLK_3
: return KEY_3
;
341 case SDLK_4
: return KEY_4
;
342 case SDLK_5
: return KEY_5
;
343 case SDLK_6
: return KEY_6
;
344 case SDLK_7
: return KEY_7
;
345 case SDLK_8
: return KEY_8
;
346 case SDLK_9
: return KEY_9
;
347 case SDLK_a
: return KEY_A
;
348 case SDLK_b
: return KEY_B
;
349 case SDLK_c
: return KEY_C
;
350 case SDLK_d
: return KEY_D
;
351 case SDLK_e
: return KEY_E
;
352 case SDLK_f
: return KEY_F
;
353 case SDLK_g
: return KEY_G
;
354 case SDLK_h
: return KEY_H
;
355 case SDLK_i
: return KEY_I
;
356 case SDLK_j
: return KEY_J
;
357 case SDLK_k
: return KEY_K
;
358 case SDLK_l
: return KEY_L
;
359 case SDLK_m
: return KEY_M
;
360 case SDLK_n
: return KEY_N
;
361 case SDLK_o
: return KEY_O
;
362 case SDLK_p
: return KEY_P
;
363 case SDLK_q
: return KEY_Q
;
364 case SDLK_r
: return KEY_R
;
365 case SDLK_s
: return KEY_S
;
366 case SDLK_t
: return KEY_T
;
367 case SDLK_u
: return KEY_U
;
368 case SDLK_v
: return KEY_V
;
369 case SDLK_w
: return KEY_W
;
370 case SDLK_x
: return KEY_X
;
371 case SDLK_y
: return KEY_Y
;
372 case SDLK_z
: return KEY_Z
;
373 case SDLK_RETURN
: return KEY_RETURN
;
374 case SDLK_ESCAPE
: return KEY_ESCAPE
;
375 case SDLK_BACKSPACE
: return KEY_BACKSPACE
;
376 case SDLK_TAB
: return KEY_TAB
;
377 case SDLK_SPACE
: return KEY_SPACE
;
378 case SDLK_MINUS
: return KEY_MINUS
;
379 case SDLK_EQUALS
: return KEY_EQUALS
;
380 case SDLK_LEFTBRACKET
: return KEY_LEFTBRACKET
;
381 case SDLK_RIGHTBRACKET
: return KEY_RIGHTBRACKET
;
382 case SDLK_BACKSLASH
: return KEY_BACKSLASH
;
383 case SDLK_SEMICOLON
: return KEY_SEMICOLON
;
384 case SDLK_QUOTE
: return KEY_APOSTROPHE
;
385 case SDLK_BACKQUOTE
: return KEY_GRAVE
;
386 case SDLK_COMMA
: return KEY_COMMA
;
387 case SDLK_PERIOD
: return KEY_PERIOD
;
388 case SDLK_SLASH
: return KEY_SLASH
;
389 case SDLK_CAPSLOCK
: return KEY_CAPSLOCK
;
390 case SDLK_F1
: return KEY_F1
;
391 case SDLK_F2
: return KEY_F2
;
392 case SDLK_F3
: return KEY_F3
;
393 case SDLK_F4
: return KEY_F4
;
394 case SDLK_F5
: return KEY_F5
;
395 case SDLK_F6
: return KEY_F6
;
396 case SDLK_F7
: return KEY_F7
;
397 case SDLK_F8
: return KEY_F8
;
398 case SDLK_F9
: return KEY_F9
;
399 case SDLK_F10
: return KEY_F10
;
400 case SDLK_F11
: return KEY_F11
;
401 case SDLK_F12
: return KEY_F12
;
402 case SDLK_PRINT
: return KEY_PRINTSCREEN
;
403 case SDLK_SCROLLOCK
: return KEY_SCROLLLOCK
;
404 case SDLK_PAUSE
: return KEY_PAUSE
;
405 case SDLK_INSERT
: return KEY_INSERT
;
406 case SDLK_HOME
: return KEY_HOME
;
407 case SDLK_PAGEUP
: return KEY_PAGEUP
;
408 case SDLK_DELETE
: return KEY_DELETE
;
409 case SDLK_END
: return KEY_END
;
410 case SDLK_PAGEDOWN
: return KEY_PAGEDOWN
;
411 case SDLK_RIGHT
: return KEY_RIGHT
;
412 case SDLK_LEFT
: return KEY_LEFT
;
413 case SDLK_DOWN
: return KEY_DOWN
;
414 case SDLK_UP
: return KEY_UP
;
415 case SDLK_NUMLOCK
: return KEY_NUMLOCK
;
416 case SDLK_KP_DIVIDE
: return KEY_KP_DIVIDE
;
417 case SDLK_KP_MULTIPLY
: return KEY_KP_MULTIPLY
;
418 case SDLK_KP_MINUS
: return KEY_KP_MINUS
;
419 case SDLK_KP_PLUS
: return KEY_KP_PLUS
;
420 case SDLK_KP_ENTER
: return KEY_KP_ENTER
;
421 case SDLK_KP0
: return KEY_KP_0
;
422 case SDLK_KP1
: return KEY_KP_1
;
423 case SDLK_KP2
: return KEY_KP_2
;
424 case SDLK_KP3
: return KEY_KP_3
;
425 case SDLK_KP4
: return KEY_KP_4
;
426 case SDLK_KP5
: return KEY_KP_5
;
427 case SDLK_KP6
: return KEY_KP_6
;
428 case SDLK_KP7
: return KEY_KP_7
;
429 case SDLK_KP8
: return KEY_KP_8
;
430 case SDLK_KP9
: return KEY_KP_9
;
431 case SDLK_KP_PERIOD
: return KEY_KP_PERIOD
;
432 case SDLK_SYSREQ
: return KEY_SYSREQ
;
433 case SDLK_LCTRL
: return KEY_LCTRL
;
434 case SDLK_LSHIFT
: return KEY_LSHIFT
;
435 case SDLK_LALT
: return KEY_LALT
;
436 case SDLK_LSUPER
: return KEY_LSUPER
;
437 case SDLK_RCTRL
: return KEY_RCTRL
;
438 case SDLK_RSHIFT
: return KEY_RSHIFT
;
439 case SDLK_RALT
: return KEY_RALT
;
440 case SDLK_RSUPER
: return KEY_RSUPER
;
441 default: return KEY_UNKNOWN
;
445 static void poll_events (void) {
448 while (SDL_PollEvent(&ev
)) {
453 case SDL_VIDEORESIZE
:
454 R_set_videomode(ev
.resize
.w
, ev
.resize
.h
, Y_get_fullscreen());
458 sym
= ev
.key
.keysym
.sym
;
459 down
= ev
.type
== SDL_KEYDOWN
;
460 key
= sdl_to_key(sym
);
463 if (down
&& text_input
&& sym
>= 0x20 && sym
<= 0x7e) {
472 static void step (void) {
475 Uint32 t
= SDL_GetTicks();
476 if (t
- ticks
> DELAY
) {
483 int main (int argc
, char *argv
[]) {
484 logo("main: initialize SDL\n");
485 if (SDL_Init(SDL_INIT_VIDEO
| SDL_INIT_TIMER
) == -1) {
486 logo("main: failed to init SDL: %s\n", SDL_GetError());
489 SDL_WM_SetCaption("Doom 2D (SDL)", "Doom 2D");
495 pl1
.kf
= KEY_PAGEDOWN
;
510 srand(SDL_GetTicks());
512 F_addwad("doom2d.wad");
513 CFG_args(argc
, argv
);
522 ticks
= SDL_GetTicks();
523 #ifdef __EMSCRIPTEN__
524 emscripten_set_main_loop(step
, 0, 1);