diff options
| author | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2022-02-24 21:14:23 +0100 |
|---|---|---|
| committer | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2022-03-30 08:20:21 +0200 |
| commit | ab09e5f2cc148b7cf9eb81d050f45e3f80904b9b (patch) | |
| tree | e512b9c935e8604ef515a76d3f54a53a294d561d /src | |
| parent | b925e5e156db07494b338e7c130b72f8e3a7725e (diff) | |
| download | jancity-ab09e5f2cc148b7cf9eb81d050f45e3f80904b9b.tar.gz | |
sdl-1.2: implement env.c
Diffstat (limited to 'src')
| -rw-r--r-- | src/gfx/sdl-1.2/src/env.c | 89 |
1 files changed, 82 insertions, 7 deletions
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 <gfx_private.h> #include <sdl-1.2/gfx_private.h> #include <SDL/SDL.h> +#include <inttypes.h> #include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> 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(); } |
