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

Conversation

mziab
Copy link
Contributor

@mziab mziab commented Mar 18, 2025

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.

Copy link
Contributor

coderabbitai bot commented Mar 18, 2025

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!
🐇🌟

Tip

⚡🧪 Multi-step agentic review comment chat (experimental)
  • We're introducing multi-step agentic chat in review comments. This experimental feature enhances review discussions with the CodeRabbit agentic chat by enabling advanced interactions, including the ability to create pull requests directly from comments.
    - To enable this feature, set early_access to true under in the settings.
✨ 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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

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
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
Copy link

@Copilot Copilot AI left a comment

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();

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.

@nicolasnoble
Copy link
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
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants