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 "memory.h" // M_startup
44 #include "game.h" // G_init G_act
45 #include "sound.h" // S_init S_done
46 #include "music.h" // S_initmusic S_updatemusic S_donemusic
47 #include "render.h" // R_init R_draw R_done
51 #define MODE_SOFTWARE 2
55 static SDL_Surface
*surf
= NULL
;
56 static int mode
= MODE_NONE
;
57 static int text_input
;
58 static videomode_t vlist
;
60 static const cfg_t arg
[] = {
61 {"file", NULL
, Y_FILES
},
62 {"cheat", &cheat
, Y_SW_ON
},
63 // {"vga", &shot_vga, Y_SW_ON},
64 {"sndvol", &snd_vol
, Y_WORD
},
65 {"musvol", &mus_vol
, Y_WORD
},
66 // {"fullscr", &fullscreen, Y_SW_ON},
67 // {"window", &fullscreen, Y_SW_OFF},
68 {"mon", &nomon
, Y_SW_OFF
},
69 // {"gamma", &gammaa, Y_DWORD},
70 {"warp", &_warp
, Y_BYTE
},
71 // {"width", &SCRW, Y_DWORD},
72 // {"height", &SCRH, Y_DWORD},
73 // {"config", NULL, cfg_file, Y_STRING},
74 {NULL
, NULL
, 0} // end
77 static const cfg_t cfg
[] = {
78 // {"screenshot", &shot_vga, Y_SW_ON},
79 {"sound_volume", &snd_vol
, Y_WORD
},
80 {"music_volume", &mus_vol
, Y_WORD
},
81 // {"fullscreen", &fullscreen, Y_SW_ON},
82 {"sky", &w_horiz
, Y_SW_ON
},
83 // {"gamma", &gammaa, Y_DWORD},
84 // {"screen_width", &SCRW, Y_DWORD},
85 // {"screen_height", &SCRH, Y_DWORD},
86 {"music_random", &music_random
, Y_SW_ON
},
87 {"music_time", &music_time
, Y_DWORD
},
88 {"music_fade", &music_fade
, Y_DWORD
},
89 {"pl1_left", &pl1
.kl
, Y_KEY
},
90 {"pl1_right",&pl1
.kr
, Y_KEY
},
91 {"pl1_up", &pl1
.ku
, Y_KEY
},
92 {"pl1_down", &pl1
.kd
, Y_KEY
},
93 {"pl1_jump", &pl1
.kj
, Y_KEY
},
94 {"pl1_fire", &pl1
.kf
, Y_KEY
},
95 {"pl1_next", &pl1
.kwr
, Y_KEY
},
96 {"pl1_prev", &pl1
.kwl
, Y_KEY
},
97 {"pl1_use", &pl1
.kp
, Y_KEY
},
98 {"pl2_left", &pl2
.kl
, Y_KEY
},
99 {"pl2_right", &pl2
.kr
, Y_KEY
},
100 {"pl2_up", &pl2
.ku
, Y_KEY
},
101 {"pl2_down", &pl2
.kd
, Y_KEY
},
102 {"pl2_jump", &pl2
.kj
, Y_KEY
},
103 {"pl2_fire", &pl2
.kf
, Y_KEY
},
104 {"pl2_next", &pl2
.kwr
, Y_KEY
},
105 {"pl2_prev", &pl2
.kwl
, Y_KEY
},
106 {"pl2_use", &pl2
.kp
, Y_KEY
},
107 {NULL
, NULL
, 0} // end
110 static void CFG_args (int argc
, char **argv
) {
112 for (i
= 1; i
< argc
; i
++) {
113 if (argv
[i
][0] == '-' && argv
[i
][1] != 0) {
115 ERR_failinit("CFG_args: not enough arguments for parameter %s\n", argv
[i
]);
117 if (CFG_update_key(&argv
[i
][1], argv
[i
+ 1], arg
) != 0) {
118 ERR_failinit("CFG_args: unknown parameter %s\n", argv
[i
]);
123 ERR_failinit("CFG_args: something wrong here: %s\n", argv
[i
]);
128 static void CFG_load (void) {
129 CFG_read_config("default.cfg", cfg
);
130 CFG_read_config("doom2d.cfg", cfg
);
133 static void CFG_save (void) {
134 CFG_update_config("doom2d.cfg", "doom2d.cfg", cfg
, "generated by doom2d, do not modify");
135 //CFG_update_config("doom2d.cfg", "doom2d.tmp", cfg, "temporary file");
136 //CFG_update_config("doom2d.tmp", "doom2d.cfg", cfg, "generated by doom2d, do not modify");
137 //remove("doom2d.tmp");
140 /* --- error.h --- */
142 void logo (const char *s
, ...) {
150 void logo_gas (int cur
, int all
) {
154 void ERR_failinit (char *s
, ...) {
163 void ERR_fatal (char *s
, ...) {
170 puts("\nКРИТИЧЕСКАЯ ОШИБКА:");
178 void ERR_quit (void) {
182 /* --- system.h --- */
184 int Y_set_videomode_opengl (int w
, int h
, int fullscreen
) {
189 if (mode
== MODE_OPENGL
&& surf
->w
== w
&& surf
->h
== h
&& Y_get_fullscreen() == fullscreen
) {
192 flags
= SDL_DOUBLEBUF
| SDL_OPENGL
;
194 flags
= flags
| SDL_FULLSCREEN
;
197 flags
= flags
| SDL_RESIZABLE
;
199 s
= SDL_SetVideoMode(w
, h
, 0, flags
);
204 logo("Y_set_videomode_opengl: error: %s\n", SDL_GetError());
210 int Y_set_videomode_software (int w
, int h
, int fullscreen
) {
215 if (mode
== MODE_OPENGL
&& surf
->w
== w
&& surf
->h
== h
&& Y_get_fullscreen() == fullscreen
) {
218 flags
= SDL_DOUBLEBUF
| SDL_SWSURFACE
| SDL_HWPALETTE
;
220 flags
= flags
| SDL_FULLSCREEN
;
223 flags
= flags
| SDL_RESIZABLE
;
225 s
= SDL_SetVideoMode(w
, h
, 8, flags
);
227 mode
= MODE_SOFTWARE
;
234 static void init_videomode_list (Uint32 flags
) {
237 if (vlist
.modes
!= NULL
) {
242 r
= SDL_ListModes(NULL
, flags
);
243 if (r
== (SDL_Rect
**)-1) {
244 if ((flags
& SDL_FULLSCREEN
) == 0) {
245 init_videomode_list(flags
| SDL_FULLSCREEN
);
247 } else if (r
!= (SDL_Rect
**)0) {
249 while (r
[n
] != NULL
) {
252 vlist
.modes
= malloc(n
* sizeof(videomode_size_t
));
253 if (vlist
.modes
!= NULL
) {
255 for (i
= 0; i
< n
; i
++) {
256 vlist
.modes
[i
] = (videomode_size_t
) {
265 const videomode_t
*Y_get_videomode_list_opengl (int fullscreen
) {
266 init_videomode_list(SDL_OPENGL
| (fullscreen
? SDL_FULLSCREEN
: 0));
270 const videomode_t
*Y_get_videomode_list_software (int fullscreen
) {
271 init_videomode_list(SDL_SWSURFACE
| SDL_HWPALETTE
| (fullscreen
? SDL_FULLSCREEN
: 0));
275 void Y_get_videomode (int *w
, int *h
) {
276 if (mode
!= MODE_NONE
) {
285 int Y_videomode_setted (void) {
286 return mode
!= MODE_NONE
;
289 void Y_unset_videomode (void) {
292 #ifndef __EMSCRIPTEN__
293 SDL_QuitSubSystem(SDL_INIT_VIDEO
);
294 SDL_InitSubSystem(SDL_INIT_VIDEO
);
298 void Y_set_fullscreen (int fullscreen
) {
299 int fs
= Y_get_fullscreen();
300 if (mode
!= MODE_NONE
&& fs
!= fullscreen
) {
301 if (SDL_WM_ToggleFullScreen(surf
) == 0) {
304 Y_set_videomode_opengl(surf
->w
, surf
->h
, fullscreen
);
307 Y_set_videomode_software(surf
->w
, surf
->h
, fullscreen
);
314 int Y_get_fullscreen (void) {
315 return (mode
!= MODE_NONE
) && ((surf
->flags
& SDL_FULLSCREEN
) != 0);
318 void Y_swap_buffers (void) {
319 assert(mode
== MODE_OPENGL
);
320 SDL_GL_SwapBuffers();
323 void Y_get_buffer (byte
**buf
, int *w
, int *h
, int *pitch
) {
324 assert(mode
== MODE_SOFTWARE
);
328 *pitch
= surf
->pitch
;
331 void Y_set_vga_palette (byte
*vgapal
) {
334 assert(vgapal
!= NULL
);
335 assert(mode
== MODE_SOFTWARE
);
336 SDL_Color colors
[256];
337 for (i
= 0; i
< 256; i
++) {
338 colors
[i
] = (SDL_Color
) {
339 .r
= p
[0] * 255 / 63,
340 .g
= p
[1] * 255 / 63,
345 SDL_SetColors(surf
, colors
, 0, 256);
348 void Y_repaint_rect (int x
, int y
, int w
, int h
) {
349 assert(mode
== MODE_SOFTWARE
);
350 SDL_UpdateRect(surf
, x
, y
, w
, h
);
353 void Y_repaint (void) {
354 assert(mode
== MODE_SOFTWARE
);
358 void Y_enable_text_input (void) {
362 void Y_disable_text_input (void) {
368 static int sdl_to_key (int code
) {
370 case SDLK_0
: return KEY_0
;
371 case SDLK_1
: return KEY_1
;
372 case SDLK_2
: return KEY_2
;
373 case SDLK_3
: return KEY_3
;
374 case SDLK_4
: return KEY_4
;
375 case SDLK_5
: return KEY_5
;
376 case SDLK_6
: return KEY_6
;
377 case SDLK_7
: return KEY_7
;
378 case SDLK_8
: return KEY_8
;
379 case SDLK_9
: return KEY_9
;
380 case SDLK_a
: return KEY_A
;
381 case SDLK_b
: return KEY_B
;
382 case SDLK_c
: return KEY_C
;
383 case SDLK_d
: return KEY_D
;
384 case SDLK_e
: return KEY_E
;
385 case SDLK_f
: return KEY_F
;
386 case SDLK_g
: return KEY_G
;
387 case SDLK_h
: return KEY_H
;
388 case SDLK_i
: return KEY_I
;
389 case SDLK_j
: return KEY_J
;
390 case SDLK_k
: return KEY_K
;
391 case SDLK_l
: return KEY_L
;
392 case SDLK_m
: return KEY_M
;
393 case SDLK_n
: return KEY_N
;
394 case SDLK_o
: return KEY_O
;
395 case SDLK_p
: return KEY_P
;
396 case SDLK_q
: return KEY_Q
;
397 case SDLK_r
: return KEY_R
;
398 case SDLK_s
: return KEY_S
;
399 case SDLK_t
: return KEY_T
;
400 case SDLK_u
: return KEY_U
;
401 case SDLK_v
: return KEY_V
;
402 case SDLK_w
: return KEY_W
;
403 case SDLK_x
: return KEY_X
;
404 case SDLK_y
: return KEY_Y
;
405 case SDLK_z
: return KEY_Z
;
406 case SDLK_RETURN
: return KEY_RETURN
;
407 case SDLK_ESCAPE
: return KEY_ESCAPE
;
408 case SDLK_BACKSPACE
: return KEY_BACKSPACE
;
409 case SDLK_TAB
: return KEY_TAB
;
410 case SDLK_SPACE
: return KEY_SPACE
;
411 case SDLK_MINUS
: return KEY_MINUS
;
412 case SDLK_EQUALS
: return KEY_EQUALS
;
413 case SDLK_LEFTBRACKET
: return KEY_LEFTBRACKET
;
414 case SDLK_RIGHTBRACKET
: return KEY_RIGHTBRACKET
;
415 case SDLK_BACKSLASH
: return KEY_BACKSLASH
;
416 case SDLK_SEMICOLON
: return KEY_SEMICOLON
;
417 case SDLK_QUOTE
: return KEY_APOSTROPHE
;
418 case SDLK_BACKQUOTE
: return KEY_GRAVE
;
419 case SDLK_COMMA
: return KEY_COMMA
;
420 case SDLK_PERIOD
: return KEY_PERIOD
;
421 case SDLK_SLASH
: return KEY_SLASH
;
422 case SDLK_CAPSLOCK
: return KEY_CAPSLOCK
;
423 case SDLK_F1
: return KEY_F1
;
424 case SDLK_F2
: return KEY_F2
;
425 case SDLK_F3
: return KEY_F3
;
426 case SDLK_F4
: return KEY_F4
;
427 case SDLK_F5
: return KEY_F5
;
428 case SDLK_F6
: return KEY_F6
;
429 case SDLK_F7
: return KEY_F7
;
430 case SDLK_F8
: return KEY_F8
;
431 case SDLK_F9
: return KEY_F9
;
432 case SDLK_F10
: return KEY_F10
;
433 case SDLK_F11
: return KEY_F11
;
434 case SDLK_F12
: return KEY_F12
;
435 case SDLK_PRINT
: return KEY_PRINTSCREEN
;
436 case SDLK_SCROLLOCK
: return KEY_SCROLLLOCK
;
437 case SDLK_PAUSE
: return KEY_PAUSE
;
438 case SDLK_INSERT
: return KEY_INSERT
;
439 case SDLK_HOME
: return KEY_HOME
;
440 case SDLK_PAGEUP
: return KEY_PAGEUP
;
441 case SDLK_DELETE
: return KEY_DELETE
;
442 case SDLK_END
: return KEY_END
;
443 case SDLK_PAGEDOWN
: return KEY_PAGEDOWN
;
444 case SDLK_RIGHT
: return KEY_RIGHT
;
445 case SDLK_LEFT
: return KEY_LEFT
;
446 case SDLK_DOWN
: return KEY_DOWN
;
447 case SDLK_UP
: return KEY_UP
;
448 case SDLK_NUMLOCK
: return KEY_NUMLOCK
;
449 case SDLK_KP_DIVIDE
: return KEY_KP_DIVIDE
;
450 case SDLK_KP_MULTIPLY
: return KEY_KP_MULTIPLY
;
451 case SDLK_KP_MINUS
: return KEY_KP_MINUS
;
452 case SDLK_KP_PLUS
: return KEY_KP_PLUS
;
453 case SDLK_KP_ENTER
: return KEY_KP_ENTER
;
454 case SDLK_KP0
: return KEY_KP_0
;
455 case SDLK_KP1
: return KEY_KP_1
;
456 case SDLK_KP2
: return KEY_KP_2
;
457 case SDLK_KP3
: return KEY_KP_3
;
458 case SDLK_KP4
: return KEY_KP_4
;
459 case SDLK_KP5
: return KEY_KP_5
;
460 case SDLK_KP6
: return KEY_KP_6
;
461 case SDLK_KP7
: return KEY_KP_7
;
462 case SDLK_KP8
: return KEY_KP_8
;
463 case SDLK_KP9
: return KEY_KP_9
;
464 case SDLK_KP_PERIOD
: return KEY_KP_PERIOD
;
465 case SDLK_SYSREQ
: return KEY_SYSREQ
;
466 case SDLK_LCTRL
: return KEY_LCTRL
;
467 case SDLK_LSHIFT
: return KEY_LSHIFT
;
468 case SDLK_LALT
: return KEY_LALT
;
469 case SDLK_LSUPER
: return KEY_LSUPER
;
470 case SDLK_RCTRL
: return KEY_RCTRL
;
471 case SDLK_RSHIFT
: return KEY_RSHIFT
;
472 case SDLK_RALT
: return KEY_RALT
;
473 case SDLK_RSUPER
: return KEY_RSUPER
;
474 default: return KEY_UNKNOWN
;
478 static void poll_events (void) {
481 while (SDL_PollEvent(&ev
)) {
486 case SDL_VIDEORESIZE
:
487 R_set_videomode(ev
.resize
.w
, ev
.resize
.h
, Y_get_fullscreen());
491 sym
= ev
.key
.keysym
.sym
;
492 down
= ev
.type
== SDL_KEYDOWN
;
493 key
= sdl_to_key(sym
);
496 if (down
&& text_input
&& sym
>= 0x20 && sym
<= 0x7e) {
505 static void step (void) {
508 Uint32 t
= SDL_GetTicks();
509 if (t
- ticks
> DELAY
) {
516 int main (int argc
, char *argv
[]) {
518 logo("main: initialize SDL\n");
519 if (SDL_Init(SDL_INIT_VIDEO
| SDL_INIT_TIMER
) == -1) {
520 logo("main: failed to init SDL: %s\n", SDL_GetError());
523 SDL_WM_SetCaption("Doom 2D (SDL)", "Doom 2D");
529 pl1
.kf
= KEY_PAGEDOWN
;
544 srand(SDL_GetTicks());
547 pw
= "/usr/share/doom2d-rembo/doom2d.wad";
554 F_addwad("doom2d.wad");
556 CFG_args(argc
, argv
);
565 ticks
= SDL_GetTicks();
566 #ifdef __EMSCRIPTEN__
567 emscripten_set_main_loop(step
, 0, 1);