Click here to Skip to main content
15,891,253 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
#include "PreCompile.h"
#include "AtlasMaster.h"
#include "ImageGL.h"
#include "XParser.h"
#include "AtlasImage.h"
#include "Util.h"
#include "ResManager.h"
#include "ImagePVR.h"
#include "ImageFactory.h"

AtlasMaster::AtlasMaster()
{
}

AtlasMaster::~AtlasMaster()
{
	//map<string, AtlasImage*>::const_iterator iterMap;
	//for (iterMap = m_mapImage.begin(); iterMap != m_mapImage.end(); iterMap++) 
	//	delete iterMap->second;
	m_allocator.Release();
	int n;
	for (n = 0; n < (int)m_listImageGL.size(); n++)
	{
		m_listImageGL[n]->Release();
		delete m_listImageGL[n];
	}
}

void AtlasMaster::Load(const string& fileName)
{
	AtlasImage* pAtlasImage = NULL;
	ImageGL* pImageGL = NULL;
	
	XParser* pXParser = g_resManager.GetXParser();
	pXParser->Open(fileName);
	pXParser->ReadNextElement();	
	if (!pXParser->IsElementOpen("atlas")) 
		throw RacException("missing section, atlas");
	int count = pXParser->GetValueInt("numImages");
	m_allocator.Init(count);
	string fileNameImage;
	bool transparent;
	string value;
	do
	{
		pXParser->ReadNextElement();	
		if (pXParser->IsElement("texture"))
		{
			fileNameImage = g_resManager.GetDirResources() + pXParser->GetValue("file");
			transparent = true;
			if (pXParser->GetOptionalValue("trans", value))
			{
				if (value == "false")
					transparent = false;
			}

			pImageGL = ImageFactory::GetImageGL(fileNameImage);
			int timeNow = GetMilliSeconds();
			pImageGL->Load(fileNameImage, transparent);
			int timeLast = GetMilliSeconds();
			int timeElapsed = abs(timeLast-timeNow);
			cout << fileNameImage << " load:" << timeElapsed << "ms" << endl;
			m_listImageGL.push_back(pImageGL);
		}
		else if (pXParser->IsElement("image"))
		{
			pAtlasImage = m_allocator.New();
			pAtlasImage->SetImageGL(pImageGL);
			string name = pXParser->GetValue("name");
			pair <fmap<string, AtlasImage*>::iterator,bool> ret;
			ret = m_mapImage.insert( std::make_pair (name, pAtlasImage) );
			if (ret.second == false)
				throw RacException("duplicate image in texture atlas: ", name);
			// first item is iterator to newly created map entry
			pAtlasImage->SetName(ret.first->first);
			pAtlasImage->Load(pXParser);
		}
	} while (pXParser->IsElementOpen("atlas"));
}

AtlasImage* AtlasMaster::GetImage(const string& nameImage) 
{
	fmap<string, AtlasImage*>::const_iterator iter;
	iter = m_mapImage.find(nameImage);
	if (iter == m_mapImage.end()) 
		throw RacException("unknown image in texture atlas: ", nameImage);
	return iter->second;
}

AtlasImage* AtlasMaster::GetImageIfExist(const string& nameImage) 
{
	fmap<string, AtlasImage*>::const_iterator iter;
	iter = m_mapImage.find(nameImage);
	if (iter == m_mapImage.end()) 
		return NULL;
	return iter->second;
}



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