public class PixmapPacker extends Object implements Disposable
pixmaps
into one or more pages
to generate an atlas of pixmap instances. Provides means to
directly convert the pixmap atlas to a TextureAtlas
. The packer supports padding and border pixel duplication,
specified during construction. The packer supports incremental inserts and updates of TextureAtlases generated with this class.
All methods can be called from any thread unless otherwise noted.
One-off usage:
// 512x512 pixel pages, RGB565 format, 2 pixels of padding, border duplication PixmapPacker packer = new PixmapPacker(512, 512, Format.RGB565, 2, true); packer.pack("First Pixmap", pixmap1); packer.pack("Second Pixmap", pixmap2); TextureAtlas atlas = packer.generateTextureAtlas(TextureFilter.Nearest, TextureFilter.Nearest, false); packer.dispose(); // ... atlas.dispose();With this usage pattern, disposing the packer will not dispose any pixmaps used by the texture atlas. The texture atlas must also be disposed when no longer needed. Incremental texture atlas usage:
// 512x512 pixel pages, RGB565 format, 2 pixels of padding, no border duplication PixmapPacker packer = new PixmapPacker(512, 512, Format.RGB565, 2, false); TextureAtlas atlas = new TextureAtlas(); // potentially on a separate thread, e.g. downloading thumbnails packer.pack("thumbnail", thumbnail); // on the rendering thread, every frame packer.updateTextureAtlas(atlas, TextureFilter.Linear, TextureFilter.Linear, false); // once the atlas is no longer needed, make sure you get the final additions. This might // be more elaborate depending on your threading model. packer.updateTextureAtlas(atlas, TextureFilter.Linear, TextureFilter.Linear, false); // ... atlas.dispose();Pixmap-only usage:
PixmapPacker packer = new PixmapPacker(512, 512, Format.RGB565, 2, true); packer.pack("First Pixmap", pixmap1); packer.pack("Second Pixmap", pixmap2); // do something interesting with the resulting pages for (Page page : packer.getPages()) { // ... } packer.dispose();
Modifier and Type | Class and Description |
---|---|
static class |
PixmapPacker.Page |
Constructor and Description |
---|
PixmapPacker(int pageWidth,
int pageHeight,
Pixmap.Format pageFormat,
int padding,
boolean duplicateBorder)
Creates a new ImagePacker which will insert all supplied pixmaps into one or more
pageWidth by
pageHeight pixmaps. |
Modifier and Type | Method and Description |
---|---|
void |
dispose()
Disposes any pixmap pages which don't have a texture.
|
TextureAtlas |
generateTextureAtlas(Texture.TextureFilter minFilter,
Texture.TextureFilter magFilter,
boolean useMipMaps)
Generates a new
TextureAtlas from the pixmaps inserted so far. |
boolean |
getDuplicateBorder() |
boolean |
getPackToTexture() |
int |
getPadding() |
PixmapPacker.Page |
getPage(String name) |
int |
getPageHeight() |
int |
getPageIndex(String name)
Returns the index of the page containing the given packed rectangle.
|
Array<PixmapPacker.Page> |
getPages() |
int |
getPageWidth() |
Rectangle |
getRect(String name) |
Rectangle |
pack(Pixmap image)
Inserts the pixmap without a name.
|
Rectangle |
pack(String name,
Pixmap image)
Inserts the pixmap.
|
void |
setPackToTexture(boolean packToTexture)
If true, when a pixmap is packed to a page that has a texture, the portion of the texture where the pixmap was packed is
updated using glTexSubImage2D.
|
void |
updatePageTextures(Texture.TextureFilter minFilter,
Texture.TextureFilter magFilter,
boolean useMipMaps)
Calls
updateTexture for each page. |
void |
updateTextureAtlas(TextureAtlas atlas,
Texture.TextureFilter minFilter,
Texture.TextureFilter magFilter,
boolean useMipMaps)
Updates the
TextureAtlas , adding any new Pixmap instances packed since the last call to this method. |
void |
updateTextureRegions(Array<TextureRegion> regions,
Texture.TextureFilter minFilter,
Texture.TextureFilter magFilter,
boolean useMipMaps)
Calls
updateTexture for each page and adds a region to the
specified array for each page texture. |
public PixmapPacker(int pageWidth, int pageHeight, Pixmap.Format pageFormat, int padding, boolean duplicateBorder)
pageWidth
by
pageHeight
pixmaps.padding
- the number of blank pixels to insert between pixmaps.duplicateBorder
- duplicate the border pixels of the inserted images to avoid seams when rendering with bi-linear
filtering on.public Rectangle pack(Pixmap image)
pack(String, Pixmap)
public Rectangle pack(String name, Pixmap image)
getRect(String)
.name
- If null, the image cannot be looked up by name.GdxRuntimeException
- in case the image did not fit due to the page size being too small or providing a duplicate
name.public Array<PixmapPacker.Page> getPages()
PixmapPacker.Page
instances created so far. If multiple threads are accessing the packer, iterating over the pages
must be done only after synchronizing on the packer.public Rectangle getRect(String name)
name
- the name of the imagepublic PixmapPacker.Page getPage(String name)
name
- the name of the imagepublic int getPageIndex(String name)
name
- the name of the imagepublic void dispose()
dispose
in interface Disposable
public TextureAtlas generateTextureAtlas(Texture.TextureFilter minFilter, Texture.TextureFilter magFilter, boolean useMipMaps)
TextureAtlas
from the pixmaps inserted so far. After calling this method, disposing the packer will
no longer dispose the page pixmaps.public void updateTextureAtlas(TextureAtlas atlas, Texture.TextureFilter minFilter, Texture.TextureFilter magFilter, boolean useMipMaps)
TextureAtlas
, adding any new Pixmap
instances packed since the last call to this method. This
can be used to insert Pixmap instances on a separate thread via pack(String, Pixmap)
and update the TextureAtlas on
the rendering thread. This method must be called on the rendering thread. After calling this method, disposing the packer
will no longer dispose the page pixmaps.public void updateTextureRegions(Array<TextureRegion> regions, Texture.TextureFilter minFilter, Texture.TextureFilter magFilter, boolean useMipMaps)
updateTexture
for each page and adds a region to the
specified array for each page texture.public void updatePageTextures(Texture.TextureFilter minFilter, Texture.TextureFilter magFilter, boolean useMipMaps)
updateTexture
for each page.public int getPageWidth()
public int getPageHeight()
public int getPadding()
public boolean getDuplicateBorder()
public boolean getPackToTexture()
public void setPackToTexture(boolean packToTexture)
getDuplicateBorder()
is true.Copyright © 2015. All rights reserved.