From f28130787c656468f8c5703896db0dd12cfabd4b Mon Sep 17 00:00:00 2001 From: erysdren Date: Sun, 13 Oct 2024 10:22:39 -0500 Subject: [PATCH] sdl3: screenshot functionality --- platform/sdl3/main.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/platform/sdl3/main.c b/platform/sdl3/main.c index 136a6a5..e545066 100644 --- a/platform/sdl3/main.c +++ b/platform/sdl3/main.c @@ -24,6 +24,8 @@ static Sint64 now = 0, then = 0; static int requested_map = -1; +static int screenshot_idx = 0; + static void die(const char *fmt, ...) { static char error[1024]; @@ -145,6 +147,19 @@ SDL_AppResult SDL_AppIterate(void *appstate) return SDL_APP_CONTINUE; } +static bool file_exists(const char *filename) +{ + SDL_IOStream *io = SDL_IOFromFile(filename, "rb"); + + if (io) + { + SDL_CloseIO(io); + return true; + } + + return false; +} + SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) { switch (event->type) @@ -158,6 +173,15 @@ SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) focused = !focused; SDL_SetWindowRelativeMouseMode(window, focused); } + else if (event->key.scancode == SDL_SCANCODE_F12) + { + char filename[256]; + SDL_snprintf(filename, sizeof(filename), "yeti%03d.bmp", screenshot_idx++); + while (file_exists(filename)) + SDL_snprintf(filename, sizeof(filename), "yeti%03d.bmp", screenshot_idx++); + SDL_SaveBMP(back, filename); + SDL_Log("Saved screenshot %s", filename); + } break; case SDL_EVENT_MOUSE_MOTION: