Skip to content

feat: add SDL2::Renderer#read_pixels#31

Open
takaokouji wants to merge 1 commit intoohai:masterfrom
smalruby:upstream/add-read-pixels
Open

feat: add SDL2::Renderer#read_pixels#31
takaokouji wants to merge 1 commit intoohai:masterfrom
smalruby:upstream/add-read-pixels

Conversation

@takaokouji
Copy link
Copy Markdown
Contributor

Summary

Add SDL2::Renderer#read_pixels method that wraps SDL_RenderReadPixels.

This is a resubmission of #29, addressing the review feedback in #29 (comment).

Changes from #29

  • Accepts SDL2::PixelFormat: The format parameter now accepts both SDL2::PixelFormat and Integer, using the existing uint32_for_format() helper (consistent with create_texture etc.)
  • Respects format=0 semantics: Passing 0 uses the rendering target's native format as defined by SDL2, instead of overriding it with ARGB8888
  • Sample program included: sample/test_read_pixels.rb demonstrates the method with BMP output

Usage

# Read the entire screen in ARGB8888 format
pixels = renderer.read_pixels(nil, SDL2::PixelFormat::ARGB8888)

# Read a specific area using the rendering target's native format
rect = SDL2::Rect.new(0, 0, 100, 100)
pixels = renderer.read_pixels(rect, 0)

Parameters

  • rectSDL2::Rect or nil (nil reads the entire target)
  • formatSDL2::PixelFormat or Integer (0 to use the rendering target's format)

Returns

Raw pixel data as a binary String.

Closes #28

Add a new method to read pixels from the current rendering target.

- Accepts SDL2::PixelFormat or Integer for the format parameter
- Passing 0 uses the rendering target's native format (per SDL2 spec)
- Queries renderer info for correct pitch calculation when format is 0
- Returns raw pixel data as a binary String

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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.

How to take a screenshot of the screen?

1 participant