public class PixmapPacker extends Object implements Disposable
Pixmap
instances into one more more PixmapPacker.Page
instances 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 except getPage(String)
and getPages()
are thread safe. The methods
#generateTextureAtlas(TextureFilter, TextureFilter, boolean)
and
#updateTextureAtlas(TextureAtlas, TextureFilter, TextureFilter, boolean)
need to be called on the rendering thread, all
other methods can be called from any thread.
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);Note that you should not dispose the packer in this usage pattern. Instead, dispose the TextureAtlas if no longer needed. Incremental usage:
// 512x512 pixel pages, RGB565 format, 2 pixels of padding, no border duplication PixmapPacker packer = new PixmapPacker(512, 512, Format.RGB565, 2, false); TextureAtlas incrementalAtlas = new TextureAtlas(); // potentially on a separate thread, e.g. downloading thumbnails packer.pack("thumbnail", thumbnail); // on the rendering thread, every frame packer.updateTextureAtlas(incrementalAtlas, TextureFilter.Linear, TextureFilter.Linear); // 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(incrementalAtlas, TextureFilter.Linear, TextureFilter.Linear); incrementalAtlas.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()) { } // dispose of the packer in this case packer.dispose();
Modifier and Type | Class and Description |
---|---|
class |
PixmapPacker.Page |
Constructor and Description |
---|
PixmapPacker(int width,
int height,
Pixmap.Format format,
int padding,
boolean duplicateBorder)
Creates a new ImagePacker which will insert all supplied images into a
width by height image. |
Modifier and Type | Method and Description |
---|---|
void |
dispose()
Disposes all resources, including Pixmap instances for the pages created so far.
|
boolean |
duplicateBorder() |
TextureAtlas |
generateTextureAtlas(Texture.TextureFilter minFilter,
Texture.TextureFilter magFilter,
boolean useMipMaps)
Generates a new
TextureAtlas from the Pixmap instances inserted so far. |
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(String name,
Pixmap image)
Inserts the given
Pixmap . |
void |
updateTextureAtlas(TextureAtlas atlas,
Texture.TextureFilter minFilter,
Texture.TextureFilter magFilter,
boolean useMipMaps)
Updates the given
TextureAtlas , adding any new Pixmap instances packed since the last call to this method. |
public PixmapPacker(int width, int height, Pixmap.Format format, int padding, boolean duplicateBorder)
Creates a new ImagePacker which will insert all supplied images into a width
by height
image.
padding
specifies the minimum number of pixels to insert between images. border
will duplicate the
border pixels of the inserted images to avoid seams when rendering with bi-linear filtering on.
width
- the width of the output imageheight
- the height of the output imagepadding
- the number of padding pixelsduplicateBorder
- whether to duplicate the borderpublic Rectangle pack(String name, Pixmap image)
Inserts the given Pixmap
. You can later on retrieve the images position in the output image via the supplied name
and the method getRect(String)
.
name
- the name of the imageimage
- the imageRuntimeException
- in case the image did not fit due to the page size being to small or providing a duplicate namepublic Array<PixmapPacker.Page> getPages()
PixmapPacker.Page
instances created so far. This method is not thread safe!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()
TextureAtlas
generated or updated by either #generateTextureAtlas(TextureFilter, TextureFilter, boolean)
or #updateTextureAtlas(TextureAtlas, TextureFilter, TextureFilter, boolean)
. Do not call this method if you
generated or updated a TextureAtlas, instead dispose the TextureAtlas.dispose
in interface Disposable
public TextureAtlas generateTextureAtlas(Texture.TextureFilter minFilter, Texture.TextureFilter magFilter, boolean useMipMaps)
TextureAtlas
from the Pixmap
instances inserted so far.minFilter
- magFilter
- 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.public int getPageWidth()
public int getPageHeight()
public int getPadding()
public boolean duplicateBorder()
Copyright © 2014. All rights reserved.