Search code examples
filelibgdxtexture-atlas

Libgdx texture atlas vs files


So in my game i have more than 32000 images (size: 32x32). How to store this amount of images? What could better: loading these images as separate files or as one big texture atlas?


Solution

  • When you load a texture (image) in your game by doing you load a texture in GPU.

    So let's say you have 3200 images and you load them separately that means 3200 textures loaded on the GPU. That's harsh and will eat loads of memory.

    Now let me explain what is TextureRegion

    TextureRegion, takes an area from the Texture according to the dimension you provide, the advantage of having it is that you don't have to load textures again and again and the bigger advantage is you don't have to load each and every texture on GPU as you can do it directly by loading one big texture and extracting sub regions(TextureRegions) from it.

    Now because you want to use TextureRegions, it will be hard to know the dimensions of each and every sub image to load them from Texture Sheet. So what we do is we pack the Textures into a bigger Texture using TexturePacker(an application) which then creates a .pack file. It will pack every texture into one image AND create a .pack file. Now when you load the .pack file, it is loaded using TextureAtlas class For example imagine a pokemon pack file which has all the pokemons into it.

     TextureAtlas  pokemonFrontAtlas = new TextureAtlas(Gdx.files.internal("pokemon//pokemon.pack"));
    

    Now you packed 3200 files using Texture Packer and you want to load a image(Texture) which has file name as "SomePokemon".

    Now to get a particular TextureRegion from it, you do

    pokemonFrontAtlas.findRegion("SomePokemon")
    

    findRegion(String name) returns you the textureRegion from the TextureAtlas.

    A TextureAtlas class contains a collection of AtlasRegion class which extends TextureRegion class.

    See Javadocs for more details TextureAtlas