Texture atlas
Find the smallest character on the sheet. Which rectangle would you copy to draw just that pose?
Many different images packed tightly into one big image.
A sprite sheet is a tidy grid for one character; an atlas packs anything that fits, at any size. The game remembers the rectangle for each piece. Drawing from one image is much faster for the graphics card than switching between hundreds.
Code
namespace texture_atlas_lesson;
import stage;
import std;
import math;
fn draw_entry(id: i64, x: f64, y: f64, size: f64) {
if id < 2 {
image(id, x, y, size);
}
else if id == 4 {
image(3, x, y, size, "moss");
}
else { tile_image(match id { 2 => 0, 3 => 2, 5 => 3, _ => 1 }, x, y, size); }
}
export "texture-atlas" fn draw(time: f64, dt: f64, px: f64, py: f64, hover: bool) {
artwork("draft");
// Each atlas entry carries its own origin and scale.
let packed = [[0, 3, 0, 0], [1, 2, 36, 0], [2, 3, 36, 24], [3, 3, 0, 36],
[4, 1, 60, 0], [5, 1, 60, 12], [6, 1, 24, 36], [5, 1, 24, 44]];
checker(10, 20, 72, 60, 6);
for entry in packed {
draw_entry(entry[0], 10 + entry[2], 20 + entry[3], entry[1]);
}
let e = packed[(time / 1.2) as i64 % 4];
let width = if e[0] < 2 { 12 } else { 8 };
let size = width * e[1];
let x = 10 + e[2];
let y = 20 + e[3];
box(x, y, size, size, MAG);
line(x + size, y + size / 2.0, 108, 50, MAG, 2, 2);
draw_entry(e[0], 126 - width * 2, 50 - width * 2, 4);
}

Comments
No comments yet.