Jazz2::Tiles::LayerLayout class

Tiles of a single TileMapLayer, in blocks and in one of two forms.

One allocation per block of BlockTiles rather than one for the whole layer, so a large layer never needs one contiguous run of a heap that levels and menus have fragmented.

The sprite layer keeps the full LayerTile (12 bytes: the destructible, suspend and parameter fields the collision code writes). Every other layer is only ever drawn — all it needs is the tile, its flips and its alpha — and where CompactDrawOnlyLayers says so it stores four bytes a tile instead: the 1023x80 background of a Christmas level is 327 KB that way instead of 982 KB, on a console with 5.8 MB of heap for everything.

A compact layer is still read as a LayerTile — operator[]() decodes into one of a few scratch tiles, so a returned reference stays valid for the next few lookups, which is longer than any reader here holds one — and it is written through Set(), which is the only way to change a compact tile.

Public static variables

static std::size_t BlockShift constexpr
Base-2 logarithm of BlockTiles.
static bool CompactDrawOnlyLayers constexpr
Whether the layers that are only drawn take the four-byte form.
static std::size_t BlockTiles constexpr
Tiles per allocated block.

Public functions

auto Allocate(std::size_t count, bool compact) -> bool
Allocates count tiles, in the compact form if compact, returning false if the memory is not there.
auto size() const -> std::size_t
Returns the number of tiles.
auto empty() const -> bool
Returns true if no tiles are allocated.
auto IsCompact() const -> bool
Returns true if the tiles are stored in the four-byte form.
auto operator[](std::size_t i) const -> LayerTile&
Returns the tile at i.
void Set(std::size_t i, const LayerTile& tile)
Replaces the tile at i, in either form.

Function documentation

LayerTile& Jazz2::Tiles::LayerLayout::operator[](std::size_t i) const

Returns the tile at i.

Like the array it replaces, a const layout still hands out mutable tiles — but writing through the returned reference reaches the layer only in the full form, so every writer goes through Set() instead.

Variable documentation

static bool Jazz2::Tiles::LayerLayout::CompactDrawOnlyLayers constexpr

Whether the layers that are only drawn take the four-byte form.

Enabled only where RAM is the binding constraint, because the compact form trades the lifetime of a returned reference for the memory (see the class description); the platforms with memory to spare keep the plain array semantics.