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



static int movieCounter;

//--------------------------------------------------------------
testApp::testApp(){
	
	movieCounter = 0;
}

//--------------------------------------------------------------
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){	
		
		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')\n\n\nbRecording %i", ofGetFrameRate(), bRecordingMovie);
	ofDrawBitmapString(temp, 360,30);
}


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

	if (key == 'a'){
		bRecordingMovie = true;
		movieMaker.startMakingMovieInRam(320,240);
	} else if (key == 's') {
		bRecordingMovie = false;
		
		char name[255];
		sprintf(name, "my_movie%i.mov", movieCounter);
		movieMaker.save(name);
		movieMaker.finishMakingMovieInRam();
		movieCounter ++;
	}
		
}

//--------------------------------------------------------------
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);
}
