Click here to Skip to main content
12,246,406 members (41,794 online)
Click here to Skip to main content

Stats

84.5K views
5.6K downloads
54 bookmarked
Posted

Texture Atlas Maker

, 1 Apr 2012 BSD
A utility to create texture atlases for 2D OpenGL games
AtlasExample
.DS_Store
__MACOSX
AtlasExample
._.DS_Store
AtlasExample
.DS_Store
AtlasExample
._.DS_Store
AtlasAppDelegate.mm
AtlasExample-Info.plist
AtlasExample-Prefix.pch
._AtlasExample-Prefix.pch
EAGLView.mm
en.lproj
InfoPlist.strings
en.lproj
._InfoPlist.strings
MainWindow.xib
ImagePNG.mm
._ImagePNG.mm
images0.tga
main.m
._main.m
Shaders
Shader.fsh
Shaders
._Shader.fsh
Shader.vsh
._Shader.vsh
AtlasExample.xcodeproj
project.pbxproj
project.xcworkspace
contents.xcworkspacedata
xcuserdata
lefty.xcuserdatad
UserInterfaceState.xcuserstate
xcuserdata
lefty.xcuserdatad
xcdebugger
Breakpoints.xcbkptlist
xcschemes
AtlasExample.xcscheme
xcschememanagement.plist
AtlasExampleTests
AtlasExampleTests-Info.plist
AtlasExampleTests-Prefix.pch
AtlasExampleTests
._AtlasExampleTests-Prefix.pch
AtlasExampleTests.m
._AtlasExampleTests.m
en.lproj
InfoPlist.strings
en.lproj
._InfoPlist.strings
.DS_Store
._.DS_Store
.DS_Store
._.DS_Store
AtlasAppDelegate.mm
AtlasExample-Info.plist
AtlasExample-Prefix.pch
._AtlasExample-Prefix.pch
EAGLView.mm
InfoPlist.strings
._InfoPlist.strings
MainWindow.xib
ImagePNG.mm
._ImagePNG.mm
imagesNelly0.tga
main.m
._main.m
Shader.fsh
._Shader.fsh
Shader.vsh
._Shader.vsh
project.pbxproj
contents.xcworkspacedata
UserInterfaceState.xcuserstate
Breakpoints.xcbkptlist
AtlasExample.xcscheme
xcschememanagement.plist
AtlasExampleTests-Info.plist
AtlasExampleTests-Prefix.pch
._AtlasExampleTests-Prefix.pch
AtlasExampleTests.m
._AtlasExampleTests.m
InfoPlist.strings
._InfoPlist.strings
AtlasMaker
.classpath
.project
bin
icon.png
images
icon.png
output.png
settings.png
output.png
settings.png
src
AtlasMaker.jar
AtlasMaker.jar
AtlasMaker.jar
AtlasMaker.jar
AtlasMaker.jar
.classpath
.project
.settings
org.eclipse.jdt.core.prefs
atlasMaker.properties
icon.png
icon.png
settings.png
settings.png
.classpath
.project
icon.png
icon.png
output.png
settings.png
output.png
settings.png
.classpath
.project
atlasMaker.properties
icon.png
icon.png
settings.png
settings.png
.classpath
.project
org.eclipse.jdt.core.prefs
atlasMaker.properties
icon.png
icon.png
settings.png
settings.png
.classpath
.project
org.eclipse.jdt.core.prefs
icon.png
icon.png
settings.png
settings.png
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

Share

About the Author

ed welch
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.

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160426.1 | Last Updated 1 Apr 2012
Article Copyright 2012 by ed welch
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid