Skip to content

LibGDX exporter ​

How it works ​

Packrat writes a LibGDX TextureAtlas descriptor and the image files it references. The descriptor keeps each frame's name, animation index, trim offset, and original size. LibGDX uses that metadata when it creates an AtlasRegion or Sprite, so application code does not calculate atlas rectangles.

API ​

LibGDX reads the generated descriptor into TextureAtlas.AtlasRegion values. The relevant region properties are:

java
public String name;
public int index;
public int originalWidth, originalHeight;
public float offsetX, offsetY;
public int packedWidth, packedHeight;
public boolean rotate;

The descriptor and runtime calls have these signatures:

java
TextureAtlas atlas = new TextureAtlas(FileHandle packFile);
AtlasRegion TextureAtlas.findRegion(String name);
Array<AtlasRegion> TextureAtlas.findRegions(String name);
Sprite TextureAtlas.createSprite(String name);
Array<Sprite> TextureAtlas.createSprites(String name);
void SpriteBatch.begin();
void Sprite.draw(SpriteBatch batch);
void SpriteBatch.end();
void TextureAtlas.dispose();
void SpriteBatch.dispose();

findRegion, createSprite, and their plural forms use the base animation name. The plural forms return the frames ordered by their stored animation index. AtlasRegion already applies trim metadata when converted to a Sprite; application code does not calculate atlas rectangles.

Integrate ​

Copy the descriptor and its image files into the game's assets directory. Use the base animation name when the source contains multiple frames:

java
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;

public final class Demo extends ApplicationAdapter {
    private SpriteBatch batch;
    private TextureAtlas atlas;
    private Array<Sprite> frames;
    private int frameIndex;

    @Override public void create() {
        batch = new SpriteBatch();
        atlas = new TextureAtlas(Gdx.files.internal("atlas.atlas"));
        frames = atlas.createSprites("run_side");
    }

    @Override public void render() {
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        Sprite sprite = frames.get(frameIndex);
        sprite.setCenter(320f, 180f);
        batch.begin();
        sprite.draw(batch);
        batch.end();
    }

    @Override public void dispose() {
        batch.dispose();
        atlas.dispose();
    }
}

For one non-animated frame, call atlas.createSprite("player") instead. Replace .png in the example filenames with the image extension selected for the export.

Packrat documentation