We now have a sprite packer! Point it at a directory and it collects images there, packing them into one or more pages of sprites. This makes effectivelyusing SpriteBatch and SpriteCache easy.
Packing is done by the SpriteSheetPacker class and is reasonably smart. It tries multiple approaches and then chooses the one that results in the best packing. It attempts to use the smallest size page images possible. It can pack into either power-of-two (POT) or non-POT page sizes. Images will be rotated if necessary to better fit. Whitespace is stripped from the images when they are packed. Alpha values below a threshold can be stripped to handle images that don’t pack well because of barely visible pixels. Packing results in a directory of page images and a text file describing all the sprites on those pages.
Unpacking is done by the SpriteSheet class. This parses the text file, loads the page images as textures, and provides Sprite instances for each packed sprite. Sprites are requested by name, which matches the original image file. When the sprites are drawn, they are automatically offset as if no whitespace was stripped off.
If an input image file ends with a number, this “index” number is preserved and not included in the sprite name. SpriteSheet can return a list of sprites with the same name, ordered by index number. This makes it very easy to do animations with packed sprites. If an image was rotated when packed, the resulting sprite will not be rotated.
In an input image has a suffix of “_4444″, “_565″, or “_a” then it will use the OpenGL image format RGBA4444, RGB565, or Alpha for the page that holds those images. RGAB8888 will be used if no format suffix is specified. Input images will only be packed on to pages with other images of the same format.
If an input image has a suffix of “_x”, “_y”, or “_xy” then it will use the texture repeat setting of GL_REPEAT in the x, y, or both directions. Input images will only be packed on to pages with other images of the same repeat setting, and with the appropriate width or height so the textures can be scrolled. Of course, “_xy” images will always appear on a page by themselves.
Subdirectories are recursively processed. Pages will only contain images from the same directory. This allows the use of subdirectories to group images together that should appear on the same page, which reduces texture binds.