Android Question libGDX and Texture Atlas Overhead

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
I'm a little fuzzy on exactly how the texture atlas works (behind the scenes, that is) and was wondering which solution would be the best to implement:

For example, in your main form you create an Atlas, and you need to create multiple copies of your Actor class (say five to twenty), each of which will have three or four animations which consist of anywhere between four and fourteen frames contained in the Atlas.

My question is, which would be the most efficient (or least memory-impacting) way to accomplish this, or are they effectively the same since we're referencing an Atlas:
  1. Pass the Atlas to the Actor class when you initialize and let each instance create its own animation textures for its animations, or...
  2. Create an "AnimationTextures" class that contains all of the textures and pass that to the Actor and have all instances use the same texture list?

Thanks in advance,
- Richard
 
Last edited:

Informatix

Expert
Licensed User
Longtime User
Your question is not clear. You cannot pass an atlas to an actor (you pass a region, a sprite or a drawable to a Scn2DImage, which can be created from an atlas) and you cannot create "animation textures" (you could do it from a pixmap, but I'm not sure that is what you want to do).

Here are a few informations about textures and atlas:
All regions, sprites or drawables created from a texture do not use extra memory (except to store their class properties) so you can create as many regions, sprites or drawables as you want. Only the number of textures really matters as it has two consequences:
- each texture takes a significant part of native memory (width * height * 4 bytes);
- displaying too many textures implies to switch them repeatedly in the texture unit of the GPU, which hinders performance.
For the latter reason, a texture atlas is interesting because you group on the same texture many different images. The usual way to use an atlas is to load assets in the background (as I do in the demo) when the game is launched (most splash screens in games are just there to make you wait while the resources are loading) and call FindRegion when you need a region of this atlas or Create9Path, CreateSprite, CreateRegionDrawable for the other types.
Calling these functions may be expensive as they browse the list of images stored in the atlas and create a new class to use the found region. It is more efficient to call them once and store the created region (or sprite or drawable...) in a global variable.
 
Upvote 0

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
I'm new to this (If you couldn't tell o_O ) so I apologize if my terminology was confusing or inexact. However, I think you still managed to answer my question so thank you!

Given the example: You have created an actor class that will need to be on the stage in 20 different places (20 instances of the actor class)

Based on your response, I think you are saying that it would be better to create all of the lgTextureAtlasRegion variables you will need for your actor classes animations, once, as global variables and then pass them to the various actors classes you need during initialization and have each actor class instance create its own lgAnimation variables.

... as opposed to ....

Passing your lgTextureAtlas to your actor class and having each class instance create its own lgTextureAtlasRegion and lgAnimation variables.


If I still have not managed to grasp the concept, please let me know.

Thanks again,

Richard
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
I'm new to this (If you couldn't tell o_O ) so I apologize if my terminology was confusing or inexact.

I just did the same thing. I didn't notice that I wrote "create a new class to use" instead of "create a new instance for"... ;)

Given the example: You have created an actor class that will need to be on the stage in 20 different places (20 instances of the actor class)

Based on your response, I think you are saying that it would be better to create all of the lgTextureAtlasRegion variables you will need for your actor classes animations, once, as global variables and then pass them to the various actors classes you need during initialization and have each actor class instance create its own lgAnimation variables.

... as opposed to ....

Passing your lgTextureAtlas to your actor class and having each class instance create its own lgTextureAtlasRegion and lgAnimation variables.
lgAnimation is meant to be initialized with an array of regions created by the FindRegions method (note the S at the end) of lgTextureAtlas. If all animations use the same regions, it is better to call FindRegions only once, assign its result to a variable and use only the variable thereafter for each animation initialization. But if you have different regions to animate different actors, there's no preference (being given that everything is initialized in the Create event).
 
Upvote 0
Top