Skip to content

Commit a813d34

Browse files
committed
Added Take Screenshot option
1 parent 8cc3aa2 commit a813d34

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

src/gui/gui.cc

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1318,6 +1318,9 @@ void PCSX::GUI::endFrame() {
13181318
if (ImGui::MenuItem(_("Hard Reset"), "Shift+F8")) {
13191319
g_system->hardReset();
13201320
}
1321+
if (ImGui::MenuItem(_("Take Screenshot"))) {
1322+
saveScreenShot();
1323+
}
13211324
ImGui::EndMenu();
13221325
}
13231326
ImGui::Separator();
@@ -2891,3 +2894,60 @@ void PCSX::GUI::changeScale(float scale) {
28912894
m_allScales.emplace(scale);
28922895
ImGui::SetCurrentFont(getMainFont());
28932896
}
2897+
2898+
clip::image PCSX::GUI::convertScreenshotToImage(PCSX::GPU::ScreenShot&& screenshot) {
2899+
clip::image_spec spec;
2900+
spec.width = screenshot.width;
2901+
spec.height = screenshot.height;
2902+
if (screenshot.bpp == PCSX::GPU::ScreenShot::BPP_16) {
2903+
spec.bits_per_pixel = 16;
2904+
spec.bytes_per_row = screenshot.width * 2;
2905+
spec.red_mask = 0x1f; // 0x7c00;
2906+
spec.green_mask = 0x3e0;
2907+
spec.blue_mask = 0x7c00; // 0x1f;
2908+
spec.alpha_mask = 0;
2909+
spec.red_shift = 0; // 10;
2910+
spec.green_shift = 5;
2911+
spec.blue_shift = 10; // 0;
2912+
spec.alpha_shift = 0;
2913+
} else {
2914+
spec.bits_per_pixel = 24;
2915+
spec.bytes_per_row = screenshot.width * 3;
2916+
spec.red_mask = 0xff0000;
2917+
spec.green_mask = 0xff00;
2918+
spec.blue_mask = 0xff;
2919+
spec.alpha_mask = 0;
2920+
spec.red_shift = 16;
2921+
spec.green_shift = 8;
2922+
spec.blue_shift = 0;
2923+
spec.alpha_shift = 0;
2924+
}
2925+
clip::image img(screenshot.data.data(), spec);
2926+
return img.to_rgba8888();
2927+
}
2928+
2929+
bool PCSX::GUI::writeImagePNG(std::string filename, clip::image&& img) { return img.export_to_png(filename); }
2930+
2931+
std::string PCSX::GUI::getDateString() {
2932+
auto now = std::chrono::system_clock::now();
2933+
auto in_time_t = std::chrono::system_clock::to_time_t(now);
2934+
2935+
auto local_time = std::localtime(&in_time_t);
2936+
2937+
std::stringstream ss;
2938+
ss << std::put_time(local_time, "%Y-%m-%d-%H-%M-%S");
2939+
2940+
std::string datetime_string = ss.str();
2941+
2942+
return datetime_string;
2943+
}
2944+
2945+
bool PCSX::GUI::saveScreenShot() {
2946+
std::filesystem::path path =
2947+
g_system->getPersistentDir() / (getSaveStatePrefix(true) + getDateString() + ".png");
2948+
auto screenshot = g_emulator->m_gpu->takeScreenShot();
2949+
clip::image img = convertScreenshotToImage(std::move(screenshot));
2950+
bool success = writeImagePNG(path.string(), std::move(img));
2951+
2952+
return success;
2953+
}

src/gui/gui.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
#include <map>
2828
#include <set>
2929
#include <string>
30+
#include <sstream>
31+
#include <ctime>
3032
#include <string_view>
3133
#include <utility>
3234
#include <vector>
@@ -419,13 +421,17 @@ class GUI final : public UI {
419421
std::string buildSaveStateFilename(int i);
420422
std::string buildSaveStateFilename(std::string name);
421423
bool saveStateExists(std::filesystem::path filename);
424+
bool saveScreenShot();
422425

423426
private:
424427
void applyTheme(int theme);
425428
void cherryTheme();
426429
void monoTheme();
427430
void draculaTheme();
428431
void oliveTheme();
432+
std::string getDateString();
433+
clip::image convertScreenshotToImage(PCSX::GPU::ScreenShot&& screenshot);
434+
bool writeImagePNG(std::string filename, clip::image&& img);
429435

430436
Notifier m_notifier = {l_("Notification")};
431437
Widgets::Console m_luaConsole = {settings.get<ShowLuaConsole>().value};

0 commit comments

Comments
 (0)