#include "text.h"
#include "windowing.h"

extern rWindowing *w;
extern rRenderer *g_Renderer;
extern long curFrame;


rOGLText::rOGLText()
{
	fchars = glGenLists(numChars);
	hFont = CreateFont(	0,
						0,
						0,
						0,
						FW_BOLD,
						0,					// Italic
						0,					// Underline
						0,					// Strikeout
						ANSI_CHARSET,
						OUT_TT_PRECIS,
						CLIP_DEFAULT_PRECIS,
						ANTIALIASED_QUALITY,
						FF_DONTCARE|DEFAULT_PITCH,
						"Verdana");	

	HDC h = *(w->getHDC());
	SelectObject(h, hFont);
	if(!wglUseFontBitmaps(h, 32, numChars, fchars))
	{
		g_Logger.Log("Fonts could not be created");
		g_Renderer->bRenderText = false;
	}
}


rOGLText::~rOGLText()
{
	glDeleteLists(fchars, numChars);
}

void rOGLText::AttachText(lText LT)
{
	g_Renderer->bRenderText = 1;
	texts.push_back(LT);
}

bool rOGLText::Draw()
{
	if(texts.empty())
		return false;

	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
	g_Renderer->OrthoMode();
	glPushAttrib(GL_TRANSFORM_BIT | GL_VIEWPORT_BIT | GL_LIST_BIT);
	glListBase(fchars-32);

	list<lText>::iterator lt = texts.begin();
	while(lt != texts.end())
	{
		if(lt->endTime < curFrame)
		{
			if(lt == texts.end())
			{
				texts.erase(lt);
				break;
			}else
			{
				list<lText>::iterator temp = lt;
				lt++;
                texts.erase(temp);
			}
		}
		else
		{		
			
			glColor3f(lt->color.x, lt->color.y, lt->color.z);	
			glRasterPos2i(lt->x,lt->y);
			glCallLists((int)strlen(lt->text), GL_UNSIGNED_BYTE, lt->text);
			lt++;
		}
	}
	glColor3f(1.0f, 1.0f, 1.0f);
	glPopAttrib();
	glPopMatrix();
	glMatrixMode(GL_MODELVIEW);
	return true;
}
