Sprite
enum SpriteSourcecase Sprite({ height = Int32, rows = Vector[String], runs = List[(Int32, Int32, Int32)], width = Int32 })Pixel art, as data.
A sprite is written out as rows of text -- # for a lit pixel, anything else for empty --
so the artwork is legible in the source and can be edited without a drawing tool. There
are no image files anywhere in this project.
Each row is collapsed into horizontal runs of lit pixels, and drawing emits one rectangle
per run rather than one per pixel -- roughly a quarter of the draw calls. The runs are
computed once, by Sprite.of, and stored: parsing the text on every frame for every
invader was enough to halve the frame rate.
Definitions
Draws a sprite centred on centre, with each sprite pixel scale screen pixels wide.
def drawScaled(sprite: Sprite, centre: Vec2, scaleX: { scaleX = Float32 }, scaleY: { scaleY = Float32 }): Unit \ Canvas§ SourceDraws a sprite centred on centre, stretched independently in each axis.
Separate axes so a shape can be made wide without also becoming tall -- which is what a flying saucer wants and a square pixel does not give you.
The two scales are named: they are both Float32, and swapping them silently
transposes the sprite.
The number of lit pixels. Handy for tests; not used when drawing.
Builds a sprite from rows of text. The width is the longest row.
Every lit run in the sprite, as (row, startColumn, length), top row first.