LayerLayout class
#include <Jazz2/Tiles/TileMap.h>
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.
A sparse layout allocates a block only once a tile that isn't empty is written into it, every other block is one block of empty tiles all sparse layouts share — a level of the original game has an empty layer in five levels of six, which the converter keeps (JJ2+ has the layers without tiles too, scripts refer to them), up to 330000 tiles of them in a level. Only a layer whose every writer goes through Set() can be sparse, the shared block must never be written through operator[]().
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.
Constructors, destructors, conversion operators
- LayerLayout() defaulted
- ~LayerLayout()
- LayerLayout(const LayerLayout&) deleted
- LayerLayout(LayerLayout&& other) noexcept
Public functions
- auto operator=(const LayerLayout&) -> LayerLayout& deleted
- auto operator=(LayerLayout&& other) -> LayerLayout& noexcept
-
auto Allocate(std::
size_t count, bool compact, bool sparse = false) -> bool - Allocates
countempty tiles, in the compact form ifcompact, returningfalseif the memory is not there. -
auto size() const -> std::
size_t - Returns the number of tiles.
- auto empty() const -> bool
- Returns
trueif no tiles are allocated. - auto IsCompact() const -> bool
- Returns
trueif the tiles are stored in the four-byte form. - auto HasAnyTile() const -> bool
- Returns
trueif any tile has an ID other than 0, which is quick for the blocks of a sparse layout never written. -
auto GetVersion() const -> std::
uint32_t - Returns a stamp that changes with every write through Set() or Allocate().
-
auto operator[](std::
size_t i) const -> LayerTile& - Returns the tile at
i. -
auto TileIDAt(std::
size_t i) const -> std:: uint16_t - Returns the ID of the tile at
i. -
void Set(std::
size_t i, const LayerTile& tile) - Replaces the tile at
i, in either form.
Function documentation
bool Jazz2:: Tiles:: LayerLayout:: Allocate(std:: size_t count,
bool compact,
bool sparse = false)
Allocates count empty tiles, in the compact form if compact, returning false if the memory is not there.
An empty tile is the one a level stores as tile 0 without any flags (see TileMap::sparse the blocks are allocated only as tiles that aren't empty are written into them (see the class description).
std:: uint32_t Jazz2:: Tiles:: LayerLayout:: GetVersion() const
Returns a stamp that changes with every write through Set() or Allocate().
Those are the only writers of the compact form, so an unchanged stamp proves that none of its tiles changed. The full form can still be written through the references operator[]() hands out, which the stamp cannot see.
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.
std:: uint16_t Jazz2:: Tiles:: LayerLayout:: TileIDAt(std:: size_t i) const
Returns the ID of the tile at i.
The same value as operator[](i).TileID, without decoding the rest of a compact tile into a scratch one first - for a reader that compares only the IDs of a whole window of tiles every frame.
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.