#include "graphicsLineModule.h"


// better this is static, then every module has a texture
// (we should do the same in the movie classes)
// also, we should have an array, and cycle through...

ofTexture	offscreenTex;
bool 		bTexAllocated = false;
		

//-------------------------------------------
graphicsLineModule::graphicsLineModule(){
	module = new graphicsModuleBase();	// is this bad?
	
	if (!bTexAllocated){
		offscreenTex.allocate(1024,768, GL_RGB);
	}
	
	setup();
}
		
//-------------------------------------------	
void graphicsLineModule::setup(){
	module->setup();
	box->setStartPointAngle(module->getStartPosition(), module->getStartAngle());	
}

//-------------------------------------------	
void graphicsLineModule::reset(){
	module->reset();	
	box->setStartPointAngle(module->getStartPosition(), module->getStartAngle());	
}

//-------------------------------------------	
bool graphicsLineModule::bAmIDone(){
	 return module->bAmIdone();
}

//-------------------------------------------	
void graphicsLineModule::update(){
	module->update();
	box->updatePts();
}

//-------------------------------------------	
bool graphicsLineModule::bHaveNewPoint(){
	return module->bHaveNewPoint();
}

//-------------------------------------------	
ofVec3f graphicsLineModule::getNewPoint(){
	ofVec3f pt = module->getNewPoint();
	return interpolatePt(box->ptsTranslated, pt.x/640.0f, (pt.y/480.0f));
}


//-------------------------------------------	
void graphicsLineModule::drawOffscreen(){

	// my laptop is pretty slow w/ loadScreenData at 1024x768
	// so perhaps 640 x 480 for now
	// should be interesting to try a pbuffer as an alternative
	// and then we can avoid the glClear call, which is slow (about 5fps) 
	
	ofSetColor(0xffffff);
	ofRect(0,0,640,480);
	ofSetColor(0xff0000);
	module->draw();
	offscreenTex.loadScreenData(0,0,640,480);

}


//-------------------------------------------	
void graphicsLineModule::draw(float offsetx, float offsety){
	
	// add the offset to the translation:
	ofVec3f ptsTransAndOffsetted[4];
	for (int i = 0; i < 4; i++){
		ptsTransAndOffsetted[i].set(box->ptsTranslated[i].x - offsetx, 	
									box->ptsTranslated[i].y - offsety, 0	
									);
	}
	
	ofSetColor(0xffffff);
	offscreenTex.draw(ptsTransAndOffsetted);
	
	/*	
	// draw the texture:
	ofSetColor(0xffffff);
	glBegin(GL_LINE_LOOP);
		for (int i = 0; i < 4; i++){
			glVertex2f(ptsTransAndOffsetted[i].x,ptsTransAndOffsetted[i].y);
		}
	glEnd();
	*/
	
	//module->draw();

}

