9227725c9876a90f48dd1c9380ab3695acda90ae
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
31 #include <string.h> // strcasecmp
37 #include "my.h" // fexists
38 #include "player.h" // pl1 pl2
39 #include "menu.h" // G_keyf
40 #include "error.h" // logo
42 #include "files.h" // F_startup F_addwad F_initwads F_allocres
43 #include "config.h" // CFG_args CFG_load CFG_save
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
;
63 void logo (const char *s
, ...) {
71 void logo_gas (int cur
, int all
) {
75 void ERR_failinit (char *s
, ...) {
84 void ERR_fatal (char *s
, ...) {
91 puts("\nКРИТИЧЕСКАЯ ОШИБКА:");
99 void ERR_quit (void) {
103 /* --- system.h --- */
105 int Y_set_videomode_opengl (int w
, int h
, int fullscreen
) {
110 if (mode
== MODE_OPENGL
&& surf
->w
== w
&& surf
->h
== h
&& Y_get_fullscreen() == fullscreen
) {
113 flags
= SDL_DOUBLEBUF
| SDL_OPENGL
;
115 flags
= flags
| SDL_FULLSCREEN
;
118 flags
= flags
| SDL_RESIZABLE
;
120 s
= SDL_SetVideoMode(w
, h
, 0, flags
);
125 logo("Y_set_videomode_opengl: error: %s\n", SDL_GetError());
131 int Y_set_videomode_software (int w
, int h
, int fullscreen
) {
136 if (mode
== MODE_OPENGL
&& surf
->w
== w
&& surf
->h
== h
&& Y_get_fullscreen() == fullscreen
) {
139 flags
= SDL_DOUBLEBUF
| SDL_SWSURFACE
| SDL_HWPALETTE
;
141 flags
= flags
| SDL_FULLSCREEN
;
144 flags
= flags
| SDL_RESIZABLE
;
146 s
= SDL_SetVideoMode(w
, h
, 8, flags
);
148 mode
= MODE_SOFTWARE
;
155 static void init_videomode_list (Uint32 flags
) {
158 if (vlist
.modes
!= NULL
) {
163 r
= SDL_ListModes(NULL
, flags
);
164 if (r
== (SDL_Rect
**)-1) {
165 if ((flags
& SDL_FULLSCREEN
) == 0) {
166 init_videomode_list(flags
| SDL_FULLSCREEN
);
168 } else if (r
!= (SDL_Rect
**)0) {
170 while (r
[n
] != NULL
) {
173 vlist
.modes
= malloc(n
* sizeof(videomode_size_t
));
174 if (vlist
.modes
!= NULL
) {
176 for (i
= 0; i
< n
; i
++) {
177 vlist
.modes
[i
] = (videomode_size_t
) {
186 const videomode_t
*Y_get_videomode_list_opengl (int fullscreen
) {
187 init_videomode_list(SDL_OPENGL
| (fullscreen
? SDL_FULLSCREEN
: 0));
191 const videomode_t
*Y_get_videomode_list_software (int fullscreen
) {
192 init_videomode_list(SDL_SWSURFACE
| SDL_HWPALETTE
| (fullscreen
? SDL_FULLSCREEN
: 0));
196 void Y_get_videomode (int *w
, int *h
) {
197 if (mode
!= MODE_NONE
) {
206 int Y_videomode_setted (void) {
207 return mode
!= MODE_NONE
;
210 void Y_unset_videomode (void) {
213 #ifndef __EMSCRIPTEN__
214 SDL_QuitSubSystem(SDL_INIT_VIDEO
);
215 SDL_InitSubSystem(SDL_INIT_VIDEO
);
219 void Y_set_fullscreen (int fullscreen
) {
220 int fs
= Y_get_fullscreen();
221 if (mode
!= MODE_NONE
&& fs
!= fullscreen
) {
222 if (SDL_WM_ToggleFullScreen(surf
) == 0) {
225 Y_set_videomode_opengl(surf
->w
, surf
->h
, fullscreen
);
228 Y_set_videomode_software(surf
->w
, surf
->h
, fullscreen
);
235 int Y_get_fullscreen (void) {
236 return (mode
!= MODE_NONE
) && ((surf
->flags
& SDL_FULLSCREEN
) != 0);
239 void Y_swap_buffers (void) {
240 assert(mode
== MODE_OPENGL
);
241 SDL_GL_SwapBuffers();
244 void Y_get_buffer (byte
**buf
, int *w
, int *h
, int *pitch
) {
245 assert(mode
== MODE_SOFTWARE
);
249 *pitch
= surf
->pitch
;
252 void Y_set_vga_palette (byte
*vgapal
) {
255 assert(vgapal
!= NULL
);
256 assert(mode
== MODE_SOFTWARE
);
257 SDL_Color colors
[256];
258 for (i
= 0; i
< 256; i
++) {
259 colors
[i
] = (SDL_Color
) {
260 .r
= p
[0] * 255 / 63,
261 .g
= p
[1] * 255 / 63,
266 SDL_SetColors(surf
, colors
, 0, 256);
269 void Y_repaint_rect (int x
, int y
, int w
, int h
) {
270 assert(mode
== MODE_SOFTWARE
);
271 SDL_UpdateRect(surf
, x
, y
, w
, h
);
274 void Y_repaint (void) {
275 assert(mode
== MODE_SOFTWARE
);
279 void Y_enable_text_input (void) {
283 void Y_disable_text_input (void) {
289 static int sdl_to_key (int code
) {
291 case SDLK_0
: return KEY_0
;
292 case SDLK_1
: return KEY_1
;
293 case SDLK_2
: return KEY_2
;
294 case SDLK_3
: return KEY_3
;
295 case SDLK_4
: return KEY_4
;
296 case SDLK_5
: return KEY_5
;
297 case SDLK_6
: return KEY_6
;
298 case SDLK_7
: return KEY_7
;
299 case SDLK_8
: return KEY_8
;
300 case SDLK_9
: return KEY_9
;
301 case SDLK_a
: return KEY_A
;
302 case SDLK_b
: return KEY_B
;
303 case SDLK_c
: return KEY_C
;
304 case SDLK_d
: return KEY_D
;
305 case SDLK_e
: return KEY_E
;
306 case SDLK_f
: return KEY_F
;
307 case SDLK_g
: return KEY_G
;
308 case SDLK_h
: return KEY_H
;
309 case SDLK_i
: return KEY_I
;
310 case SDLK_j
: return KEY_J
;
311 case SDLK_k
: return KEY_K
;
312 case SDLK_l
: return KEY_L
;
313 case SDLK_m
: return KEY_M
;
314 case SDLK_n
: return KEY_N
;
315 case SDLK_o
: return KEY_O
;
316 case SDLK_p
: return KEY_P
;
317 case SDLK_q
: return KEY_Q
;
318 case SDLK_r
: return KEY_R
;
319 case SDLK_s
: return KEY_S
;
320 case SDLK_t
: return KEY_T
;
321 case SDLK_u
: return KEY_U
;
322 case SDLK_v
: return KEY_V
;
323 case SDLK_w
: return KEY_W
;
324 case SDLK_x
: return KEY_X
;
325 case SDLK_y
: return KEY_Y
;
326 case SDLK_z
: return KEY_Z
;
327 case SDLK_RETURN
: return KEY_RETURN
;
328 case SDLK_ESCAPE
: return KEY_ESCAPE
;
329 case SDLK_BACKSPACE
: return KEY_BACKSPACE
;
330 case SDLK_TAB
: return KEY_TAB
;
331 case SDLK_SPACE
: return KEY_SPACE
;
332 case SDLK_MINUS
: return KEY_MINUS
;
333 case SDLK_EQUALS
: return KEY_EQUALS
;
334 case SDLK_LEFTBRACKET
: return KEY_LEFTBRACKET
;
335 case SDLK_RIGHTBRACKET
: return KEY_RIGHTBRACKET
;
336 case SDLK_BACKSLASH
: return KEY_BACKSLASH
;
337 case SDLK_SEMICOLON
: return KEY_SEMICOLON
;
338 case SDLK_QUOTE
: return KEY_APOSTROPHE
;
339 case SDLK_BACKQUOTE
: return KEY_GRAVE
;
340 case SDLK_COMMA
: return KEY_COMMA
;
341 case SDLK_PERIOD
: return KEY_PERIOD
;
342 case SDLK_SLASH
: return KEY_SLASH
;
343 case SDLK_CAPSLOCK
: return KEY_CAPSLOCK
;
344 case SDLK_F1
: return KEY_F1
;
345 case SDLK_F2
: return KEY_F2
;
346 case SDLK_F3
: return KEY_F3
;
347 case SDLK_F4
: return KEY_F4
;
348 case SDLK_F5
: return KEY_F5
;
349 case SDLK_F6
: return KEY_F6
;
350 case SDLK_F7
: return KEY_F7
;
351 case SDLK_F8
: return KEY_F8
;
352 case SDLK_F9
: return KEY_F9
;
353 case SDLK_F10
: return KEY_F10
;
354 case SDLK_F11
: return KEY_F11
;
355 case SDLK_F12
: return KEY_F12
;
356 case SDLK_PRINT
: return KEY_PRINTSCREEN
;
357 case SDLK_SCROLLOCK
: return KEY_SCROLLLOCK
;
358 case SDLK_PAUSE
: return KEY_PAUSE
;
359 case SDLK_INSERT
: return KEY_INSERT
;
360 case SDLK_HOME
: return KEY_HOME
;
361 case SDLK_PAGEUP
: return KEY_PAGEUP
;
362 case SDLK_DELETE
: return KEY_DELETE
;
363 case SDLK_END
: return KEY_END
;
364 case SDLK_PAGEDOWN
: return KEY_PAGEDOWN
;
365 case SDLK_RIGHT
: return KEY_RIGHT
;
366 case SDLK_LEFT
: return KEY_LEFT
;
367 case SDLK_DOWN
: return KEY_DOWN
;
368 case SDLK_UP
: return KEY_UP
;
369 case SDLK_NUMLOCK
: return KEY_NUMLOCK
;
370 case SDLK_KP_DIVIDE
: return KEY_KP_DIVIDE
;
371 case SDLK_KP_MULTIPLY
: return KEY_KP_MULTIPLY
;
372 case SDLK_KP_MINUS
: return KEY_KP_MINUS
;
373 case SDLK_KP_PLUS
: return KEY_KP_PLUS
;
374 case SDLK_KP_ENTER
: return KEY_KP_ENTER
;
375 case SDLK_KP0
: return KEY_KP_0
;
376 case SDLK_KP1
: return KEY_KP_1
;
377 case SDLK_KP2
: return KEY_KP_2
;
378 case SDLK_KP3
: return KEY_KP_3
;
379 case SDLK_KP4
: return KEY_KP_4
;
380 case SDLK_KP5
: return KEY_KP_5
;
381 case SDLK_KP6
: return KEY_KP_6
;
382 case SDLK_KP7
: return KEY_KP_7
;
383 case SDLK_KP8
: return KEY_KP_8
;
384 case SDLK_KP9
: return KEY_KP_9
;
385 case SDLK_KP_PERIOD
: return KEY_KP_PERIOD
;
386 case SDLK_SYSREQ
: return KEY_SYSREQ
;
387 case SDLK_LCTRL
: return KEY_LCTRL
;
388 case SDLK_LSHIFT
: return KEY_LSHIFT
;
389 case SDLK_LALT
: return KEY_LALT
;
390 case SDLK_LSUPER
: return KEY_LSUPER
;
391 case SDLK_RCTRL
: return KEY_RCTRL
;
392 case SDLK_RSHIFT
: return KEY_RSHIFT
;
393 case SDLK_RALT
: return KEY_RALT
;
394 case SDLK_RSUPER
: return KEY_RSUPER
;
395 default: return KEY_UNKNOWN
;
399 static void poll_events (void) {
402 while (SDL_PollEvent(&ev
)) {
407 case SDL_VIDEORESIZE
:
408 R_set_videomode(ev
.resize
.w
, ev
.resize
.h
, Y_get_fullscreen());
412 sym
= ev
.key
.keysym
.sym
;
413 down
= ev
.type
== SDL_KEYDOWN
;
414 key
= sdl_to_key(sym
);
417 if (down
&& text_input
&& sym
>= 0x20 && sym
<= 0x7e) {
426 static void step (void) {
429 Uint32 t
= SDL_GetTicks();
430 if (t
- ticks
> DELAY
) {
437 int main (int argc
, char *argv
[]) {
439 logo("main: initialize SDL\n");
440 if (SDL_Init(SDL_INIT_VIDEO
| SDL_INIT_TIMER
) == -1) {
441 logo("main: failed to init SDL: %s\n", SDL_GetError());
444 SDL_WM_SetCaption("Doom 2D (SDL)", "Doom 2D");
450 pl1
.kf
= KEY_PAGEDOWN
;
465 srand(SDL_GetTicks());
468 pw
= "/usr/share/doom2d-rembo/doom2d.wad";
475 F_addwad("doom2d.wad");
477 CFG_args(argc
, argv
);
486 ticks
= SDL_GetTicks();
487 #ifdef __EMSCRIPTEN__
488 emscripten_set_main_loop(step
, 0, 1);