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()