/****************************/
/*      MISC ROUTINES       */
/* (c)2004 Pangea Software  */
/* By Brian Greenstone      */
/****************************/


/**********************/
/*     CONSTANTS      */
/**********************/

#define		MIN_FPS		10.0			// minimum fps that our physics can handle
#define		MAX_FPS		1000.0			// max fps that our physics can handle (we set this high here just for demo purposes - in a real game limit this to like 120)



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

Boolean			gAltiVec;

float			gFramesPerSecond;
float			gOneOverFramesPerSecond;

FSRef		gMyResourcesFolderFSRef;
FSSpec		gMyResourcesFolderFSSpec;


/*********************** DO FATAL ALERT *******************/
//
// Show an alert dialog with the input string, and then quit the app.
//

void DoFatalAlert(Str255 s)
{
SInt16      alertItemHit;

	Enter2D();					// make sure we can see this
	
	StandardAlert(kAlertNoteAlert, s, NULL, NULL, &alertItemHit);

	ExitToShell();
}


/*********************** DO ALERT *******************/
//
// Show an alert dialog with the input string, and then continue on
//

void DoAlert(Str255 s)
{
SInt16      alertItemHit;

	Enter2D();					// make sure we can see this
	
	StandardAlert(kAlertNoteAlert, s, NULL, NULL, &alertItemHit);

	Exit2D();
}

/**************** CALC FRAMES PER SECOND **********************/
//
// Called once per frame to calculate our FPS values.
//

void CalcFramesPerSecond(void)
{
	AbsoluteTime 			currTime,deltaTime;
	static AbsoluteTime 	time = {0,0};
	Nanoseconds			nano;

loop:
			/* GET CURRENT TIMER */

	currTime = UpTime();


		/* GET DELTA FROM PREVIOUS TIME */

	deltaTime 	= SubAbsoluteFromAbsolute(currTime, time);
	nano 		= AbsoluteToNanoseconds(deltaTime);


		/* CONVERT NANOSECONDS TO SECONDS */

	gFramesPerSecond = (float)kSecondScale / (float)nano.lo;


		/* MAKE SURE WE DONÕT GO UNDER THE MINIMUM */

	if (gFramesPerSecond < MIN_FPS)			
		gFramesPerSecond = MIN_FPS;


		/* MAKE SURE WE DONÕT GO OVER THE MAXIMUM */

	if (gFramesPerSecond > MAX_FPS)			
		goto loop;
		

			/* CALC 1.0 / FPS */

	gOneOverFramesPerSecond = 1.0f/gFramesPerSecond;		

	time 		= currTime;			// reset for next time interval
}


/****************** GET MY APPLICATION RESOURCES FOLDER ******************/
//
// Called InitApplication() to find a FSSpec and FSRef to our Resources folder.
//

void GetMyApplicationResourcesFolder(void)
{
	OSErr			iErr;
	CFBundleRef     appBundle;
	CFURLRef        appResourcesURL;

			/* GET REFERENCES TO APPLICATIONÕS BUNDLE */

	appBundle = CFBundleGetMainBundle();


			/* GET THE URL OF THE RESOURCES FOLDER */

	appResourcesURL = CFBundleCopyResourcesDirectoryURL(appBundle);
	if (appResourcesURL == nil)
		DoFatalAlert("\pYou don't seem to have a Resources folder");


			/* CONVERT THE URL TO AN FSREF */

	CFURLGetFSRef(appResourcesURL, &gMyResourcesFolderFSRef);


		/* ALSO GET THE FSSPEC WHILE WEÕRE HERE */
		//
		// FSGetCatalogInfo() will return a FSSpec for the Resources folder,
		// but the volume and directory ID's are for the parent folder, not the
		// actual Resources folder.  So, we do a trick by calling FSMakeFSSpec()
		// and we pass in the parent volume and directory ID along with a hardcoded
		// path to a file that we know is in our Resources folder:  Game.nib.
		// The result is a new FSSpec with volume and directory ID's that point
		// directly to the Resources folder.
		//

	FSGetCatalogInfo(&gMyResourcesFolderFSRef,
					kFSCatInfoNone,				// no catinfo needed
					nil, 
					nil,
					&gMyResourcesFolderFSSpec,	// fsspec to save into
					nil);	


	iErr = FSMakeFSSpec(gMyResourcesFolderFSSpec.vRefNum,	// parent volume/directory info
						gMyResourcesFolderFSSpec.parID,
						"\p:Resources:Game.nib",			// path from parent to resource file
						&gMyResourcesFolderFSSpec);

	if (iErr != noErr)
		DoFatalAlert("\perror converting FSRef to FSSpec");

	gMyResourcesFolderFSSpec.name[0] = 0;					// clear the name out of the FSSpec

}






