X-Git-Url: http://deadsoftware.ru/gitweb?a=blobdiff_plain;f=src%2Fsdl%2Fmain.c;h=9227725c9876a90f48dd1c9380ab3695acda90ae;hb=4a0a2f1f4922d5346c4aee584e7d4a13803bffab;hp=3aaad80c00583e818a1401faae5c9793107c6f4a;hpb=de05b18ef2a99168b0a1a8a921a4b18ec7023833;p=flatwaifu.git diff --git a/src/sdl/main.c b/src/sdl/main.c index 3aaad80..9227725 100644 --- a/src/sdl/main.c +++ b/src/sdl/main.c @@ -20,25 +20,271 @@ 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ -#include -#include // srand +#ifdef __EMSCRIPTEN__ +# include +#endif + +#include +#include +#include +#include // srand exit #include // strcasecmp +#include +#include +#include "system.h" #include "input.h" #include "my.h" // fexists #include "player.h" // pl1 pl2 -#include "menu.h" // GM_init G_keyf +#include "menu.h" // G_keyf #include "error.h" // logo #include "files.h" // F_startup F_addwad F_initwads F_allocres -#include "config.h" // CFG_args CFG_load +#include "config.h" // CFG_args CFG_load CFG_save #include "memory.h" // M_startup -#include "game.h" // G_Init G_act +#include "game.h" // G_init G_act #include "sound.h" // S_init S_done #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 --- */ + +void logo (const char *s, ...) { + va_list ap; + va_start(ap, s); + vprintf(s, ap); + va_end(ap); + fflush(stdout); +} + +void logo_gas (int cur, int all) { + // stub +} + +void ERR_failinit (char *s, ...) { + va_list ap; + va_start(ap, s); + vprintf(s, ap); + va_end(ap); + puts(""); + exit(1); +} + +void ERR_fatal (char *s, ...) { + va_list ap; + R_done(); + S_done(); + S_donemusic(); + M_shutdown(); + SDL_Quit(); + puts("\nКРИТИЧЕСКАЯ ОШИБКА:"); + va_start(ap, s); + vprintf(s, ap); + va_end(ap); + puts(""); + exit(1); +} + +void ERR_quit (void) { + 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; +} + +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 (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); +} + +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) { switch (code) { @@ -150,37 +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; } } } -int SDL_main (int argc, char *argv[]) { +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"); - pl1.id = -1; // TODO move to generic code - pl2.id = -2; // TODO move to generic code + SDL_WM_SetCaption("Doom 2D (SDL)", "Doom 2D"); // Player 1 defaults pl1.ku = KEY_KP_8; pl1.kd = KEY_KP_5; @@ -218,24 +479,19 @@ int SDL_main (int argc, char *argv[]) { F_initwads(); M_startup(); F_allocres(); - G_init(); S_init(); S_initmusic(); R_init(); - GM_init(); // TODO move to game - F_loadmus("MENU"); // TODO move to menu - S_startmusic(0); // TODO move to menu + 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(); S_done();