TileLod class
#include <Jazz2/Editor/Runtime/TileLod.h>
Draws the tile layers from far away as images instead of tiles.
From far away, there are far too many tiles in view to draw each one. Square chunks of tiles of a layer are drawn into images instead, with at least as many pixels for a tile as a tile takes on the screen, so they're never stretched — 1, 2, 3, 4, 6, 8, 12 or 16, each size at most half again as large as the one before, so they're never much larger either (see GetLargerTileSize()) — each the average of its part of the tile, and the images are drawn in place of the tiles (see Tiles::
The averages are kept for every tile in all sizes, so an image is put together by copying them. An image is made again when its tiles change or the view gets closer or farther, as many as fit in a few milliseconds of a frame, those missing first and at a lower resolution, then those of another resolution. A chunk not made yet is shown by the larger one around it, or by the smaller ones in it, made before, or by the image of the layer made ahead, and only where none of them is there yet are the tiles of the chunk drawn, so nothing is missing for a moment. The least recently drawn images are dropped once they take more memory than allowed, never those drawn in the frame, and the resolution is lowered when the images in view wouldn't fit; all but those made ahead are dropped a while after the view got closer again. A layer that repeats shows the same images again where its pattern repeats: its chunks start where its pattern does, and the last one of the pattern is shown only up to where it ends, or a chunk has the pattern a few times when it's shorter.
Base classes
- class Jazz2::Tiles::ITileMapLayerOverride
- Draws images in place of the tiles of layers, as the level editor does for a view from far away.
Public static variables
-
static std::
int32_t MaxChunkPixels constexpr - Most pixels of a chunk image on a side.
-
static std::
int32_t MinChunkTiles constexpr - Fewest tiles on a side of a chunk, with the most pixels for a tile.
-
static std::
int32_t MaxChunkTiles constexpr - Most tiles on a side of a chunk, with few pixels for a tile.
-
static std::
int32_t TilePixels constexpr - static float MaxZoom constexpr
- static float MaxScreenTilePixels constexpr
- static float Hysteresis constexpr
- How much closer the view has to get again before the tiles are drawn again, so it doesn't flicker there.
- static float WarmUpMilliseconds constexpr
- Time a frame the level is edited in takes to prepare the colors of tiles, see WarmUp().
- static bool UseBaseChunks constexpr
-
static std::
int32_t MaxBaseCoverTilePixels constexpr - Most pixels of a tile in the images made now for the images of the layers made ahead to stand in.
Public static functions
-
static auto GetLargerTileSize(std::
int32_t tilePixels) -> std:: int32_t constexpr - Returns the next size of a tile in the images, 1, 2, 3, 4, 6, 8, 12, 16 and so on.
-
static auto GetSmallerTileSize(std::
int32_t tilePixels) -> std:: int32_t constexpr - Returns the size of a tile in the images before
tilePixels, see GetLargerTileSize(). -
static auto GetChunkTiles(std::
int32_t tilePixels) -> std:: int32_t constexpr - Returns the tiles on a side of a chunk with so many pixels for a tile, a power of two.
Constructors, destructors, conversion operators
- TileLod(const LevelDocument& document, TileSetDataCache& tileSets)
- Creates a new instance for a level, the pixels of its tiles are read from
tileSets. - ~TileLod() override
Public functions
- auto UpdateActive(float zoom, float screenTilePixels) -> bool
- Returns whether the images are drawn, at a zoom and with a tile so many pixels on the screen.
- void Invalidate(bool keepBlocks = false)
- Drops all images.
- void OnDocumentChanged(const DocumentChanges& changes)
- Makes the images that show changed tiles again.
-
void InvalidateTileIds(const LevelDocument& document,
ArrayView<const std::
uint16_t> sortedTileIds) - Makes the images that show any of the tiles again, after they changed, the tile IDs have to be sorted.
- void BeginFrame(float scale)
- Called once a frame before the level is drawn, with the scale of the view.
- void Evict()
- Drops the images while they aren't drawn, as while the level is played.
- void WarmUp(float milliseconds)
- Prepares the colors of the tiles the level uses for some time, called in frames the level is edited in.
-
auto GetMemoryUsage() const -> std::
size_t - Returns the memory the images take in bytes.
-
auto GetTilePixels() const -> std::
int32_t - Returns the pixels of a tile on a side in the images made now.
-
auto GetWantedTilePixels() const -> std::
int32_t - Returns the pixels of a tile on a side the scale of the view asks for, more than GetTilePixels() if memory is short.
-
auto GetDrawnChunks(std::
size_t* bytes = nullptr) const -> std:: int32_t - Returns the number of images drawn in the last frame, each once, and the memory they take in bytes.
-
auto OverrideLayer(std::
int32_t layerIndex, Vector2f origin, const Rectf& cullingRect, SmallVectorImpl<Tiles:: TileMapLayerImage>& images, SmallVectorImpl<Recti>& tileAreas) -> bool override - Called for each visible tile layer that is drawn, with where the tile (0, 0) of it is in the level.
Function documentation
static std:: int32_t Jazz2:: Editor:: TileLod:: GetLargerTileSize(std:: int32_t tilePixels) constexpr
Returns the next size of a tile in the images, 1, 2, 3, 4, 6, 8, 12, 16 and so on.
A power of two and half again as much as it, so the sizes between them are made from those twice as large too.
bool Jazz2:: Editor:: TileLod:: UpdateActive(float zoom,
float screenTilePixels)
Returns whether the images are drawn, at a zoom and with a tile so many pixels on the screen.
The cost of drawing the tiles grows with the number of them in view, which depends on the zoom relative to how large the game shows the level, so the images take over below MaxZoom. Not while a tile is larger on the screen than MaxScreenTilePixels though, the images would be blurred. Where memory is short, that's all that decides it. Once they're drawn, they're drawn until the view gets closer than that by Hysteresis.
void Jazz2:: Editor:: TileLod:: Invalidate(bool keepBlocks = false)
Drops all images.
With keepBlocks, the colors of the tiles are kept, which depend only on the pixels of the tiles and the palette, so a level loaded again with the same tiles doesn't have them computed again.
void Jazz2:: Editor:: TileLod:: Evict()
Drops the images while they aren't drawn, as while the level is played.
Those of the layers made ahead and the colors of the tiles are kept, they show the level from far away at once when it's seen again, until the images are made again. Done by itself a while after the view got closer.
void Jazz2:: Editor:: TileLod:: WarmUp(float milliseconds)
Prepares the colors of the tiles the level uses for some time, called in frames the level is edited in.
Computing the colors of a tile is what takes the time when a part of the level is first seen from far away, so they're computed ahead, a few at a time, after the level is opened and whenever the colors are dropped. All tiles of all layers are counted first, as they're flipped, then the colors of those used the most are computed first. Then images of whole layers with a pixel for a tile are made and kept, see UseBaseChunks, they show places not made yet from far away, as the view gets farther fast, so the layer isn't drawn as tiles then. Those that change are made again later, once the level isn't changed for a moment. Only while the view is far enough for the images to be drawn soon, or nothing changed for a while, and not for layers hidden in the editor.
bool Jazz2:: Editor:: TileLod:: OverrideLayer(std:: int32_t layerIndex,
Vector2f origin,
const Rectf& cullingRect,
SmallVectorImpl<Tiles:: TileMapLayerImage>& images,
SmallVectorImpl<Recti>& tileAreas) override
Called for each visible tile layer that is drawn, with where the tile (0, 0) of it is in the level.
Returns true to draw images at the depth of the layer instead of its tiles. The tiles of tileAreas are drawn as usual with them, where there's no image yet — in tiles of the layer from its tile (0, 0), past its size where it repeats, not overlapping any image.
Variable documentation
static std:: int32_t Jazz2:: Editor:: TileLod:: MaxBaseCoverTilePixels constexpr
Most pixels of a tile in the images made now for the images of the layers made ahead to stand in.
They show places of chunks not made yet. They're blurred, but drawing the tiles instead takes much longer from far away. With more pixels for a tile, they show only the chunks that can't be made in the frame anymore.