Skip to content

Add Take Screenshot option - #1908

Open
mziab wants to merge 2 commits into
grumpycoders:mainfrom
mziab:take-screenshot
Open

Add Take Screenshot option#1908
mziab wants to merge 2 commits into
grumpycoders:mainfrom
mziab:take-screenshot

Conversation

@mziab

@mziab mziab commented Mar 18, 2025

Copy link
Copy Markdown
Contributor

This adds a Take Screenshot option to the Emulation menu.

I want to stress that this is a tentative implementation and I would like to request comments on the following:

  1. I have copied the convertScreenshotToImage and writeImagePNG helpers from the webserver to PCSX::GUI (without removing the originals). Since now there are two consumers for these, what would be the proper place to put them?
  2. Should the helpers and the new getDateString one be renamed to something else? Additionally, is PCSX::GUI the proper place for the date string helper?
  3. Maybe a hotkey should be added for this? I was thinking of F12, like Steam uses.

@coderabbitai

coderabbitai Bot commented Mar 18, 2025

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

Next included review available in 58 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 4 included reviews currently available.

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Learn how review limits work.

Review configuration:

⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Team

Run ID: 170eaf78-4b40-48eb-9573-79f8404599d0

📥 Commits

Reviewing files that changed from the base of the PR and between 2a36099 and 4b9258e.

📒 Files selected for processing (2)
  • src/gui/gui.cc
  • src/gui/gui.h
📝 Walkthrough

Walkthrough

This pull request introduces screenshot functionality within the GUI. The changes add a new "Take Screenshot" menu item that triggers screenshot capture via the emulator's GPU. New methods are implemented to convert the captured screenshot into an image, generate a timestamp for unique file naming, and write the image to a PNG file. Both source and header files have been updated to include these new functionalities.

Changes

File(s) Change Summary
src/gui/gui.cc Added screenshot functionality: updated endFrame, inserted "Take Screenshot" menu item, and implemented saveScreenShot(), convertScreenshotToImage(), writeImagePNG(), and getDateString() methods.
src/gui/gui.h Declared new methods for screenshot functionality: public saveScreenShot() and private getDateString(), convertScreenshotToImage(), and writeImagePNG(); added <sstream> and <ctime> includes.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant GUI
    participant GPU
    participant FS

    User->>GUI: Select "Take Screenshot"
    GUI->>GPU: Capture current screen data
    GPU-->>GUI: Return screenshot data
    GUI->>GUI: Convert screenshot to image format
    GUI->>GUI: Generate timestamped filename
    GUI->>FS: Write PNG image file
    FS-->>GUI: Confirm file saved
Loading

Suggested reviewers

  • nicolasnoble

Poem

I’m a rabbit, hopping through code so bright,
Capturing screens in the blink of light.
A menu hop, a click so keen,
PNGs saved with a timestamp gleam.
Celebrate these changes with a joyful sight!
🐇🌟

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (4)
src/gui/gui.h (1)

432-434: Consider moving helper functions to a shared utility.

These helper functions are currently duplicated from the webserver module. To avoid code duplication and potential maintenance issues, consider moving these to a shared utility class or namespace that both the GUI and webserver can use.

-    std::string getDateString();
-    clip::image convertScreenshotToImage(PCSX::GPU::ScreenShot&& screenshot);
-    bool writeImagePNG(std::string filename, clip::image&& img);

These functions could be moved to a new utility namespace, for example:

// In a new header file like src/utils/screenshot_utils.h
namespace PCSX::Utils {
    std::string getDateString();
    clip::image convertScreenshotToImage(GPU::ScreenShot&& screenshot);
    bool writeImagePNG(std::string filename, clip::image&& img);
}
src/gui/gui.cc (3)

2931-2943: Consider adding more flexibility to date format.

The date format is currently hardcoded. Consider making it configurable or moving it to a constant to make it easier to change in the future.

- ss << std::put_time(local_time, "%Y-%m-%d-%H-%M-%S");
+ constexpr const char* SCREENSHOT_DATE_FORMAT = "%Y-%m-%d-%H-%M-%S";
+ ss << std::put_time(local_time, SCREENSHOT_DATE_FORMAT);

2945-2953: Add user feedback for screenshot operations.

The method returns a success status, but there's no visible feedback to the user when a screenshot is taken. Consider adding a notification when a screenshot is successfully saved or if it fails.

 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));
+    
+    if (success) {
+        addNotification(fmt::format(_("Screenshot saved to {}"), path.string()));
+    } else {
+        addNotification(_("Failed to save screenshot"));
+    }
 
     return success;
 }

2945-2953: Consider adding a hotkey for the screenshot feature.

As mentioned in your PR description, adding a hotkey (like F12) would improve usability. This would require adding a key handler in the startFrame method similar to other hotkeys.

Would you like me to suggest an implementation for the screenshot hotkey?

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8cc3aa2 and a813d34.

📒 Files selected for processing (2)
  • src/gui/gui.cc (2 hunks)
  • src/gui/gui.h (2 hunks)
🧰 Additional context used
🧬 Code Definitions (1)
src/gui/gui.cc (3)
src/gui/gui.h (6) (6)
  • screenshot (433:433)
  • filename (411:411)
  • filename (412:412)
  • filename (413:413)
  • filename (423:423)
  • filename (434:434)
src/core/web-server.cc (4) (4)
  • screenshot (713:742)
  • screenshot (713:713)
  • filename (743:743)
  • filename (743:743)
src/core/gpu.h (2) (2)
  • g_emulator (62:62)
  • g_emulator (63:63)
⏰ Context from checks skipped due to timeout of 90000ms (11)
  • GitHub Check: pcsx-redux (aarch64-linux)
  • GitHub Check: pcsx-redux (x86_64-linux)
  • GitHub Check: Codacy Static Code Analysis
  • GitHub Check: coverage
  • GitHub Check: aur-build
  • GitHub Check: macos-build-and-test-toolchain
  • GitHub Check: cross-arm64
  • GitHub Check: build
  • GitHub Check: toolchain
  • GitHub Check: build-openbios
  • GitHub Check: asan
🔇 Additional comments (4)
src/gui/gui.h (2)

30-31: Necessary includes for the screenshot functionality.

These includes support date formatting and string operations needed for the new screenshot feature.


424-424: Appropriate public interface for screenshot functionality.

The public method provides a clean interface for the screenshot feature, returning a boolean to indicate success or failure.

src/gui/gui.cc (2)

1321-1323: Good placement for the screenshot menu item.

Adding the screenshot option to the Emulation menu is intuitive and follows standard application conventions.


2898-2927: Check color channel mask values.

There appears to be a difference in the red and blue mask values compared to the webserver implementation. The comments suggest that the values might have been intentionally swapped. Verify that this is correct for the GUI's needs.

-        spec.red_mask = 0x1f;  // 0x7c00;
-        spec.blue_mask = 0x7c00;  // 0x1f;
+        spec.red_mask = 0x7c00;
+        spec.blue_mask = 0x1f;

Original webserver implementation for reference:

spec.red_mask = 0x7c00;
spec.blue_mask = 0x1f;

@nicolasnoble

Copy link
Copy Markdown
Member

Bit busy this week, I'll look at the various PRs over the week-end.

@nicolasnoble
nicolasnoble requested a review from Copilot April 15, 2025 04:25

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

Comments suppressed due to low confidence (1)

src/gui/gui.h:432

  • [nitpick] Since these helper functions are now used in multiple modules, consider renaming or relocating them into a common utilities module to prevent code duplication and ease future maintenance.
std::string getDateString();

Comment thread src/gui/gui.cc
auto local_time = std::localtime(&in_time_t);

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

Copilot AI Apr 15, 2025

Copy link

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.
@nicolasnoble

Copy link
Copy Markdown
Member

Sorry, took a while to get back to you, lots of real life stuff.

So, yeah, it might be relevant to create a new file, like screenshot.h/screenshot.cc to factorize this a bit. We can create a few static functions in a namespace like PCSX::ScreenShot.

For the date helper, it probably would belong into the screenshot namespace for now? We can move it if needs be if we start re-using it elsewhere maybe?

For the hotkey, sure, I don't mind. Note there's already a hotkey to take a screenshot into the clipboard, and the code probably needs to be refactored with the new screenshot code too:

pcsx-redux/src/gui/gui.cc

Lines 1095 to 1125 in 34ea4f1

if (ImGui::IsKeyDown(ImGuiKey_PrintScreen) && io.KeyCtrl) {
auto screenshot = g_emulator->m_gpu->takeScreenShot();
clip::image_spec spec;
spec.width = screenshot.width;
spec.height = screenshot.height;
if (screenshot.bpp == GPU::ScreenShot::BPP_16) {
spec.bits_per_pixel = 16;
spec.bytes_per_row = screenshot.width * 2;
spec.red_mask = 0x7c00;
spec.green_mask = 0x3e0;
spec.blue_mask = 0x1f;
spec.alpha_mask = 0;
spec.red_shift = 10;
spec.green_shift = 5;
spec.blue_shift = 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);
clip::set_image(img.to_rgba8888());
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants