/*
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 FONT_SPACING 2 //5
#define FONT_SIZE_X 5 //15
#define FONT_SIZE_Y 5 //15

class CBitmapFont
{
protected:
	void DrawChar(float x, float y, int i,int pX, int pY, float xscale, float yscale, float bpxWidth, float bpxHeight);

	CTextureListNode* fontNode;
public:
	CBitmapFont(char* fontName);
	~CBitmapFont();

	void Enable();
	void Disable();
	void RenderText(float x, float y, float xscale, float yscale, char* text);
};

CBitmapFont::CBitmapFont(char* fontName)
{
	fontNode = g_textureList->AddTexture(200,fontName,IMAGE_TYPE_BMP);
}

CBitmapFont::~CBitmapFont()
{
}

void CBitmapFont::Enable()
{
	if (!fontNode)
		return;
	
//	glDisable(GL_BLEND);
	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D,fontNode->GetTextureId());
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
	glColor4f(1,1,1,1);
	
	glBegin(GL_TRIANGLES);
}

void CBitmapFont::Disable()
{
	glEnd();
	glDisable(GL_TEXTURE_2D);
//	glEnable(GL_BLEND);
}

void CBitmapFont::RenderText(float x, float y, float xscale, float yscale, char* text)
{
//	glEnable(GL_TEXTURE_2D);
//	glColor3f(1,0,0);
	
	int len = strlen(text);
	float bpxWidth, bpxHeight;
	int i = 0;

//	fontNode = g_textureList->FindNode(200);
	if (fontNode)
	{
		bpxWidth = ((float)1)/((float)fontNode->GetWidth());
		bpxHeight = ((float)1)/((float)fontNode->GetHeight());
//		bpxWidth = bpxHeight = 1;
		{
		for (int z=0; z < len; z++)
		{
			if (text[z] == '1')
			{
				DrawChar(x,y,i,0,0,xscale,yscale,bpxWidth,bpxHeight);
/*				glTexCoord2f(XPixelToTexCoord(16,bpxWidth),YPixelToTexCoord( 0,fontNode->GetHeight(),bpxHeight));
				glVertex2f(x+((i+1)*FONT_SPACING*spxWidth),y);
				glTexCoord2f(XPixelToTexCoord( 0,bpxWidth),YPixelToTexCoord( 0,fontNode->GetHeight(),bpxHeight));
				glVertex2f(x+((i)*FONT_SPACING*spxWidth),y);
				glTexCoord2f(XPixelToTexCoord(0,bpxWidth),YPixelToTexCoord(15,fontNode->GetHeight(),bpxHeight));
				glVertex2f(x+((i)*FONT_SPACING*spxWidth),y-(15*spxHeight));

				glTexCoord2f(XPixelToTexCoord(16,bpxWidth),YPixelToTexCoord( 0,fontNode->GetHeight(),bpxHeight));
				glVertex2f(x+((i+1)*FONT_SPACING*spxWidth),y);
				glTexCoord2f(XPixelToTexCoord(0,bpxWidth),YPixelToTexCoord(15,fontNode->GetHeight(),bpxHeight));
				glVertex2f(x+((i)*FONT_SPACING*spxWidth),y-(15*spxHeight));
				glTexCoord2f(XPixelToTexCoord(16,bpxWidth),YPixelToTexCoord(15,fontNode->GetHeight(),bpxHeight));
				glVertex2f(x+((i+1)*FONT_SPACING*spxWidth),y-(15*spxHeight));*/
			}
			else if (text[z] == '2')
				DrawChar(x,y,i,1*(FONT_SIZE_X+1),0,xscale,yscale,bpxWidth,bpxHeight);
			else if (text[z] == '3')
				DrawChar(x,y,i,2*(FONT_SIZE_X+1),0,xscale,yscale,bpxWidth,bpxHeight);
			else if (text[z] == '4')
				DrawChar(x,y,i,3*(FONT_SIZE_X+1),0,xscale,yscale,bpxWidth,bpxHeight);
			else if (text[z] == '5')
				DrawChar(x,y,i,4*(FONT_SIZE_X+1),0,xscale,yscale,bpxWidth,bpxHeight);
			else if (text[z] == '6')
				DrawChar(x,y,i,5*(FONT_SIZE_X+1),0,xscale,yscale,bpxWidth,bpxHeight);
			else if (text[z] == '7')
				DrawChar(x,y,i,6*(FONT_SIZE_X+1),0,xscale,yscale,bpxWidth,bpxHeight);
			else if (text[z] == '8')
				DrawChar(x,y,i,7*(FONT_SIZE_X+1),0,xscale,yscale,bpxWidth,bpxHeight);
			else if (text[z] == '9')
				DrawChar(x,y,i,8*(FONT_SIZE_X+1),0,xscale,yscale,bpxWidth,bpxHeight);
			else if (text[z] == '0')
				DrawChar(x,y,i,9*(FONT_SIZE_X+1),0,xscale,yscale,bpxWidth,bpxHeight);

			else if (text[z] == 'a' || text[z] == 'A')
				DrawChar(x,y,i, 0,1*(FONT_SIZE_Y+1),xscale,yscale,bpxWidth,bpxHeight);
			else if (text[z] == 'b' || text[z] == 'B')
				DrawChar(x,y,i,1*(FONT_SIZE_X+1),1*(FONT_SIZE_Y+1),xscale,yscale,bpxWidth,bpxHeight);
			else if (text[z] == 'c' || text[z] == 'C')
				DrawChar(x,y,i,2*(FONT_SIZE_X+1),1*(FONT_SIZE_Y+1),xscale,yscale,bpxWidth,bpxHeight);
			else if (text[z] == 'd' || text[z] == 'D')
				DrawChar(x,y,i,3*(FONT_SIZE_X+1),1*(FONT_SIZE_Y+1),xscale,yscale,bpxWidth,bpxHeight);
			else if (text[z] == 'e' || text[z] == 'E')
				DrawChar(x,y,i,4*(FONT_SIZE_X+1),1*(FONT_SIZE_Y+1),xscale,yscale,bpxWidth,bpxHeight);
			else if (text[z] == 'f' || text[z] == 'F')
				DrawChar(x,y,i,5*(FONT_SIZE_X+1),1*(FONT_SIZE_Y+1),xscale,yscale,bpxWidth,bpxHeight);
			else if (text[z] == 'g' || text[z] == 'G')
				DrawChar(x,y,i,6*(FONT_SIZE_X+1),1*(FONT_SIZE_Y+1),xscale,yscale,bpxWidth,bpxHeight);
			else if (text[z] == 'h' || text[z] == 'H')
				DrawChar(x,y,i,7*(FONT_SIZE_X+1),1*(FONT_SIZE_Y+1),xscale,yscale,bpxWidth,bpxHeight);
			else if (text[z] == 'i' || text[z] == 'I')
				DrawChar(x,y,i,8*(FONT_SIZE_X+1),1*(FONT_SIZE_Y+1),xscale,yscale,bpxWidth,bpxHeight);
			else if (text[z] == 'j' || text[z] == 'J')
				DrawChar(x,y,i,9*(FONT_SIZE_X+1),1*(FONT_SIZE_Y+1),xscale,yscale,bpxWidth,bpxHeight);

			else if (text[z] == 'k' || text[z] == 'K')
				DrawChar(x,y,i, 0,2*(FONT_SIZE_Y+1),xscale,yscale,bpxWidth,bpxHeight);
			else if (text[z] == 'l' || text[z] == 'L')
				DrawChar(x,y,i,1*(FONT_SIZE_X+1),2*(FONT_SIZE_Y+1),xscale,yscale,bpxWidth,bpxHeight);
			else if (text[z] == 'm' || text[z] == 'M')
				DrawChar(x,y,i,2*(FONT_SIZE_X+1),2*(FONT_SIZE_Y+1),xscale,yscale,bpxWidth,bpxHeight);
			else if (text[z] == 'n' || text[z] == 'N')
				DrawChar(x,y,i,3*(FONT_SIZE_X+1),2*(FONT_SIZE_Y+1),xscale,yscale,bpxWidth,bpxHeight);
			else if (text[z] == 'o' || text[z] == 'O')
				DrawChar(x,y,i,4*(FONT_SIZE_X+1),2*(FONT_SIZE_Y+1),xscale,yscale,bpxWidth,bpxHeight);
			else if (text[z] == 'p' || text[z] == 'P')
				DrawChar(x,y,i,5*(FONT_SIZE_X+1),2*(FONT_SIZE_Y+1),xscale,yscale,bpxWidth,bpxHeight);
			else if (text[z] == 'q' || text[z] == 'Q')
				DrawChar(x,y,i,6*(FONT_SIZE_X+1),2*(FONT_SIZE_Y+1),xscale,yscale,bpxWidth,bpxHeight);
			else if (text[z] == 'r' || text[z] == 'R')
				DrawChar(x,y,i,7*(FONT_SIZE_X+1),2*(FONT_SIZE_Y+1),xscale,yscale,bpxWidth,bpxHeight);
			else if (text[z] == 's' || text[z] == 'S')
				DrawChar(x,y,i,8*(FONT_SIZE_X+1),2*(FONT_SIZE_Y+1),xscale,yscale,bpxWidth,bpxHeight);
			else if (text[z] == 't' || text[z] == 'T')
				DrawChar(x,y,i,9*(FONT_SIZE_X+1),2*(FONT_SIZE_Y+1),xscale,yscale,bpxWidth,bpxHeight);

			else if (text[z] == 'u' || text[z] == 'U')
				DrawChar(x,y,i, 0,3*(FONT_SIZE_Y+1),xscale,yscale,bpxWidth,bpxHeight);
			else if (text[z] == 'v' || text[z] == 'V')
				DrawChar(x,y,i,1*(FONT_SIZE_X+1),3*(FONT_SIZE_Y+1),xscale,yscale,bpxWidth,bpxHeight);
			else if (text[z] == 'w' || text[z] == 'W')
				DrawChar(x,y,i,2*(FONT_SIZE_X+1),3*(FONT_SIZE_Y+1),xscale,yscale,bpxWidth,bpxHeight);
			else if (text[z] == 'x' || text[z] == 'X')
				DrawChar(x,y,i,3*(FONT_SIZE_X+1),3*(FONT_SIZE_Y+1),xscale,yscale,bpxWidth,bpxHeight);
			else if (text[z] == 'y' || text[z] == 'Y')
				DrawChar(x,y,i,4*(FONT_SIZE_X+1),3*(FONT_SIZE_Y+1),xscale,yscale,bpxWidth,bpxHeight);
			else if (text[z] == 'z' || text[z] == 'Z')
				DrawChar(x,y,i,5*(FONT_SIZE_X+1),3*(FONT_SIZE_Y+1),xscale,yscale,bpxWidth,bpxHeight);
				
			else if (text[z] == '+')
				DrawChar(x,y,i,0,4*(FONT_SIZE_Y+1),xscale,yscale,bpxWidth,bpxHeight);
			else if (text[z] == '-')
				DrawChar(x,y,i,1*(FONT_SIZE_X+1),4*(FONT_SIZE_Y+1),xscale,yscale,bpxWidth,bpxHeight);
			else if (text[z] == '/')
				DrawChar(x,y,i,2*(FONT_SIZE_X+1),4*(FONT_SIZE_Y+1),xscale,yscale,bpxWidth,bpxHeight);
			else if (text[z] == '*')
				DrawChar(x,y,i,3*(FONT_SIZE_X+1),4*(FONT_SIZE_Y+1),xscale,yscale,bpxWidth,bpxHeight);
			else if (text[z] == ':')
				DrawChar(x,y,i,4*(FONT_SIZE_X+1),4*(FONT_SIZE_Y+1),xscale,yscale,bpxWidth,bpxHeight);
			else if (text[z] == '.')
				DrawChar(x,y,i,5*(FONT_SIZE_X+1),4*(FONT_SIZE_Y+1),xscale,yscale,bpxWidth,bpxHeight);
			else if (text[z] == '<')
				DrawChar(x,y,i,6*(FONT_SIZE_X+1),4*(FONT_SIZE_Y+1),xscale,yscale,bpxWidth,bpxHeight);
			else if (text[z] == '>')
				DrawChar(x,y,i,7*(FONT_SIZE_X+1),4*(FONT_SIZE_Y+1),xscale,yscale,bpxWidth,bpxHeight);
			else if (text[z] == ',')
				DrawChar(x,y,i,8*(FONT_SIZE_X+1),4*(FONT_SIZE_Y+1),xscale,yscale,bpxWidth,bpxHeight);
			else if (text[z] == '|')
				DrawChar(x,y,i,9*(FONT_SIZE_X+1),4*(FONT_SIZE_Y+1),xscale,yscale,bpxWidth,bpxHeight);

			else if (text[z] == '(')
				DrawChar(x,y,i,0,5*(FONT_SIZE_Y+1),xscale,yscale,bpxWidth,bpxHeight);
			else if (text[z] == ')')
				DrawChar(x,y,i,1*(FONT_SIZE_X+1),5*(FONT_SIZE_Y+1),xscale,yscale,bpxWidth,bpxHeight);
			else if (text[z] == '!')
				DrawChar(x,y,i,2*(FONT_SIZE_X+1),5*(FONT_SIZE_Y+1),xscale,yscale,bpxWidth,bpxHeight);
			else if (text[z] == '?')
				DrawChar(x,y,i,3*(FONT_SIZE_X+1),5*(FONT_SIZE_Y+1),xscale,yscale,bpxWidth,bpxHeight);
			else if (text[z] == '"')
				DrawChar(x,y,i,4*(FONT_SIZE_X+1),5*(FONT_SIZE_Y+1),xscale,yscale,bpxWidth,bpxHeight);
			else if (text[z] == '\'')
				DrawChar(x,y,i,5*(FONT_SIZE_X+1),5*(FONT_SIZE_Y+1),xscale,yscale,bpxWidth,bpxHeight);

			if (text[z] == '\n')
			{
				y += FONT_SIZE_Y*yscale + 2;
				i = 0;
			}
			else
				i++;
		}
		}
	}
	
//	glColor3f(1,1,1);
//	glEnable(GL_TEXTURE_2D);
}

void CBitmapFont::DrawChar(float x, float y, int i,int pX, int pY, float xscale, float yscale, float bpxWidth, float bpxHeight)
{
	glTexCoord2f(XPixelToTexCoord(pX+FONT_SIZE_X,bpxWidth),YPixelToTexCoord(pY,fontNode->GetHeight(),bpxHeight));
	glVertex2f(x+((FONT_SPACING*i)+((i+1)*FONT_SIZE_X)*xscale),y);
	glTexCoord2f(XPixelToTexCoord(pX,bpxWidth),YPixelToTexCoord(pY,fontNode->GetHeight(),bpxHeight));
	glVertex2f(x+((FONT_SPACING*i)+((i)*FONT_SIZE_X)*xscale),y);
	glTexCoord2f(XPixelToTexCoord(pX,bpxWidth),YPixelToTexCoord(pY+FONT_SIZE_Y,fontNode->GetHeight(),bpxHeight));
	glVertex2f(x+((FONT_SPACING*i)+((i)*FONT_SIZE_X)*xscale),y+(FONT_SIZE_Y*yscale));

	glTexCoord2f(XPixelToTexCoord(pX+FONT_SIZE_X,bpxWidth),YPixelToTexCoord(pY,fontNode->GetHeight(),bpxHeight));
	glVertex2f(x+((FONT_SPACING*i)+((i+1)*FONT_SIZE_X)*xscale),y);
	glTexCoord2f(XPixelToTexCoord(pX,bpxWidth),YPixelToTexCoord(pY+FONT_SIZE_Y,fontNode->GetHeight(),bpxHeight));
	glVertex2f(x+((FONT_SPACING*i)+((i)*FONT_SIZE_X)*xscale),y+(FONT_SIZE_Y*yscale));
	glTexCoord2f(XPixelToTexCoord(pX+FONT_SIZE_X,bpxWidth),YPixelToTexCoord(pY+FONT_SIZE_Y,fontNode->GetHeight(),bpxHeight));
	glVertex2f(x+((FONT_SPACING*i)+((i+1)*FONT_SIZE_X)*xscale),y+(FONT_SIZE_Y*yscale));

/*
	glTexCoord2f(XPixelToTexCoord(pX+16,bpxWidth),YPixelToTexCoord(pY,fontNode->GetHeight(),bpxHeight));
	glVertex2f(x+((FONT_SPACING*i)+((i+1)*FONT_SIZE_X)*xscale),y);
	glTexCoord2f(XPixelToTexCoord(pX,bpxWidth),YPixelToTexCoord(pY,fontNode->GetHeight(),bpxHeight));
	glVertex2f(x+((FONT_SPACING*i)+((i)*FONT_SIZE_X)*xscale),y);
	glTexCoord2f(XPixelToTexCoord(pX,bpxWidth),YPixelToTexCoord(pY+16,fontNode->GetHeight(),bpxHeight));
	glVertex2f(x+((FONT_SPACING*i)+((i)*FONT_SIZE_X)*xscale),y+(FONT_SIZE_Y*yscale));

	glTexCoord2f(XPixelToTexCoord(pX+16,bpxWidth),YPixelToTexCoord(pY,fontNode->GetHeight(),bpxHeight));
	glVertex2f(x+((FONT_SPACING*i)+((i+1)*FONT_SIZE_X)*xscale),y);
	glTexCoord2f(XPixelToTexCoord(pX,bpxWidth),YPixelToTexCoord(pY+16,fontNode->GetHeight(),bpxHeight));
	glVertex2f(x+((FONT_SPACING*i)+((i)*FONT_SIZE_X)*xscale),y+(FONT_SIZE_Y*yscale));
	glTexCoord2f(XPixelToTexCoord(pX+16,bpxWidth),YPixelToTexCoord(pY+16,fontNode->GetHeight(),bpxHeight));
	glVertex2f(x+((FONT_SPACING*i)+((i+1)*FONT_SIZE_X)*xscale),y+(FONT_SIZE_Y*yscale));
*/
}
