1c16dc33307100048c40af58c3a38ee2060b7319
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
24 # include <emscripten.h>
30 #include <stdlib.h> // srand exit
35 #include "my.h" // fexists
36 #include "player.h" // pl1 pl2
37 #include "menu.h" // G_keyf
38 #include "error.h" // logo
39 #include "monster.h" // nomon
41 #include "files.h" // F_startup F_addwad F_initwads F_allocres
42 #include "config.h" // CFG_args CFG_load CFG_save
43 #include "args.h" // ARG_parse
44 #include "memory.h" // M_startup
45 #include "game.h" // G_init G_act
46 #include "sound.h" // S_init S_done
47 #include "music.h" // S_initmusic S_updatemusic S_donemusic
48 #include "render.h" // R_init R_draw R_done
52 #define MODE_SOFTWARE 2
56 static SDL_Surface
*surf
= NULL
;
57 static int mode
= MODE_NONE
;
58 static int text_input
;
59 static videomode_t vlist
;
61 static const cfg_t arg
[] = {
62 {"file", NULL
, Y_FILES
},
63 {"cheat", &cheat
, Y_SW_ON
},
64 // {"vga", &shot_vga, Y_SW_ON},
65 {"sndvol", &snd_vol
, Y_WORD
},
66 {"musvol", &mus_vol
, Y_WORD
},
67 {"mon", &nomon
, Y_SW_OFF
},
68 {"warp", &_warp
, Y_BYTE
},
69 // {"config", NULL, cfg_file, Y_STRING},
70 {NULL
, NULL
, 0} // end
73 static const cfg_t cfg
[] = {
74 // {"screenshot", &shot_vga, Y_SW_ON},
75 {"sound_volume", &snd_vol
, Y_WORD
},
76 {"music_volume", &mus_vol
, Y_WORD
},
77 {"music_random", &music_random
, Y_SW_ON
},
78 {"music_time", &music_time
, Y_DWORD
},
79 {"music_fade", &music_fade
, Y_DWORD
},
80 {"pl1_left", &pl1
.kl
, Y_KEY
},
81 {"pl1_right",&pl1
.kr
, Y_KEY
},
82 {"pl1_up", &pl1
.ku
, Y_KEY
},
83 {"pl1_down", &pl1
.kd
, Y_KEY
},
84 {"pl1_jump", &pl1
.kj
, Y_KEY
},
85 {"pl1_fire", &pl1
.kf
, Y_KEY
},
86 {"pl1_next", &pl1
.kwr
, Y_KEY
},
87 {"pl1_prev", &pl1
.kwl
, Y_KEY
},
88 {"pl1_use", &pl1
.kp
, Y_KEY
},
89 {"pl2_left", &pl2
.kl
, Y_KEY
},
90 {"pl2_right", &pl2
.kr
, Y_KEY
},
91 {"pl2_up", &pl2
.ku
, Y_KEY
},
92 {"pl2_down", &pl2
.kd
, Y_KEY
},
93 {"pl2_jump", &pl2
.kj
, Y_KEY
},
94 {"pl2_fire", &pl2
.kf
, Y_KEY
},
95 {"pl2_next", &pl2
.kwr
, Y_KEY
},
96 {"pl2_prev", &pl2
.kwl
, Y_KEY
},
97 {"pl2_use", &pl2
.kp
, Y_KEY
},
98 {NULL
, NULL
, 0} // end
101 static void CFG_args (int argc
, char **argv
) {
102 const cfg_t
*list
[] = { arg
, R_args() };
103 ARG_parse(argc
, argv
, 2, list
);
106 static void CFG_load (void) {
107 const cfg_t
*list
[] = { cfg
, R_conf() };
108 CFG_read_config("default.cfg", 2, list
);
109 CFG_read_config("doom2d.cfg", 2, list
);
112 static void CFG_save (void) {
113 const cfg_t
*list
[] = { cfg
, R_conf() };
114 CFG_update_config("doom2d.cfg", "doom2d.cfg", 2, list
, "generated by doom2d, do not modify");
117 /* --- error.h --- */
119 void logo (const char *s
, ...) {
127 void logo_gas (int cur
, int all
) {
131 void ERR_failinit (char *s
, ...) {
140 void ERR_fatal (char *s
, ...) {
147 puts("\nКРИТИЧЕСКАЯ ОШИБКА:");
155 void ERR_quit (void) {
159 /* --- system.h --- */
161 int Y_set_videomode_opengl (int w
, int h
, int fullscreen
) {
166 if (mode
== MODE_OPENGL
&& surf
->w
== w
&& surf
->h
== h
&& Y_get_fullscreen() == fullscreen
) {
169 flags
= SDL_DOUBLEBUF
| SDL_OPENGL
;
171 flags
= flags
| SDL_FULLSCREEN
;
174 flags
= flags
| SDL_RESIZABLE
;
176 s
= SDL_SetVideoMode(w
, h
, 0, flags
);
181 logo("Y_set_videomode_opengl: error: %s\n", SDL_GetError());
187 int Y_set_videomode_software (int w
, int h
, int fullscreen
) {
192 if (mode
== MODE_OPENGL
&& surf
->w
== w
&& surf
->h
== h
&& Y_get_fullscreen() == fullscreen
) {
195 flags
= SDL_DOUBLEBUF
| SDL_SWSURFACE
| SDL_HWPALETTE
;
197 flags
= flags
| SDL_FULLSCREEN
;
200 flags
= flags
| SDL_RESIZABLE
;
202 s
= SDL_SetVideoMode(w
, h
, 8, flags
);
204 mode
= MODE_SOFTWARE
;
211 static void init_videomode_list (Uint32 flags
) {
214 if (vlist
.modes
!= NULL
) {
219 r
= SDL_ListModes(NULL
, flags
);
220 if (r
== (SDL_Rect
**)-1) {
221 if ((flags
& SDL_FULLSCREEN
) == 0) {
222 init_videomode_list(flags
| SDL_FULLSCREEN
);
224 } else if (r
!= (SDL_Rect
**)0) {
226 while (r
[n
] != NULL
) {
229 vlist
.modes
= malloc(n
* sizeof(videomode_size_t
));
230 if (vlist
.modes
!= NULL
) {
232 for (i
= 0; i
< n
; i
++) {
233 vlist
.modes
[i
] = (videomode_size_t
) {
242 const videomode_t
*Y_get_videomode_list_opengl (int fullscreen
) {
243 init_videomode_list(SDL_OPENGL
| (fullscreen
? SDL_FULLSCREEN
: 0));
247 const videomode_t
*Y_get_videomode_list_software (int fullscreen
) {
248 init_videomode_list(SDL_SWSURFACE
| SDL_HWPALETTE
| (fullscreen
? SDL_FULLSCREEN
: 0));
252 void Y_get_videomode (int *w
, int *h
) {
253 if (mode
!= MODE_NONE
) {
262 int Y_videomode_setted (void) {
263 return mode
!= MODE_NONE
;
266 void Y_unset_videomode (void) {
269 #ifndef __EMSCRIPTEN__
270 SDL_QuitSubSystem(SDL_INIT_VIDEO
);
271 SDL_InitSubSystem(SDL_INIT_VIDEO
);
275 void Y_set_fullscreen (int fullscreen
) {
276 int fs
= Y_get_fullscreen();
277 if (mode
!= MODE_NONE
&& fs
!= fullscreen
) {
278 if (SDL_WM_ToggleFullScreen(surf
) == 0) {
281 Y_set_videomode_opengl(surf
->w
, surf
->h
, fullscreen
);
284 Y_set_videomode_software(surf
->w
, surf
->h
, fullscreen
);
291 int Y_get_fullscreen (void) {
292 return (mode
!= MODE_NONE
) && ((surf
->flags
& SDL_FULLSCREEN
) != 0);
295 void Y_swap_buffers (void) {
296 assert(mode
== MODE_OPENGL
);
297 SDL_GL_SwapBuffers();
300 void Y_get_buffer (byte
**buf
, int *w
, int *h
, int *pitch
) {
301 assert(mode
== MODE_SOFTWARE
);
305 *pitch
= surf
->pitch
;
308 void Y_set_vga_palette (byte
*vgapal
) {
311 assert(vgapal
!= NULL
);
312 assert(mode
== MODE_SOFTWARE
);
313 SDL_Color colors
[256];
314 for (i
= 0; i
< 256; i
++) {
315 colors
[i
] = (SDL_Color
) {
316 .r
= p
[0] * 255 / 63,
317 .g
= p
[1] * 255 / 63,
322 SDL_SetColors(surf
, colors
, 0, 256);
325 void Y_repaint_rect (int x
, int y
, int w
, int h
) {
326 assert(mode
== MODE_SOFTWARE
);
327 SDL_UpdateRect(surf
, x
, y
, w
, h
);
330 void Y_repaint (void) {
331 assert(mode
== MODE_SOFTWARE
);
335 void Y_enable_text_input (void) {
339 void Y_disable_text_input (void) {
345 static int sdl_to_key (int code
) {
347 case SDLK_0
: return KEY_0
;
348 case SDLK_1
: return KEY_1
;
349 case SDLK_2
: return KEY_2
;
350 case SDLK_3
: return KEY_3
;
351 case SDLK_4
: return KEY_4
;
352 case SDLK_5
: return KEY_5
;
353 case SDLK_6
: return KEY_6
;
354 case SDLK_7
: return KEY_7
;
355 case SDLK_8
: return KEY_8
;
356 case SDLK_9
: return KEY_9
;
357 case SDLK_a
: return KEY_A
;
358 case SDLK_b
: return KEY_B
;
359 case SDLK_c
: return KEY_C
;
360 case SDLK_d
: return KEY_D
;
361 case SDLK_e
: return KEY_E
;
362 case SDLK_f
: return KEY_F
;
363 case SDLK_g
: return KEY_G
;
364 case SDLK_h
: return KEY_H
;
365 case SDLK_i
: return KEY_I
;
366 case SDLK_j
: return KEY_J
;
367 case SDLK_k
: return KEY_K
;
368 case SDLK_l
: return KEY_L
;
369 case SDLK_m
: return KEY_M
;
370 case SDLK_n
: return KEY_N
;
371 case SDLK_o
: return KEY_O
;
372 case SDLK_p
: return KEY_P
;
373 case SDLK_q
: return KEY_Q
;
374 case SDLK_r
: return KEY_R
;
375 case SDLK_s
: return KEY_S
;
376 case SDLK_t
: return KEY_T
;
377 case SDLK_u
: return KEY_U
;
378 case SDLK_v
: return KEY_V
;
379 case SDLK_w
: return KEY_W
;
380 case SDLK_x
: return KEY_X
;
381 case SDLK_y
: return KEY_Y
;
382 case SDLK_z
: return KEY_Z
;
383 case SDLK_RETURN
: return KEY_RETURN
;
384 case SDLK_ESCAPE
: return KEY_ESCAPE
;
385 case SDLK_BACKSPACE
: return KEY_BACKSPACE
;
386 case SDLK_TAB
: return KEY_TAB
;
387 case SDLK_SPACE
: return KEY_SPACE
;
388 case SDLK_MINUS
: return KEY_MINUS
;
389 case SDLK_EQUALS
: return KEY_EQUALS
;
390 case SDLK_LEFTBRACKET
: return KEY_LEFTBRACKET
;
391 case SDLK_RIGHTBRACKET
: return KEY_RIGHTBRACKET
;
392 case SDLK_BACKSLASH
: return KEY_BACKSLASH
;
393 case SDLK_SEMICOLON
: return KEY_SEMICOLON
;
394 case SDLK_QUOTE
: return KEY_APOSTROPHE
;
395 case SDLK_BACKQUOTE
: return KEY_GRAVE
;
396 case SDLK_COMMA
: return KEY_COMMA
;
397 case SDLK_PERIOD
: return KEY_PERIOD
;
398 case SDLK_SLASH
: return KEY_SLASH
;
399 case SDLK_CAPSLOCK
: return KEY_CAPSLOCK
;
400 case SDLK_F1
: return KEY_F1
;
401 case SDLK_F2
: return KEY_F2
;
402 case SDLK_F3
: return KEY_F3
;
403 case SDLK_F4
: return KEY_F4
;
404 case SDLK_F5
: return KEY_F5
;
405 case SDLK_F6
: return KEY_F6
;
406 case SDLK_F7
: return KEY_F7
;
407 case SDLK_F8
: return KEY_F8
;
408 case SDLK_F9
: return KEY_F9
;
409 case SDLK_F10
: return KEY_F10
;
410 case SDLK_F11
: return KEY_F11
;
411 case SDLK_F12
: return KEY_F12
;
412 case SDLK_PRINT
: return KEY_PRINTSCREEN
;
413 case SDLK_SCROLLOCK
: return KEY_SCROLLLOCK
;
414 case SDLK_PAUSE
: return KEY_PAUSE
;
415 case SDLK_INSERT
: return KEY_INSERT
;
416 case SDLK_HOME
: return KEY_HOME
;
417 case SDLK_PAGEUP
: return KEY_PAGEUP
;
418 case SDLK_DELETE
: return KEY_DELETE
;
419 case SDLK_END
: return KEY_END
;
420 case SDLK_PAGEDOWN
: return KEY_PAGEDOWN
;
421 case SDLK_RIGHT
: return KEY_RIGHT
;
422 case SDLK_LEFT
: return KEY_LEFT
;
423 case SDLK_DOWN
: return KEY_DOWN
;
424 case SDLK_UP
: return KEY_UP
;
425 case SDLK_NUMLOCK
: return KEY_NUMLOCK
;
426 case SDLK_KP_DIVIDE
: return KEY_KP_DIVIDE
;
427 case SDLK_KP_MULTIPLY
: return KEY_KP_MULTIPLY
;
428 case SDLK_KP_MINUS
: return KEY_KP_MINUS
;
429 case SDLK_KP_PLUS
: return KEY_KP_PLUS
;
430 case SDLK_KP_ENTER
: return KEY_KP_ENTER
;
431 case SDLK_KP0
: return KEY_KP_0
;
432 case SDLK_KP1
: return KEY_KP_1
;
433 case SDLK_KP2
: return KEY_KP_2
;
434 case SDLK_KP3
: return KEY_KP_3
;
435 case SDLK_KP4
: return KEY_KP_4
;
436 case SDLK_KP5
: return KEY_KP_5
;
437 case SDLK_KP6
: return KEY_KP_6
;
438 case SDLK_KP7
: return KEY_KP_7
;
439 case SDLK_KP8
: return KEY_KP_8
;
440 case SDLK_KP9
: return KEY_KP_9
;
441 case SDLK_KP_PERIOD
: return KEY_KP_PERIOD
;
442 case SDLK_SYSREQ
: return KEY_SYSREQ
;
443 case SDLK_LCTRL
: return KEY_LCTRL
;
444 case SDLK_LSHIFT
: return KEY_LSHIFT
;
445 case SDLK_LALT
: return KEY_LALT
;
446 case SDLK_LSUPER
: return KEY_LSUPER
;
447 case SDLK_RCTRL
: return KEY_RCTRL
;
448 case SDLK_RSHIFT
: return KEY_RSHIFT
;
449 case SDLK_RALT
: return KEY_RALT
;
450 case SDLK_RSUPER
: return KEY_RSUPER
;
451 default: return KEY_UNKNOWN
;
455 static void poll_events (void) {
458 while (SDL_PollEvent(&ev
)) {
463 case SDL_VIDEORESIZE
:
464 R_set_videomode(ev
.resize
.w
, ev
.resize
.h
, Y_get_fullscreen());
468 sym
= ev
.key
.keysym
.sym
;
469 down
= ev
.type
== SDL_KEYDOWN
;
470 key
= sdl_to_key(sym
);
473 if (down
&& text_input
&& sym
>= 0x20 && sym
<= 0x7e) {
482 static void step (void) {
485 Uint32 t
= SDL_GetTicks();
486 if (t
- ticks
> DELAY
) {
493 int main (int argc
, char *argv
[]) {
495 logo("main: initialize SDL\n");
496 if (SDL_Init(SDL_INIT_VIDEO
| SDL_INIT_TIMER
) == -1) {
497 logo("main: failed to init SDL: %s\n", SDL_GetError());
500 SDL_WM_SetCaption("Doom 2D (SDL)", "Doom 2D");
506 pl1
.kf
= KEY_PAGEDOWN
;
521 srand(SDL_GetTicks());
524 pw
= "/usr/share/doom2d-rembo/doom2d.wad";
531 F_addwad("doom2d.wad");
533 CFG_args(argc
, argv
);
542 ticks
= SDL_GetTicks();
543 #ifdef __EMSCRIPTEN__
544 emscripten_set_main_loop(step
, 0, 1);