Export to GitHub

libgdx - issue #515

Build PIxmap from TextureAtlas region data


Posted on Oct 10, 2011 by Quick Bird

I am not sure how useful is this issue, in my case I need to create Pixmaps from TextureAtlas regions to reuse the same TextureAtlas data.

I have created a utility class named PixmapTextureAtlas which creates new Pixmaps using a texture atlas region data, here is the code:

public static class PixmapTextureAtlas implements Disposable {

    private TextureAtlas textureAtlas;
    private Pixmap textureAtlasPixmap;

    boolean shouldDispose = false;

    public PixmapTextureAtlas(FileHandle textureAtlasImageFile, FileHandle textureAtlasFile) {
        this(new TextureAtlas(textureAtlasFile), new Pixmap(textureAtlasImageFile));
        this.shouldDispose = true;
    }

    public PixmapTextureAtlas(TextureAtlas textureAtlas, Pixmap textureAtlasPixmap) {
        this.textureAtlas = textureAtlas;
        this.textureAtlasPixmap = textureAtlasPixmap;
    }

    public Pixmap createPixmap(String regionName) {
        AtlasRegion region = textureAtlas.findRegion(regionName);

        int width = MathUtils.nextPowerOfTwo(region.getRegionWidth());
        int height = MathUtils.nextPowerOfTwo(region.getRegionHeight());

        Pixmap regionPixmap = new Pixmap(width, height, textureAtlasPixmap.getFormat());

        int x = (width / 2) - (region.getRegionWidth() / 2);
        int y = (height / 2) - (region.getRegionHeight() / 2);

        regionPixmap.drawPixmap(textureAtlasPixmap, x, y, region.getRegionX(), region.getRegionY(), region.getRegionWidth(), region.getRegionHeight());

        return regionPixmap;
    }

    @Override
    public void dispose() {
        if (shouldDispose) {
            textureAtlas.dispose();
            textureAtlasPixmap.dispose();
        }
    }

}

Maybe could be of help and you could add it to libGDX code (in a better way, maybe using TextureAtlas internal information instead).

Comment #1

Posted on Oct 12, 2011 by Massive Horse

While it might be of help to some people, i'm reluctant adding it to libgdx. The use of Pixmaps is something i want to discourage as their upload to the GPU as texture is terribly slow and will give people the wrong impression.

May i suggest that you post your above code in the libgdx contributions forums over at http://www.badlogicgames.com/forum.

To make things clear: i seriously appreciate the contribution, however, i don't think it should be part of the core API. This should not discourage you from adding more contributions, quite the contrary! Hope you understand.

Comment #2

Posted on Oct 12, 2011 by Quick Bird

No problem, when I have the code "unified" in on of our common libraries I will share it in the forums.

Status: WontFix