gizgame/sprites
A game's flat drawing: its sprites, its icons and the shapes they are made of.
import * as Sprites from "gizgame/sprites";Types
Sprites
class Sprites { brush: Brush; private atlas: Atlas; private batches: Vec<Batch2D> = []; private versions: Vec<i64> = []; letters: Letters; private used: i64 = 0; private pass: i64 = -1; private most: i64; private images = new Map<string, Vec<f64>>(); private lit: Lights2D | undefined = undefined; private litWidth: i64 = 0; private litHeight: i64 = 0; circle: Vec<f64>; solid: Vec<f64>; round: Vec<f64>; fade: Vec<f64>; constructor(fonts: Fonts, size: i64 = 2048, most: i64 = 6000, lend: ((code: i64) => Lent | undefined) | undefined = undefined) throws string { let atlas = new Atlas(size, size, 256); this.brush = brush(atlas); this.circle = atlas.add(ring(48), 48, 48); let white: bytes = new Array(64).fill(255u); this.solid = atlas.add(white, 4, 4); this.atlas = atlas; this.most = most; this.round = atlas.add(rounded(), ROUND_SIDE, ROUND_SIDE); this.fade = atlas.add(picture(64, (x, y): Vec<f64> => { const t = x/64; return [1, Math.smoothstep(0, 1, 1-t)]; }), 64, 64); this.letters = new Letters(fonts, atlas, [], lend); } add(pixels: bytes, width: i64, height: i64): Vec<f64> throws string { return this.atlas.add(pixels, width, height); } ring(size: i64 = 48): Vec<f64> throws string { return this.atlas.add(ring(size), size, size); } icon(name: string): Vec<f64> throws string { const key = `icon ${name }`; const kept = this.images.get(key); if (kept !== undefined) { return kept!; } const inside = shape(name); const half = f64(ICON)*0.5; const uv = this.add(picture(ICON, (x, y): Vec<f64> => [1, inside((x-half)/(half-1), (y-half)/(half-1)) ? 1: 0]), ICON, ICON); this.images.set(key, uv); return uv; } load(game: Game, name: string): void throws string { if (this.images.has(name)) { return; } const image = Image.rgba(Image.decode(game.file(name), 4096)); this.images.set(name, this.add(image.pixels, image.width, image.height)); } image(name: string): Vec<f64> throws string { const uv = this.images.get(name); if (uv === undefined) { throw `No image ${name }: load it first`; } return uv; } lightsFor(picture: Picture): Lights2D throws string { const kept = this.lit; if (kept !== undefined && this.litWidth === picture.screen.width && this.litHeight === picture.screen.height) { return kept!; } if (kept !== undefined) { kept!.close(); } const made = new Lights2D(picture.resources, picture.screen.width, picture.screen.height); this.lit = made; this.litWidth = picture.screen.width; this.litHeight = picture.screen.height; return made; } canvas(picture: Picture, width: f64, height: f64): Canvas2D throws string { return new Canvas2D(picture.screen.width, picture.screen.height, width, height, this.brush, this.letters); } draw(picture: Picture, canvas: Canvas2D, clear: Vec<f64> = [0, 0, 0, 1]): void throws string { if (picture.passes <= this.pass) { this.used = 0; } const quads = canvas.quads; for (let at = 0; at === 0 || at <quads.length; at+ = this.most) { if (this.used === this.batches.length) { this.batches.push(new Batch2D(picture.resources, this.atlas.pixels, this.atlas.width, this.atlas.height, this.most, false)); this.versions.push(this.atlas.version); } if (this.versions[this.used] !== this.atlas.version) { const rows = this.atlas.changed(this.versions[this.used]); this.batches[this.used].update(this.atlas.pixels, rows[0], rows[1]-rows[0]); this.versions[this.used] = this.atlas.version; } this.pass = picture.passes; picture.batch(this.batches[this.used], quads.slice(at, Math.minI64(quads.length, at+this.most)), clear); this.used+ = 1; } } }Images packed in one atlas with the brush's glow and disc, and the batches that draw them. Add every image before the first draw, which uploads the atlas.
brush:gizlang_draw.Brushatlas:gizlang_draw.Atlasbatches:Vec<gizlang_draw.Batch2D>versions:Vec<i64>— The atlas version each batch holds: a batch behind the atlas takes its pixels again before it draws.letters:gizlang_draw.Letters— The faces the canvases write with, their glyphs packed into the atlas.used:i64pass:i64most:i64images:_gizlang_sdk_collections.Map<string, Vec<f64>>lit:_gizlang_sdk_core.Option<gizlang_draw.Lights2D>litWidth:i64litHeight:i64circle:Vec<f64>— A ring for a marker, a life or a crosshair, packed first.solid:Vec<f64>— A white square, for a solid box (Hud.box).round:Vec<f64>— A white square with round corners (ROUNDED of its side), drawn in nine slices: a card, a pill, a chip (Hud.card).fade:Vec<f64>— A white strip whose alpha falls from 1 at its left to 0 at its right: a smooth edge of a shade.
Functions
constructor
constructor(fonts: Fonts, size: i64 = 2048, most: i64 = 6000, lend: ((code: i64) => Lent | undefined) | undefined = undefined) throws stringSprites of an atlas SIZE texels square, with the glyphs of text in FONTS too, at most MOST a batch.
Effects: fail, read, write, console, io, alloc.
clock
function clock(seconds: f64): stringMinutes and seconds of SECONDS, as a clock shows them: 3:07.
Effects: fail, alloc.
Methods
Sprites.add
add(pixels: bytes, width: i64, height: i64): Vec<f64> throws stringPacks straight RGBA8 PIXELS of WIDTH by HEIGHT: where they are, for Canvas2D.sprite.
Effects: fail, read, write, alloc.
Sprites.ring
ring(size: i64 = 48): Vec<f64> throws stringA ring SIZE pixels across, for a marker or a crosshair.
Effects: fail, read, write, console, io, alloc.
Sprites.icon
icon(name: string): Vec<f64> throws stringThe icon NAME of a touch button (disc, ring, strike, jump, dodge, talk, chat, lock, swap, pause, speak, skill, item, build, photo, water, siren, scoop), drawn into the atlas once.
Effects: fail, read, write, console, io, alloc.
Sprites.load
load(game: Game, name: string): void throws stringPacks the image file NAME (PNG or JPEG) of GAME once.
Effects: fail, read, write, console, io, alloc.
Sprites.image
image(name: string): Vec<f64> throws stringWhere the loaded image NAME is in the atlas.
Effects: fail, read, alloc.
Sprites.lightsFor
lightsFor(picture: Picture): Lights2D throws stringThe light pass of PICTURE's surface: made once, and again when the surface changes size.
Effects: fail, read, write, console, io, alloc.
Sprites.canvas
canvas(picture: Picture, width: f64, height: f64): Canvas2D throws stringA canvas over PICTURE of WIDTH by HEIGHT logical units, fitted to the surface.
Effects: fail, read, write, alloc.
Sprites.draw
draw(picture: Picture, canvas: Canvas2D, clear: Vec<f64> = [0, 0, 0, 1]): void throws stringCANVAS drawn into PICTURE: over its passes, or on CLEAR as its first. A picture may take several canvases, such as a world that shakes and a display that does not.
Effects: fail, read, write, console, io, alloc.
Constants
ROUND_SIDE
const ROUND_SIDE: i64 = 64The side of the round-cornered square (Sprites.round) in texels, and its corners' radius: a nine-slice keeps them.
ROUND_CORNER
const ROUND_CORNER: f64 = 24The radius of the round-cornered square's corners, in texels.