Coldwave Read the docs

2D#

Draw a 2D game with rectangles, sprites, text, tilemaps and shapes, ordered by layer and seen through a camera. This page is the tour, with one small game you can copy; every field is in the drawing reference.

The sprites-2d sample at step 150
The sprites-2d sample at step 150

That frame is the sprites-2d sample: a tilemap level wider than the frame, a sprite that walks along it, a camera that follows, a turned bar, a pulsing ring and a label that stays put. Its pictures are drawn by its make.py.

TOML
# example: sprites-2d/game.toml
# The 2D guide's game: a tilemap level wider than the frame, a sprite that walks along
# it, a camera that follows on whole units, a turned and a scaled thing, a ring, and a
# line of text that stays on the frame. make.py draws tiles.png and hero.png.
#
#   frost dev game.toml
#   frost run --headless --steps 150 --shot shot.png game.toml
scene = "level.toml"
scripts = ["walk.luau"]
seed = 1
shots = [{ name = "walk", step = 150 }]
replay = { steps = 150, checksum = "28b5e912cdbd2af1", engine = "0.0.1 r280" }
# The run's checksum on the way there, written by `frost test --write`: a test that
# fails says between which two of these steps the game first went another way.
checkpoints = []

[window]
size = [320, 180]
background = 0x1b2030
filter = "nearest"

[input]
actions = ["move_x"]
recording = [{ steps = 20 }, { steps = 130, move_x = 1 }]

[input.keys]
move_x = [{ negative = "A", positive = "D" }, { negative = "Left", positive = "Right" }]

The frame and the world#

A game sees its whole frame ([window] size, 960 by 540 when left out), with the origin at the bottom-left corner, x to the right and y up, and one world unit to one pixel. A Transform is the centre of what is drawn: a rectangle 12 wide at x = 40 covers 34 to

  1. A frame keeps its shape in any window, as large as fits, with the background beside it; [window] fit = "fill" takes the screen's shape instead, and frame.size() says the frame the game runs in.

What is drawn#

A thing is drawn when it has a Transform and one or more of these:

ComponentDraws
RectA rectangle in one colour, 0xRRGGBB
SpriteA PNG picture, or a part of one (src_x, src_y, src_w, src_h), at a size (w, h: 0 is one unit an image pixel), tinted and flipped
TextThe built-in pixel font, or a font of the game's own (see interface)
TilemapA level from text: each character one tile of a tileset picture
ShapeA ring, a disc or a line: an arc, a wedge, dashes

The level and the things on it:

TOML
# example: sprites-2d/level.toml
# The camera's Transform is the world point at the middle of the frame.
[camera]
Transform = { x = 160, y = 90 }
Camera = { zoom = 1 }

# A tilemap's Transform is its bottom-left corner; the last line of rows is the bottom.
[ground]
Transform = { x = 0, y = 0 }

[ground.Tilemap]
image = "tiles.png"
tile = 16
legend = "#=^"
rows = """
...............................
...............................
...............................
...............................
...............................
........===.........===........
...............................
............^^..........^......
###############################
###############################
"""

# One frame of hero.png, drawn twice its size: one image pixel is two units.
[hero]
Transform = { x = 40, y = 48 }
Sprite = { image = "hero.png", src_w = 16, src_h = 16, w = 32, h = 32, layer = 2 }

# Turned about its centre (Rotation), and drawn larger without touching w and h (Scale).
[windmill]
Transform = { x = 260, y = 120 }
Rect = { w = 40, h = 6, color = 0xeceae6, layer = 1 }
Rotation = { angle = 0 }

[coin]
Transform = { x = 290, y = 60 }
Shape = { kind = "ring", w = 14, h = 14, width = 3, color = 0xf5c542, layer = 1 }
Scale = { x = 1, y = 1 }

# Screen: in frame units, wherever the camera is.
[label]
Transform = { x = 8, y = 168 }
Screen = {}
Text = { text = "WALK RIGHT", size = 1, color = 0xeceae6, align = 0, layer = 9 }

A tilemap's Transform is its bottom-left corner, not its centre, and the last line of rows is the bottom row. A character not in legend draws nothing, and rows is a field like any other: a script can read it to find where a C stands, or set it to break a block. Only the tiles the camera sees are sent to the device, and a whole map is one draw call. Many pictures on one page, each a frame by name, are an atlas.

Order, turn and size#

  • Layers. Lower layers are drawn first; within a layer the order is the engine's, so give things that overlap different layers. YSort sorts a layer by height, for a game seen from above.
  • Rotation. Rotation = { angle = 30 } turns a rectangle or a sprite about its centre, anticlockwise; Pivot moves the point it turns about (an arm at its shoulder).
  • Scale. Scale = { x = 1.3, y = 1.3 } draws a thing larger without touching its w and h: how a coin pops or a jump squashes. Below 0 mirrors.
  • Opacity, Tint, Blend. Opacity fades a thing and what is under it; Tint multiplies its colour; Blend adds it to what is behind, for glows and sparks.
  • Slice cuts a sprite in nine, so one small picture of a frame makes a panel of any size; Repeat tiles a picture across a sprite larger than it.

Rotation, Opacity and Scale are components, not fields of Sprite: a script cannot add one later, so give a thing that will turn or fade Rotation = {} or Opacity = {} in its scene.

The camera#

With no Camera the world and the frame are the same. A thing with a Camera and a Transform is the camera: its Transform is the world point at the middle of the frame, and zoom scales the world about it. It is world state like any other, so a script moves it with set, and a thing with Screen stays where the frame puts it whatever the camera does: a score, a title. The sample's script:

Luau
-- example: sprites-2d/walk.luau
-- Walks the hero with move_x, flips it to face the way it goes, keeps the camera on it
-- (on whole units, so pixel art stays sharp), turns the windmill and makes the coin pulse.
local SPEED = 60

state.t = state.t or 0

function step(dt)
    state.t += dt
    local hero = find("hero")
    local move = input.get(0, "move_x")
    set(hero, "Transform", "x", get(hero, "Transform", "x") + move * SPEED * dt)
    if move ~= 0 then
        set(hero, "Sprite", "flip_x", move < 0)
    end

    local camera = find("camera")
    set(camera, "Transform", "x", math.max(160, math.floor(get(hero, "Transform", "x") + 0.5)))

    local windmill = find("windmill")
    set(windmill, "Rotation", "angle", (get(windmill, "Rotation", "angle") + 90 * dt) % 360)

    local pulse = 1 + 0.25 * math.sin(state.t * 4)
    local coin = find("coin")
    set(coin, "Scale", "x", pulse)
    set(coin, "Scale", "y", pulse)
end

Sharp pixels, the same everywhere#

  • [window] filter = "nearest" (the default) keeps pixel art sharp; "linear" blends.
  • The renderer does not round. Keep the camera, and what moves, on whole units, as the sample does with math.floor.
  • For the same picture on every graphics backend, keep every drawn edge on a whole frame pixel: give sprites even sizes, or place an odd-sized one half a unit off.
  • A turned edge falls between pixels, so it is the same picture on Metal and in both web builds as measured, not promised for every adapter.

What it costs#

The whole world goes to the device as one batch: quads that follow each other in drawing order and share a picture, or have none, are one draw call. Rectangles, text and shapes cost nothing extra; another picture on the same layer costs a call. frost bench --draw counts the draw calls (profiling).