#include "ofAppRunner.h"

//========================================================================
// static variables:
static ofSimpleApp * 		OFSAptr;
int							windowMode;
bool 						bMousePressed;
bool						bRightButton;
int							width, height;
static float 				ofFrameRate;
//========================================================================
// callbacks:
#include "ofAppGlutGlue.h"	


//--------------------------------------
void ofSetupOpenGL(int w, int h, int screenMode){
	int argc = 1;
	char *argv = "openframeworks";
	char **vptr = &argv;
	glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_ALPHA);
	glutInit(&argc, vptr);
	//if (screenMode == OF_WINDOW)		
	glutInitWindowSize(w, h);
	glutCreateWindow("");
	windowMode = screenMode;
	

	ofBackground(200,200,200);		// default bg color
	ofSetColor(0xFFFFFF); 			// default draw color
									// used to be black, but 
									// black + texture = black
									// so maybe grey bg
									// and "white" fg color
									// as default works the best...

	
							
}

//----------------------- 	gets called when the app exits
// 							currently looking at who to turn off
//							at the end of the application

void ofExitCallback();
void ofExitCallback(){
	
	// who to kill off ? 
	// -- fmod
	// -- quicktime
	// -- freeImage
	// anyone else?
	
	#ifdef WIN32_HIGH_RES_TIMING
		timeEndPeriod(1);
	#endif
	
	//delete OFSAptr;		
	
}

//--------------------------------------
void ofRunApp(ofSimpleApp * OFSA){
	
	OFSAptr = OFSA;
	
	atexit(ofExitCallback);
	
	#ifdef WIN32_HIGH_RES_TIMING	
		timeBeginPeriod(1);		// ! experimental, sets high res time
								// you need to call timeEndPeriod.  
								// if you quit the app other than "esc" 
								// (ie, close the console, kill the process, etc)
								// at exit wont get called, and the time will 
								// remain high res, that could mess things
								// up on your system. 
								// info here:http://www.geisswerks.com/ryan/FAQS/timing.html
								
	#endif							
								
	
	bFrameRateSet = false;
	
	//----------------------
	// setup the callbacks 
	glutMouseFunc(mouse_cb);
	glutMotionFunc(motion_cb);
	glutPassiveMotionFunc(passive_motion_cb);
	glutIdleFunc(idle_cb);
	glutDisplayFunc(display);
	glutKeyboardFunc(keyboard_cb);
	glutSpecialFunc(special_key_cb);
	//----------------------
	
	ofSeedRandom();	
	
	// 		seed the random generator 
	// 		(users can seed it to a value in setup 
	// 		if they want it fixed);
	
	OFSA->setup();
	glutMainLoop();
}

//--------------------------------------
float ofGetFrameRate(){
	return ofFrameRate;
}	

//--------------------------------------
void ofSetFrameRate(int targetRate){
	// given this FPS, what is the amount of millis per frame
	// that should elapse?
	
	// --- > f / s
	
	if (targetRate == 0){
		bFrameRateSet = false; 
		return;
	}
	
	bFrameRateSet 			= true;
	float durationOfFrame 	= 1.0f / (float)targetRate;
	millisForFrame 			= (int)(1000.0f * durationOfFrame);
}

//--------------------------------------
void ofHideCursor(){
	glutSetCursor(GLUT_CURSOR_NONE);
}

//--------------------------------------
void ofShowCursor(){
	glutSetCursor(GLUT_CURSOR_RIGHT_ARROW);
}

//--------------------------------------
void ofSetVerticalSync(bool bSync){
	//----------------------------
	#ifdef TARGET_WIN32
	//----------------------------
		if (bSync) {
			if (GLEE_WGL_EXT_swap_control) wglSwapIntervalEXT (1);
		} else {
			if (GLEE_WGL_EXT_swap_control) wglSwapIntervalEXT (0);
		}
	//----------------------------
	#endif
	//----------------------------
	
	//--------------------------------------
	#ifdef TARGET_OSX
	//--------------------------------------
		long sync = bSync == true ? 1 : 0;
		CGLSetParameter (CGLGetCurrentContext(), kCGLCPSwapInterval, &sync); 
	//--------------------------------------
	#endif
	//--------------------------------------

}


