Click here to Skip to main content
15,868,016 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 141.6K   7K   55  
A utility to create texture atlases for 2D OpenGL games
#ifndef XPARSER_H
#define XPARSER_H

#include "Allocator.h"

// used to parses a text file with number of keys and values  
class XParser 
{
public: 
	XParser() { m_fileSize = 0;}
	~XParser() {}
	void Open(const string& fileName);
	void Open(FILE* pFile);
	int GetLine()
	{
		return m_currLine;
	}
	void ReadNextElement();
	bool IsElement(const char* szComp) const
	{
		return m_elementName.compare(szComp) == 0;
	}
	const string& GetValue(const char* szKey) const;
	bool GetOptionalValue(const char* szKey, string& value) const;
	int GetValueInt(const char* szKey) const
	{
		int num = atoi(GetValue(szKey).c_str());
		return num;
	}
	float GetValueFloat(const char* szKey) const
	{
		#ifdef WIN32
		float num = (float)atof(GetValue(szKey).c_str());
		#else
		float num = strtof(GetValue(szKey).c_str(), NULL);
		#endif
		return num;
	}
	bool GetValueBool(const char* szKey) const
	{
		if (GetValue(szKey) == "true") return true;
		return false;
	}
	int GetOptionalInt(const char* szKey, int defaultValue) const;
	bool EndFileReached() const
	{
		return m_currPos >= m_fileSize;
	}
	bool IsElementOpen(const char* szKey) const
	{
		fmap<string, int>::const_iterator iter;
		iter = m_mapElement.find(szKey);
		if (iter == m_mapElement.end()) return false;
		return iter->second > 0;
	}
	void RestartAt(int currPos)
	{
		m_currPos = currPos;
		m_mapAttrib.clear();
		m_currChar = m_buffer.Get(m_currPos);
		m_mapElement.clear();
	}
	int GetCurrPos() const
	{
		return m_currPos;
	}
private:	
	void GetNextWord(string& token);
	void GetNextValue(string& token);
	void EatWhiteSpace();
	void CheckComment();
	string m_elementName;
	fmap<string, string> m_mapAttrib;
	fmap<string, int> m_mapElement;
	// used for error reporting, each newline increases line count
	int m_currLine;	
	Allocator<char> m_buffer;
	int m_currPos;
	int m_fileSize;
	unsigned char m_currChar;
};

#endif // XPARSER_H

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