Skip to content

Add Take Screenshot option #1908

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
60 changes: 60 additions & 0 deletions src/gui/gui.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/***************************************************************************

Check notice on line 1 in src/gui/gui.cc

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

✅ Getting better: Overall Code Complexity

The mean cyclomatic complexity decreases from 11.08 to 10.41, threshold = 4. This file has many conditional statements (e.g. if, for, while) across its implementation, leading to lower code health. Avoid adding more conditionals.

Check notice on line 1 in src/gui/gui.cc

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

✅ Getting better: Primitive Obsession

The ratio of primitive types in function arguments decreases from 37.93% to 37.70%, threshold = 30.0%. The functions in this file have too many primitive types (e.g. int, double, float) in their function argument lists. Using many primitive types lead to the code smell Primitive Obsession. Avoid adding more primitive arguments.
* Copyright (C) 2019 PCSX-Redux authors *
* *
* This program is free software; you can redistribute it and/or modify *
Expand Down Expand Up @@ -1318,6 +1318,9 @@
if (ImGui::MenuItem(_("Hard Reset"), "Shift+F8")) {
g_system->hardReset();
}
if (ImGui::MenuItem(_("Take Screenshot"))) {
saveScreenShot();
}
ImGui::EndMenu();
}
ImGui::Separator();
Expand Down Expand Up @@ -2891,3 +2894,60 @@
m_allScales.emplace(scale);
ImGui::SetCurrentFont(getMainFont());
}

clip::image PCSX::GUI::convertScreenshotToImage(PCSX::GPU::ScreenShot&& screenshot) {
clip::image_spec spec;
spec.width = screenshot.width;
spec.height = screenshot.height;
if (screenshot.bpp == PCSX::GPU::ScreenShot::BPP_16) {
spec.bits_per_pixel = 16;
spec.bytes_per_row = screenshot.width * 2;
spec.red_mask = 0x1f; // 0x7c00;
spec.green_mask = 0x3e0;
spec.blue_mask = 0x7c00; // 0x1f;
spec.alpha_mask = 0;
spec.red_shift = 0; // 10;
spec.green_shift = 5;
spec.blue_shift = 10; // 0;
spec.alpha_shift = 0;
} else {
spec.bits_per_pixel = 24;
spec.bytes_per_row = screenshot.width * 3;
spec.red_mask = 0xff0000;
spec.green_mask = 0xff00;
spec.blue_mask = 0xff;
spec.alpha_mask = 0;
spec.red_shift = 16;
spec.green_shift = 8;
spec.blue_shift = 0;
spec.alpha_shift = 0;
}
clip::image img(screenshot.data.data(), spec);
return img.to_rgba8888();
}

bool PCSX::GUI::writeImagePNG(std::string filename, clip::image&& img) { return img.export_to_png(filename); }

std::string PCSX::GUI::getDateString() {
auto now = std::chrono::system_clock::now();
auto in_time_t = std::chrono::system_clock::to_time_t(now);

auto local_time = std::localtime(&in_time_t);

std::stringstream ss;
ss << std::put_time(local_time, "%Y-%m-%d-%H-%M-%S");
Copy link
Preview

Copilot AI Apr 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It appears that is needed for std::put_time to function correctly. Please add the necessary include to avoid potential compile errors.

Copilot uses AI. Check for mistakes.


std::string datetime_string = ss.str();

return datetime_string;
}

bool PCSX::GUI::saveScreenShot() {
std::filesystem::path path =
g_system->getPersistentDir() / (getSaveStatePrefix(true) + getDateString() + ".png");
auto screenshot = g_emulator->m_gpu->takeScreenShot();
clip::image img = convertScreenshotToImage(std::move(screenshot));
bool success = writeImagePNG(path.string(), std::move(img));

return success;
}
6 changes: 6 additions & 0 deletions src/gui/gui.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#include <map>
#include <set>
#include <string>
#include <sstream>
#include <ctime>
#include <string_view>
#include <utility>
#include <vector>
Expand Down Expand Up @@ -419,13 +421,17 @@ class GUI final : public UI {
std::string buildSaveStateFilename(int i);
std::string buildSaveStateFilename(std::string name);
bool saveStateExists(std::filesystem::path filename);
bool saveScreenShot();

private:
void applyTheme(int theme);
void cherryTheme();
void monoTheme();
void draculaTheme();
void oliveTheme();
std::string getDateString();
clip::image convertScreenshotToImage(PCSX::GPU::ScreenShot&& screenshot);
bool writeImagePNG(std::string filename, clip::image&& img);

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