Currently a TextureAtlas stores all the image data forever. This is actually required, as new textures might be added later on, which might in turn require the underlying array texture to resize to fit all tiles, which then invalidates the already existing tiles.
However keeping all those textures around forever is quite expensive. Therefore a way to free all texture data should exist.
Of course, once all the textures are freed, no new textures can be added anymore, as this might require a resize again. To prevent this, upon freeing the textures the TextureAtlas should be in a frozen state, preventing any further modification.
It is very rare, that you actually want to keep all the texture data around, once everything has been loaded and the array texture been generated. Freeing everything is almost always the right choice afterwards.
Simple implementation:
- Have an
update function, that only updates the texture without freeing any image data.
- Have an additional
freeze function, that updates the texture and frees all image data.
- Any modification attempts after a freeze should then throw an exception.
Idea without exceptions:
- Make the freeze function a move only function and return different type
FrozenTextureAtlas that is read-only.
- Gets rid of frozen checks that throw exceptions.
FrozenTextureAtlas has an actual TextureAtlas member variable, which gets moved in but only exposes const functions.
- Don't make the member const, as this prevents moves, which are still perfectly valid.
- A bit more annoying to maintain when new functions are added, but probably worth the effort.
- Will also be a bit more complicated with decoupling of the Texture2DArray in mind.
Additional afterthoughts:
- Add the ability to mark tiles that should not be freed.
- E.g. GUI elements might still need manual texture lookup for hit-tests.
- Maybe even add special cases to only store relevant parts (only transparency information for hit-tests for example).
- Turn image into a
std::variant of various image formats or even just a bitfield for hit-tests.
- Do I want an
Image<bool> specialization for that? Sounds like a neat idea.
Currently a
TextureAtlasstores all the image data forever. This is actually required, as new textures might be added later on, which might in turn require the underlying array texture to resize to fit all tiles, which then invalidates the already existing tiles.However keeping all those textures around forever is quite expensive. Therefore a way to free all texture data should exist.
Of course, once all the textures are freed, no new textures can be added anymore, as this might require a resize again. To prevent this, upon freeing the textures the
TextureAtlasshould be in a frozen state, preventing any further modification.It is very rare, that you actually want to keep all the texture data around, once everything has been loaded and the array texture been generated. Freeing everything is almost always the right choice afterwards.
Simple implementation:
updatefunction, that only updates the texture without freeing any image data.freezefunction, that updates the texture and frees all image data.Idea without exceptions:
FrozenTextureAtlasthat is read-only.FrozenTextureAtlashas an actualTextureAtlasmember variable, which gets moved in but only exposes const functions.Additional afterthoughts:
std::variantof various image formats or even just a bitfield for hit-tests.Image<bool>specialization for that? Sounds like a neat idea.