4f529db45c1667da9e7b40e0c3b1e41444174cd2
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
40 #include "files.h" // F_startup F_addwad F_initwads F_allocres
41 #include "config.h" // CFG_args CFG_load CFG_save
42 #include "memory.h" // M_startup
43 #include "game.h" // G_init G_act
44 #include "sound.h" // S_init S_done
45 #include "music.h" // S_initmusic S_updatemusic S_donemusic
46 #include "render.h" // R_init R_draw R_done
50 #define MODE_SOFTWARE 2
54 static SDL_Surface
*surf
= NULL
;
55 static int mode
= MODE_NONE
;
56 static int text_input
;
57 static videomode_t vlist
;
61 void logo (const char *s
, ...) {
69 void logo_gas (int cur
, int all
) {
73 void ERR_failinit (char *s
, ...) {
82 void ERR_fatal (char *s
, ...) {
89 puts("\nКРИТИЧЕСКАЯ ОШИБКА:");
97 void ERR_quit (void) {
101 /* --- system.h --- */
103 int Y_set_videomode_opengl (int w
, int h
, int fullscreen
) {
108 if (mode
== MODE_OPENGL
&& surf
->w
== w
&& surf
->h
== h
&& Y_get_fullscreen() == fullscreen
) {
111 flags
= SDL_DOUBLEBUF
| SDL_OPENGL
;
113 flags
= flags
| SDL_FULLSCREEN
;
116 flags
= flags
| SDL_RESIZABLE
;
118 s
= SDL_SetVideoMode(w
, h
, 0, flags
);
123 logo("Y_set_videomode_opengl: error: %s\n", SDL_GetError());
129 int Y_set_videomode_software (int w
, int h
, int fullscreen
) {
134 if (mode
== MODE_OPENGL
&& surf
->w
== w
&& surf
->h
== h
&& Y_get_fullscreen() == fullscreen
) {
137 flags
= SDL_DOUBLEBUF
| SDL_SWSURFACE
| SDL_HWPALETTE
;
139 flags
= flags
| SDL_FULLSCREEN
;
142 flags
= flags
| SDL_RESIZABLE
;
144 s
= SDL_SetVideoMode(w
, h
, 8, flags
);
146 mode
= MODE_SOFTWARE
;
153 static void init_videomode_list (Uint32 flags
) {
156 if (vlist
.modes
!= NULL
) {
161 r
= SDL_ListModes(NULL
, flags
);
162 if (r
== (SDL_Rect
**)-1) {
163 if ((flags
& SDL_FULLSCREEN
) == 0) {
164 init_videomode_list(flags
| SDL_FULLSCREEN
);
166 } else if (r
!= (SDL_Rect
**)0) {
168 while (r
[n
] != NULL
) {
171 vlist
.modes
= malloc(n
* sizeof(videomode_size_t
));
172 if (vlist
.modes
!= NULL
) {
174 for (i
= 0; i
< n
; i
++) {
175 vlist
.modes
[i
] = (videomode_size_t
) {
184 const videomode_t
*Y_get_videomode_list_opengl (int fullscreen
) {
185 init_videomode_list(SDL_OPENGL
| (fullscreen
? SDL_FULLSCREEN
: 0));
189 const videomode_t
*Y_get_videomode_list_software (int fullscreen
) {
190 init_videomode_list(SDL_SWSURFACE
| SDL_HWPALETTE
| (fullscreen
? SDL_FULLSCREEN
: 0));
194 void Y_get_videomode (int *w
, int *h
) {
195 if (mode
!= MODE_NONE
) {
204 int Y_videomode_setted (void) {
205 return mode
!= MODE_NONE
;
208 void Y_unset_videomode (void) {
211 #ifndef __EMSCRIPTEN__
212 SDL_QuitSubSystem(SDL_INIT_VIDEO
);
213 SDL_InitSubSystem(SDL_INIT_VIDEO
);
217 void Y_set_fullscreen (int fullscreen
) {
218 int fs
= Y_get_fullscreen();
219 if (mode
!= MODE_NONE
&& fs
!= fullscreen
) {
220 if (SDL_WM_ToggleFullScreen(surf
) == 0) {
223 Y_set_videomode_opengl(surf
->w
, surf
->h
, fullscreen
);
226 Y_set_videomode_software(surf
->w
, surf
->h
, fullscreen
);
233 int Y_get_fullscreen (void) {
234 return (mode
!= MODE_NONE
) && ((surf
->flags
& SDL_FULLSCREEN
) != 0);
237 void Y_swap_buffers (void) {
238 assert(mode
== MODE_OPENGL
);
239 SDL_GL_SwapBuffers();
242 void Y_get_buffer (byte
**buf
, int *w
, int *h
, int *pitch
) {
243 assert(mode
== MODE_SOFTWARE
);
247 *pitch
= surf
->pitch
;
250 void Y_set_vga_palette (byte
*vgapal
) {
253 assert(vgapal
!= NULL
);
254 assert(mode
== MODE_SOFTWARE
);
255 SDL_Color colors
[256];
256 for (i
= 0; i
< 256; i
++) {
257 colors
[i
] = (SDL_Color
) {
258 .r
= p
[0] * 255 / 63,
259 .g
= p
[1] * 255 / 63,
264 SDL_SetColors(surf
, colors
, 0, 256);
267 void Y_repaint_rect (int x
, int y
, int w
, int h
) {
268 assert(mode
== MODE_SOFTWARE
);
269 SDL_UpdateRect(surf
, x
, y
, w
, h
);
272 void Y_repaint (void) {
273 assert(mode
== MODE_SOFTWARE
);
277 void Y_enable_text_input (void) {
281 void Y_disable_text_input (void) {
287 static int sdl_to_key (int code
) {
289 case SDLK_0
: return KEY_0
;
290 case SDLK_1
: return KEY_1
;
291 case SDLK_2
: return KEY_2
;
292 case SDLK_3
: return KEY_3
;
293 case SDLK_4
: return KEY_4
;
294 case SDLK_5
: return KEY_5
;
295 case SDLK_6
: return KEY_6
;
296 case SDLK_7
: return KEY_7
;
297 case SDLK_8
: return KEY_8
;
298 case SDLK_9
: return KEY_9
;
299 case SDLK_a
: return KEY_A
;
300 case SDLK_b
: return KEY_B
;
301 case SDLK_c
: return KEY_C
;
302 case SDLK_d
: return KEY_D
;
303 case SDLK_e
: return KEY_E
;
304 case SDLK_f
: return KEY_F
;
305 case SDLK_g
: return KEY_G
;
306 case SDLK_h
: return KEY_H
;
307 case SDLK_i
: return KEY_I
;
308 case SDLK_j
: return KEY_J
;
309 case SDLK_k
: return KEY_K
;
310 case SDLK_l
: return KEY_L
;
311 case SDLK_m
: return KEY_M
;
312 case SDLK_n
: return KEY_N
;
313 case SDLK_o
: return KEY_O
;
314 case SDLK_p
: return KEY_P
;
315 case SDLK_q
: return KEY_Q
;
316 case SDLK_r
: return KEY_R
;
317 case SDLK_s
: return KEY_S
;
318 case SDLK_t
: return KEY_T
;
319 case SDLK_u
: return KEY_U
;
320 case SDLK_v
: return KEY_V
;
321 case SDLK_w
: return KEY_W
;
322 case SDLK_x
: return KEY_X
;
323 case SDLK_y
: return KEY_Y
;
324 case SDLK_z
: return KEY_Z
;
325 case SDLK_RETURN
: return KEY_RETURN
;
326 case SDLK_ESCAPE
: return KEY_ESCAPE
;
327 case SDLK_BACKSPACE
: return KEY_BACKSPACE
;
328 case SDLK_TAB
: return KEY_TAB
;
329 case SDLK_SPACE
: return KEY_SPACE
;
330 case SDLK_MINUS
: return KEY_MINUS
;
331 case SDLK_EQUALS
: return KEY_EQUALS
;
332 case SDLK_LEFTBRACKET
: return KEY_LEFTBRACKET
;
333 case SDLK_RIGHTBRACKET
: return KEY_RIGHTBRACKET
;
334 case SDLK_BACKSLASH
: return KEY_BACKSLASH
;
335 case SDLK_SEMICOLON
: return KEY_SEMICOLON
;
336 case SDLK_QUOTE
: return KEY_APOSTROPHE
;
337 case SDLK_BACKQUOTE
: return KEY_GRAVE
;
338 case SDLK_COMMA
: return KEY_COMMA
;
339 case SDLK_PERIOD
: return KEY_PERIOD
;
340 case SDLK_SLASH
: return KEY_SLASH
;
341 case SDLK_CAPSLOCK
: return KEY_CAPSLOCK
;
342 case SDLK_F1
: return KEY_F1
;
343 case SDLK_F2
: return KEY_F2
;
344 case SDLK_F3
: return KEY_F3
;
345 case SDLK_F4
: return KEY_F4
;
346 case SDLK_F5
: return KEY_F5
;
347 case SDLK_F6
: return KEY_F6
;
348 case SDLK_F7
: return KEY_F7
;
349 case SDLK_F8
: return KEY_F8
;
350 case SDLK_F9
: return KEY_F9
;
351 case SDLK_F10
: return KEY_F10
;
352 case SDLK_F11
: return KEY_F11
;
353 case SDLK_F12
: return KEY_F12
;
354 case SDLK_PRINT
: return KEY_PRINTSCREEN
;
355 case SDLK_SCROLLOCK
: return KEY_SCROLLLOCK
;
356 case SDLK_PAUSE
: return KEY_PAUSE
;
357 case SDLK_INSERT
: return KEY_INSERT
;
358 case SDLK_HOME
: return KEY_HOME
;
359 case SDLK_PAGEUP
: return KEY_PAGEUP
;
360 case SDLK_DELETE
: return KEY_DELETE
;
361 case SDLK_END
: return KEY_END
;
362 case SDLK_PAGEDOWN
: return KEY_PAGEDOWN
;
363 case SDLK_RIGHT
: return KEY_RIGHT
;
364 case SDLK_LEFT
: return KEY_LEFT
;
365 case SDLK_DOWN
: return KEY_DOWN
;
366 case SDLK_UP
: return KEY_UP
;
367 case SDLK_NUMLOCK
: return KEY_NUMLOCK
;
368 case SDLK_KP_DIVIDE
: return KEY_KP_DIVIDE
;
369 case SDLK_KP_MULTIPLY
: return KEY_KP_MULTIPLY
;
370 case SDLK_KP_MINUS
: return KEY_KP_MINUS
;
371 case SDLK_KP_PLUS
: return KEY_KP_PLUS
;
372 case SDLK_KP_ENTER
: return KEY_KP_ENTER
;
373 case SDLK_KP0
: return KEY_KP_0
;
374 case SDLK_KP1
: return KEY_KP_1
;
375 case SDLK_KP2
: return KEY_KP_2
;
376 case SDLK_KP3
: return KEY_KP_3
;
377 case SDLK_KP4
: return KEY_KP_4
;
378 case SDLK_KP5
: return KEY_KP_5
;
379 case SDLK_KP6
: return KEY_KP_6
;
380 case SDLK_KP7
: return KEY_KP_7
;
381 case SDLK_KP8
: return KEY_KP_8
;
382 case SDLK_KP9
: return KEY_KP_9
;
383 case SDLK_KP_PERIOD
: return KEY_KP_PERIOD
;
384 case SDLK_SYSREQ
: return KEY_SYSREQ
;
385 case SDLK_LCTRL
: return KEY_LCTRL
;
386 case SDLK_LSHIFT
: return KEY_LSHIFT
;
387 case SDLK_LALT
: return KEY_LALT
;
388 case SDLK_LSUPER
: return KEY_LSUPER
;
389 case SDLK_RCTRL
: return KEY_RCTRL
;
390 case SDLK_RSHIFT
: return KEY_RSHIFT
;
391 case SDLK_RALT
: return KEY_RALT
;
392 case SDLK_RSUPER
: return KEY_RSUPER
;
393 default: return KEY_UNKNOWN
;
397 static void poll_events (void) {
400 while (SDL_PollEvent(&ev
)) {
405 case SDL_VIDEORESIZE
:
406 R_set_videomode(ev
.resize
.w
, ev
.resize
.h
, Y_get_fullscreen());
410 sym
= ev
.key
.keysym
.sym
;
411 down
= ev
.type
== SDL_KEYDOWN
;
412 key
= sdl_to_key(sym
);
415 if (down
&& text_input
&& sym
>= 0x20 && sym
<= 0x7e) {
424 static void step (void) {
427 Uint32 t
= SDL_GetTicks();
428 if (t
- ticks
> DELAY
) {
435 int main (int argc
, char *argv
[]) {
437 logo("main: initialize SDL\n");
438 if (SDL_Init(SDL_INIT_VIDEO
| SDL_INIT_TIMER
) == -1) {
439 logo("main: failed to init SDL: %s\n", SDL_GetError());
442 SDL_WM_SetCaption("Doom 2D (SDL)", "Doom 2D");
448 pl1
.kf
= KEY_PAGEDOWN
;
463 srand(SDL_GetTicks());
466 pw
= "/usr/share/doom2d-rembo/doom2d.wad";
473 F_addwad("doom2d.wad");
475 CFG_args(argc
, argv
);
484 ticks
= SDL_GetTicks();
485 #ifdef __EMSCRIPTEN__
486 emscripten_set_main_loop(step
, 0, 1);