Teek API Documentation

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

available?

Whether at least one audio output device is available.

Returns Boolean

device_count

Number of audio output devices detected by SDL2.

Returns Integer

driver_name

Name of the current SDL2 audio driver (e.g. “wasapi”, “dummy”).

Returns String, nil

Audio (C-defined module functions)

channel_paused?(channel)

Whether the given channel is paused.

Parameters
  • channel Integer

Returns Boolean

channel_volume(channel, vol -1)

Set or query volume for a channel.

Parameters
  • channel Integer
  • vol Integer — 0–128, or -1 to query without changing

Returns Integer — current volume

close_audio

Shut down the audio mixer and free resources.

Returns nil

fade_out_channel(channel, ms)

Gradually fade out a channel.

Parameters
  • channel Integer
  • ms Integer — fade duration in milliseconds

Returns nil

fade_out_music(ms)

Gradually fade out the currently playing music.

Parameters
  • ms Integer — fade duration in milliseconds

Returns nil

halt(channel)

Immediately stop playback on a channel.

Parameters
  • channel Integer — channel number (returned by Sound#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
  • vol Integer — 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

pause_channel(channel)

Pause playback on a channel.

Parameters
  • channel Integer

Returns nil

playing?(channel)

Whether the given channel is currently playing.

Parameters
  • channel Integer

Returns Boolean

resume_channel(channel)

Resume a paused channel.

Parameters
  • channel Integer

Returns nil

start_audio_capture(path)

Begin recording mixed audio output to a WAV file. Everything that plays through the mixer (sounds, music) is captured.

Parameters
  • path String — output WAV file path

Returns nil

@raise RuntimeError if capture is already in progress

stop_audio_capture

Stop recording and finalize the WAV file. Safe to call even if no capture is in progress.

Returns nil

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): - :addsrc dst (all renderers) - :subtractsrc - dst - :rev_subtractdst - src - :minimummin(src, dst) - :maximummax(src, dst)+

Parameters
  • src_color_factor Symbol — multiplier for source RGB
  • dst_color_factor Symbol — multiplier for destination RGB
  • color_op Symbol — operation combining color components
  • src_alpha_factor Symbol — multiplier for source alpha
  • dst_alpha_factor Symbol — multiplier for destination alpha
  • alpha_op Symbol — 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_ms Integer — 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

channels

Number of audio channels (1 = mono, 2 = stereo).

Returns Integer

clear

Flush all queued audio data.

Returns nil

destroy

Close the audio device. Further method calls will raise.

Returns nil

destroyed?

Whether the audio stream has been destroyed.

Returns Boolean

format

Audio sample format.

Returns Symbol:s16, :f32, or :u8

frequency

Sample rate in Hz.

Returns Integer

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
  • frequency Integer — sample rate in Hz (default: 44100)
  • format Symbol — sample format — :s16 (signed 16-bit), :f32 (32-bit float), or :u8 (unsigned 8-bit)
  • channels Integer — 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
  • text String — the text to measure (UTF-8)

Returns Array(Integer, Integer)[width, height]

pause

Pause audio playback. Queued data is preserved.

Returns nil

playing?

Whether the audio device is currently playing (not paused).

Returns Boolean

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
  • data String — raw PCM samples (binary encoding)

Returns nil

queued_bytes

Bytes of audio data currently queued for playback.

Returns Integer

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
  • text String — the text to render (UTF-8)
  • r Integer — red (0–255)
  • g Integer — green (0–255)
  • b Integer — blue (0–255)
  • a Integer — alpha (0–255)
  • premultiply Boolean — premultiply alpha for custom blend modes

Returns Texture — a new texture containing the rendered text

resume

Start or unpause audio playback.

Returns nil

Members: Classes (9)