X-Git-Url: http://deadsoftware.ru/gitweb?a=blobdiff_plain;f=src%2Fsdl%2Fmain.c;h=b3627fc2f92ef66cb54a83e39e228d16380aaac9;hb=b047ed3a09b3738defa4f0c323dbe698ff6831ce;hp=ef757fc3b6a0c67c3fa9db17a801df21193f5297;hpb=a79a6e75633c38c64c7f0460f01216bf80703ad3;p=flatwaifu.git diff --git a/src/sdl/main.c b/src/sdl/main.c index ef757fc..b3627fc 100644 --- a/src/sdl/main.c +++ b/src/sdl/main.c @@ -20,11 +20,17 @@ 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 "system.h" #include "input.h" #include "my.h" // fexists @@ -40,7 +46,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; + +/* --- error.h --- */ void logo (const char *s, ...) { va_list ap; @@ -79,11 +94,146 @@ 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_opengl (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_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()); + } + } + 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; +} + +void Y_get_videomode (int *w, int *h) { + if (mode != MODE_NONE) { + *w = surf->w; + *h = surf->h; + } else { + *w = 0; + *h = 0; + } +} + +int Y_videomode_setted (void) { + 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 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; + } + } + } +} + +int Y_get_fullscreen (void) { + return (mode != MODE_NONE) && ((surf->flags & SDL_FULLSCREEN) != 0); +} + +void Y_swap_buffers (void) { + assert(mode == MODE_OPENGL); + SDL_GL_SwapBuffers(); +} + +void Y_get_buffer (byte **buf, int *w, int *h, int *pitch) { + assert(mode == MODE_SOFTWARE); + *buf = surf->pixels; + *w = surf->w; + *h = surf->h; + *pitch = surf->pitch; +} + +void Y_set_vga_palette (byte *vgapal) { + int i; + byte *p = vgapal; + assert(vgapal != NULL); + assert(mode == MODE_SOFTWARE); + SDL_Color colors[256]; + for (i = 0; i < 256; i++) { + colors[i] = (SDL_Color) { + .r = p[0] * 255 / 63, + .g = p[1] * 255 / 63, + .b = p[2] * 255 / 63 + }; + p += 3; + } + SDL_SetColors(surf, colors, 0, 256); +} + +void Y_repaint_rect (int x, int y, int w, int h) { + assert(mode == MODE_SOFTWARE); + SDL_UpdateRect(surf, x, y, w, h); +} + +void Y_repaint (void) { + assert(mode == MODE_SOFTWARE); + SDL_Flip(surf); +} + +/* --- main --- */ + static int sdl_to_key (int code) { switch (code) { case SDLK_0: return KEY_0; @@ -202,6 +352,9 @@ static void poll_events (void (*h)(int key, int down)) { 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); @@ -214,9 +367,19 @@ static void poll_events (void (*h)(int key, int down)) { } } -int SDL_main (int argc, char *argv[]) { +static void step (void) { + poll_events(&G_keyf); + 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()); @@ -265,20 +428,17 @@ int SDL_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_done(); S_donemusic(); + S_done(); M_shutdown(); SDL_Quit(); return 0;