#include "stdafx.h"
#include "basic.h"
#include "Eroc.h"
#include "OGLState.h"
#include "Textures.h"

#ifdef import_globals
#undef import_globals
#endif
#include "globals.h"

// Font
GLuint	gList;								// Storage for font display list
GLuint  gFontBase;
SDL_Surface* gSDLFont;
int gFontTexture = 0;

/////////////////////////////////////////////////////////////////////////////

GLvoid BuildFont(GLvoid)							// Build Our Font Display List
{
	OGLState::PushTexture2D(true);

	float	cx;								// Holds Our X Character Coord
	float	cy;								// Holds Our Y Character Coord

	// load font if not done yet
	if (!gFontTexture)
		gFontTexture = CreateTextureFromFile("data/font.bmp", false);
	Texture* theTex = GetTexture(gFontTexture);

	gFontBase=glGenLists(256);						// Creating 256 Display Lists
	OGLState::PushTextureUsed(theTex->mOpenGLTex); 	// Select Our Font Texture

	for (int loop=0; loop<256; loop++)				// Loop Through All 256 Lists
	{
		cx=float(loop%16)/16.0f;					// X Position Of Current Character
		cy=float(loop/16)/16.0f;					// Y Position Of Current Character

		glNewList(gFontBase+loop,GL_COMPILE);		// Start Building A List

		glBegin(GL_QUADS);						// Use A Quad For Each Character
			glTexCoord2f(cx,cy+0.0625f);		// Texture Coord (Bottom Left)
			glVertex2i(0,0);					// Vertex Coord (Bottom Left)
			glTexCoord2f(cx+0.0625f,cy+0.0625f);	// Texture Coord (Bottom Right)
			glVertex2i(16,0);					// Vertex Coord (Bottom Right)
			glTexCoord2f(cx+0.0625f,cy);		// Texture Coord (Top Right)
			glVertex2i(16,16);					// Vertex Coord (Top Right)
			glTexCoord2f(cx,cy);				// Texture Coord (Top Left)
			glVertex2i(0,16);					// Vertex Coord (Top Left)
		glEnd();
		glTranslated(13,0,0);					// Move To The Right Of The Character
		glEndList();							// Done Building The Display List
	}									// Loop Until All 256 Are Built

	OGLState::PopTextureUsed();
	OGLState::PopTexture2D();
}

GLvoid KillFont(GLvoid)								// Delete The Font From Memory
{
	glDeleteLists(gFontBase,256);					// Delete All 256 Display Lists
	// Texture is deleted with the rest
}

GLvoid glPrint(GLint x, GLint y, char *string, int set)		// Where The Printing Happens
{
	OGLState::PushTexture2D(true);
	OGLState::PushDepth(false);

	if (set>1)									// Is set Greater Than One?
		set=1;									// If So, Make Set Equal One
	
	Texture* theTex = GetTexture(gFontTexture);
	OGLState::PushTextureUsed(theTex->mOpenGLTex);// Select Our Font Texture
	glColor4f ( 1, 1, 1, 1 ); // Set color

	glMatrixMode(GL_PROJECTION);				// Select The Projection Matrix
	glPushMatrix();								// Store The Projection Matrix

	glLoadIdentity();							// Reset The Projection Matrix
	glOrtho(0,eScreenX,0,eScreenY,-1,1);		// Set Up An Ortho Screen

	glMatrixMode(GL_MODELVIEW);					// Select The Modelview Matrix
	glPushMatrix();								// Store The Modelview Matrix
	glLoadIdentity();							// Reset The Modelview Matrix

	glTranslated(x,y,-0.5);						// Position The Text (0,0 - Bottom Left)
	glListBase(gFontBase-32+(128*set));			// Choose The Font Set (0 or 1)

	glCallLists(strlen(string),GL_UNSIGNED_BYTE,string); // Write The Text To The Screen

	glMatrixMode(GL_PROJECTION);				// Select The Projection Matrix
	glPopMatrix();								// Restore The Old Projection Matrix

	glMatrixMode(GL_MODELVIEW);					// Select The Modelview Matrix
	glPopMatrix();								// Restore The Old Projection Matrix

	OGLState::PopTextureUsed();
	OGLState::PopDepth();
	OGLState::PopTexture2D();
}