Teek::SDL2::Renderer Class
GPU-accelerated 2D renderer backed by SDL2.
Renderer wraps an SDL_Renderer and provides both low-level
positional-arg methods (defined in C) and higher-level Ruby
convenience wrappers with keyword arguments.
You don’t create a Renderer directly — it’s created automatically
by Viewport and accessible via Viewport#renderer.
C-defined methods
These are defined in the C extension (sdl2surface.c) and available
on every Renderer instance:
#clear— clear the rendering target#present— flip the back buffer to screen#fill_rect— draw a filled rectangle#draw_rect— draw a rectangle outline#draw_line— draw a line#copy— copy a texture to the rendering target#create_texture— create a new texture#output_size— query the renderer output dimensions#destroy— destroy the renderer#destroyed?— check if the renderer has been destroyed
Inherits: Object
Instance Methods ↑
blit(texture, src: nil, dst: nil)
Copy a texture (keyword-arg wrapper for #copy).
Parameters
textureTexture— the source texturesrcArray(Integer, Integer, Integer, Integer), nil— source rectangle[x, y, w, h]ornilfor entire texturedstArray(Integer, Integer, Integer, Integer), nil— destination rectangle[x, y, w, h]ornilfor entire target
Returns self
clear(r 0, g 0, b 0, a 255)
Clear the entire rendering target with the given color.
Parameters
rInteger— red (0–255)gInteger— green (0–255)bInteger— blue (0–255)aInteger— alpha (0–255)
Returns self
copy(texture, src_rect nil, dst_rect nil)
Copy a texture (or portion of it) to the rendering target.
Parameters
textureTexture— the source texturesrc_rectArray(Integer, Integer, Integer, Integer), nil— source rectangle[x, y, w, h]ornilfor entire texturedst_rectArray(Integer, Integer, Integer, Integer), nil— destination rectangle[x, y, w, h]ornilfor entire target
Returns self
create_texture(width, height, access :static)
Create a new texture owned by this renderer.
Parameters
widthInteger— texture width in pixelsheightInteger— texture height in pixelsaccessSymbol—:static,:streaming, or:target
Returns Texture
destroyed?
Returns Boolean — whether this renderer has been destroyed
draw_line(x1, y1, x2, y2, r, g, b, a 255)
Draw a line between two points.
Parameters
x1Integer— start xy1Integer— start yx2Integer— end xy2Integer— end yrInteger— red (0–255)gInteger— green (0–255)bInteger— blue (0–255)aInteger— alpha (0–255)
Returns self
draw_rect(x, y, w, h, r, g, b, a 255)
Draw a rectangle outline.
Parameters
xInteger— left edgeyInteger— top edgewInteger— widthhInteger— heightrInteger— red (0–255)gInteger— green (0–255)bInteger— blue (0–255)aInteger— alpha (0–255)
Returns self
draw_text(x, y, text, font:, r: 255, g: 255, b: 255, a: 255)
Render text and blit it at the given position in a single call.
Creates a temporary texture, copies it to the renderer, then
destroys it. For repeated rendering of the same text, prefer
calling Font#render_text once and reusing the texture.
Parameters
xInteger— left edgeyInteger— top edgetextString— the text to render (UTF-8)fontFont— the font to userInteger— red (0–255)gInteger— green (0–255)bInteger— blue (0–255)aInteger— alpha (0–255)
Returns self
Example
renderer.draw_text(10, 10, "Hello!", font: font, r: 255, g: 255, b: 255)
fill(x, y, w, h, r:, g:, b:, a: 255)
Draw a filled rectangle (keyword-arg wrapper for #fill_rect).
Parameters
xInteger— left edgeyInteger— top edgewInteger— widthhInteger— heightrInteger— red (0–255)gInteger— green (0–255)bInteger— blue (0–255)aInteger— alpha (0–255)
Returns self
fill_rect(x, y, w, h, r, g, b, a 255)
Draw a filled rectangle.
Parameters
xInteger— left edgeyInteger— top edgewInteger— widthhInteger— heightrInteger— red (0–255)gInteger— green (0–255)bInteger— blue (0–255)aInteger— alpha (0–255)
Returns self
line(x1, y1, x2, y2, r:, g:, b:, a: 255)
Draw a line (keyword-arg wrapper for #draw_line).
Parameters
x1Integer— start xy1Integer— start yx2Integer— end xy2Integer— end yrInteger— red (0–255)gInteger— green (0–255)bInteger— blue (0–255)aInteger— alpha (0–255)
Returns self
load_font(path, size)
Load a TrueType font file at the given point size.
Parameters
pathString— path to a.ttfor.otffont filesizeInteger— point size
Returns Font
Example
font = renderer.load_font("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 16)
load_image(path)
Load an image file into a GPU texture via SDL2_image. Supports PNG, JPG, BMP, GIF, WebP, TGA, and more. Alpha blending is enabled automatically.
Parameters
pathString— path to the image file
Returns Texture
Example
sprite = renderer.load_image("assets/player.png")
renderer.copy(sprite, nil, [x, y, sprite.width, sprite.height])
outline(x, y, w, h, r:, g:, b:, a: 255)
Draw a rectangle outline (keyword-arg wrapper for #draw_rect).
Parameters
xInteger— left edgeyInteger— top edgewInteger— widthhInteger— heightrInteger— red (0–255)gInteger— green (0–255)bInteger— blue (0–255)aInteger— alpha (0–255)
Returns self
output_size
Query the renderer’s output dimensions.
Returns Array(Integer, Integer) — [width, height]
read_pixels
Read the current renderer contents as raw RGBA pixel data.
Returns String — binary string of width*height*4 bytes (RGBA8888)
render
Yield self for a drawing block, then present.
Returns self
@yield renderer draw commands
@yieldparam renderer Renderer
Example
renderer.render do |r|
r.clear(0, 0, 0)
r.fill(10, 10, 100, 100, r: 255, g: 0, b: 0)
end
save_png(path)
Save the current renderer contents to a PNG file via ImageMagick.
Reads raw pixels from the GPU and pipes them through ImageMagick’s
convert command to produce a PNG. Useful for visual inspection
and screenshot-based regression testing.
Parameters
pathString— output file path (should end in.png)
Returns void
@raise RuntimeError if ImageMagick is not installed or conversion fails
Example
renderer.clear(0, 0, 0)
renderer.fill_rect(10, 10, 100, 50, 255, 0, 0)
renderer.save_png("screenshot.png")