If a lot of similar tiles are added to a texture atlas, multiple layers for the same tile size will be created. If a lot of random tiles are removed now, this is going to create a lot of unused space in all layers, which can only be reclaimed by actually using it for newly added tiles.
Therefore a deflate function could try and reclaim all that unused space and shrink down the layer count.
Implementation:
Go through all tiles, call layerForTile to find the smallest layer index and see if it is smaller than the current one, which means an earlier layer has space for this tile. If this is the case, remove it from the current layer and add it to that layer.
The most efficient way here, would be to start with the tiles in the last layer. Otherwise, a lot of tiles might get shifted along as new spaces keeps opening up in lower layers.
The order to process the layer itself in doesn't really matter all that much unless shrink_to_fit is called on the tiles vector. Then back to front is favorable again.
If a lot of similar tiles are added to a texture atlas, multiple layers for the same tile size will be created. If a lot of random tiles are removed now, this is going to create a lot of unused space in all layers, which can only be reclaimed by actually using it for newly added tiles.
Therefore a
deflatefunction could try and reclaim all that unused space and shrink down the layer count.Implementation:
Go through all tiles, call
layerForTileto find the smallest layer index and see if it is smaller than the current one, which means an earlier layer has space for this tile. If this is the case, remove it from the current layer and add it to that layer.The most efficient way here, would be to start with the tiles in the last layer. Otherwise, a lot of tiles might get shifted along as new spaces keeps opening up in lower layers.
The order to process the layer itself in doesn't really matter all that much unless
shrink_to_fitis called on the tiles vector. Then back to front is favorable again.