#include "testApp.h"

//--------------------------------------------------------------
void testApp::setup(){	 
	
	//ofSetFrameRate(30);
	//vidGrabber.setVerbose(true);
	
	int vidError = 0;
	vidError = vidGrabber.initGrabber();
	
	if(vidError != 0)
	{
		//we have an error so exit
		printf("Could not initialise Camera correctly, quitting \n");
		vidGrabber.failedToInit = true;
	}
	
	camWidth 		= vidGrabber.width;	// try to grab at this size. 
	camHeight 		= vidGrabber.height;
	
	cout<<"cam w/h"<<camWidth<<camHeight<<endl;
	
	videoTexture.allocate(camWidth,camHeight, GL_LUMINANCE);
	
	printf("\n\n\ndone!\n\n\n");
	
	d=true;
	f=false;
}


//--------------------------------------------------------------
void testApp::update(){
	
	ofBackground(100,100,100);
	if(!vidGrabber.failedToInit)
	{
		unsigned char * temp;
		temp = NULL;
		if(!f)
		{
			temp = vidGrabber.getPixels();
		}
		f=!f;
		if(temp!=NULL)
		{
			vidGrabber.unlock();
			pixels = temp;
			videoTexture.loadData(pixels, camWidth, camHeight, GL_LUMINANCE);
		}		
	}
	else
	{
		printf("No camera connected \n");
	}

}

//--------------------------------------------------------------
void testApp::draw(){
	ofSetColor(0xffffff);
	videoTexture.draw(20,20);
	
	ofSetColor(0,0,0);
	ofDrawBitmapString(ofToString(ofGetFrameRate()),10,600);
}


//--------------------------------------------------------------
void testApp::keyPressed  (int key){
}


//--------------------------------------------------------------
void testApp::keyReleased(int key){ 
	
}

//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ){
	
}

//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){
	
}

//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){
	
}

//--------------------------------------------------------------
void testApp::mouseReleased(){

}
