/*
Solar Conquest
Copyright (C) 2006 Greg Beaman

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#define IMAGE_TYPE_BMP 1
#define IMAGE_TYPE_PNG 2
#define IMAGE_TYPE_JPEG 3

class CTextureListNode
{
private:
	int width;
	int height;
	int id;
	GLuint texture;

public:
	bool loaded;

	CTextureListNode* prevNode;
	CTextureListNode* nextNode;

	CTextureListNode(int nid, char* fileName, int imageType);
	~CTextureListNode();

	int GetId();
	GLuint GetTextureId();
	int GetWidth();
	int GetHeight();
};

CTextureListNode::CTextureListNode(int nid, char* fileName, int imageType)
{
	printf("Loading texture ");
	printf(fileName);
	printf("\n");
	
	nextNode = NULL;
	prevNode = NULL;
	id = nid;
	loaded = false;

	SDL_Surface* bitmap;
	bitmap = IMG_Load(fileName);
	if (bitmap)
	{
		//Make the black portions of the surface transparent
		SDL_SetColorKey(bitmap,SDL_SRCCOLORKEY,SDL_MapRGB(bitmap->format,0,0,0));
		glGenTextures(1,&texture);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
		glBindTexture(GL_TEXTURE_2D,texture);
		if (imageType == IMAGE_TYPE_BMP)
			glTexImage2D(GL_TEXTURE_2D,0,3,bitmap->w,bitmap->h,0,GL_BGR,GL_UNSIGNED_BYTE,bitmap->pixels);
//		else if (imageType == IMAGE_TYPE_JPEG)
//			glTexImage2D(GL_TEXTURE_2D,0,4,bitmap->w,bitmap->h,0,GL_ARGB,GL_UNSIGNED_BYTE,bitmap->pixels);
		else if (imageType == IMAGE_TYPE_PNG)
			glTexImage2D(GL_TEXTURE_2D,0,4,bitmap->w,bitmap->h,0,GL_RGBA,GL_UNSIGNED_BYTE,bitmap->pixels);
		width = bitmap->w;
		height = bitmap->h;

		loaded = true;
		SDL_FreeSurface(bitmap);
	}
	else
		printf("ERROR! CAN NOT FIND TEXTURE!\n");
}

CTextureListNode::~CTextureListNode()
{
	glDeleteTextures(1,&texture);
}

int CTextureListNode::GetId() { return id; }
GLuint CTextureListNode::GetTextureId() { return texture; }
int CTextureListNode::GetWidth() { return width; }
int CTextureListNode::GetHeight() { return height; }

class CTextureList
{
private:
	CTextureListNode* firstNode;

public:
	CTextureList();
	~CTextureList();

	CTextureListNode* AddTexture(int id, char* fileName, int imageType);
	CTextureListNode* FindNode(int id);
	void DeleteTexture(int id);
};

CTextureList::CTextureList()
{
	firstNode = NULL;
}

CTextureList::~CTextureList()
{
	CTextureListNode* cNode;
	CTextureListNode* tNode;
	cNode = firstNode;
	while (cNode)
	{
		tNode = cNode->nextNode;
		delete cNode;
		cNode = tNode;
	}
	firstNode = NULL;
}

CTextureListNode* CTextureList::AddTexture(int id, char* fileName, int imageType)
{
	CTextureListNode* newNode;
	newNode = new CTextureListNode(id,fileName,imageType);
	if (!newNode)
		return NULL;
	if (!newNode->loaded)
	{
		delete newNode;
		return NULL;
	}
	if (firstNode)
	{
		newNode->nextNode = firstNode;
		firstNode->prevNode = newNode;
	}
	firstNode = newNode;
	return newNode;
}

CTextureListNode* CTextureList::FindNode(int id)
{
	CTextureListNode* cNode = firstNode;
	while (cNode)
	{
		if (cNode->GetId() == id)
			return cNode;
		cNode = cNode->nextNode;
	}
	return NULL;
}

void CTextureList::DeleteTexture(int id)
{
	CTextureListNode* delNode = FindNode(id);
	if (!delNode)
		return;
	if (delNode->prevNode)
		delNode->prevNode->nextNode = delNode->nextNode;
	else
		firstNode = delNode->nextNode;
	if (delNode->nextNode)
		delNode->nextNode->prevNode = delNode->prevNode;
	delete delNode;
}

CTextureList* g_textureList = NULL;

