Teek::SDL2 Module
GPU-accelerated 2D rendering via SDL2, embedded inside Tk windows.
Teek::SDL2 lets you drop an SDL2 hardware-accelerated surface into any Tk application. The surface lives inside a Tk frame so it coexists with normal Tk widgets (buttons, labels, menus) while all pixel work is GPU-driven.
The main entry point is Viewport, which creates a Tk frame, obtains
its native window handle, and hands it to SDL2 for rendering.
Class Methods ↑
_viewports
@api private
Audio (C-defined module functions)
channel_paused?(channel)
Whether the given channel is paused.
Parameters
channelInteger
Returns Boolean
channel_volume(channel, vol -1)
Set or query volume for a channel.
Parameters
channelIntegervolInteger— 0–128, or -1 to query without changing
Returns Integer — current volume
fade_out_channel(channel, ms)
Gradually fade out a channel.
Parameters
channelIntegermsInteger— fade duration in milliseconds
Returns nil
fade_out_music(ms)
Gradually fade out the currently playing music.
Parameters
msInteger— fade duration in milliseconds
Returns nil
halt(channel)
Immediately stop playback on a channel.
Parameters
channelInteger— channel number (returned bySound#play)
Returns nil
master_volume
Current master volume (requires SDL2_mixer >= 2.6).
Returns Integer — 0–128
@raise NotImplementedError if SDL2_mixer < 2.6
master_volume=(vol)
Set the master volume (requires SDL2_mixer >= 2.6).
Parameters
volInteger— 0–128
Returns Integer — previous volume
@raise NotImplementedError if SDL2_mixer < 2.6
open_audio
Explicitly initialize the audio mixer. Safe to call multiple times.
Called automatically by Sound and Music constructors.
Returns nil
playing?(channel)
Whether the given channel is currently playing.
Parameters
channelInteger
Returns Boolean
start_audio_capture(path)
Begin recording mixed audio output to a WAV file. Everything that plays through the mixer (sounds, music) is captured.
Parameters
pathString— output WAV file path
Returns nil
@raise RuntimeError if capture is already in progress
See also
stop_audio_capture
Stop recording and finalize the WAV file. Safe to call even if no capture is in progress.
Returns nil
See also
Blending (C-defined module functions)
compose_blend_mode(src_color_factor, dst_color_factor, color_op, src_alpha_factor, dst_alpha_factor, alpha_op)
Create a custom blend mode for use with Texture#blend_mode=.
The blend equations are: dstRGB = color_op(srcRGB * src_color_factor, dstRGB * dst_color_factor) dstA = alpha_op(srcA * src_alpha_factor, dstA * dst_alpha_factor)
Factors (Symbol):
- :zero, :one
- :src_color, :one_minus_src_color
- :src_alpha, :one_minus_src_alpha
- :dst_color, :one_minus_dst_color
- :dst_alpha, :one_minus_dst_alpha
Operations (Symbol):
- :add — src dst (all renderers)
- :subtract — src - dst
- :rev_subtract — dst - src
- :minimum — min(src, dst)
- :maximum — max(src, dst)+
Parameters
src_color_factorSymbol— multiplier for source RGBdst_color_factorSymbol— multiplier for destination RGBcolor_opSymbol— operation combining color componentssrc_alpha_factorSymbol— multiplier for source alphadst_alpha_factorSymbol— multiplier for destination alphaalpha_opSymbol— operation combining alpha components
Returns Integer — opaque blend mode ID for Texture#blend_mode=
Example
# Inverse/invert effect (text shows opposite of background)
inverse = Teek::SDL2.compose_blend_mode(
:one_minus_dst_color, :one_minus_src_alpha, :add,
:zero, :one, :add
)
white_text = font.render_text("Hello", 255, 255, 255)
white_text.blend_mode = inverse
register_event_source(interval_ms: 16)
Register SDL2 as a Tcl event source. Called automatically when the
first Viewport is created. Uses a C function pointer for the hot
path — no Ruby in the poll loop.
Parameters
interval_msInteger— polling interval in milliseconds
Returns void
@api private
unregister_event_source
Remove SDL2 from Tcl’s event loop. Called automatically when the last
Viewport is destroyed.
Returns void
@api private
Instance Methods ↑
initialize(frequency: 44100, format: :s16, channels: 2)
Open a push-based audio output device.
The stream starts paused — call #resume after queuing initial data.
Parameters
frequencyInteger— sample rate in Hz (default: 44100)formatSymbol— sample format —:s16(signed 16-bit),:f32(32-bit float), or:u8(unsigned 8-bit)channelsInteger— 1 for mono, 2 for stereo (default: 2)
measure(text)
Measure the pixel dimensions the text would occupy when rendered. Does not create a texture — useful for layout calculations.
Parameters
textString— the text to measure (UTF-8)
Returns Array(Integer, Integer) — [width, height]
queue(data)
Push raw PCM data to the audio device.
The data must be a binary String whose format matches the stream’s
format and channels (e.g. packed signed 16-bit integers for :s16).
Parameters
dataString— raw PCM samples (binary encoding)
Returns nil
queued_samples
Number of audio sample frames currently queued. One sample frame = one value per channel. Useful for pacing audio generation (e.g. keep 2000–4000 samples buffered).
Returns Integer
render_text(text, r, g, b, a 255, premultiply false)
Render a string to a new Texture using TTF_RenderUTF8_Blended.
The texture has the exact pixel dimensions of the rendered text.
When premultiply is true, each pixel’s RGB is multiplied by its
alpha before creating the texture. This is required for custom blend
modes (from SDL2.compose_blend_mode) that read source RGB
independently of source alpha — without it, the “transparent”
background of the text surface retains the foreground color,
causing the entire texture rect to be visible.
Parameters
textString— the text to render (UTF-8)rInteger— red (0–255)gInteger— green (0–255)bInteger— blue (0–255)aInteger— alpha (0–255)premultiplyBoolean— premultiply alpha for custom blend modes
Returns Texture — a new texture containing the rendered text
Members: Classes (9)
- C AudioStream
- C Font
- C Gamepad
- C Music
- C NullAudioStream
- C Renderer
- C Sound
- C Texture
- C Viewport