From ab09e5f2cc148b7cf9eb81d050f45e3f80904b9b Mon Sep 17 00:00:00 2001 From: Xavier Del Campo Romero Date: Thu, 24 Feb 2022 21:14:23 +0100 Subject: sdl-1.2: implement env.c --- src/gfx/sdl-1.2/src/env.c | 89 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 82 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/gfx/sdl-1.2/src/env.c b/src/gfx/sdl-1.2/src/env.c index d6d1f33..d77503d 100644 --- a/src/gfx/sdl-1.2/src/env.c +++ b/src/gfx/sdl-1.2/src/env.c @@ -2,23 +2,46 @@ #include #include #include +#include #include +#include +#include int screen_w = SCREEN_W, screen_h = SCREEN_H; static bool fullscreen; static SDL_Surface *screen; +static struct sprite **list; +static size_t list_len; void gfx_deinit(void) { - if (screen) - SDL_FreeSurface(screen); - + /* screen should be already freed by SDL_Quit. */ + free(list); SDL_QuitSubSystem(SDL_INIT_VIDEO); } -static int resize_screen(const int w, const int h, const bool full_screen) +static struct { - Uint32 flags = SDL_HWSURFACE | SDL_RESIZABLE | SDL_DOUBLEBUF; + int w, h; +} display; + +void gfx_register_sprite(struct sprite *const s) +{ + /* realloc(3) could had been used directly, but Win9x relies on + * a C89/C90 library implementation, where calling realloc(3) + * with a NULL pointer is undefined behaviour. */ + if (!list) + list = malloc(sizeof *list); + else + list = realloc(list, (list_len + 1) * sizeof *list); + + if (list) + list[list_len++] = s; +} + +static int resize_screen(int w, int h, const bool full_screen) +{ + Uint32 flags = SDL_HWSURFACE | SDL_RESIZABLE | SDL_ANYFORMAT | SDL_DOUBLEBUF; const SDL_VideoInfo *const info = SDL_GetVideoInfo(); @@ -101,7 +124,59 @@ bool gfx_inside_drawenv(const short x, const short y, const short w, && y < screen_h; } -SDL_Surface *gfx_port_screen(void) +static int get_resize_events(void) { - return screen; + int n; + SDL_Event ev; + + while ((n = SDL_PeepEvents(&ev, 1, SDL_GETEVENT, + SDL_VIDEORESIZEMASK)) > 0) + { + if (ev.type == SDL_VIDEORESIZE) + { + const SDL_ResizeEvent *const res = &ev.resize; + + if (resize_screen(res->w, res->h, false)) + return -1; + } + } + + if (n < 0) + { + fprintf(stderr, "%s: SDL_PeepEvents: %s\n", + __func__, SDL_GetError()); + return -1; + } + + return 0; +} + +int gfx_toggle_fullscreen(void) +{ + fullscreen ^= true; + + const int w = fullscreen ? display.w : screen_w; + const int h = fullscreen ? display.h : screen_h; + + if ((resize_screen(w, h, fullscreen))) + return -1; + + return 0; +} + +void gfx_draw(void) +{ + enum {FPS = 50, REFRESH_MS = 1000 / FPS}; + static Uint32 prev; + const Uint32 cur = SDL_GetTicks(); + + if (cur - prev < REFRESH_MS) + SDL_Delay(REFRESH_MS - (cur - prev)); + + prev = SDL_GetTicks(); + + if (SDL_Flip(screen)) + fprintf(stderr, "SDL_Flip: %s\n", SDL_GetError()); + + get_resize_events(); } -- cgit v1.2.3