tiles API

← tiles · version 0.7.2

30 public items, in source order. Generated from the registry index, which re-derives them from the package source at publish time — so a signature here is the one that version actually ships.

pub fn rgb(r: integer, g: integer, b: integer) -> integer

An RGB565 colour from 8-bit channels: 5 bits of red, 6 of green, 5 of blue.

pub fn sheet(rgba: const vector<u8>, width: integer, height: integer, tile_w: integer, tile_h: integer, spacing: integer = 0, margin: integer = 0) -> integer?

A sheet from width * height RGBA bytes, cut into tile_w * tile_h tiles with margin pixels round the sheet and spacing between tiles (Tiled's names). A pixel with alpha below 128 is transparent. Tiles are numbered from 0, row by row. Null when the sizes do not fit or the sheet has more than 255 colours once in RGB565.

pub fn sheet_from_image(img: const imaging::Image, tile_w: integer, tile_h: integer, spacing: integer = 0, margin: integer = 0) -> integer?

A sheet from a decoded image (imaging::png).

pub fn sheet_from_png(path: text, tile_w: integer, tile_h: integer, spacing: integer = 0, margin: integer = 0) -> integer?

A sheet from a PNG file.

pub fn tile_count(sheet: integer) -> integer

How many tiles a sheet holds, and how many colours (at most 255).

pub fn colour_count(sheet: integer) -> integer
pub fn free_sheet(sheet: integer)
pub fn surface(width: integer, height: integer) -> integer?

An RGB565 surface of width x height pixels, cleared to black, its camera at one pixel per unit (camera changes it).

pub fn free_surface(surface: integer)
pub fn clear(surface: integer, colour: integer)

Fill a surface with one RGB565 colour.

pub fn rect(surface: integer, x: integer, y: integer, w: integer, h: integer, colour: integer)

Fill the w * h rectangle with its top-left at (x, y) with one RGB565 colour. What falls off the surface is skipped, and a rectangle with no width or height draws nothing.

pub fn frame(surface: integer, x: integer, y: integer, w: integer, h: integer, colour: integer)

The one-pixel outline of that rectangle, inside it: a cursor, a selection, the frame round a palette. Clipped like rect.

pub fn draw(surface: integer, sheet: integer, tile: integer, x: integer, y: integer, flip_h: boolean = false, flip_v: boolean = false, flip_d: boolean = false, shade: integer = 0, ghost: boolean = false)

Draw tile tile of sheet with its top-left at (x, y), in any of the eight orientations. A sprite is a tile drawn this way. Transparent pixels and whatever falls off the surface are skipped. shade darkens it: 0 is the tile as it is, and each shade up to SHADES - 1 is darker, down to about a quarter of the light, so the darkest still shows what is there. A shade costs nothing: each sheet keeps its palette at every shade. A ghost draws every other pixel only, in a checkerboard fixed to the surface — what is behind shows through: a figure seen through what stands in front of it.

pub fn display_surface(width: integer, height: integer) -> integer?

A surface for the display, width x height pixels — the screen's own size. It is always drawn through its camera, and where the machine draws faster another way it is drawn that way: in a page, by the GPU, straight onto the canvas (when the canvas is this size). What it draws is the same picture either way; pixels reads it back. Ask on_display whether there is anything left to show.

pub fn on_display(surface: integer) -> boolean

True when the surface is drawn straight onto the display, so the frame is on the screen as soon as it is drawn and nothing needs showing.

pub fn camera(surface: integer, zoom: float, x: float = 0.0, y: float = 0.0)

Point the surface's camera: zoom surface pixels per unit, with the point (x, y), in units, at the surface's top-left. Everything drawn after it is placed in units and rasterised at the surface's own pixels, so a surface the size of the display draws a game at the display's resolution: a sprite placed between pixels, turned and stretched lands where it is put, and zooming in on two figures or out over the land is a change of camera. A new surface's camera is 1 pixel per unit at (0, 0), where every call draws as it does without one. A zoom outside 1/256 .. 4096 is ignored.

pub fn sprite(surface: integer, sheet: integer, tile: integer, x: float, y: float, turn: float = 0.0, pivot_x: float = 0.0, pivot_y: float = 0.0, scale_x: float = 1.0, scale_y: float = 1.0, flip_h: boolean = false, flip_v: boolean = false, flip_d: boolean = false, shade: integer = 0, ghost: boolean = false)

Draw tile tile of sheet as a sprite: its top-left at (x, y) units, stretched by (scale_x, scale_y) and turned turn degrees clockwise, both about its point (pivot_x, pivot_y) — in the tile's own pixels, so (8.0, 16.0) is the middle of a 16 px tile's bottom edge. Every value may be fractional: under a camera the sprite is drawn at the surface's resolution, each pixel showing the tile's pixel under its centre, so pixel art stays crisp at every zoom and angle. Flips, shade and ghost are draw's; a ghost leaves out every other pixel of the tile, so it turns and stretches with it.

pub fn fill(surface: integer, x: float, y: float, w: float, h: float, colour: integer)

Fill the rectangle with its top-left at (x, y) and w x h units in one RGB565 colour, at the surface's resolution: the pixels whose centres lie inside it.

pub fn draw_turned(surface: integer, sheet: integer, tile: integer, x: integer, y: integer, degrees: integer, pivot: (integer, integer), flip_h: boolean = false, flip_v: boolean = false, flip_d: boolean = false, shade: integer = 0, ghost: boolean = false)

Draw tile tile of sheet turned degrees clockwise about its pixel pivot (x, y within the tile): placed as draw places it at (x, y), oriented by the flips first, then turned — a character's slight lean as it walks, a weapon held at an angle or swung. Under a camera it is sprite turned about the pivot pixel's centre: a true rotation at the surface's resolution. At one pixel per unit every pixel of the tile lands on exactly one pixel of the surface instead (the turn is whole quarter turns and three shears of whole rows and columns), so the pixel art keeps its pixels and colours where there are no finer pixels to turn it with. 0 degrees is draw, and 90, 180 and 270 are its quarter turns. The shade, the ghost, transparency and clipping are draw's.

pub fn draw_ground(surface: integer, sheet: integer, tile: integer, x: integer, y: integer, lift: (integer, integer, integer, integer), shade: (integer, integer, integer, integer) = (0, 0, 0, 0), flip_h: boolean = false, flip_v: boolean = false, flip_d: boolean = false)

Draw tile tile of sheet as a piece of ground that slopes and is lit gradually: its place is the plain draw's at (x, y), with its corners — top-left, top-right, bottom-left, bottom-right — lifted lift pixels (up is positive, so (4, 4, 0, 0) leans its far edge up a step) and darkened shade sixteenths of a shade (SHADE_PARTS is one whole shade darker; past SHADES - 1 whole shades it stays the darkest).

Each column of the tile stays a column of pixels, stretched or squeezed between its top and bottom edges, whose lifts are interpolated from the corners at the column's centre. So two tiles that give their shared corners the same lifts meet exactly, with no gap — a slope, a mound, a ramp onto a higher floor; and a drop between them shows as the gap a wall tile (or a darker one) fills, drawn the same way. The darkness is interpolated across the tile and shared between the two nearest shades by a fixed 4x4 dither, so light fades smoothly within a tile and the pattern stays with the ground as the view moves. Over a plain draw it costs a little per column and a table lookup per pixel; a tile with every lift and shade 0 draws exactly what draw draws. Orientation, transparency and clipping are draw's.

pub fn pixels(surface: integer, width: integer, height: integer) -> vector<i32>

The surface's pixels, row by row, each an RGB565 value.

pub fn present(surface: integer, width: integer, height: integer, scale: integer, rotate: boolean = false) -> vector<i32>

The surface scaled by the integer scale, as present hands it to a display: row by row, (width * scale) x (height * scale) RGB565 values — or, rotated a quarter turn clockwise for a portrait panel, (height * scale) x (width * scale), the surface's top-left at the top-right.

pub fn cell(tile: integer, flip_h: boolean = false, flip_v: boolean = false, flip_d: boolean = false) -> integer

A layer cell: tile tile (from 0) in one of the eight orientations. 0 is an empty cell.

pub fn from_tiled(gid: i64, firstgid: integer = 1) -> integer

A Tiled global tile id (with its flip bits: 31 horizontal, 30 vertical, 29 diagonal) as a cell of a sheet whose first id is firstgid; 0 for an empty one or a tile of another sheet (an id below firstgid).

pub fn layer(cells: const vector<integer>, cols: integer, rows: integer) -> integer?

A map layer of cols * rows cells, row by row. Null when the cell count does not match.

pub fn layer_from_tiled_csv(csv: text, cols: integer, rows: integer, firstgid: integer = 1) -> integer?

A layer from a Tiled layer's CSV data (<data encoding="csv">), its gids read by from_tiled.

pub fn free_layer(layer: integer)
pub fn set_cell(layer: integer, x: integer, y: integer, cell: integer)

Set or read one cell; a position off the map is ignored, and reads 0.

pub fn get_cell(layer: integer, x: integer, y: integer) -> integer
pub fn draw_layer(surface: integer, layer: integer, sheet: integer, cam_x: integer = 0, cam_y: integer = 0)

Draw every cell of layer that shows on surface when the layer's pixel (cam_x, cam_y) is the surface's top-left — one call per layer, scrolling to the pixel. Draw layers back to front.