#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*240*3; i++){
		pix[i] = rand()%255;
	}

	bRecordingMovie = false;
		
}

//--------------------------------------------------------------
void testApp::update(){
	if (bRecordingMovie == true){	
		for (int i = 0; i < 320*240*3; i++){
			pix[i] += rand()%2;
		}
		movieMaker.addFrame(pix);
	}
	player.idleMovie();
}

//--------------------------------------------------------------
void testApp::draw(){
	ofSetColor(0xFFFFFF);
	player.draw(20,20);
	ofSetColor(0x000000);
	char temp[255];
	sprintf(temp, "frameRate %f\nstart recording ('a')\nend recording('s')\nsave file(' ')", 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 == ' '){
		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);
}
