Draw a 2D scene
Start a frame, draw rectangles or batched quads, and present the result.
game.begin_frame()
arena {
game.draw_rect(80.0, 90.0, 32.0, 32.0, color::green())
game.present()
}PRACTICAL RECIPES · BUILT FROM REPO EXAMPLES
Start with what you want to build. Each guide links to the repository folder or engine source and uses the same APIs and patterns as the project code.
2D
Start a frame, draw rectangles or batched quads, and present the result.
game.begin_frame()
arena {
game.draw_rect(80.0, 90.0, 32.0, 32.0, color::green())
game.present()
}Convert world positions to screen positions and pan or zoom your view.
let camera = render::camera_new(800.0, 600.0)
let screen_pos = camera.world_to_screen(player_pos)
game.draw_rect(screen_pos.x, screen_pos.y, 24.0, 24.0, color::yellow())Combine input edges, gravity, jump timing, and AABB collision resolution.
let wants_jump = controls.is_key_pressed(input::key_space())
if controls.is_key_down(input::key_left()) { player.vx = -speed }
let hit = physics2d::check_aabb_xy(
player.x, player.y, player.w, player.h,
ground.x, ground.y, ground.w, ground.h
)
if hit.has_collision { player.y -= hit.normal.y * hit.depth }Use overlap tests, collision manifolds, and the spatial hash broadphase.
let hit = physics2d::check_aabb_xy(ax, ay, aw, ah, bx, by, bw, bh)
if hit.has_collision {
ax -= hit.normal.x * hit.depth
ay -= hit.normal.y * hit.depth
}Upload tightly packed RGBA8 pixels, bind the returned texture ID, and map its UV range onto a batched quad.
let pixels = []u8{
255, 64, 64, 255, 64, 255, 64, 255,
64, 64, 255, 255, 255, 255, 255, 255
}
let texture_id = platform::create_texture(2, 2, pixels)
let mut batch = render::batcher_new(1)
batch.draw_quad(100.0, 80.0, 128.0, 128.0,
0.0, 0.0, 1.0, 1.0, color::white())
platform::bind_texture(texture_id)
platform::submit(batch.vertices)
platform::present(0.04, 0.04, 0.06)3D
Set a perspective camera, load primitive meshes, and submit transform batches.
let camera = render3d::camera3d_new(eye, target, 800.0 / 600.0)
platform3d::set_camera(camera.view_projection(), camera.position)
let mut models = []math::Mat4{}
models.push(math::mat4_translate(0.0, 0.0, 0.0))
platform3d::draw_instances(cube_mesh, models)Create vertex and index buffers in Ube, upload them, then draw instances.
let mesh_id = platform3d::upload_mesh(vertices, indices)
let mut models = []math::Mat4{}
models.push(math::mat4_mul(
math::mat4_translate(x, y, z),
math::mat4_scale(sx, sy, sz)
))
platform3d::draw_instances(mesh_id, models)CAMERA
Derive the forward vector from yaw and pitch, clamp pitch, and move on the ground plane.
pitch = clamp_f32(pitch, -1.48, 1.48)
let cos_p = math::cos(pitch)
let forward = math::vec3(
math::sin(yaw) * cos_p, math::sin(pitch),
0.0 - math::cos(yaw) * cos_p
)
let target = eye.add(forward)
let camera = render3d::camera3d_new(eye, target, aspect)Orbit around the target with yaw, pitch, and boom distance; smooth the camera toward its desired position.
let offset = math::vec3(
math::sin(orbit_yaw) * math::cos(orbit_pitch) * orbit_dist,
math::sin(orbit_pitch) * orbit_dist,
math::cos(orbit_yaw) * math::cos(orbit_pitch) * orbit_dist
)
let desired_eye = player_pos.add(offset)
camera_eye = lerp_vec3(camera_eye, desired_eye, clamp_f32(9.0 * dt, 0.0, 1.0))
let camera = render3d::camera3d_new(camera_eye, player_pos, aspect)Pan relative to camera heading, rotate in fixed steps, and zoom around a ground focal point.
let pan_forward = math::vec3(math::sin(cam_yaw), 0.0, 0.0 - math::cos(cam_yaw)).normalized()
let pan_right = math::vec3(math::cos(cam_yaw), 0.0, math::sin(cam_yaw)).normalized()
if game.input.is_key_pressed(input::key_q()) { cam_yaw -= 0.785398 }
if game.input.is_key_pressed(input::key_e()) { cam_yaw += 0.785398 }Decay shake trauma over time and use a clamped interpolation factor for spring-like camera follow.
shake_trauma = clamp_f32(shake_trauma - 1.4 * dt, 0.0, 1.0)
let shake_power = shake_trauma * shake_trauma
let shake = math::vec3(math::sin(time * 47.0), math::cos(time * 53.0), math::sin(time * 61.0)).scale(shake_power)
let follow = clamp_f32(7.5 * dt, 0.0, 1.0)
camera_pos = lerp_vec3(camera_pos, ideal_cam_pos, follow)LIGHTING
Rotate a directional sun and blend light and clear colors for the time of day.
let angle = time_of_day * 6.28318
let sun_dir = math::vec3(math::cos(angle), math::sin(angle), 0.35).normalized()
platform3d::set_directional_light(sun_dir, sun_color, sun_intensity)
platform3d::present(sky.r as f64, sky.g as f64, sky.b as f64)Apply quadratic radial falloff to tile colors around a moving light source.
let dist_sq = dx * dx + dy * dy
let radius_sq = radius * radius
let mut intensity = ambient
if dist_sq < radius_sq {
intensity += (1.0 - dist_sq / radius_sq) * brightness
}
batch.draw_rect_rgba(x, y, tile_size, tile_size, r * intensity, g * intensity, b * intensity, 1.0)Cycle a directional key light through hues and compare its effect on tinted meshes.
platform3d::set_directional_light(light_dir, key_color, 1.0)
platform3d::draw_instances_tinted(mesh_id, models, material_tints)Calculate distance attenuation per object, then submit the result as per-instance tint colors.
let dist_sq = dx * dx + dy * dy + dz * dz
let dist = sqrt_approx(dist_sq)
let att = 1.0 / (1.0 + 0.08 * dist + 0.015 * dist * dist)
let r = 0.08 + att * 0.95
let g = 0.10 + att * 0.70
let b = 0.15 + att * 0.25
tints.push(color::rgba(r, g, b, 1.0))Brighten grazing surfaces by cubing one minus the view-facing normal dot product.
let inv = 1.0 - ndotv
let fresnel = inv * inv * inv
let r = 0.08 + fresnel * 0.20
let g = 0.12 + fresnel * 0.85
let b = 0.20 + fresnel * 0.80
tints.push(color::rgba(r, g, b, 1.0))INPUT
Use is_key_down for continuous movement and is_key_pressed for one-shot actions.
if controls.is_key_down(input::key_a()) { player.x -= speed * dt }
if controls.is_key_pressed(input::key_space()) {
if grounded { velocity_y = jump_speed }
}
if controls.is_key_pressed(input::key_escape()) { game.stop() }GAMEPLAY
Create entities in a World, validate handles, and despawn safely when they leave play.
let mut world = ecs::world_new(256)
let id = world.create_sprite_rgba(x, y, w, h, 0.2, 0.8, 0.3, 1.0)
if let entity = world.get(id) {
game.draw_rect(entity.transform.position.x, entity.transform.position.y,
entity.sprite.size.x, entity.sprite.size.y, entity.sprite.color)
}Scope temporary slices and rendering data so scratch allocations are reclaimed together.
while game.is_running {
game.begin_frame()
arena {
let mut transforms = []math::Mat4{}
// Fill frame-local data and submit it here.
game.present()
}
}Draw compact bitmap HUD labels, then use an arena scoped grid to find broadphase candidates. Follow candidates with exact overlap tests.
batch.draw_text("SCORE: 10", 16.0, 16.0, 2.0, color::white())
let mut grid = physics2d::grid_new(32.0)
grid.insert(entity_id, x, y, width, height)
let nearby = grid.query_aabb(query_x, query_y, query_w, query_h)
// A grid hit is a candidate; check exact bounds before resolving collision.
if physics2d::overlaps(entity_bounds, query_bounds) {
resolve_collision(entity_id)
}PERSISTENCE
Serialize game values to bytes, load them at startup, and handle failed writes. Ube values outside an arena live between frames; storage persists bytes after the process exits.
fn encode_score(score: i32) -> []u8 {
let n = score as u32
return []u8{
(n & 255) as u8,
((n >> 8) & 255) as u8,
((n >> 16) & 255) as u8,
((n >> 24) & 255) as u8
}
}
fn decode_score(view bytes: []u8) -> i32 {
if bytes.len < 4 { return 0 }
return (bytes[0] as i32) |
((bytes[1] as i32) << 8) |
((bytes[2] as i32) << 16) |
((bytes[3] as i32) << 24)
}
let loaded = storage::load_bytes("profile/best-score")
let mut best_score = 0
if loaded.found { best_score = decode_score(loaded.data) }
// After updating the record:
let bytes = encode_score(best_score)
if !storage::save_bytes("profile/best-score", bytes) {
// Report the error or retry the save later.
}SHADERS
The 2D shader samples the selected texture at the interpolated UV and multiplies it by vertex color. The 3D shader applies host-defined directional lighting and per-instance tint.
let texture_id = platform::create_texture(width, height, rgba_pixels)
let mut batch = render::batcher_new(1)
batch.draw_quad(x, y, w, h, 0.0, 0.0, 1.0, 1.0, color::white())
platform::bind_texture(texture_id)
platform::submit(batch.vertices)
platform::present(0.02, 0.02, 0.04)
// 3D appearance uses supported host inputs:
platform3d::set_directional_light(light_dir, light_color, intensity)
platform3d::draw_instances_tinted(mesh_id, models, tints)