/*
*	Draw GL.cpp
*	----------------------------------------
*	A part of "Intelligent AI"
*	© 2001 SCC Team 064
*	----------------------------------------
*
*	This file:
*		€ Handles the various functions for drawing the sceene as directed by the 
*		GL World file and other window assosiated functions.
*/


#include "MacInput.h"
#include "Project.h"


GLfloat 			vertices [822][3];
GLint 				indices [956][4];

#pragma mark -
//-------------------------------------------------------------------------------------------------
// OpenGL Drawing
void DrawGLScene(WindowPtr pWindow, AGLContext aglContext, GLuint fontList)
{
	glClearColor(0.15f, 0.15f, 0.15f, 1.0f);		// Clear color buffer to dark grey
	glClear(GL_COLOR_BUFFER_BIT);

	glEnableClientState (GL_VERTEX_ARRAY);
	glVertexPointer (3, GL_FLOAT, 0, &vertices);

	for (int i = 0; i < getCurrentNumPlayers(); i++)
	{
		glMatrixMode(GL_MODELVIEW);	
		glPushMatrix();
		glLoadIdentity();							// Reset The Projection Matrix

		glColor3d(.5, .3, player[i].whichPlayer);

		if (player[i].flashColor)
			glColor3d(0., 1., 1.);

		glTranslatef(-player[i].xPos *.5, -player[i].zPos *.5, player[i].yPos*.5);
		glRotatef(player[i].yaw * RAD_TO_DEG, 0.0, 1.0, 0.0);
		glRotatef(player[i].pitch * RAD_TO_DEG, 0.0, 0.0, 1.0);
		glRotatef(player[i].roll * RAD_TO_DEG, 1.0, 0.0, 0.0);

		for (int loop = 0; loop < iNumVert; loop++)
		{
			if( indices[loop][3] == 0)
			{
				glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, indices[loop]);
			}
			else
			{
				glDrawElements(GL_QUADS, 4, GL_UNSIGNED_INT, indices[loop]);
			}
		}

		glFlush();
		glPopMatrix();
		glPopAttrib();
	}

	glDisableClientState (GL_VERTEX_ARRAY);


//Draws objects, text etc., in the Window
	glMatrixMode(GL_PROJECTION);	
	glLoadIdentity();

	// Draw frame rate (set color and position first)
	glColor3d(1.0, 1.0, 1.0);
	glRasterPos3d (-0.95, ((pWindow->portRect.bottom - pWindow->portRect.top) - 40.0) / 
		(float) (pWindow->portRect.bottom - pWindow->portRect.top), 0.0); 
	DrawFrameRate (fontList);

	glRasterPos3d (-0.95, ((pWindow->portRect.bottom - pWindow->portRect.top) - 40.0) / 
		(float) (pWindow->portRect.bottom - pWindow->portRect.top), 0.0); 
	DrawMessages (fontList);

	//Draw the Window Resolution
	glRasterPos3d (-0.95, ((pWindow->portRect.bottom - pWindow->portRect.top) - 65.0) / 
		(float) (pWindow->portRect.bottom - pWindow->portRect.top), 0.0); 
	DrawCStringGL (gstrContext, fontList);

	aglSwapBuffers(aglContext);										// send swap command
}


#pragma mark -
//-------------------------------------------------------------------------------------------------
void DrawCStringGL (char * cstrOut, GLuint fontList)
{
	GLint i = 0;
	while (cstrOut [i])
		glCallList (fontList + cstrOut[i++]);
}