SDL2
Examples
Required version: Sponge >=1.0.4
Basic Window
The simplest example that creates a window:
use sdl2;
sdl2.init();
sdl2.create_window("Hello Sponge", 800, 600);
while sdl2.is_window_open() {
sdl2.clear_screen();
sdl2.render_present();
}Drawing Shapes
Draw various shapes with different colors:
use sdl2;
sdl2.init();
sdl2.create_window("Shapes Demo", 800, 600);
while sdl2.is_window_open() {
sdl2.clear_screen();
# Red filled circle
sdl2.set_draw_color(255, 0, 0);
sdl2.fill_circle(200, 200, 50);
# Green circle outline
sdl2.set_draw_color(0, 255, 0);
sdl2.draw_circle(400, 200, 50);
# Blue rectangle
sdl2.set_draw_color(0, 0, 255);
sdl2.draw_rect(200, 350, 100, 100);
# Yellow filled rectangle
sdl2.set_draw_color(255, 255, 0);
sdl2.fill_rect(400, 350, 100, 100);
# White lines
sdl2.set_draw_color(255, 255, 255);
sdl2.draw_line(0, 0, 800, 600);
sdl2.draw_line(800, 0, 0, 600);
sdl2.render_present();
}Animation
Create an animated scene:
use sdl2;
sdl2.init();
sdl2.create_window("Animation Demo", 800, 600);
let frame = 0;
let center_x = 400;
let center_y = 300;
while sdl2.is_window_open() {
sdl2.clear_screen();
# Draw pulsing circle
sdl2.set_draw_color(255, 0, 0);
let radius = 30 + (frame % 30);
sdl2.fill_circle(center_x, center_y, radius);
# Draw rotating lines
sdl2.set_draw_color(0, 255, 0);
let line_offset = (frame * 2) % 400;
sdl2.draw_line(center_x - line_offset, center_y, center_x + line_offset, center_y);
sdl2.render_present();
frame = frame + 1;
}Interactive Grid
Draw an interactive grid pattern:
use sdl2;
sdl2.init();
sdl2.create_window("Grid Demo", 800, 600);
let cell_size = 40;
while sdl2.is_window_open() {
sdl2.clear_screen();
# Draw grid
sdl2.set_draw_color(100, 100, 100);
let x = 0;
while x < 800 {
sdl2.draw_line(x, 0, x, 600);
x = x + cell_size;
}
let y = 0;
while y < 600 {
sdl2.draw_line(0, y, 800, y);
y = y + cell_size;
}
# Draw center point
sdl2.set_draw_color(255, 0, 0);
sdl2.fill_circle(400, 300, 5);
sdl2.render_present();
}Window Info
Get and display window information:
use sdl2;
sdl2.init();
sdl2.create_window("Window Info", 1024, 768);
let width = sdl2.get_window_width();
let height = sdl2.get_window_height();
print("Window created with dimensions: ");
print(width);
print("x");
print(height);
sdl2.set_draw_color(255, 255, 255);
sdl2.draw_line(0, 0, width, height);
sdl2.draw_line(width, 0, 0, height);
while sdl2.is_window_open() {
sdl2.clear_screen();
sdl2.set_draw_color(255, 255, 255);
sdl2.draw_line(0, 0, width, height);
sdl2.draw_line(width, 0, 0, height);
sdl2.render_present();
}Common Patterns
Game Loop
The standard game loop structure:
use sdl2;
def main() {
sdl2.init();
sdl2.create_window("Game", 800, 600);
while sdl2.is_window_open() {
# Clear
sdl2.clear_screen();
# Update game state
# ... your code here ...
# Draw
# ... drawing code here ...
# Present
sdl2.render_present();
}
}Color Constants
Define reusable colors:
def set_red() {
sdl2.set_draw_color(255, 0, 0);
}
def set_green() {
sdl2.set_draw_color(0, 255, 0);
}
def set_blue() {
sdl2.set_draw_color(0, 0, 255);
}
def set_white() {
sdl2.set_draw_color(255, 255, 255);
}
def set_black() {
sdl2.set_draw_color(0, 0, 0);
}Drawing Functions
Create reusable drawing functions:
def draw_grid(width, height, spacing) {
let x = 0;
while x < width {
sdl2.draw_line(x, 0, x, height);
x = x + spacing;
}
let y = 0;
while y < height {
sdl2.draw_line(0, y, width, y);
y = y + spacing;
}
}
def draw_cross(cx, cy, size) {
sdl2.draw_line(cx - size, cy, cx + size, cy);
sdl2.draw_line(cx, cy - size, cx, cy + size);
}Delta Time Example
Required version: Sponge >=1.0.6
Create frame-rate independent movement:
use sdl2;
def main() {
sdl2.init();
sdl2.create_window("Delta Time Demo", 800, 600);
sdl2.set_max_fps(60);
let x = 0;
let speed = 200; # pixels per second
while sdl2.is_window_open() {
let dt = sdl2.delta_time();
x = x + speed * dt;
sdl2.clear_screen();
sdl2.set_draw_color(255, 0, 0);
sdl2.fill_rect(x, 100, 50, 50);
sdl2.render_present();
}
}Movement Example
Required version: Sponge >=1.0.6
A simple movement example using input handling:
use sdl2;
def main() {
sdl2.init();
sdl2.create_window("Rectangle Movement", 800, 600);
sdl2.set_max_fps(60);
# Rectangle properties
let rect_x = 350;
let rect_y = 250;
let rect_width = 100;
let rect_height = 100;
let speed = 300; # pixels per second
while sdl2.is_window_open() {
let dt = sdl2.delta_time();
# Handle input
if sdl2.get_input_held("w") {
rect_y = rect_y - (speed * dt);
}
if sdl2.get_input_held("s") {
rect_y = rect_y + (speed * dt);
}
if sdl2.get_input_held("a") {
rect_x = rect_x - (speed * dt);
}
if sdl2.get_input_held("d") {
rect_x = rect_x + (speed * dt);
}
if sdl2.get_input_pressed("escape") {
sdl2.destroy_window();
}
# Clear screen
sdl2.clear_screen();
# Draw rectangle
sdl2.set_draw_color(0, 150, 255);
sdl2.fill_rect(rect_x, rect_y, rect_width, rect_height);
# Draw a border
sdl2.set_draw_color(255, 255, 255);
sdl2.draw_rect(rect_x, rect_y, rect_width, rect_height);
# Present frame
sdl2.render_present();
}
}