sdl3: screenshot functionality

This commit is contained in:
erysdren 2024-10-13 10:22:39 -05:00
parent ffb24fc06f
commit f28130787c

View file

@ -24,6 +24,8 @@ static Sint64 now = 0, then = 0;
static int requested_map = -1; static int requested_map = -1;
static int screenshot_idx = 0;
static void die(const char *fmt, ...) static void die(const char *fmt, ...)
{ {
static char error[1024]; static char error[1024];
@ -145,6 +147,19 @@ SDL_AppResult SDL_AppIterate(void *appstate)
return SDL_APP_CONTINUE; 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) SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
{ {
switch (event->type) switch (event->type)
@ -158,6 +173,15 @@ SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
focused = !focused; focused = !focused;
SDL_SetWindowRelativeMouseMode(window, 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; break;
case SDL_EVENT_MOUSE_MOTION: case SDL_EVENT_MOUSE_MOTION: