Skip to content

Commit

Permalink
Add ‘gl_screenshot_template’ variable.
Browse files Browse the repository at this point in the history
Also properly create full path for user supplied screenshot name, which
may contain slashes.
  • Loading branch information
skullernet committed Feb 16, 2022
1 parent a43863f commit b078135
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 10 deletions.
6 changes: 6 additions & 0 deletions doc/client.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,12 @@ gl_screenshot_async::
- 1 — save PNG screenshots in background thread
- 2 — save JPG and PNG screenshots in background thread

gl_screenshot_template::
Specifies filename template in "fileXXX" format for ‘screenshot’ command.
Template must contain at least 3 and at most 9 consecutive ‘X’ in the last
component. Template may contain slashes to save under subdirectory. Default
value is "quakeXXX".

r_override_textures::
Enables automatic overriding of palettized textures (in WAL or PCX format)
with truecolor replacements (in PNG, JPG or TGA format) by stripping off
Expand Down
59 changes: 49 additions & 10 deletions src/refresh/images.c
Original file line number Diff line number Diff line change
Expand Up @@ -1107,18 +1107,39 @@ static cvar_t *r_screenshot_compression;
#endif

#if USE_TGA || USE_JPG || USE_PNG
static cvar_t *r_screenshot_template;

static int suffix_pos(const char *s, int ch)
{
int pos = strlen(s);
while (pos > 0 && s[pos - 1] == ch)
pos--;
return pos;
}

static int parse_template(cvar_t *var, char *buffer, size_t size)
{
if (FS_NormalizePathBuffer(buffer, var->string, size) < size) {
FS_CleanupPath(buffer);
int start = suffix_pos(buffer, 'X');
int width = strlen(buffer) - start;
buffer[start] = 0;
if (width >= 3 && width <= 9)
return width;
}

Com_WPrintf("Bad value '%s' for '%s'. Falling back to '%s'.\n",
var->string, var->name, var->default_string);
Cvar_Reset(var);
Q_strlcpy(buffer, "quake", size);
return 3;
}

static int create_screenshot(char *buffer, size_t size, FILE **f,
const char *name, const char *ext)
{
char temp[MAX_OSPATH];
int i, ret;

if (Q_snprintf(temp, sizeof(temp), "%s/screenshots/", fs_gamedir) >= sizeof(temp)) {
return Q_ERR(ENAMETOOLONG);
}
if ((ret = FS_CreatePath(temp)) < 0) {
return ret;
}
int i, ret, width, count;

if (name && *name) {
// save to user supplied name
Expand All @@ -1129,15 +1150,32 @@ static int create_screenshot(char *buffer, size_t size, FILE **f,
if (Q_snprintf(buffer, size, "%s/screenshots/%s%s", fs_gamedir, temp, ext) >= size) {
return Q_ERR(ENAMETOOLONG);
}
if ((ret = FS_CreatePath(buffer)) < 0) {
return ret;
}
if (!(*f = fopen(buffer, "wb"))) {
return Q_ERRNO;
}
return 0;
}

width = parse_template(r_screenshot_template, temp, sizeof(temp));

// create the directory
if (Q_snprintf(buffer, size, "%s/screenshots/%s", fs_gamedir, temp) >= size) {
return Q_ERR(ENAMETOOLONG);
}
if ((ret = FS_CreatePath(buffer)) < 0) {
return ret;
}

count = 1;
for (i = 0; i < width; i++)
count *= 10;

// find a file name to save it to
for (i = 0; i < 1000; i++) {
if (Q_snprintf(buffer, size, "%s/screenshots/quake%03d%s", fs_gamedir, i, ext) >= size) {
for (i = 0; i < count; i++) {
if (Q_snprintf(buffer, size, "%s/screenshots/%s%0*d%s", fs_gamedir, temp, width, i, ext) >= size) {
return Q_ERR(ENAMETOOLONG);
}
if ((*f = Q_fopen(buffer, "wxb"))) {
Expand Down Expand Up @@ -1983,6 +2021,7 @@ void IMG_Init(void)
#if USE_PNG
r_screenshot_compression = Cvar_Get("gl_screenshot_compression", "6", 0);
#endif
r_screenshot_template = Cvar_Get("gl_screenshot_template", "quakeXXX", 0);
#endif // USE_PNG || USE_JPG || USE_TGA

Cmd_Register(img_cmd);
Expand Down

0 comments on commit b078135

Please sign in to comment.