class

Tryst::SDL::Renderer

Inherits Reference < Object

The drawing API over a Viewport's SDL renderer.

viewport.render do |r|
  r.clear(Tryst::SDL::Color::BLACK)
  r.fill_rect(10, 10, 100, 50, color: Tryst::SDL::Color.new(255, 0, 0))
end

Every call raises on failure rather than returning a boolean. SDL3 reports errors that way and a drawing call that quietly did nothing is close to impossible to find later, because the symptom is a blank area rather than an error.

Constructors

new(renderer : Pointer(LibSDL::Renderer))

@api private - built by Viewport, which owns the SDL renderer.

Source

Instance methods

blend_mode
Source
blend_mode=(value : BlendMode) : BlendMode
Source
clear(color : Color | Nil = nil) : self

Fills the whole target. With no colour, uses the current one.

Source
color

The colour subsequent draws use.

Source
color=(value : Color) : Color
Source
copy(texture : Texture, src : Rect | Nil = nil, dest : Rect | Nil = nil) : self

Draws a texture. With no rects, the whole texture over the whole target; src takes part of the texture, dest places it.

Source
create_texture(width : Int32, height : Int32, access : Texture::Access = Texture::Access::Static) : Texture

A new texture belonging to this renderer. See Texture for which access to pick; the caller owns it and should #destroy it.

Source
draw_geometry(vertices : Array(Vertex), texture : Texture | Nil = nil, indices : Array(Int32) | Nil = nil) : self

Draws an arbitrary list of coloured (and, with a texture, textured) triangles - the one primitive #fill_rect/#draw_line/ #copy don't cover, since all of them are axis-aligned or whole- texture. Gradients, polygons, particle fans, custom meshes.

With no indices, every three vertices in order become one triangle - SDL's own default. With indices, each group of three indexes into vertices instead, so a shared vertex (a fan's centre, an edge shared between two triangles) is written once and reused rather than duplicated.

Source
draw_line(x1 : Number, y1 : Number, x2 : Number, y2 : Number, color : Color | Nil = nil) : self
Source
draw_lines(points : Array(Point) | Slice(Point), color : Color | Nil = nil) : self

A CONNECTED polyline through the points, not a set of separate segments - the same distinction SDL draws between RenderLines and repeated RenderLine.

An Array/Slice(Point) is handed to SDL directly with no per-call copy - see #point_ptr. Anything else (a general Enumerable, a lazily generated sequence) still works via the fallback below, which builds the array SDL needs.

Source
draw_lines(points : Enumerable(Point), color : Color | Nil = nil) : self
Source
draw_point(x : Number, y : Number, color : Color | Nil = nil) : self
Source
draw_points(points : Array(Point) | Slice(Point), color : Color | Nil = nil) : self

See #draw_lines for the Array/Slice(Point) vs. Enumerable(Point) split.

Source
draw_points(points : Enumerable(Point), color : Color | Nil = nil) : self
Source
draw_rect(x : Number, y : Number, w : Number, h : Number, color : Color | Nil = nil) : self
Source
draw_rect(rect : Rect, color : Color | Nil = nil) : self

The outline only, one pixel wide.

Source
draw_text(x : Number, y : Number, text : String, font : Font, color : Color = Color::WHITE) : self

Renders text and draws it at (x, y) in one call.

This creates a texture and destroys it again every call - fine for text that changes every frame (a score, a clock), wasteful for anything static. Render once through Font#render_text and reuse the texture instead when the text does not change.

Source
draw_to(texture : Texture, & : Renderer -> _) : self

Draws into a texture instead of the window, for the duration of the block.

The previous target is restored afterwards even if the block raises - forgetting to put it back leaves every later draw going somewhere invisible, which presents as the whole program having stopped rendering.

Source
fill_rect(x : Number, y : Number, w : Number, h : Number, color : Color | Nil = nil) : self
Source
fill_rect(rect : Rect, color : Color | Nil = nil) : self
Source
load_font(path : String, size : Number) : Font

A TrueType/OpenType font at the given point size, for text drawn with #draw_text or rendered directly through Font#render_text.

Source
load_image(path : String) : Texture

An image file loaded straight into a GPU texture - PNG, JPG, BMP, GIF, WebP, TGA and whatever else this build's SDL3_image supports. Alpha blending is on by default, since a loaded image with transparency (a PNG sprite) is the common case and a surprising blank rect is the alternative.

Source
present

Puts everything drawn since the last present on screen.

Nothing appears without this, which is the single most common reason a renderer looks like it is doing nothing at all - Viewport#render exists so it cannot be forgotten.

Source
read_pixels

Reads the drawn pixels back, yielding them and freeing the snapshot afterwards. Yielded rather than returned so the surface cannot outlive its own cleanup.

Slow - it stalls the pipeline waiting for the GPU - so this is for tests and screenshots.

Source

Nested types