Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change Screenshot Functionality #7431

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 25 additions & 30 deletions Source/capture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "engine/backbuffer_state.hpp"
#include "engine/dx.h"
#include "engine/palette.h"
#include "plrmsg.h"
#include "utils/file_util.h"
#include "utils/log.hpp"
#include "utils/paths.h"
Expand All @@ -45,7 +46,7 @@ FILE *CaptureFile(std::string *dstPath)
const std::time_t tt = std::time(nullptr);
const std::tm *tm = std::localtime(&tt);
const std::string filename = tm != nullptr
? fmt::format("Screenshot from {:04}-{:02}-{:02} {:02}-{:02}-{:02}",
? fmt::format("{:04}-{:02}-{:02} {:02}-{:02}-{:02}",
tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec)
: "Screenshot";
*dstPath = StrCat(paths::PrefPath(), filename, ext);
Expand All @@ -57,40 +58,31 @@ FILE *CaptureFile(std::string *dstPath)
return OpenFile(dstPath->c_str(), "wb");
}

/**
* @brief Make a red version of the given palette and apply it to the screen.
*/
void RedPalette()
} // namespace

std::string FileNameForChat(const std::string &fullPath)
kphoenix137 marked this conversation as resolved.
Show resolved Hide resolved
{
for (int i = 0; i < 256; i++) {
system_palette[i].g = 0;
system_palette[i].b = 0;
// Extracts just the file name from the full path
auto pos = fullPath.find_last_of("\\/");
if (pos == std::string::npos) {
return fullPath;
}
palette_update();
BltFast(nullptr, nullptr);
RenderPresent();
return fullPath.substr(pos + 1);
}

} // namespace

void CaptureScreen()
{
SDL_Color palette[256];
std::string fileName;
const uint32_t startTime = SDL_GetTicks();

FILE *outStream = CaptureFile(&fileName);
if (outStream == nullptr) {
LogError("Failed to open {} for writing: {}", fileName, std::strerror(errno));
auto errorMessage = fmt::format(fmt::runtime(_(/* TRANSLATORS: {fileName} is the file path where the screenshot was attempted to be saved. */ "Failed to open {} for writing: {}")), fileName, std::strerror(errno));
LogError("{}", errorMessage);

auto chatMessage = fmt::format(fmt::runtime(_(/* TRANSLATORS: {fileName} is the name of the file where the screenshot was attempted to be saved. */ "Failed to open {} for writing")), FileNameForChat(fileName));
EventPlrMsg(chatMessage, UiFlags::ColorWhitegold);
return;
}
DrawAndBlit();
PaletteGetEntries(256, palette);
RedPalette();
for (int i = 0; i < 256; i++) {
system_palette[i] = palette[i];
}
palette_update();

const tl::expected<void, std::string> result =
#if DEVILUTIONX_SCREENSHOT_FORMAT == DEVILUTIONX_SCREENSHOT_FORMAT_PCX
Expand All @@ -100,16 +92,19 @@ void CaptureScreen()
#endif

if (!result.has_value()) {
LogError("Failed to save screenshot at {}: ", fileName, result.error());
auto errorMessage = fmt::format(fmt::runtime(_(/* TRANSLATORS: {fileName} is the file path where the screenshot was attempted to be saved. {result.error()} is the error message returned during the save attempt. */ "Failed to save screenshot {}: {}")), fileName, result.error());
LogError("{}", errorMessage);

auto chatMessage = fmt::format(fmt::runtime(_(/* TRANSLATORS: {fileName} is the name of the file where the screenshot was attempted to be saved. */ "Failed to save screenshot {}")), FileNameForChat(fileName));
EventPlrMsg(chatMessage, UiFlags::ColorWhitegold);
RemoveFile(fileName.c_str());
} else {
Log("Screenshot saved at {}", fileName);
}
const uint32_t timePassed = SDL_GetTicks() - startTime;
if (timePassed < 300) {
SDL_Delay(300 - timePassed);
auto successMessage = fmt::format(fmt::runtime(_(/* TRANSLATORS: {fileName} is the file path where the screenshot was successfully saved. */ "Screenshot saved at {}")), fileName);
Log("{}", successMessage);

auto chatMessage = fmt::format(fmt::runtime(_(/* TRANSLATORS: {fileName} is the name of the file where the screenshot was successfully saved. */ "Screenshot saved as {}")), FileNameForChat(fileName));
EventPlrMsg(chatMessage, UiFlags::ColorWhitegold);
}
RedrawEverything();
}

} // namespace devilution
Loading