
#include "TextureManager.h"
#include "MemoryManager/DebugNew.h"

using namespace RenderSystem;

//la ligne pour allouer la mémoire pour l'unique instance de MaterialManager par le compilateur
TextureManager *TextureManager::texManager = NULL;

TextureManager::TextureManager()
{
	this->textures.reserve(16);	//on reserve un peu de place
}

TextureManager::~TextureManager()
{
	//libération mémoire vidéo
	this->unloadAllTexture();
	//libération mémoire system
	int size = this->textures.size();
	for(int i=0;i<size;i++)
	{
		delete this->textures[i];
	}
	this->textures.clear();
}

bool TextureManager::addTexture2D(char* rgbPath,char* alphaPath,int _downSizingLevel,int _magTexFilter,int _minTexFilter,int _AnisotropyLevel,int _sRepeat,int _tRepeat,int _uRepeat, bool _generateAlpha, unsigned char _rBlended, unsigned char _gBlended, unsigned char _bBlended, unsigned char _alphaBlendValue, unsigned char _alphaDefault)
{
	if(rgbPath==NULL) return false;

	if(this->getTextureByName(rgbPath)!=NULL)
	{
		return true;	//la texture exist déjà : on ne génere pas d'erreur
	}

	//on tente de lire l'image pour voir si elle existe
	FILE* f = fopen(rgbPath,"r");
	if(f==NULL) return false;
	fclose(f);
	if(alphaPath!=NULL)
	{
		f = fopen(alphaPath,"r");
		if(f==NULL) return false;
		fclose(f);
	}

	bool ret=true;
	RegistredTexture_2D* rt2d = new RegistredTexture_2D;
	rt2d->setProperties(_downSizingLevel,_magTexFilter,_minTexFilter,_AnisotropyLevel,_sRepeat,_tRepeat,_uRepeat);
	rt2d->setName(rgbPath);
	rt2d->setAlphaMap(alphaPath);
	rt2d->alphaBlendValue = _alphaBlendValue;
	rt2d->alphaDefault = _alphaDefault;
	rt2d->bBlended = _bBlended;
	rt2d->rBlended = _rBlended;
	rt2d->gBlended = _gBlended;
	rt2d->generateAlpha = _generateAlpha;

	this->textures.push_back(rt2d);

	return ret;
}

RegistredTexture* TextureManager::getTextureByName(char* name)
{
	RegistredTexture* ret = NULL;

	if(name==NULL)return NULL;

	int size = this->textures.size();
	for(int i=0;i<size;i++)
	{
		if(strcmp(name,this->textures[i]->getName())==0)
		{
			ret = this->textures[i];
			if(!this->textures[i]->isInVideoMem())//si la texture n'est pas en mémoire vidéo
			{
				if(!this->textures[i]->loadInVideoMem())//si le chargement s'est mal passé
				{
					console->DispMsg(1.0f,0.5f,0.5f,1.0f,"TextureManager::getTextureByName : Une texture n'a pas ete charger : ");
					console->DispMsg(1.0f,0.5f,0.5f,1.0f,this->textures[i]->name);
				}
				/*else
				{
					console->DispMsg(1,1,0,1,name);
				}*/
			}
			break;
		}
	}

	return ret;
}

int TextureManager::getTextureIdByName(char* name)
{
	int ret = -1;

	if(name==NULL)return NULL;

	int size = this->textures.size();
	for(int i=0;i<size;i++)
	{
		if(strcmp(name,this->textures[i]->getName())==0)
		{
			ret=i;
			if(!this->textures[i]->isInVideoMem())
			{
				if(!this->textures[i]->loadInVideoMem())
				{
					console->DispMsg(1.0f,0.5f,0.5f,1.0f,"TextureManager::getTextureIdByName : Une texture n'a pas ete charger : ");
					console->DispMsg(1.0f,0.5f,0.5f,1.0f,this->textures[i]->name);
				}
				/*else
				{
					console->DispMsg(1,1,0,1,name);
				}*/
			}
			break;
		}
	}
	return ret;
}

RegistredTexture* TextureManager::getTextureById(int id)
{
	if( id<0 || id>=(int)(this->textures.size()) )
		return NULL;

	return this->textures[id];
}

void TextureManager::unloadAllTexture()
{
	int size = this->textures.size();
	for(int i=0;i<size;i++)
	{
		this->textures[i]->unloadFromVideoMem();
	}
}
	
void TextureManager::reLoadAllTexture()
{
	int size = this->textures.size();
	for(int i=0;i<size;i++)
	{
		if(this->textures[i]->isInVideoMem())
			this->textures[i]->loadInVideoMem();
	}
}

void TextureManager::loadAllTexture()
{
	int size = this->textures.size();
	for(int i=0;i<size;i++)
	{
		this->textures[i]->loadInVideoMem();
	}
}

void TextureManager::debug()
{
	int size = this->textures.size();
	for(int i=0;i<size;i++)
	{
		std::cout<<"texture "<<this->textures[i]->getName()<<std::endl;
	}
}


