#include "textureMovie.h"

//--------------------------------------------------------------
void textureMovie::setup(){	

	frames = new ofTexture[MAX_NUM_FRAMES_FOR_MOVIE];
	
	unsigned char * grayPixels = new unsigned char [WIDTH_MOV*HEIGHT_MOV];
	
	for (int i = 0; i < MAX_NUM_FRAMES_FOR_MOVIE; i++){
		frames[i].allocate(WIDTH_MOV,HEIGHT_MOV,GL_LUMINANCE);
		
		// gray pixels, set them randomly
		for (int i = 0; i < WIDTH_MOV*HEIGHT_MOV; i++){
			grayPixels[i] = (unsigned char)(ofRandomuf() * 255);	
		}
		
		frames[i].loadData(grayPixels, WIDTH_MOV,HEIGHT_MOV, GL_LUMINANCE); 
	}
	
	delete grayPixels;
	
	currentFrame = 0;
	speed	= 1;
	lastFrameTime =  ofGetElapsedTimef(); 
	
	printf("%i \n", (int)ceil(1.4f));
}
 

//--------------------------------------------------------------
void textureMovie::idle(){
	
	// some logic, how far did we go ?
	
	float frameRate = 30;	// 30 fps;
	
	
	float diffTime = ofGetElapsedTimef() - lastFrameTime; 
	lastFrameTime = ofGetElapsedTimef();
	
	// bump how many frames?
	int nFramesToAdvance =  ((speed * diffTime) * frameRate);
	currentFrame += nFramesToAdvance;
	while (currentFrame < 0) currentFrame += MAX_NUM_FRAMES_FOR_MOVIE;
	while (currentFrame > MAX_NUM_FRAMES_FOR_MOVIE) currentFrame -= MAX_NUM_FRAMES_FOR_MOVIE;

}

//--------------------------------------------------------------
void textureMovie::draw(float x, float y){
	frames[(int)currentFrame].draw(x,y);
}