#include "TextureManager.h"

TextureManager* TextureManager::instance = NULL;

TextureManager &TextureManager::getInstance(void)
{
	//singleton instance
	if ( TextureManager::instance  == NULL )
	{
		TextureManager::instance = new TextureManager();
	}

	return *TextureManager::instance;
}
TextureManager::TextureManager()
{
}
TextureManager::~TextureManager()
{
	for (hash_map<NAME, ID>::iterator it=this->textures.begin(); it!=this->textures.end(); ++it)
	{
		glDeleteTextures(1, &it->second);
	}

	this->textures.clear();
}

bool TextureManager::insertTexture( const string &name, ID textureId)
{
	hash_map< NAME, ID >::const_iterator it;

	it=this->textures.find(name);

	if (it==this->textures.end())
	{
		//textura s danym jmenem nenalezena, vlozim novou texturu

		this->textures.insert( make_pair(name, textureId) );

		return true;
	}
	else
	{
		//vlozeni se nezdarilo...textura s danym jmenem jiz existuje

		return false;
	}
}

ID TextureManager::getTexture(const NAME &textureName) const
{
	hash_map<NAME, ID>::const_iterator it=this->textures.find(textureName);


	if (it!=this->textures.end()) return it->second;
	else return 0;
}

ID TextureManager::getWhiteTexture(void)
{
	static ID whiteTexture = 0;

	if ( whiteTexture == 0) //was whiteTexture created?
	{
		//if not then create it
		Texture<unsigned char> texData;
		prepareWhiteTexture( texData);
		whiteTexture = TextureManager::createTexture( texData);
		this->insertTexture( "@@pre-alocated-white-texture@@", whiteTexture );
	}
	return whiteTexture;
}

bool TextureManager::deleteTexture(const string& textureName)
{
	hash_map< NAME, ID >::const_iterator it;

	it=this->textures.find(textureName);

	if (it==this->textures.end())
	{
		//textura s danym jmenem nenalezena

		return false;
	}

	this->textures.erase(it); //smazu texturu z databaze
		
																				
	glDeleteTextures(1,&it->second); //smazu texturu z GPU 

	return true;
}

TextureParam::TextureParam(GLenum glParam, GLint glValue)
{
	this->glParam=glParam;
	this->glValue=glValue;
}








//---------

