// Font.h: interface for the Font class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_FONT_H__5132331E_7269_413A_B26A_D1E9A5109E80__INCLUDED_)
#define AFX_FONT_H__5132331E_7269_413A_B26A_D1E9A5109E80__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <gl\gl.h>			// Header File For The OpenGL32 Library
#include <gl\glu.h>			// Header File For The GLu32 Library
#include <gl\glaux.h>		// Header File For The Glaux Library
#include <gl\glut.h>
#include "Dictionary.h"
#include "Square.h"

HDC			hDC=NULL;		// Private GDI Device Context
HGLRC		hRC=NULL;		// Permanent Rendering Context
HWND		hWnd=NULL;		// Holds Our Window Handle
HINSTANCE	hInstance;		// Holds The Instance Of The Application

GLuint	base;				// Base Display List For The Font Set
GLfloat	cnt1;				// 1st Counter Used To Move Text & For Coloring
GLfloat	cnt2;				// 2nd Counter Used To Move Text & For Coloring

bool	keys[256];			// Array Used For The Keyboard Routine
int score = 0;
int longest = 0;

GLfloat deep = 0.0f;
GLfloat rot = 0.0f;

GLfloat LightAmbient[]=		{ 0.5f, 0.5f, 0.5f, 1.0f };
GLfloat LightDiffuse[]=		{ 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat LightPosition[]=	{ 0.0f, 0.0f, 2.0f, 1.0f };

GLuint	texture[30];			// Storage For 6 Textures (MODIFIED)

Dictionary dico;
Square mySquare[14];
	
GLvoid BuildFont(GLvoid)								// Build Our Bitmap Font
{
	HFONT	font;										// Windows Font ID
	HFONT	oldfont;									// Used For Good House Keeping

	base = glGenLists(96);								// Storage For 96 Characters

	font = CreateFont(	-24,							// Height Of Font
						0,								// Width Of Font
						0,								// Angle Of Escapement
						0,								// Orientation Angle
						FW_BOLD,						// Font Weight
						FALSE,							// Italic
						FALSE,							// Underline
						FALSE,							// Strikeout
						ANSI_CHARSET,					// Character Set Identifier
						OUT_TT_PRECIS,					// Output Precision
						CLIP_DEFAULT_PRECIS,			// Clipping Precision
						ANTIALIASED_QUALITY,			// Output Quality
						FF_DONTCARE|DEFAULT_PITCH,		// Family And Pitch
						"Courier New");					// Font Name

	oldfont = (HFONT)SelectObject(hDC, font);           // Selects The Font We Want
	wglUseFontBitmaps(hDC, 32, 96, base);				// Builds 96 Characters Starting At Character 32
	SelectObject(hDC, oldfont);							// Selects The Font We Want
	DeleteObject(font);									// Delete The Font
}

GLvoid KillFont(GLvoid)									// Delete The Font List
{
	glDeleteLists(base, 96);							// Delete All 96 Characters
}

GLvoid glPrint(const char *fmt, ...)					// Custom GL "Print" Routine
{
	char		text[256];								// Holds Our String
	va_list		ap;										// Pointer To List Of Arguments

	if (fmt == NULL)									// If There's No Text
		return;											// Do Nothing

	va_start(ap, fmt);									// Parses The String For Variables
	    vsprintf(text, fmt, ap);						// And Converts Symbols To Actual Numbers
	va_end(ap);											// Results Are Stored In Text

	glPushAttrib(GL_LIST_BIT);							// Pushes The Display List Bits
	glListBase(base - 32);								// Sets The Base Character to 32
	glCallLists(strlen(text), GL_UNSIGNED_BYTE, text);	// Draws The Display List Text
	glPopAttrib();										// Pops The Display List Bits
}




int charToInt(char c)
{
	if(c == '(') return 27;
	else if(c == ')') return 28;
	else if(c == ' ') return 26;
	else  return c -97;
}

AUX_RGBImageRec *LoadBMP(const char *Filename)				// Loads A Bitmap Image
{
	FILE *File=NULL;									// File Handle

	if (!Filename)										// Make Sure A Filename Was Given
	{
		return NULL;									// If Not Return NULL
	}

	File=fopen(Filename,"r");							// Check To See If The File Exists

	if (File)											// Does The File Exist?
	{
		fclose(File);									// Close The Handle
		return auxDIBImageLoad(Filename);				// Load The Bitmap And Return A Pointer
	}

	return NULL;										// If Load Failed Return NULL
}

int LoadGLTextures()									// Load Bitmaps And Convert To Textures
{
	int Status=FALSE;									// Status Indicator

	AUX_RGBImageRec *TextureImage[30];					// Create Storage Space For The Texture

	memset(TextureImage,0,sizeof(void *)*30);           	// Set The Pointer To NULL

	// Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
	bool loaded = true;
	char charName = 'a';
	string charString = "a";
	
	string imageName ="";
    TextureImage[26]=LoadBMP("images/empty.bmp");
	TextureImage[27]=LoadBMP("images/skip1.bmp");
	TextureImage[28]=LoadBMP("images/skip2.bmp");
	TextureImage[29]=LoadBMP("images/back.bmp");
	if(!TextureImage[26] || !TextureImage[27] || !TextureImage[28] || !TextureImage[29])
	{
		loaded = false;	
		MessageBox(NULL,"not 26","ERROR",MB_OK|MB_ICONEXCLAMATION);
	}
	for(int i=0;i<26;i++)
	{
		imageName = "images/"+charString+".bmp";
		TextureImage[i]=LoadBMP(imageName.c_str());
		if (TextureImage[i])
		{
			loaded = loaded && true;
		}
		else
		{
			loaded = false;
			MessageBox(NULL,imageName.c_str(),"ERROR",MB_OK|MB_ICONEXCLAMATION);
		}
		charString[0]=++charName;
	}

	if (loaded)
	{
		Status=TRUE;									// Set The Status To TRUE

		glGenTextures(30, &texture[0]);					// Create Three Textures

		for (int loop=0; loop<=29; loop++)
		{

			// Create Linear Filtered Texture
			glBindTexture(GL_TEXTURE_2D, texture[loop]);	// Gen Tex 2 and 3 4
			glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
			glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
			glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[loop]->sizeX, TextureImage[loop]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[loop]->data);
	
	  }
		for (loop=0; loop<=29; loop++)
		{
	        if (TextureImage[loop])							// If Texture Exists
		    {
			        if (TextureImage[loop]->data)			// If Texture Image Exists
				    {
					        free(TextureImage[loop]->data);	// Free The Texture Image Memory
					}
					free(TextureImage[loop]);				// Free The Image Structure
			}
		}
	}
	return Status;										// Return The Status
}


void PrintMouseCoords(HWND hwnd, int xPos, int yPos)
{
	HDC hdc = GetDC(hwnd); // Get the windows device context

		// Error Check
		if(!hdc)
			return; // Can't fill the window without an HDC, we'll just return

	RECT rect;
	GetClientRect(hwnd,&rect); // Get the window's client area RECT

	// Clear the window (the client area of the window) to all white
	FillRect(hdc,&rect,(HBRUSH)GetStockObject(WHITE_BRUSH));

	char buffX[128] = {0}; // char buffer for the X position
	char buffY[128] = {0}; // char buffer for the Y position

	// Fill both buffers with the apporopiate text
	sprintf(buffX,"X Pos = %d",xPos);
	sprintf(buffY,"Y Pos = %d",yPos);

	int centerX = rect.right / 2;
	int centerY = rect.bottom / 2;

	TEXTMETRIC textmetric;

	GetTextMetrics(hdc,&textmetric); 
	int start = centerX - ((strlen(buffX) / 2) * textmetric.tmAveCharWidth);

	TextOut(hdc, start, centerY - 25, buffX, strlen(buffX));

	start = centerX - ((strlen(buffY) / 2) * textmetric.tmAveCharWidth);
	

	TextOut(hdc, start, centerY + 25, buffY, strlen(buffY));

	ReleaseDC(hwnd,hdc); // Be sure to always free up your HDC

}


GLvoid start()
{

	dico.shuffle();
    for(int i=0;i<13;i++) mySquare[i].clear();

	for(i=0;i<dico.front.size();i++)
		mySquare[i].front = dico.front[i];
	
	for(i=0;i<dico.top.size();i++)
		mySquare[i].top = dico.top[i];

	for(i=0;i<dico.back.size();i++)
		mySquare[i].back = dico.back[i];

	for(i=0;i<dico.bottom.size();i++)
		mySquare[i].bottom = dico.bottom[i];

	int random = 0;
	for(i=0;i<13;i++)
	{
		random = rand()%4;
		if(random == 0) mySquare[i].rot = 0;
		if(random == 1) mySquare[i].rot = 90;
		if(random == 2) mySquare[i].rot = 180;
		if(random == 3) mySquare[i].rot = 270;

	}

    mySquare[13].front = '(';
	mySquare[13].top = ')';
	mySquare[13].back = '(';
	mySquare[13].bottom = ')';


}

GLint getSquare(int x,int y)
{
	GLint ret = -1;
	if(y>=0 && y <=320)
	{
		GLint lx=9;
		for(int i=0;i<13;i++)
		{
			if(x>= lx && x<=(lx+44))
			{
				ret = i;
			}
			lx+=48;
		}

	}
	return ret;
}
bool countPoints()
{
	int rot = mySquare[0].rot;
	bool done = true;
	for(int i=0;i<13;i++)
	{
		if(! mySquare[i].empty())
		{
			++longest;
			done = done && (mySquare[i].rot == rot);
			rot = mySquare[i].rot;
		}

	}
	if(done) 
	{
		score += (longest*10);
		longest = 0;
	}
	return done;
}
#endif // !defined(AFX_FONT_H__5132331E_7269_413A_B26A_D1E9A5109E80__INCLUDED_)
