Cauldron2D.Sectors.Light (Cauldron2D v0.2.2)

Copy Markdown View Source

Light as table lookups: a ramp of light tables built from a palette, and which table a surface at some light level and distance is drawn through.

tables = Light.tables(palette)
Light.table(160, 384.0)

A light table maps a colour index to the index of that colour darkened, so lighting a picture costs one lookup a pixel whatever the light. tables/2 builds maps of them, table 0 the palette as it is and each further table darker, the last nearly black. A game may instead bring its own tables made for its palette.

A surface's light level is 0..255. start/2 turns it into the table it is drawn through close up, and table/3 moves that table brighter or darker with distance: start - floor(fade / distance), clamped to the tables, so nearer is brighter and a surface fades by its light level's own amount as it recedes.

Options

  • :maps — how many tables the ramp has. Default 32
  • :levels — how many steps the 0..255 light level is cut into. Default 16
  • :fade — how strongly distance darkens, in world units. Default 1280.0

Summary

Functions

The table a surface at light (0..255) is drawn through close up.

The table a surface at light is drawn through from distance away.

maps light tables for a 768-byte palette, darkening towards black: table n holds for each colour the palette entry nearest that colour scaled by (maps - n) / maps.

Types

option()

@type option() ::
  {:maps, pos_integer()} | {:levels, pos_integer()} | {:fade, number()}

Functions

start(light, opts \\ [])

@spec start(integer(), [option()]) :: integer()

The table a surface at light (0..255) is drawn through close up.

table(light, distance, opts \\ [])

@spec table(integer(), number(), [option()]) :: non_neg_integer()

The table a surface at light is drawn through from distance away.

tables(palette, opts \\ [])

@spec tables(binary(), [option()]) :: binary()

maps light tables for a 768-byte palette, darkening towards black: table n holds for each colour the palette entry nearest that colour scaled by (maps - n) / maps.