From 10030a43402efd5fb95a02cf272808a26723b887 Mon Sep 17 00:00:00 2001 From: Misa Date: Tue, 9 Jan 2024 15:05:08 -0800 Subject: [PATCH] Save screenshots with proper filenames Filenames are timestamped now, down to the second. If you take multiple screenshots in the same second, then the last one will overwrite the others. This seems to be how other screenshot programs operate so I don't think it matters if you can't take more than one per second. Additionally, 1x screenshots (320x240) will go in the 1x/ subdirectory, and 2x screenshots (640x480) will go in the 2x/ subdirectory. --- desktop_version/src/FileSystemUtils.cpp | 13 +++++++++++++ desktop_version/src/GraphicsResources.cpp | 20 ++++++++++++++++---- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/desktop_version/src/FileSystemUtils.cpp b/desktop_version/src/FileSystemUtils.cpp index f23f86d3..5643182d 100644 --- a/desktop_version/src/FileSystemUtils.cpp +++ b/desktop_version/src/FileSystemUtils.cpp @@ -270,6 +270,19 @@ int FILESYSTEM_init(char *argvZero, char* baseDir, char *assetsPath, char* langD mkdir(screenshotDir, 0777); vlog_info("Screenshot directory: %s", screenshotDir); + /* We also need to make the subdirectories */ + { + char temp[MAX_PATH]; + SDL_snprintf(temp, sizeof(temp), "%s%s%s", + screenshotDir, "1x", pathSep + ); + mkdir(temp, 0777); + SDL_snprintf(temp, sizeof(temp), "%s%s%s", + screenshotDir, "2x", pathSep + ); + mkdir(temp, 0777); + } + basePath = SDL_GetBasePath(); if (basePath == NULL) diff --git a/desktop_version/src/GraphicsResources.cpp b/desktop_version/src/GraphicsResources.cpp index be87f821..4410085f 100644 --- a/desktop_version/src/GraphicsResources.cpp +++ b/desktop_version/src/GraphicsResources.cpp @@ -1,5 +1,6 @@ #include "GraphicsResources.h" +#include #include #include "Alloc.h" @@ -516,8 +517,17 @@ bool SaveScreenshot(void) vlog_error("Could not take screenshot"); return false; } - // TODO: Timestamp in filename - success = SaveImage(graphics.tempScreenshot, "screenshots/test.png"); + + const time_t now = time(NULL); + const tm* date = localtime(&now); + + char timestamp[32]; + strftime(timestamp, sizeof(timestamp), "%Y-%m-%d_%H-%M-%S", date); + + char filename[64]; + SDL_snprintf(filename, sizeof(filename), "screenshots/1x/%s_1x.png", timestamp); + + success = SaveImage(graphics.tempScreenshot, filename); if (!success) { return false; @@ -530,12 +540,14 @@ bool SaveScreenshot(void) return false; } - success = SaveImage(graphics.tempScreenshot2x, "screenshots/test2x.png"); + SDL_snprintf(filename, sizeof(filename), "screenshots/2x/%s_2x.png", timestamp); + + success = SaveImage(graphics.tempScreenshot2x, filename); if (!success) { return false; } - vlog_info("Saved screenshot"); + vlog_info("Saved screenshot %s", timestamp); return true; }