X-Git-Url: https://deadsoftware.ru/gitweb?a=blobdiff_plain;f=src%2Fsdl%2Fmain.c;h=9227725c9876a90f48dd1c9380ab3695acda90ae;hb=4a0a2f1f4922d5346c4aee584e7d4a13803bffab;hp=07dccf98f5fee9198b1f4f748165619438e402b3;hpb=9b7dd9e2d0728c50f830fff8f5f7c7caa87e5b6f;p=flatwaifu.git diff --git a/src/sdl/main.c b/src/sdl/main.c index 07dccf9..9227725 100644 --- a/src/sdl/main.c +++ b/src/sdl/main.c @@ -20,11 +20,16 @@ 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ -#include +#ifdef __EMSCRIPTEN__ +# include +#endif + +#include #include #include #include // srand exit #include // strcasecmp +#include #include #include "system.h" #include "input.h" @@ -42,8 +47,16 @@ #include "music.h" // S_initmusic S_updatemusic S_donemusic #include "render.h" // R_init R_draw R_done +#define MODE_NONE 0 +#define MODE_OPENGL 1 +#define MODE_SOFTWARE 2 + +static Uint32 ticks; static int quit = 0; static SDL_Surface *surf = NULL; +static int mode = MODE_NONE; +static int text_input; +static videomode_t vlist; /* --- error.h --- */ @@ -84,39 +97,104 @@ void ERR_fatal (char *s, ...) { } void ERR_quit (void) { - puts("Спасибо за то, что вы играли в Операцию \"Смятка\"!"); - //F_loadres(F_getresid("ENDOOM"),p,0,4000); quit = 1; } /* --- system.h --- */ -int Y_set_videomode (int w, int h, int flags) { - SDL_Surface *s; - int colors; - Uint32 f; +int Y_set_videomode_opengl (int w, int h, int fullscreen) { assert(w > 0); assert(h > 0); - f = SDL_DOUBLEBUF; - if (flags & SYSTEM_USE_FULLSCREEN) { - f = flags | SDL_FULLSCREEN; - } - if (flags & SYSTEM_USE_OPENGL) { - f = flags | SDL_OPENGL; - colors = 0; + Uint32 flags; + SDL_Surface *s; + if (mode == MODE_OPENGL && surf->w == w && surf->h == h && Y_get_fullscreen() == fullscreen) { + s = surf; } else { - f = flags | SDL_SWSURFACE | SDL_HWPALETTE; - colors = 8; + flags = SDL_DOUBLEBUF | SDL_OPENGL; + if (fullscreen) { + flags = flags | SDL_FULLSCREEN; + } +# ifdef WIN32 + flags = flags | SDL_RESIZABLE; +# endif + s = SDL_SetVideoMode(w, h, 0, flags); + if (s != NULL) { + mode = MODE_OPENGL; + surf = s; + } else { + logo("Y_set_videomode_opengl: error: %s\n", SDL_GetError()); + } } - s = SDL_SetVideoMode(w, h, colors, f); - if (s != NULL) { - surf = s; + return s != NULL; +} + +int Y_set_videomode_software (int w, int h, int fullscreen) { + assert(w > 0); + assert(h > 0); + Uint32 flags; + SDL_Surface *s; + if (mode == MODE_OPENGL && surf->w == w && surf->h == h && Y_get_fullscreen() == fullscreen) { + s = surf; + } else { + flags = SDL_DOUBLEBUF | SDL_SWSURFACE | SDL_HWPALETTE; + if (fullscreen) { + flags = flags | SDL_FULLSCREEN; + } +# ifdef WIN32 + flags = flags | SDL_RESIZABLE; +# endif + s = SDL_SetVideoMode(w, h, 8, flags); + if (s != NULL) { + mode = MODE_SOFTWARE; + surf = s; + } } return s != NULL; } +static void init_videomode_list (Uint32 flags) { + int i, n; + SDL_Rect **r; + if (vlist.modes != NULL) { + free(vlist.modes); + vlist.modes = NULL; + vlist.n = 0; + } + r = SDL_ListModes(NULL, flags); + if (r == (SDL_Rect **)-1) { + if ((flags & SDL_FULLSCREEN) == 0) { + init_videomode_list(flags | SDL_FULLSCREEN); + } + } else if (r != (SDL_Rect**)0) { + n = 0; + while (r[n] != NULL) { + n++; + } + vlist.modes = malloc(n * sizeof(videomode_size_t)); + if (vlist.modes != NULL) { + vlist.n = n; + for (i = 0; i < n; i++) { + vlist.modes[i] = (videomode_size_t) { + .w = r[i]->w, + .h = r[i]->h + }; + } + } + } +} + +const videomode_t *Y_get_videomode_list_opengl (int fullscreen) { + init_videomode_list(SDL_OPENGL | (fullscreen ? SDL_FULLSCREEN : 0)); + return &vlist; +} + +const videomode_t *Y_get_videomode_list_software (int fullscreen) { + init_videomode_list(SDL_SWSURFACE | SDL_HWPALETTE | (fullscreen ? SDL_FULLSCREEN : 0)); + return &vlist; +} + void Y_get_videomode (int *w, int *h) { - if (surf != NULL) { + if (mode != MODE_NONE) { *w = surf->w; *h = surf->h; } else { @@ -126,37 +204,45 @@ void Y_get_videomode (int *w, int *h) { } int Y_videomode_setted (void) { - return surf != NULL; + return mode != MODE_NONE; } void Y_unset_videomode (void) { surf = NULL; + mode = MODE_NONE; +#ifndef __EMSCRIPTEN__ SDL_QuitSubSystem(SDL_INIT_VIDEO); SDL_InitSubSystem(SDL_INIT_VIDEO); +#endif } -void Y_set_fullscreen (int yes) { - assert(surf != NULL); - Uint32 flags = surf->flags & ~SDL_FULLSCREEN; - if ((surf->flags & SDL_FULLSCREEN) == 0) { - flags |= SDL_FULLSCREEN; +void Y_set_fullscreen (int fullscreen) { + int fs = Y_get_fullscreen(); + if (mode != MODE_NONE && fs != fullscreen) { + if (SDL_WM_ToggleFullScreen(surf) == 0) { + switch (mode) { + case MODE_OPENGL: + Y_set_videomode_opengl(surf->w, surf->h, fullscreen); + break; + case MODE_SOFTWARE: + Y_set_videomode_software(surf->w, surf->h, fullscreen); + break; + } + } } - Y_set_videomode(surf->w, surf->h, flags); } int Y_get_fullscreen (void) { - return (surf != NULL) && ((surf->flags & SDL_FULLSCREEN) != 0); + return (mode != MODE_NONE) && ((surf->flags & SDL_FULLSCREEN) != 0); } void Y_swap_buffers (void) { - assert(surf != NULL); - assert(surf->flags & SDL_OPENGL); + assert(mode == MODE_OPENGL); SDL_GL_SwapBuffers(); } void Y_get_buffer (byte **buf, int *w, int *h, int *pitch) { - assert(surf != NULL); - assert((surf->flags & SDL_OPENGL) == 0); + assert(mode == MODE_SOFTWARE); *buf = surf->pixels; *w = surf->w; *h = surf->h; @@ -167,8 +253,7 @@ void Y_set_vga_palette (byte *vgapal) { int i; byte *p = vgapal; assert(vgapal != NULL); - assert(surf != NULL); - assert((surf->flags & SDL_OPENGL) == 0); + assert(mode == MODE_SOFTWARE); SDL_Color colors[256]; for (i = 0; i < 256; i++) { colors[i] = (SDL_Color) { @@ -182,17 +267,23 @@ void Y_set_vga_palette (byte *vgapal) { } void Y_repaint_rect (int x, int y, int w, int h) { - assert(surf != NULL); - assert((surf->flags & SDL_OPENGL) == 0); + assert(mode == MODE_SOFTWARE); SDL_UpdateRect(surf, x, y, w, h); } void Y_repaint (void) { - assert(surf != NULL); - assert((surf->flags & SDL_OPENGL) == 0); + assert(mode == MODE_SOFTWARE); SDL_Flip(surf); } +void Y_enable_text_input (void) { + text_input = 1; +} + +void Y_disable_text_input (void) { + text_input = 0; +} + /* --- main --- */ static int sdl_to_key (int code) { @@ -305,35 +396,52 @@ static int sdl_to_key (int code) { } } -static void poll_events (void (*h)(int key, int down)) { - int key; +static void poll_events (void) { + int key, sym, down; SDL_Event ev; while (SDL_PollEvent(&ev)) { switch (ev.type) { case SDL_QUIT: ERR_quit(); break; + case SDL_VIDEORESIZE: + R_set_videomode(ev.resize.w, ev.resize.h, Y_get_fullscreen()); + break; case SDL_KEYDOWN: case SDL_KEYUP: - key = sdl_to_key(ev.key.keysym.sym); - I_press(key, ev.type == SDL_KEYDOWN); - if (h != NULL) { - (*h)(key, ev.type == SDL_KEYDOWN); + sym = ev.key.keysym.sym; + down = ev.type == SDL_KEYDOWN; + key = sdl_to_key(sym); + I_press(key, down); + GM_key(key, down); + if (down && text_input && sym >= 0x20 && sym <= 0x7e) { + // TODO fix this + GM_input(sym); } break; } } } +static void step (void) { + poll_events(); + S_updatemusic(); + Uint32 t = SDL_GetTicks(); + if (t - ticks > DELAY) { + ticks = t; + G_act(); + } + R_draw(); +} + int main (int argc, char *argv[]) { char *pw; - Uint32 t, ticks; logo("main: initialize SDL\n"); if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1) { logo("main: failed to init SDL: %s\n", SDL_GetError()); return 1; } - SDL_WM_SetCaption("Doom 2D v1.351", "Doom 2D"); + SDL_WM_SetCaption("Doom 2D (SDL)", "Doom 2D"); // Player 1 defaults pl1.ku = KEY_KP_8; pl1.kd = KEY_KP_5; @@ -376,16 +484,13 @@ int main (int argc, char *argv[]) { R_init(); G_init(); ticks = SDL_GetTicks(); +#ifdef __EMSCRIPTEN__ + emscripten_set_main_loop(step, 0, 1); +#else while (!quit) { - poll_events(&G_keyf); - S_updatemusic(); - t = SDL_GetTicks(); - if (t - ticks > DELAY) { - ticks = t; - G_act(); - } - R_draw(); + step(); } +#endif CFG_save(); R_done(); S_donemusic();