Skip to content

Commit 862d4ea

Browse files
committedMar 31, 2015
Change format of screenshot names
Filename screenshot_ + ISO 8601 format + [-serial] i.e. screenshot_YYYY-MM-DDTHH::MM::SS[-serial].png Serial is added if the filename + timestamp already exists and is in the range 1 to 999
1 parent ed10005 commit 862d4ea

File tree

2 files changed

+40
-16
lines changed

2 files changed

+40
-16
lines changed
 

Diff for: ‎src/client.cpp

+34-16
Original file line numberDiff line numberDiff line change
@@ -1704,25 +1704,42 @@ void Client::makeScreenshot(IrrlichtDevice *device)
17041704
{
17051705
irr::video::IVideoDriver *driver = device->getVideoDriver();
17061706
irr::video::IImage* const raw_image = driver->createScreenShot();
1707-
if (raw_image) {
1708-
irr::video::IImage* const image = driver->createImage(video::ECF_R8G8B8,
1709-
raw_image->getDimension());
17101707

1711-
if (image) {
1712-
raw_image->copyTo(image);
1708+
if (!raw_image)
1709+
return;
17131710

1714-
std::string filename;
1711+
time_t t = time(NULL);
1712+
struct tm *tm = localtime(&t);
17151713

1716-
time_t t = time(NULL);
1717-
struct tm *tm = localtime(&t);
1718-
char timetstamp_c[16]; // YYYYMMDD_HHMMSS + '\0'
1719-
strftime(timetstamp_c, sizeof(timetstamp_c), "%Y%m%d_%H%M%S", tm);
1714+
char timetstamp_c[64];
1715+
strftime(timetstamp_c, sizeof(timetstamp_c), "%FT%T", tm);
17201716

1721-
filename = g_settings->get("screenshot_path")
1722-
+ DIR_DELIM
1723-
+ std::string("screenshot_")
1724-
+ std::string(timetstamp_c)
1725-
+ ".png";
1717+
std::string filename_base = g_settings->get("screenshot_path")
1718+
+ DIR_DELIM
1719+
+ std::string("screenshot_")
1720+
+ std::string(timetstamp_c);
1721+
std::string filename_ext = ".png";
1722+
std::string filename;
1723+
1724+
// Try to find a unique filename
1725+
unsigned serial = 0;
1726+
1727+
while (serial < SCREENSHOT_MAX_SERIAL_TRIES) {
1728+
filename = filename_base + (serial > 0 ? ("-" + itos(serial)) : "") + filename_ext;
1729+
std::ifstream tmp(filename.c_str());
1730+
if (!tmp.good())
1731+
break; // File did not apparently exist, we'll go with it
1732+
serial++;
1733+
}
1734+
1735+
if (serial == SCREENSHOT_MAX_SERIAL_TRIES) {
1736+
infostream << "Could not find suitable filename for screenshot" << std::endl;
1737+
} else {
1738+
irr::video::IImage* const image =
1739+
driver->createImage(video::ECF_R8G8B8, raw_image->getDimension());
1740+
1741+
if (image) {
1742+
raw_image->copyTo(image);
17261743

17271744
std::ostringstream sstr;
17281745
if (driver->writeImageToFile(image, filename.c_str())) {
@@ -1734,8 +1751,9 @@ void Client::makeScreenshot(IrrlichtDevice *device)
17341751
infostream << sstr.str() << std::endl;
17351752
image->drop();
17361753
}
1737-
raw_image->drop();
17381754
}
1755+
1756+
raw_image->drop();
17391757
}
17401758

17411759
// IGameDef interface

Diff for: ‎src/constants.h

+6
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ with this program; if not, write to the Free Software Foundation, Inc.,
9797
// TODO: Use case-insensitive player names instead of this hack.
9898
#define PLAYER_FILE_ALTERNATE_TRIES 1000
9999

100+
// For screenshots a serial number is appended to the filename + datetimestamp
101+
// if filename + datetimestamp is not unique.
102+
// This is the maximum number of attempts to try and add a serial to the end of
103+
// the file attempting to ensure a unique filename
104+
#define SCREENSHOT_MAX_SERIAL_TRIES 1000
105+
100106
/*
101107
GUI related things
102108
*/

0 commit comments

Comments
 (0)
Please sign in to comment.