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.
This commit is contained in:
Misa 2024-01-09 15:05:08 -08:00 committed by Misa Elizabeth Kai
parent 40f6f83328
commit 10030a4340
2 changed files with 29 additions and 4 deletions

View File

@ -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)

View File

@ -1,5 +1,6 @@
#include "GraphicsResources.h"
#include <time.h>
#include <tinyxml2.h>
#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;
}