# drawing

Part of the [scripting reference](index.md), written by the engine so it matches your `frost`. Start with [your first game](../start/first-game.md) if you are new.

Use when drawing 2D sprites, rectangles or tilemaps, moving a camera, changing colour, layers, rotation or scale.

| In a script | Does |
| --- | --- |
| `frame.size()` | The frame's width and height: `[window] size`, or the screen's shape for a game that fills (`fit = "fill"`), as its recording says |
| `frame.safe()` | Left, right, top and bottom: how far in from the frame's edges its safe part begins, in its units, for a frame that runs under a phone's notch and home bar (`[window] safe = "under"`). All 0 on a desktop and in a run given none (`frost run --safe`); input, as the frame is, taken when the game starts |
| `frame.background(colour)` | The colour behind everything from now on, `0xRRGGBB`: kept in the world, so a snapshot keeps it |
| `placed(entity)` | Where an entity is in the world: its own `Transform` composed with its parents' when it is under a thing with a `Transform` (its own fields are local to that one, and `get` reads them). Three numbers in 2D, x, y and its angle; seven in 3D, x, y, z and its turn. What a camera that follows a thing under a parent reads. In 2D too |

## What is drawn

An entity is drawn when it has a `Transform` and one or more of `Rect`, `Sprite`, `Text` and
`Tilemap`. These are the components the engine declares; a game declares its own in the
project file's `[components]` table, and scripts read and write both kinds the same way.

| Component | Field | Type | Default | Meaning |
| --- | --- | --- | --- | --- |
| `Transform` | `x` | number | 0 | The centre of what is drawn, in world units from the left edge |
| `Transform` | `y` | number | 0 | The centre of what is drawn, in world units from the bottom edge |
| `Rect` | `w` | number | 1 | Width in world units |
| `Rect` | `h` | number | 1 | Height in world units |
| `Rect` | `color` | whole number | `0xffffff` | The colour as `0xRRGGBB`: red, green and blue, two hex digits each. Higher bits are ignored |
| `Rect` | `layer` | whole number | 0 | Drawing order: a higher layer is drawn over a lower one |
| `Sprite` | `image` | text | `""` | The picture: a PNG or JPEG file's path, relative to the project file, or an atlas's (a `.atlas.toml` file). Empty draws nothing |
| `Sprite` | `frame` | text | `""` | The frame to show of an atlas `image` (see Atlases); `src_*` then cut a part of it, stopping at its edge. Unknown: `E0531` |
| `Sprite` | `w` | number | 0 | Width in world units. 0 is the width of the part shown, one unit to an image pixel |
| `Sprite` | `h` | number | 0 | Height in world units. 0 is the height of the part shown |
| `Sprite` | `src_x` | whole number | 0 | The left edge of the part shown, in image pixels from the image's left |
| `Sprite` | `src_y` | whole number | 0 | The top edge of the part shown, in image pixels from the image's top |
| `Sprite` | `src_w` | whole number | 0 | The width of the part shown, in image pixels. 0 is everything to the right of `src_x` |
| `Sprite` | `src_h` | whole number | 0 | The height of the part shown, in image pixels. 0 is everything below `src_y` |
| `Sprite` | `tint` | whole number | `0xffffff` | A colour as `0xRRGGBB` that every pixel is multiplied by. White leaves the picture as it is |
| `Sprite` | `flip_x` | true or false | false | Mirror the picture left to right |
| `Sprite` | `flip_y` | true or false | false | Mirror the picture top to bottom |
| `Sprite` | `layer` | whole number | 0 | Drawing order, shared with `Rect` and `Text` |
| `Tilemap` | `image` | text | `""` | The tileset: a PNG file's path. It is cut into cells, counted along its rows from the top-left |
| `Tilemap` | `tile` | whole number | 16 | The width of a cell in image pixels, and of a tile in world units |
| `Tilemap` | `tile_h` | whole number | 0 | The height of both. 0: the same as `tile`, a square |
| `Tilemap` | `legend` | text | `""` | Which character draws which cell: the first character the first cell, and so on. A character that is not here draws nothing |
| `Tilemap` | `rows` | text | `""` | The map: one line of characters a row of tiles, the top row first |
| `Tilemap` | `layer` | whole number | 0 | Drawing order, shared with the others |
| `Camera` | `zoom` | number | 1 | How many frame units one world unit covers. Not above 0 is 1 |
| `Pass` | `of` | text | `""` | The pass's name in `[render]`: `bright`, `add`, `panini`, `aces`: these read numbers (`frost check` warns of another, W0457) |
| `Pass` | `threshold` | number | 0.6 | Its threshold, as the pass's own |
| `Pass` | `strength` | number | 1 | Bloom strength, Panini distance or ACES exposure |
| `Screen` | | | | Keeps the entity in place on the frame: the camera does not move it |
| `Opacity` | `alpha` | number | 1 | How much of what the entity draws covers what is behind: 1 all of it, 0.5 half. Not above 0 hides the entity: nothing is drawn for it. Above 1 is 1 |
| `Tint` | `color` | whole number | `0xffffff` | The colour multiplied by; white changes nothing |
| `Rotation` | `angle` | number | 0 | How far the entity's rectangle and sprite are turned about `Transform`, in degrees, anticlockwise |
| `Pivot` | `x` | number | 0 | The point of the entity's rectangle and sprite that stands on `Transform`, in units right of their centre |
| `Pivot` | `y` | number | 0 | The same point, in units above their centre |
| `Blend` | `add` | number | 1 | How much the entity is added to what is behind it instead of covering it: 1 all light, 0 the usual covering. Outside 0 to 1 is the nearer end |
| `Slice` | `left` | whole number | 0 | How many pixels at the left of the sprite's picture are a border that keeps its size |
| `Slice` | `right` | whole number | 0 | The same at the right |
| `Slice` | `top` | whole number | 0 | The same at the top |
| `Slice` | `bottom` | whole number | 0 | The same at the bottom |
| `YSort` | `foot` | number | 0 | The entity sorts by height within its layer: this is where its feet are, in units above `Transform` (below is negative) |
| `Scale` | `x` | number | 1 | How many times wider everything the entity draws is. Below 0 mirrors it |
| `Scale` | `y` | number | 1 | How many times taller |
| `Shape` | `kind` | text | `"ring"` | `"ring"`: a band inside the ellipse the rectangle holds; `"disc"`: all of it; `"line"`: a line along the rectangle's width, through its middle |
| `Shape` | `w` | number | 1 | The rectangle's width in world units: a circle's diameter |
| `Shape` | `h` | number | 1 | Its height. Unequal to `w`, a ring is an ellipse |
| `Shape` | `width` | number | 1 | How thick a ring's band or a line is, in world units |
| `Shape` | `from` | number | 0 | Where a ring or a disc starts, in degrees anticlockwise from the right: with `to` below a whole turn on, a ring is an arc and a disc a wedge |
| `Shape` | `to` | number | 360 | Where it ends |
| `Shape` | `dash` | number | 0 | Dashes this long with gaps as long, round a ring or along a line, in world units. 0: unbroken |
| `Shape` | `color` | whole number | `0xffffff` | The colour as `0xRRGGBB` |
| `Shape` | `layer` | whole number | 0 | Drawing order, shared with the others |
| `Repeat` | | | | Repeats the sprite's picture where its part (`src_x` to `src_x + src_w`, and down) runs past the picture's edge, instead of stretching the edge: a scrolling belt, a tiled floor. Move `src_x` to scroll it. The whole picture repeats, not a frame of it |

- **Axes.** A game sees its
  whole frame, with the origin at the bottom-left corner, x growing to the right and y
  growing up, and one world unit to one pixel of the frame (`[window] size`, 960 by
  540 when left out).

- **Anchor.** `Transform` is the centre of a rectangle and of a sprite: each covers
  `x - w/2` to `x + w/2` and `y - h/2` to `y + h/2`. For text it is the middle of the
  block's height, and `align` says where along a line it is. There is no scale. The
  engine's own font is drawn pixel for pixel, never smoothed, so each letter is put on
  whole pixels of the picture it is drawn into: in a picture smaller or larger than the
  frame (a window of another size, `[ui] fit`) a text keeps every stroke down to one
  picture pixel to a font pixel. Below that (a text of size 1 at
  half size) a picture cannot hold its strokes: give the text a larger `size`, or a `Font`.

- **Order.** Lower layers first; layers may be negative. Within one layer the order
  is by entity slot, which a game does not choose: give things that overlap different
  layers. An entity with several of the three is drawn rectangle, sprite, then text.

- **Colour.** sRGB bytes, written to the picture unchanged: `0xf2364b` reads back from
  a captured frame as exactly (0xf2, 0x36, 0x4b). A picture's own alpha is respected.
  A colour has no alpha of its own: see-through is the entity's `Opacity`.

- **Rotation.** An entity with `Rotation` draws its rectangle and its sprite turned by
  `angle` about their centre: 90 stands a bar on end, a negative angle turns clockwise.
  Text and tilemaps are not turned. To point one thing at another:
  `set(e, "Rotation", "angle", math.deg(math.atan2(dy, dx)))` for a picture drawn facing
  right. A `Collider` on the entity is turned with it (see Physics), and physics gives
  a body that turns a `Rotation` of its own; a script that tests overlaps itself decides
  what a turned thing touches. Like `Opacity`, a script cannot add
  it later: give `Rotation = {}` in the scene file or when spawning. The engine's tests draw
  the same picture, pixel for pixel, on Metal and in both web builds; a turned edge falls
  between pixels, so that is measured there and not promised for every graphics adapter.

- **Shadow.** An entity with `Shadow` draws its text twice: first a copy in the shadow's
  colour, moved by `x` and `y`, then the text over it, so a banner stands off whatever is
  behind it without a second entity kept in step. It does nothing for a rectangle, a
  sprite or a tilemap. Give it in the scene file or when spawning: `Shadow = {}` is black,
  2 right and 2 down.

- **Pivot.** A rectangle or a sprite stands with its centre on `Transform`. With `Pivot`
  another point of it does, and `Rotation` turns it about that point: `Pivot = { x = -10 }`
  on a bar 20 long holds it by its left end, like an arm at the shoulder or a door at
  its hinge. The point is counted on the picture as drawn before it is turned.
  It is in units, not a share of the size, so it does not follow a size that changes:
  for a bar that empties towards one end, hold it by that end with `Pivot` once and
  shrink it with `Scale.x` (0 to 1), rather than writing `Rect.w` and `Pivot.x` together.

- **Blend.** An entity with `Blend` is added to what is behind it: its colours make the
  picture lighter and hide nothing, which is how a glow, a flame, a spark or a beam of
  light is drawn. Black adds nothing, so a picture of light on black needs no alpha.
  `add` between 0 and 1 mixes adding with covering; `Opacity` still scales how much is
  drawn. Draw order is unchanged: layers, then entities.
  A tint can only darken a picture. To flash a sprite bright when it is hit, spawn a
  second entity on it with the same `Sprite` and `Blend = {}` for a few steps: the
  picture is added to itself, towards white. It is not a flat white shape.

- **Scale.** An entity with `Scale` draws everything it draws that many times larger,
  across (`x`) and up (`y`), about the point that stands on `Transform`: a rectangle or
  a sprite about its centre or its `Pivot`, a text from where it starts, a tilemap from
  its corner. Below 0 mirrors. It is how a thing pops, squashes or breathes without its
  `w` and `h` being touched: `set(coin, "Scale", "x", 1.3)`.

- **Slice.** A sprite with `Slice` is cut in nine: the four corners are drawn one unit
  a pixel, the edges stretch along their length and the middle fills the rest. One small
  picture of a frame then makes a panel, a button or a speech bubble of any `w` and
  `h`. `src_*` still picks the part of a sheet, and flips and `Rotation` work as on any
  sprite.

- **YSort.** In a game seen from above, who is in front depends on who stands lower.
  Entities with `YSort` in one layer are drawn from the highest to the lowest, by
  `Transform.y + foot`, after the layer's entities that have none (the floor). Give a
  tall sprite its feet: `YSort = { foot = -12 }`.

- **Opacity.** Everything drawn (rectangle, sprite, text, tilemap, 3D `Mesh`) uses
  its picture's alpha times `alpha` times every ancestor's `Opacity` (`Parent`): one
  root write fades a figure's parts. Below 1, 3D things join the see-through ones
  after opaque things, farthest first, revealing what's behind; shadows stay whole.
  Hide with `set(e, "Opacity", "alpha", 0)`;
  restore to 1. Hidden entities cost nothing to draw but still move, collide and
  can be found. Without `Opacity`, an entity draws in full. Scripts cannot add it
  later: give anything that will fade or hide `Opacity = {}` in its scene or when spawning.

- **Numbers.** A number field is a 32-bit float. A scene file or a script may give it
  any number and gets the nearest 32-bit float back (0.1 reads back as
  0.10000000149011612). A whole-number field holds 32 bits with a sign and refuses a
  fraction.

- **Camera.** With no `Camera` the world and the frame are the same: the origin is
  the frame's bottom-left corner. An entity 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 that point. With several, the one in the lowest entity slot
  is used. The camera is world state like any other: a script moves it with `set`.
  The renderer does not round: for sharp pixel art keep the camera, and what moves,
  on whole units.

- **Screen.** A score, a title or a backdrop that must stay put has `Screen`: its
  `Transform` is then in frame units from the frame's bottom-left corner, whatever
  the camera does. Layers order world and screen entities together.

- An entity without a `Transform` is not drawn. A rectangle or sprite with a width or
  height of zero draws nothing.

In a scene file:

```toml
[player]
Transform = { x = 120, y = 118 }
Rect = { w = 36, h = 36, color = 0xf2364b, layer = 2 }

[coin]
Transform = { x = 300, y = 200 }
Sprite = { image = "art/coins.png", src_w = 16, src_h = 16, w = 32, h = 32, layer = 3 }

[score]
Transform = { x = 480, y = 500 }
Text = { text = "0", size = 4, color = 0xd9b98a, layer = 9 }
```

### Sprites

A sprite's picture is a PNG or a JPEG file (`.jpg`, `.jpeg`) named by its path, relative
to the project file. Every sprite of one picture shares it. A JPEG is baseline or
progressive, 8 bits a channel, told by its first bytes, drawn as stored (EXIF orientation
is not applied); one that cannot be read is `E0534`. `frost export` ships it as the PNG of
its pixels under its own name, so a JPEG saves no download. Animation is a script changing `src_x` and `src_y` to
another part of one sheet. Pictures are sampled the way the project file's
`[window] filter` says, the same for all of them: `"nearest"` for pixel art, which is the
default, or `"linear"`.

### Atlases

`NAME.atlas.toml` puts pictures on one page of at most 4096 by 4096, each a frame with a
name: `pictures = ["hero/*.png"]` packs the PNG files its patterns match (`*` is any run
of a file's name in one folder; `hero/run_0.png` is the frame `run_0`), or `sheet =
"coins.png"`, `grid = [8, 8]`, `names = [...]` cuts one sheet. Errors: `E0530` frames
that do not fit, `E0532` a pattern that matches nothing, `E0533` a `frame` on a sprite
with a `Repeat`.

### Tilemaps

A tilemap draws a level from text. Its `Transform` is the bottom-left corner of the
grid (not its centre): the last line of `rows` is the bottom row, and each character
is one square of `tile` units, or `tile` wide and `tile_h` high when `tile_h` is set
(bricks: `tile = 16`, `tile_h = 8`).

```toml
[ground]
Transform = { x = 0, y = 0 }

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

Here `#` draws the first cell of `tiles.png`, `=` the second and `^` the third. `.`
and `C` are not in the legend, so they draw nothing: a game's script can read `rows`
(it is a field like any other) and put a coin where it finds a `C`, test what
character is under a foot, or change a character with `set` to break a block. Lines
may differ in length. The legend and the rows are ASCII. Only the tiles the camera
sees are sent to the device, and a whole map is one draw call.

### Cost

The whole world goes to the device as one batch. Rectangles and text cost nothing
extra in draw calls beyond a change of picture: quads that follow each other in
drawing order and share a picture, or have none, are one draw call. The frames of one
atlas are one picture; other pictures interleaved on one layer cost a call each.
