sdl

SDL2 bindings for Kit - cross-platform windowing, input, and graphics

Files

FileDescription
kit.tomlPackage manifest with metadata and dependencies
src/sdl.kitWindow, renderer, events, input, and drawing API
tests/sdl-properties-test.kitColor, coordinate, and state pattern validation
tests/sdl.test.kitEvent constants, scancodes, and record patterns
examples/animation.kitBouncing ball with pause and trail effects
examples/basic.kitWindow setup with rectangles, lines, and points
examples/input.kitWASD/arrow movement with mouse tracking
LICENSEMIT license file

Dependencies

No Kit package dependencies.

Installation

kit add gitlab.com/kit-lang/packages/kit-sdl.git

Usage

import Kit.Sdl

License

MIT License - see LICENSE for details.

Exported Functions & Types

quit

Shut down SDL and clean up all subsystems.

Returns:

Unit

quit()

get-error

Get the last SDL error message.

Returns:

String

error = get-error()
print "SDL error: ${error}"

create-window-ex

Create a window with explicit position and flags.

Parameters:

Returns:

String -> Int -> Int -> Int -> Int -> Int -> Result {ptr: Ptr} String

flags = window-shown | window-resizable
match create-window-ex "My Window" 100 100 800 600 flags
  | Ok window -> print "Window created"
  | Err err -> print "Failed: ${err}"

destroy-window

Destroy a window and free its resources.

Parameters:

Returns:

{ptr: Ptr} -> Unit

destroy-window window

set-window-title

{ptr: Ptr} -> String -> Unit

get-window-title

{ptr: Ptr} -> String

set-window-size

{ptr: Ptr} -> Int -> Int -> Unit

get-window-width

{ptr: Ptr} -> Int

get-window-height

{ptr: Ptr} -> Int

set-window-position

{ptr: Ptr} -> Int -> Int -> Unit

get-window-x

{ptr: Ptr} -> Int

get-window-y

{ptr: Ptr} -> Int

show-window

{ptr: Ptr} -> Unit

hide-window

{ptr: Ptr} -> Unit

raise-window

{ptr: Ptr} -> Unit

maximize-window

{ptr: Ptr} -> Unit

minimize-window

{ptr: Ptr} -> Unit

restore-window

{ptr: Ptr} -> Unit

set-fullscreen

{ptr: Ptr} -> Bool -> Unit

is-fullscreen?

{ptr: Ptr} -> Bool

create-renderer

Create a hardware-accelerated renderer for a window.

Parameters:

Returns:

{ptr: Ptr} -> Result {ptr: Ptr} String

match create-renderer window
  | Ok renderer ->
    # Use renderer...
    destroy-renderer renderer
  | Err err -> print "Failed: ${err}"

create-software-renderer

Create a software renderer for a window (slower, no hardware acceleration).

Parameters:

Returns:

{ptr: Ptr} -> Result {ptr: Ptr} String

match create-software-renderer window
  | Ok renderer -> print "Software renderer created"
  | Err err -> print "Failed: ${err}"

destroy-renderer

Destroy a renderer and free its resources.

Parameters:

Returns:

{ptr: Ptr} -> Unit

destroy-renderer renderer

clear

Clear the rendering target with the current draw color.

Parameters:

Returns:

{ptr: Ptr} -> Unit

set-draw-color renderer 0 0 0 255
clear renderer

present

Present the current rendering to the window (swap buffers).

Parameters:

Returns:

{ptr: Ptr} -> Unit

clear renderer
# Draw stuff...
present renderer

set-draw-color

Set the color used for drawing operations.

Parameters:

Returns:

{ptr: Ptr} -> Int -> Int -> Int -> Int -> Unit

set-draw-color renderer 255 0 0 255  # Red

draw-point

Draw a single point at the specified position.

Parameters:

Returns:

{ptr: Ptr} -> Int -> Int -> Unit

set-draw-color renderer 255 255 255 255
draw-point renderer 100 100

draw-line

Draw a line between two points.

Parameters:

Returns:

{ptr: Ptr} -> Int -> Int -> Int -> Int -> Unit

draw-line renderer 0 0 100 100

draw-rect

Draw a rectangle outline.

Parameters:

Returns:

{ptr: Ptr} -> Int -> Int -> Int -> Int -> Unit

draw-rect renderer 50 50 100 100

fill-rect

Draw a filled rectangle.

Parameters:

Returns:

{ptr: Ptr} -> Int -> Int -> Int -> Int -> Unit

set-draw-color renderer 0 255 0 255
fill-rect renderer 50 50 100 100

set-viewport

{ptr: Ptr} -> Int -> Int -> Int -> Int -> Unit

reset-viewport

{ptr: Ptr} -> Unit

load-texture

{ptr: Ptr} -> String -> Result {ptr: Ptr} String

destroy-texture

{ptr: Ptr} -> Unit

texture-width

{ptr: Ptr} -> Int

texture-height

{ptr: Ptr} -> Int

draw-texture

{ptr: Ptr} -> {ptr: Ptr} -> Int -> Int -> Unit

draw-texture-sized

{ptr: Ptr} -> {ptr: Ptr} -> Int -> Int -> Int -> Int -> Unit

draw-texture-ex

{ptr: Ptr} -> {ptr: Ptr} -> Int -> Int -> Int -> Int -> Float -> Bool -> Bool -> Unit

set-texture-alpha

{ptr: Ptr} -> Int -> Unit

set-texture-color

{ptr: Ptr} -> Int -> Int -> Int -> Unit

poll-event

Poll for pending events without blocking.

Returns:

Option {event-type: Int, key-scancode: Int, key-keycode: Int, key-mod: Int, key-repeat: Int, mouse-x: Int, mouse-y: Int, mouse-button: Int, mouse-clicks: Int, mouse-rel-x: Int, mouse-rel-y: Int, wheel-x: Int, wheel-y: Int, window-event: Int}

match poll-event()
  | Some event ->
    if event.event-type == event-quit then
      print "Quit requested"
    else if event.event-type == event-keydown then
      print "Key pressed: ${Int.to-string event.key-scancode}"
  | None -> ()  # No events

wait-event

Wait indefinitely for an event (blocks until event arrives).

Returns:

Option {event-type: Int, key-scancode: Int, key-keycode: Int, key-mod: Int, key-repeat: Int, mouse-x: Int, mouse-y: Int, mouse-button: Int, mouse-clicks: Int, mouse-rel-x: Int, mouse-rel-y: Int, wheel-x: Int, wheel-y: Int, window-event: Int}

match wait-event()
  | Some event -> print "Got event: ${Int.to-string event.event-type}"
  | None -> print "Error waiting for event"

wait-event-timeout

Wait for an event with timeout.

Parameters:

Returns:

Int -> Option {event-type: Int, key-scancode: Int, key-keycode: Int, key-mod: Int, key-repeat: Int, mouse-x: Int, mouse-y: Int, mouse-button: Int, mouse-clicks: Int, mouse-rel-x: Int, mouse-rel-y: Int, wheel-x: Int, wheel-y: Int, window-event: Int}

match wait-event-timeout 1000
  | Some event -> print "Got event within 1 second"
  | None -> print "Timeout or no events"

is-key-pressed?

Int -> Bool

get-mod-state

Int

is-shift-pressed?

Bool

is-ctrl-pressed?

Bool

is-alt-pressed?

Bool

get-mouse-x

Int

get-mouse-y

Int

get-mouse-buttons

Int

is-mouse-button-pressed?

Int -> Bool

warp-mouse

{ptr: Ptr} -> Int -> Int -> Unit

set-relative-mouse-mode

Bool -> Unit

is-relative-mouse-mode?

Bool

show-cursor

Unit

hide-cursor

Unit

is-cursor-visible?

Bool

get-ticks

Get milliseconds since SDL initialization.

Returns:

Int

start = get-ticks()
# Do work...
elapsed = get-ticks() - start
print "Took ${Int.to-string elapsed}ms"

delay

Delay execution for specified milliseconds.

Parameters:

Returns:

Int -> Unit

print "Waiting..."
delay 1000  # Wait 1 second
print "Done!"

get-performance-counter

Get high-precision performance counter value.

Returns:

Int

start = get-performance-counter()
# Do work...
end = get-performance-counter()
freq = get-performance-frequency()
seconds = (end - start) / freq

get-performance-frequency

Get performance counter frequency (ticks per second).

Returns:

Int

freq = get-performance-frequency()
print "Timer frequency: ${Int.to-string freq} Hz"

get-clipboard-text

Option String

set-clipboard-text

String -> Unit

has-clipboard-text?

Bool

get-num-displays

Int

get-display-width

Int -> Int

get-display-height

Int -> Int

get-display-refresh-rate

Int -> Int

get-display-name

Int -> String

show-info

String -> String -> Unit

show-warning

String -> String -> Unit

show-error

String -> String -> Unit

num-joysticks

Int

close-joystick

{ptr: Ptr} -> Unit

joystick-name

{ptr: Ptr} -> String

joystick-num-axes

{ptr: Ptr} -> Int

joystick-num-buttons

{ptr: Ptr} -> Int

joystick-get-axis

{ptr: Ptr} -> Int -> Int

is-joystick-button-pressed?

{ptr: Ptr} -> Int -> Bool

is-game-controller?

Int -> Bool

close-game-controller

{ptr: Ptr} -> Unit

game-controller-name

{ptr: Ptr} -> String

game-controller-get-axis

{ptr: Ptr} -> Int -> Int

is-controller-button-pressed?

{ptr: Ptr} -> Int -> Bool

game-controller-update

Unit

game-controller-name-for-index

Int -> String

game-controller-is-attached?

{ptr: Ptr} -> Bool

game-controller-get-joystick

{ptr: Ptr} -> Result {ptr: Ptr} String

game-controller-get-type

{ptr: Ptr} -> Int

game-controller-has-axis?

{ptr: Ptr} -> Int -> Bool

game-controller-has-button?

{ptr: Ptr} -> Int -> Bool

game-controller-rumble

{ptr: Ptr} -> Int -> Int -> Int -> Unit

game-controller-has-rumble?

{ptr: Ptr} -> Bool

game-controller-set-led

{ptr: Ptr} -> Int -> Int -> Int -> Unit

game-controller-has-led?

{ptr: Ptr} -> Bool

get-version

{major: Int, minor: Int, patch: Int}

get-revision

String

get-platform

String

get-base-path

String

get-pref-path

String -> String -> String

get-power-info

{state: Int, seconds: Int, percent: Int}

is-on-battery?

Bool

is-charging?

Bool

is-charged?

Bool

set-hint

String -> String -> Unit

get-hint

String -> String

is-hint-boolean?

String -> Bool -> Bool

clear-hints

Unit

get-num-audio-drivers

Int

get-audio-driver

Int -> String

get-current-audio-driver

String

get-num-audio-devices

Bool -> Int

get-audio-device-name

Int -> Bool -> String

close-audio-device

Int -> Unit

pause-audio-device

Int -> Bool -> Unit

play-audio-device

Int -> Unit

get-audio-device-status

Int -> Int

is-audio-playing?

Int -> Bool

get-queued-audio-size

Int -> Int

clear-queued-audio

Int -> Unit

load-wav

String -> Result {buffer: Ptr, length: Int, freq: Int, format: Int, channels: Int} String

free-wav

Unit

num-haptics

Int

haptic-name

Int -> String

close-haptic

{ptr: Ptr} -> Unit

haptic-rumble-init

{ptr: Ptr} -> Unit

haptic-rumble-play

{ptr: Ptr} -> Float -> Int -> Unit

haptic-rumble-stop

{ptr: Ptr} -> Unit

is-haptic-rumble-supported?

{ptr: Ptr} -> Bool

haptic-set-gain

{ptr: Ptr} -> Int -> Unit

haptic-pause

{ptr: Ptr} -> Unit

haptic-unpause

{ptr: Ptr} -> Unit

haptic-stop-all

{ptr: Ptr} -> Unit

joystick-is-haptic?

{ptr: Ptr} -> Bool

mouse-is-haptic?

Bool

get-num-touch-devices

Int

get-touch-device

Int -> Int

get-touch-device-type

Int -> Int

get-num-touch-fingers

Int -> Int

get-touch-finger

Int -> Int -> {id: Int, x: Float, y: Float, pressure: Float}

num-sensors

Int

sensor-get-device-name

Int -> String

sensor-get-device-type

Int -> Int

close-sensor

{ptr: Ptr} -> Unit

sensor-get-name

{ptr: Ptr} -> String

sensor-get-type

{ptr: Ptr} -> Int

sensor-update

Unit

gl-set-attribute

Int -> Int -> Unit

gl-get-attribute

Int -> Int

gl-delete-context

{ptr: Ptr} -> Unit

gl-make-current

{ptr: Ptr} -> {ptr: Ptr} -> Unit

gl-swap-window

{ptr: Ptr} -> Unit

gl-set-swap-interval

Int -> Unit

gl-get-swap-interval

Int

gl-extension-supported?

String -> Bool

gl-reset-attributes

Unit

gl-get-drawable-size

{ptr: Ptr} -> {width: Int, height: Int}

start-text-input

Unit

stop-text-input

Unit

is-text-input-active?

Bool

set-text-input-rect

Int -> Int -> Int -> Int -> Unit

has-screen-keyboard-support?

Bool

is-screen-keyboard-shown?

{ptr: Ptr} -> Bool

create-system-cursor

Int -> Result {ptr: Ptr} String

set-cursor

{ptr: Ptr} -> Unit

free-cursor

{ptr: Ptr} -> Unit

joystick-update

Unit

joystick-name-for-index

Int -> String

joystick-num-hats

{ptr: Ptr} -> Int

joystick-num-balls

{ptr: Ptr} -> Int

joystick-get-hat

{ptr: Ptr} -> Int -> Int

joystick-is-attached?

{ptr: Ptr} -> Bool

joystick-instance-id

{ptr: Ptr} -> Int

joystick-current-power-level

{ptr: Ptr} -> Int

joystick-rumble

{ptr: Ptr} -> Int -> Int -> Int -> Unit

joystick-has-rumble?

{ptr: Ptr} -> Bool

joystick-set-led

{ptr: Ptr} -> Int -> Int -> Int -> Unit

joystick-has-led?

{ptr: Ptr} -> Bool

joystick-get-type

{ptr: Ptr} -> Int