SDL2::Render#

📦 std

Hardware-accelerated 2D drawing: renderers and textures.

A renderer draws into a window, using the GPU when one is available and falling back to software when it is not. It is the fast path for 2D work: clear the target, draw shapes and textures, then present the finished frame.

Rectangles#

Anywhere a rectangle is wanted you may pass an array reference [$x, $y, $w, $h], a hash reference {x => .., y => .., w => .., h => ..}, or undef to mean “the whole target”. The array form is the short one and is what the examples use.

The SDL2::Renderer object#

Usually obtained from a window rather than constructed directly:

my $ren = $win->renderer;                    # accelerated, vsync off
my $ren = $win->renderer(vsync => 1);        # wait for the refresh
my $ren = $win->renderer(software => 1);     # force software rendering

Its methods:

  • $ren->clear - fill the whole target with the draw color.

  • $ren->present - show the frame just drawn.

  • $ren->set_draw_color($r, $g, $b, $a) - alpha defaults to 255.

  • $ren->draw_color - the list ($r, $g, $b, $a).

  • $ren->set_blend_mode($mode) - one of the SDL_BLENDMODE_* constants.

  • $ren->draw_point($x, $y), $ren->draw_line($x1, $y1, $x2, $y2).

  • $ren->draw_rect($rect) - outline; $ren->fill_rect($rect) - solid.

  • $ren->copy($texture, $src, $dst) - blit a texture.

  • $ren->copy_ex($texture, $src, $dst, $angle, $center, $flip) - blit rotated and/or flipped.

  • $ren->output_size - the list ($w, $h) of the drawable area.

  • $ren->set_logical_size($w, $h) - draw in fixed coordinates and let SDL scale to the real window size.

  • $ren->read_pixels($rect) - the raw RGBA bytes of the target, four bytes per pixel, row by row. Useful for saving a frame or checking in a test what was actually drawn.

  • $ren->handle, $ren->destroy.

The SDL2::Texture object#

A texture is an image living in the renderer’s memory, which is what makes drawing it fast.

  • SDL2::Texture->new($renderer, %opts) - w, h, access, format.

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

  • $tex->set_alpha_mod($a), $tex->set_color_mod($r, $g, $b), $tex->set_blend_mode($mode) - modulation applied when it is drawn.

  • $tex->handle, $tex->destroy.