Click here to Skip to main content
15,884,388 members
Articles / Mobile Apps / Android

Texture Atlas Maker

Rate me:
Please Sign up or sign in to vote.
4.94/5 (16 votes)
1 Apr 2012BSD6 min read 142.1K   7K   55  
A utility to create texture atlases for 2D OpenGL games
import java.util.ArrayList;
import java.util.Collections;



public class BinPacker
{
	private TreeNode treeNode;
	private TexturePiece texturePiece;
	public TexturePiece getTexturePiece()
	{
		return texturePiece;
	}
	public BinPacker(int width, int height)
	{
		treeNode = new TreeNode(0, 0, width, height, "");
		texturePiece = new TexturePiece(width, height);
	}
	public boolean add(AtlasImage image)
	{
		boolean added = treeNode.add(image);
		if (added)
			texturePiece.add(image);
		return added;
	}
	public void pack(ArrayList<AtlasImage> listImage)
	{
		int ixImage;
		for (ixImage = listImage.size()-1; ixImage >= 0; ixImage--)
		{
			if (!add(listImage.get(ixImage)))
				return;
			listImage.remove(ixImage);			
		} 
	}		
	public void resize(int width, int height, ArrayList<AtlasImage> listImage)
	{
		// puts images removed back on the list
		texturePiece.remove(listImage);
		// need to resort, because they may have been put in wrong order
		Collections.sort(listImage, new CompareArea());
		// resize resets everything
		treeNode.resize(width, height);
		texturePiece.resize(width, height);
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The BSD License


Written By
Software Developer Astronautz
Spain Spain
After working in the software industry for many years, I've started my own games company that specialises in strategy games for mobile platforms.

Comments and Discussions