#include "testApp.h"
#include "stdio.h"

//--------------------------------------------------------------
testApp::testApp(){
	
}

//--------------------------------------------------------------
void testApp::setup(){	 
	ofBackground(255,255,255);	
	
	movieMaker.listCodecs();
 	movieMaker.setCodecType(21);		// graphics....
 	
 	pix = new unsigned char[320*240*3];
	for (int i = 0; i < 320; i++){
	for (int j = 0; j < 240; j++){
		float diffx = 100 - i;
		float diffy = 100 - j;
		float dist = sqrt(diffx *diffx + diffy*diffy);
		pix[j*320*3+i*3] = (int)dist;
		pix[j*320*3+i*3+1] =  (int)dist;
		pix[j*320*3+i*3+2] =  (int)dist;
	}
	}

	bRecordingMovie = false;
		
}

//--------------------------------------------------------------
void testApp::update(){
	if (bRecordingMovie == true){	
		pix = new unsigned char[320*240*3];
		int randX = ofRandom(0,400);
		for (int i = 0; i < 320; i++){
		for (int j = 0; j < 240; j++){
			float diffx = randX - i;
			float diffy = 100 - j;
			float dist = sqrt(diffx *diffx + diffy*diffy);
			pix[j*320*3+i*3] = (int)dist;
			pix[j*320*3+i*3+1] =  (int)dist;
			pix[j*320*3+i*3+2] =  (int)dist;
		}
		}
		movieMaker.addFrame(pix);
	}
	player.idleMovie();
}

//--------------------------------------------------------------
void testApp::draw(){
	ofSetColor(0xFFFFFF);
	player.draw(20,20);
	ofSetColor(0x000000);
	char temp[255];
	sprintf(temp, "frameRate %f\n\nstart recording ('a')\nend recording('s')\nsave with a file name 'myNewMovie.mov' ('x')\nsave file via dialog (' ')", ofGetFrameRate());
	ofDrawBitmapString(temp, 360,30);
}


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

	if (key == 'a'){
		bRecordingMovie = true;
		movieMaker.startMakingMovieInRam(320,240);
	} else if (key == 's') {
		bRecordingMovie = false;
		movieMaker.finishMakingMovieInRam();
		player.loadMovie(movieMaker.returnMyMovie());
	} else if (key == 'x'){
		movieMaker.save("myNewMovie.mov");
	} else if (key == ' '){
		movieMaker.saveViaFileDialog();
	}
		
}

//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ){
	int width = ofGetWidth();
	float pct = (float)x / (float)width;
	float speed = (2 * pct - 1) * 10.0f;
	player.setSpeed(speed);
}

//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){
	int width = ofGetWidth();
	float pct = (float)x / (float)width;
	player.setPosition(pct);
}

//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){
	player.setPaused(true);
}

//--------------------------------------------------------------
void testApp::mouseReleased(){
	player.setPaused(false);
}
