Click here to Skip to main content
15,884,298 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
#ifndef _ALLOCATOR_H_INCLUDED_
#define _ALLOCATOR_H_INCLUDED_


template <class T>
class Allocator
{
public:
	Allocator() 
	{
		m_size = 0;
		m_pCache = NULL;
		m_currUsed = 0;
	}
	~Allocator() 
	{
		Release();
	}
	void Release() 
	{
		if (m_pCache != NULL)
		{
			free(m_pCache);
			m_size = 0;
			m_pCache = NULL;
			m_currUsed = 0;
		}
	}
	void Deallocate() { m_currUsed = 0; }
	void Init(int size)	
	{
		if (size > m_size)
		{
			T* pNewCache = (T*)malloc(size*sizeof(T));
			if (pNewCache == NULL)
				throw RacException("not enough memory ");
			if (m_currUsed > 0)
			{
				memcpy(pNewCache, m_pCache, m_currUsed*sizeof(T));
			}
			Release();
			m_pCache = pNewCache;
			m_size = size;
		}
	}
	T* New()
	{
//		assert(m_currUsed < m_size);
		if (m_currUsed >= m_size)
		{
			cout << "Warning: surpise allocation!!\n";
			assert(false);
			return NULL;
		}
		return &m_pCache[m_currUsed++];
	}
	T* GetCache()
	{
		return m_pCache;
	}
	T& Get(int index)
	{
		return m_pCache[index];
	}
	T* GetPtr(int index)
	{
		return &m_pCache[index];
	}
	void Remove()
	{
		m_currUsed--;
	}
private:
	T* m_pCache;
	int m_size;
	int m_currUsed;
};





#endif // _ALLOCATOR_H_INCLUDED_

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