/ Opening a Window
Documentation

Getting Started

Language Guide

SDL2 and Graphics

CLI Reference

SDL2 and Graphics

Vexel has first-class SDL2 support for building games and graphical applications. SDL2 functions are available as built-in globals when you compile with the --sdl2 flag.

Setup

SDL2 requires the SDL2.dll file in your project directory on Windows. Run the setup helper once to download it:

python setup_sdl2.py

Compile with the SDL2 flag to link the library:

vexel compile game.vx --sdl2 -o game

Opening a Window

Call sdl2_init to create a window. It returns 1 on success or 0 on failure. Always call sdl2_quit before the program exits.

fn main():
    let ok: int = sdl2_init("My Game", 800, 600)
    if ok == 0:
        return
    # ... game loop ...
    sdl2_quit()

Game Loop

A typical game loop polls for events, clears the screen, draws the frame, and presents it. sdl2_poll returns 0 when the user closes the window.

fn main():
    let ok: int = sdl2_init("Bouncing Square", 800, 600)
    if ok == 0:
        return

    let x: int = 100
    let y: int = 100
    let vx: int = 4
    let vy: int = 3
    let running: int = 1

    while running == 1:
        running = sdl2_poll()

        sdl2_clear(20, 20, 40)

        sdl2_set_color(255, 80, 80, 255)
        sdl2_draw_rect(x, y, 40, 40)

        sdl2_present()
        sdl2_delay(16)   # ~60 fps

        x = x + vx
        y = y + vy
        if x > 760 or x < 0:
            vx = -vx
        if y > 560 or y < 0:
            vy = -vy

    sdl2_quit()

SDL2 Function Reference

sdl2_init(title, w, h) Create a window. Returns 1 on success, 0 on failure.
sdl2_quit() Destroy the window and shut down SDL2.
sdl2_poll() Process events. Returns 0 if the window was closed.
sdl2_clear(r, g, b) Fill the screen with the given RGB color.
sdl2_set_color(r, g, b, a) Set the current draw color (RGBA, 0-255).
sdl2_draw_rect(x, y, w, h) Draw a filled rectangle at (x, y) with size (w, h).
sdl2_present() Flip the back buffer and display the current frame.
sdl2_delay(ms) Pause execution for the given number of milliseconds.
← Type Aliases CLI Reference →