coffeenet
coffeenet

Reputation: 573

LIBGDX: When should I dispose of TextureAtlas?

In the LIBGDX docs "A TextureAtlas must be disposed to free up the resources consumed by the backing textures." Last line in the LIBGDX wiki says "TextureAtlas holds on to all the page textures. Disposing the TextureAtlas will dispose all the page textures." So when am I supposed to dispose of a local TextureAtlas that its textures are used by other classes?

public class loader(String region) {
  public TextureRegion TextureLoader() {
    TextureAtlas atlas = new TextureAtlas("zzz.pack");
    TextureRegion textureRegion = atlas.findRegion(region);
    //atlas.dispose();//if I uncomment this line, no texture will be drawn, just a blank black area instead of the loaded textureRegion
    return ;
    }
  }

public class player() {
  public player() {
    TextureRegion textureRegion = loader(region);
    //draw textureRegion
    }
  }

Upvotes: 0

Views: 1173

Answers (1)

noone
noone

Reputation: 19776

You dispose it when it's not needed anymore. But this depends on which textures you have in this atlas.

It might be when you quit the game and go back to the main menu. A texture atlas with all gameplay related textures could be disposed in this case.

In case you have one texture atlas for each level of your game, because the levels look differently, then you could dispose them when you switch a level.

However in most cases you'll probably only dispose the atlas when the player quits the game completely. You should use an AssetManager to load the atlas and it's always a good idea to dispose the asset manager when the game is quit. This will dispose all assets that are still loaded and will cleanup everything.

Upvotes: 2

Related Questions