/*
*	DS World.cpp
*	----------------------------------------
*	A part of "Intelligent AI"
*	© 2001 SCC Team 064
*	----------------------------------------
*
*	This file:
*		€ Initilizes the Draw Sprocket window and directs the actions assosiated
*		with the window.
*/


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


bool					Left, Right, Up, Down;

/*Menu Definitions --------------------------------------------- */
enum
{
	kForegroundSleep = 0,
	kBackgroundSleep = 10
};

//const RGBColor	rgbWhite	= { 0xFFFF, 0xFFFF, 0xFFFF };
SInt32 			gSleepTime = kForegroundSleep;
Boolean 			gfFrontProcess = true;

structGLWindowInfo 	glInfo;
GLuint 					gFontList = 0;

Boolean 			gQuit = false, pDone = false;
WindowPtr 		gpWindow = NULL;
AGLContext 	aglContext = 0;
char 			gstrContext [256] = "";

Rect 			rectWin = {50, 20, 650, 820};		//top, left, bottom, right


/* Functions (Internal/Private) --------------------------------------------- */
void InitToolbox(void)
{
	MenuHandle menu;

	MaxApplZone ();

	InitGraf((Ptr) &qd.thePort);
	InitFonts();
	InitWindows();
	InitMenus();
	TEInit();
	InitDialogs(nil);
	InitCursor();

	qd.randSeed =  TickCount();
}


// --------------------------------------------------------------------------
Boolean SetUpWindow (void)
{
	short i;
	short fNum;

	InitToolbox ();

	if (PreflightGL (false))
	{
		memset(&glInfo, 0, sizeof glInfo);
		glInfo.fDraggable = true; 		// desired vertical refresh frquency in Hz (0 = any)
		glInfo.fmt = 0;					// output pixel format

		i = 0;
		glInfo.aglAttributes [i++] = AGL_RGBA;
		glInfo.aglAttributes [i++] = AGL_DOUBLEBUFFER;
//		glInfo.aglAttributes [i++] = AGL_ACCELERATED; // can't use this for dragable windows
		glInfo.aglAttributes [i++] = AGL_DEPTH_SIZE;
		glInfo.aglAttributes [i++] = 16;
		glInfo.aglAttributes [i++] = AGL_NONE;
		gpWindow = (WindowPtr) NewCWindow (NULL, &rectWin, "\pPreditor AI", true, 
			kWindowFloatGrowProc, (WindowPtr)-1, 0, 0);
		BuildGLFromWindow ((CGrafPtr) gpWindow, &aglContext, &glInfo);
		if (!aglContext)
		{
			DestroyGLFromWindow (&aglContext, &glInfo);
			sprintf (gstrContext, "No context");			
		}
		else// set mode string
			sprintf (gstrContext, "%d x %d", gpWindow->portRect.right - gpWindow->portRect.left, 
				gpWindow->portRect.bottom - gpWindow->portRect.top);			
	}

// Build Font
	GetFNum("\pGeneva", &fNum);
	if (aglContext)
		gFontList = BuildFontGL (aglContext, fNum, normal, 9);
	return true;
}


// --------------------------------------------------------------------------
void DoMenu (SInt32 menuResult)
{
	SInt16 theMenu;
	SInt16 theItem;
	Str255 daName;
	MenuRef theMenuHandle;

	theMenu = HiWord(menuResult);
	theItem = LoWord(menuResult);
	theMenuHandle = GetMenuHandle(theMenu);

	switch (theMenu)
	{
		case kMenuApple:
			switch (theItem)
			{
				case kAppleAbout:
					break;
				default:
					GetMenuItemText (theMenuHandle, theItem, daName);
					OpenDeskAcc(daName);
					break;
			}
			break;
		case kMenuFile:
			switch (theItem)
			{
				case kFileQuit:
					pDone = true;
					break;
			}
			break;
	}
	HiliteMenu(0);
	DrawMenuBar();
}

// --------------------------------------------------------------------------
void EventDSLoop (void)
{
	EventRecord			event;
	unsigned char		theKeyMap[16];
	char go;

	// Get the next event in the event que (could use WaitNextEvent(), but you have to wait, so
	// we'll use GetNextEvent() which is faster)
	if ( GetNextEvent( everyEvent, &event ) )
		DoDSEvent( &event );
		
	GetKeys( ( unsigned long * )theKeyMap );

	
	// Handle some keyboard input here
	if ( IsKeyDown( theKeyMap, MAC_ESCAPE_KEY ) )
		gQuit = true;

	if ( IsKeyDown( theKeyMap, MAC_P_KEY ) )
		cin >> go;

	if ( IsKeyDown( theKeyMap, MAC_W_KEY ) )
		Up = true;
	else
		Up = false;
	if ( IsKeyDown( theKeyMap, MAC_S_KEY ) )
		Down = true;
	else
		Down = false;
	if ( IsKeyDown( theKeyMap, MAC_A_KEY ) )
		Left = true;
	else
		Left = false;
	if ( IsKeyDown( theKeyMap, MAC_D_KEY ) )
		Right = true;
	else
		Right = false;

	if ( IsKeyDown( theKeyMap, MAC_ARROW_UP_KEY ) )
		movingForward = true;
	if ( IsKeyDown( theKeyMap, MAC_ARROW_DOWN_KEY ) )
		movingBackward = true;
	if ( IsKeyDown( theKeyMap, MAC_ARROW_RIGHT_KEY ) )
	{
		movingRight = true;
		movingLeft = false;
	}
	else
	{
		movingRight = false;
	}
	if ( IsKeyDown( theKeyMap, MAC_ARROW_LEFT_KEY ) )
	{
		movingLeft = true;
		movingRight = false;
	}
	else
	{
		movingLeft = false;
	}

}

// --------------------------------------------------------------------------
void DoDSEvent( EventRecord *event )
{
	WindowRef whichWindow;
	GrafPtr pGrafSave;
//	Boolean fProcessed = false;
	SInt8		theKey;

	switch ( event->what )
	{
		case mouseDown:
			HandleMouseDown( event );
		case keyDown:
		case autoKey:
			HandleKeyDown( event );
			theKey = event->message & charCodeMask;
			switch (theKey)
			{
				case 'r':
				case 'R':
					cRotate = !cRotate;
					sideways = !sideways;
					break;
				case 'x':
					cameraZ -= 10;
					break;
				case 'z':
					cameraZ += 10;
					break;
			}		
			break;
		case updateEvt:
			whichWindow = (WindowRef) event->message;
			GetPort (&pGrafSave);
			SetPort ((GrafPtr) whichWindow);
			BeginUpdate(whichWindow);
			UpdateWorld(whichWindow);
			SetPort ((GrafPtr) whichWindow);
			EndUpdate(whichWindow);
			SetPort (pGrafSave);
			break;
		case kHighLevelEvent:
			AEProcessAppleEvent(event);
			break;
	}
}
