Click here to Skip to main content
15,892,005 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.7K   7K   55  
A utility to create texture atlases for 2D OpenGL games
import java.util.ArrayList;

public class TexturePuzzle
{
	private ArrayList<TextureSize> listSize;
	public TexturePuzzle(TextureSize textureSize)
	{
		listSize = new ArrayList<TextureSize>();
		listSize.add(textureSize);
	}
	public int getNumTextures()
	{
		return listSize.size();
	}
	public int getHeight(int index)
	{
		return listSize.get(index).getHeight();
	}
	public int getWidth(int index)
	{
		return listSize.get(index).getWidth();
	}
	public int getNumPixels()
	{
		int numPixels = 0;
        for (int n = 0; n < listSize.size(); n++) 
        {
        	numPixels += listSize.get(n).getNumPixels();
        }
		return numPixels;
	}	
	public void getNextSizeFits(int pixels, int maxWidth, int maxHeight)
	{
        do
        {
        	if (pixels <= getNumPixels() && 
        			maxWidth <= getWidth(0) &&
        			maxHeight <= getHeight(0))
        	{
        		break;
        	}
        	incTextureSize();
        } while(true);
	}
	
	public void incTextureSize()
	{
		TextureSize lastTextureSize = listSize.get(listSize.size()-1);
		if (lastTextureSize.isMaxSize())
			listSize.add(lastTextureSize.clone());
		else
			lastTextureSize.incTextureSize();
	}
}

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