Teek::SDL2::Viewport Class
An SDL2-accelerated rendering surface embedded in a Tk frame.
Viewport creates a Tk frame, obtains its native window handle via
the Tk C API, then embeds an SDL2 renderer inside it using
SDL_CreateWindowFrom. All drawing goes through SDL2 with GPU
acceleration — no Tk involvement in the rendering path.
Keyboard input is tracked automatically via Tk bindings so you can
poll key state with #key_down? in a game loop.
Inherits: Object
Instance Methods ↑
bind(event, *subs, &block)
Bind a Tk event on the viewport frame.
Automatically chains with internal behavior (key tracking,
click-to-focus) so user callbacks don’t clobber #key_down?.
Parameters
eventString— Tk event name (e.g.'KeyPress','ButtonPress-1')subsArray<Symbol, String>— Tk substitution codes
Returns void
@yield called when the event fires
destroyed?
Returns Boolean — whether this viewport has been destroyed
grid(**kwargs)
Grid the viewport into its parent using Tk’s grid geometry manager.
Parameters
kwargs— options passed to the Tkgridcommand
Returns self
initialize(app, parent: nil, width: 640, height: 480, vsync: true)
Parameters
appTeek::App— the Teek applicationparentTeek::Widget, String, nil— parent widget (nil for root)widthInteger— initial width in pixelsheightInteger— initial height in pixelsvsyncBoolean— enable VSync (default: true). Disable for applications that manage their own frame pacing (e.g. emulators).
Returns Viewport — a new instance of Viewport
key_down?(keysym)
Check if a key is currently held down. Uses Tk keysym names (lowercase).
Parameters
keysymString, Symbol— Tk keysym name (e.g.'left','space','a')
Returns Boolean
Example
viewport.key_down?('left') # arrow key
viewport.key_down?('space') # spacebar
viewport.key_down?('a') # letter key
pack(**kwargs)
Pack the viewport into its parent using Tk’s pack geometry manager.
Parameters
kwargs— options passed to the Tkpackcommand
Returns self
render(&block)
Draw with the renderer in a block, auto-presenting at the end.
Returns self
@raise Teek::SDL2::Error if the viewport has been destroyed
@yield renderer the SDL2 renderer for this viewport
@yieldparam renderer Teek::SDL2::Renderer
Example
viewport.render do |r|
r.clear(0, 0, 0)
r.fill_rect(10, 10, 100, 50, 255, 0, 0)
end
Attributes ↑
app [R]
Returns Teek::App — the Teek application
frame [R]
Returns Teek::Widget — the underlying Tk frame
keys_down [R]
Returns Set<String> — currently held key names (lowercase keysyms)
renderer [R]
Returns Teek::SDL2::Renderer — the SDL2 renderer