-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tr2/screenshots: improve screenshots support
- Loading branch information
Showing
36 changed files
with
357 additions
and
402 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#include "game/clock.h" | ||
|
||
#include <stdio.h> | ||
#include <time.h> | ||
|
||
size_t Clock_GetDateTime(char *const buffer, const size_t size) | ||
{ | ||
time_t lt = time(0); | ||
struct tm *tptr = localtime(<); | ||
|
||
return snprintf( | ||
buffer, size, "%04d%02d%02d_%02d%02d%02d", tptr->tm_year + 1900, | ||
tptr->tm_mon + 1, tptr->tm_mday, tptr->tm_hour, tptr->tm_min, | ||
tptr->tm_sec); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
#pragma once | ||
|
||
#include <stddef.h> | ||
|
||
size_t Clock_GetDateTime(char *buffer, size_t size); | ||
extern double Clock_GetHighPrecisionCounter(void); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#pragma once | ||
|
||
extern bool Output_MakeScreenshot(const char *path); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
extern Output_MakeScreenshot(const char *const path); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#pragma once | ||
|
||
#include <stdbool.h> | ||
|
||
typedef enum { | ||
SCREENSHOT_FORMAT_JPEG, | ||
SCREENSHOT_FORMAT_PNG, | ||
} SCREENSHOT_FORMAT; | ||
|
||
bool Screenshot_Make(SCREENSHOT_FORMAT format); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
#include "screenshot.h" | ||
|
||
#include "filesystem.h" | ||
#include "game/game.h" | ||
#include "game/gameflow/common.h" | ||
#include "game/output.h" | ||
#include "memory.h" | ||
|
||
#include <stdio.h> | ||
#include <string.h> | ||
|
||
#define SCREENSHOTS_DIR "screenshots" | ||
|
||
static char *M_GetScreenshotName(void); | ||
|
||
static char *M_GetScreenshotName(void) | ||
{ | ||
const int32_t level_num = Game_GetCurrentLevelNum(); | ||
char *level_title = NULL; | ||
if (level_num < 0) { | ||
level_title = Memory_DupStr("Intro"); | ||
} else { | ||
level_title = Memory_DupStr(Gameflow_GetLevelTitle(level_num)); | ||
} | ||
|
||
// Prepare level title for screenshot | ||
char *check = level_title; | ||
bool prev_us = true; // '_' after timestamp before title | ||
int idx = 0; | ||
|
||
while (*check != '\0') { | ||
if (*check == ' ') { | ||
// Replace spaces with a single underscore | ||
if (prev_us) { | ||
memmove( | ||
&level_title[idx], &level_title[idx + 1], | ||
strlen(level_title) - idx); | ||
} else { | ||
*check++ = '_'; | ||
idx++; | ||
prev_us = true; | ||
} | ||
} else if (((*check < 'A' || *check > 'Z') | ||
&& (*check < 'a' || *check > 'z') | ||
&& (*check < '0' || *check > '9'))) { | ||
// Strip non alphanumeric chars | ||
memmove( | ||
&level_title[idx], &level_title[idx + 1], | ||
strlen(level_title) - idx); | ||
} else { | ||
check++; | ||
idx++; | ||
prev_us = false; | ||
} | ||
} | ||
|
||
// If title totally invalid, name it based on level number | ||
if (strlen(level_title) == 0) { | ||
sprintf(level_title, "Level_%d", level_num); | ||
prev_us = false; | ||
} | ||
|
||
// Strip trailing underscores | ||
if (prev_us) { | ||
check--; | ||
idx--; | ||
memmove( | ||
&level_title[idx], &level_title[idx + 1], strlen(level_title) - 1); | ||
prev_us = false; | ||
} | ||
|
||
// Get timestamp | ||
char date_time[30]; | ||
Clock_GetDateTime(date_time); | ||
|
||
// Full screenshot name | ||
size_t out_size = snprintf(NULL, 0, "%s_%s", date_time, level_title) + 1; | ||
char *out = Memory_Alloc(out_size); | ||
snprintf(out, out_size, "%s_%s", date_time, level_title); | ||
return out; | ||
} | ||
|
||
bool Screenshot_Make(const SCREENSHOT_FORMAT format) | ||
{ | ||
File_CreateDirectory(SCREENSHOTS_DIR); | ||
|
||
char *const filename = M_GetScreenshotName(); | ||
|
||
const char *ext; | ||
switch (format) { | ||
case SCREENSHOT_FORMAT_JPEG: | ||
ext = "jpg"; | ||
break; | ||
case SCREENSHOT_FORMAT_PNG: | ||
ext = "png"; | ||
break; | ||
default: | ||
ext = "jpg"; | ||
break; | ||
} | ||
|
||
bool result = false; | ||
char *full_path = Memory_Alloc( | ||
strlen(SCREENSHOTS_DIR) + strlen(filename) + strlen(ext) + 6); | ||
sprintf(full_path, "%s/%s.%s", SCREENSHOTS_DIR, filename, ext); | ||
if (!File_Exists(full_path)) { | ||
result = Output_MakeScreenshot(full_path); | ||
} else { | ||
// name already exists, so add a number to name | ||
for (int i = 2; i < 100; i++) { | ||
sprintf( | ||
full_path, "%s/%s_%d.%s", SCREENSHOTS_DIR, filename, i, ext); | ||
if (!File_Exists(full_path)) { | ||
result = Output_MakeScreenshot(full_path); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
Memory_FreePointer(&filename); | ||
Memory_FreePointer(&full_path); | ||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.