/****************************/
/*		OPENGL.C			*/
/* (c)2004 Pangea Software  */
/* By Brian Greenstone      */
/****************************/


/****************************/
/*    PROTOTYPES            */
/****************************/

static void DrawSceneGeometry(void);


/**************************/
/*    EXTERNS             */
/**************************/

extern	Boolean						gPlayInWindow;
extern	WindowPtr					gGameWindow;



/****************************/
/*    VARIABLES             */
/****************************/

AGLDrawable			gAGLWin;
AGLContext			gAGLContext = nil;

GLuint				gFontList;
static				gFrameCounter = 0;


/****************** CREATE OPENGL DRAW CONTEXT *********************/

void OGL_CreateDrawContext(void)
{
GDHandle 		gdevice;
AGLPixelFormat 	fmt;
GLint          attribWindow[]	= {AGL_RGBA, AGL_DOUBLEBUFFER, AGL_DEPTH_SIZE, 32, AGL_ALL_RENDERERS, AGL_ACCELERATED, AGL_NO_RECOVERY, AGL_NONE};
GLint          attribFullScreen[] = {AGL_RGBA, AGL_FULLSCREEN, AGL_DOUBLEBUFFER, AGL_DEPTH_SIZE, 32, AGL_ALL_RENDERERS, AGL_ACCELERATED, AGL_NO_RECOVERY, AGL_NONE};

	gdevice = GetMainDevice();						// get the main display

			/***********************/
			/* CHOOSE PIXEL FORMAT */
			/***********************/

			/* PLAY IN WINDOW */
												
	if (gPlayInWindow)
		fmt = aglChoosePixelFormat(&gdevice, 1, attribWindow);
		
			/* FULL-SCREEN */
	else
		fmt = aglChoosePixelFormat(&gdevice, 1, attribFullScreen);

	if ((fmt == nil) || (aglGetError() != AGL_NO_ERROR))
		DoFatalAlert("\pCannot make a draw context!");

			/**********************/
			/* CREATE AGL CONTEXT */
			/**********************/
			
	gAGLContext = aglCreateContext(fmt, nil);
	if ((gAGLContext == nil) || (aglGetError() != AGL_NO_ERROR))
		DoFatalAlert("\paglCreateContext failed!");
		
		
			/* PLAYING IN A WINDOW */
				
	if (gPlayInWindow)
	{
		gAGLWin = (AGLDrawable)GetWindowPort(gGameWindow);
		aglSetDrawable(gAGLContext, gAGLWin);
	}
	
			/* PLAYING FULL-SCREEN */
	else
	{
		gAGLWin = nil;
		aglEnable(gAGLContext, AGL_FS_CAPTURE_SINGLE);		
		aglSetFullScreen(gAGLContext, 0, 0, 0, 0);	
	}	

			/* ACTIVATE THE CONTEXT */

	aglSetCurrentContext(gAGLContext);


			/* NO LONGER NEED PIXEL FORMAT */
			
	aglDestroyPixelFormat(fmt);

}

#pragma mark -

/******************* DRAW OPENGL SCENE *********************/
//
// Called from the main loop once each frame to draw the scene.
//

void OGL_DrawScene(void)
{
			/* MAKE OUR CONTEXT THE ACTIVE ONE */
			
  	aglSetCurrentContext(gAGLContext);
	
			/* CLEAR THE DRAW BUFFER & Z-BUFFER */
				
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
	
		
				/* DO OUR GEOMETRY DRAWING HERE */
				
	DrawSceneGeometry();
				

			/* END RENDER & SWAP THE BUFFER TO MAKE IT VISIBLE */
           		
	aglSwapBuffers(gAGLContext);	
}



/***************** DRAW SCENE GEOMETRY *******************/
//
// Called from DrawOpenGLScene() when it's time to submit our
// geometry data to OpenGL.
//

static void DrawSceneGeometry(void)
{
static float	x = 0;
static float	dx = .5f;

			/* DRAW A FRAME COUNTER IN THE UPPER-LEFT CORNER */
			
	OGL_DrawString("\pframe #:", 20, 20);
	OGL_DrawInt(gFrameCounter++, 70, 20);
	
			
			/* DRAW MESSAGE IN OPPOSITE CORNER */
			
	OGL_DrawString("\pClick the mouse to Exit", 500, 440);
		
		
			/* DRAW A GREEN SQUARE */
		
	glColor3f(0,1,0);
	
	glBegin(GL_QUADS);
	glVertex2f(x, 200);
	glVertex2f(x+100, 200);
	glVertex2f(x+100, 300);
	glVertex2f(x, 300);
	glEnd();
	
			/* MOVE X FOR NEXT FRAME */
			
	x += dx;
	if ((x > 640.0f) || (x < 0.0f))
		dx = -dx;
}


#pragma mark -

/****************** INIT OPENGL FONT ***********************/
//
// Initializes a font list that we can use for drawing text
// into a draw context for simple debug information.
//

void OGL_InitFont(void)
{	
	GLboolean	success;

	gFontList = glGenLists(256);
 
 	success = aglUseFont(gAGLContext,		// our draw context 
						kFontIDMonaco,		// font to use
						bold,				// style of font
						9,					// point size of font
						0,					// first character
						256,				// # characters
						gFontList);			// list to store font in


	if (!success)
		DoFatalAlert("\paglUseFont failed");
}


/******************* DRAW OPENGL STRING ***********************/
//
// Draws a pascal string to the 2D window coordinates specified by x,y.
//
// INPUT:	s = pascal string
//			x/y = screen coords (based on 640x480)
//

void OGL_DrawString(Str255 s, GLint x, GLint y)
{
			/* SET THE DRAWING MATRICES */

	glMatrixMode (GL_MODELVIEW);
	glLoadIdentity();
	glMatrixMode (GL_PROJECTION);
	glLoadIdentity();
	glOrtho(0, 640, 0, 480, -10.0, 10.0);

	
			/* SET SOME STATE VALUES */

	glDisable(GL_LIGHTING);
	glDisable(GL_TEXTURE_2D);	
	glColor4f (1,1,1,1);


		/* SET COORDINATE TO DRAW STRING */

	glRasterPos2i(x, 480-y);


			/* DRAW THE STRING */

	glListBase(gFontList);
	glCallLists(s[0], GL_UNSIGNED_BYTE, &s[1]);
}


/**************** DRAW INT WITH OPENGL FONT ********************/

void OGL_DrawInt(int f, GLint x, GLint y)
{
Str255	s;

	NumToString(f,s);
	OGL_DrawString(s,x,y);

}












