SDL2::Surface#

📦 std

Surfaces - images in ordinary memory.

A surface is a block of pixels the CPU can read and write, as opposed to a texture, which lives in the renderer’s memory and is fast to draw but awkward to inspect. Loading an image file gives you a surface; $ren->read_pixels gives you the bytes to make one from; and saving a frame goes through one.

The SDL2::Surface object#

# wrap pixels you already have - four bytes per pixel, RGBA order
my $surf = SDL2::Surface->from_pixels($bytes, $w, $h);

$surf->save_bmp('frame.bmp');            # core SDL2, always available
my $tex = $ren->texture_from_surface($surf);

Its methods:

  • SDL2::Surface->from_pixels($bytes, $w, $h, [$format]) - a surface over a copy of $bytes. The default format is RGBA, four bytes per pixel, which is what read_pixels returns.

  • $surf->size - the list ($w, $h).

  • $surf->pitch - the bytes per row, which is not always $w * 4.

  • $surf->pixels - the raw bytes back again.

  • $surf->save_bmp($path) - write it out. BMP needs no extra library, so it works everywhere; for PNG or JPEG see SDL2::Image.

  • $surf->handle, $surf->destroy, and SDL2::Surface->from_handle($h) to adopt a raw handle from the direct layer.