Skip to content

Texture Atlas

There are two important concepts if you want to create a texture animation: spritesheets and atlases. Read both articles first before deciding which solution is most suitable for your game.

A texture atlas is a single image that contains multiple smaller images (called regions), often with irregular sizes. Each region represents a different version of a character, object, or visual element in a game.

Unlike spritesheets, which are organized in a strict grid, atlases allow each image to have its own width and height. This is very useful when your frames are not all the same size or when you want to pack many images efficiently into a single file.

For example, a character might have walk frames, attack frames, and idle frames — instead of storing them as separate images, you can pack them into one atlas.

In addition, there is an .atlas file that tells the program where each region is located in the image.

Using texture atlases gives you the same benefits as using spritesheets. The only difference is, that we can give each region a specific name and that we can use different sizes for each region.

The image below shows you the link between the image and the atlas file.

Texture atlases explained

Figure 1. Example of texture atlas

We have already created an example atlas for you to practice with. It is based on one of the free asset packs from kenney.nl: Toon Characters 1 pack.

Please download:

Creating your own texture atlas

You can create a texture atlas using tools that pack multiple images into a single atlas image and generate a corresponding .atlas file. One popular option is LibGDX’s TexturePacker or go directly to the releases to download an executable version here.

  1. Place all the images you want to include (e.g., walk frames, attack frames, idle frames) in a folder. Make sure that if you have multiple frames that are in sequence (for example walking), that they are index like this: walk_0.png, walk_1.png, walk_2.png, etc.
  2. Run the TexturePacker tool, which combines the images into a single PNG atlas and generates a .atlas file that describes the location and size of each region.
  3. Give meaningful names to your images (e.g., walk_0.png, attack_1.png) — these names will be used to reference the regions in your code.
  4. Copy the resulting .png and .atlas files into your project’s assets folder.

Now you can load the atlas in your game using GameApp.addTextureAtlas("name", "path/to/atlas.atlas") and draw individual regions by name.

Tip: This is a very easy way of creating your own spritesheets and texture atlases!

Adding a Texture Atlas

// Load a texture atlas from your project
GameApp.addTextureAtlas("robot", "textures/hero.atlas");
  • name: a unique name to identify the atlas (e.g., "robot").
  • filePath: the path to the .atlas file inside your project.

Note: The .atlas file describes the location and size of each region in the atlas image. You don’t need to worry about slicing manually; the atlas handles it for you.

Drawing Atlas Regions

You can draw individual regions from the atlas using the region name. The GameApp provides similar methods to spritesheets.

Draw at a position

GameApp.drawAtlasRegion("robot", "walk_0", 100, 200); // Draw the region at x=100, y=200

Draw with scaling

GameApp.drawAtlasRegion("robot", "walk_1", 50, 50, 100, 100); // Scaled to 100x100

Draw with rotation and flipping

GameApp.drawAtlasRegion("robot", "walk_2", 200, 100, 64, 64, 45, true, false);
  • rotationDegrees: rotates clockwise.
  • flipX: mirror horizontally.
  • flipY: mirror vertically.

Draw centered at a position

GameApp.drawAtlasRegionCentered("robot", "walk_0", 400, 300);

Similarly, you can draw centered with scaling, rotation, and flipping.

Useful Atlas Helpers

Check if an atlas exists

boolean exists = GameApp.hasTextureAtlas("robot");

Get a region from the atlas

If you want access to the underlying object, you can call this method to retrieve the TextureRegion.

TextureRegion frame = GameApp.getAtlasRegion("robot", "walk_0");

Remove an atlas and free memory

Always dispose of atlases when they are no longer needed to avoid using too much memory.

GameApp.disposeTextureAtlas("robot");

Example

The example below shows you how to load a texture atlas and render a few regions from it:

public class RobotScreen extends ScalableGameScreen {
    public RobotScreen() {
        super(1280, 720);
    }

    @Override
    public void show() {
        // Load the texture atlas
        GameApp.addTextureAtlas("robot", "images/hero.atlas");
    }

    @Override
    public void render(float delta) {
        super.render(delta);

        GameApp.startSpriteRendering();

        // Draw the walk frame at position x=100, y=100
        GameApp.drawAtlasRegion("robot", "idle", 100, 100);

        // Draw another frame at x=200, y=150
        GameApp.drawAtlasRegion("robot", "walk_0", 200, 150);

        // Draw a rotated and flipped region
        GameApp.drawAtlasRegion("robot", "attack_0", 400, 200, 100, 100, 45, true, false);

        GameApp.endSpriteRendering();
    }

    @Override
    public void hide() {
        // Dispose the atlas to free memory
        GameApp.disposeTextureAtlas("robot");
    }
}