#include "testApp.h"

//--------------------------------------------------------------
void testApp::setup(){	 
	bikers.loadImage("images/bikers.jpg");
	gears.loadImage("images/gears.gif");
	tdf.loadImage("images/tdf_1972_poster.jpg");
	tdfSmall.loadImage("images/tdf_1972_poster.jpg");
	tdfSmall.resize(tdf.width / 4, tdf.height / 4);
	tdfSmall.setImageType(OF_IMAGE_GRAYSCALE);
	transparency.loadImage("images/transparency.png");
	bikeIcon.loadImage("images/bike_icon.png");
	bikeIcon.setImageType(OF_IMAGE_GRAYSCALE);
	screen.allocate(100, 100, OF_IMAGE_COLOR);
	bDrawMagnifier = false;
}


//--------------------------------------------------------------
void testApp::update(){
	ofBackground(255,255,255);
}

//--------------------------------------------------------------
void testApp::draw(){
	ofSetupScreen();
	
	ofSetColor(0xFFFFFF);
	
	bikers.draw(0,0);
	gears.draw(600,0);
	tdf.draw(600,300);
	
	ofSetColor(0xDD3333);
	tdfSmall.draw(200,300);
	
	ofSetColor(0xFFFFFF);
	ofEnableAlphaBlending();
	transparency.draw(sin(ofGetElapsedTimeMillis()/1000.0f) * 500 + 500,20);
	ofDisableAlphaBlending();
	
	
	ofSetColor(0x000000);
	ofFill();

	unsigned char * pixels = bikeIcon.getPixels();
	int w = bikeIcon.width;
	int h = bikeIcon.height;
	
	for (int i = 0; i < w; i++){
		for (int j = 0; j < h; j++){
			int value = pixels[j * w + i];
			//printf("value = %i \n", value);
			//ofSetColor(255,0,0);
			float pct = 1 - (value / 255.0f);
			ofCircle(i*10,500 + j*10,1 + 5*pct);		
		}
	}
	
	ofSetColor(0xFFFFFF);
	
	bikeIcon.draw(300,500,100,100);
	
	// if we draw the magnifier, we grab a bit from the screen where the 
	// mouse is into an ofImage, then draw it larger, to magnify...
	// (note, this can also be done in a texture, and it is likely quicker, 
	// because with an ofImage, the grabScreen sends data back to ram, 
	// and with ofTexture.grabScreen() the data stays on the graphics card)

	if (bDrawMagnifier == true){
		screen.grabScreen(mouseX-20, mouseY-20, 40, 40);
		screen.draw(mouseX-50,mouseY-50,100,100);
		ofNoFill();
		ofSetColor(0x000000);
		ofRect(mouseX-50,mouseY-50,100,100);
	}
}


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

//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ){
	
}

//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){
}

//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){
	bDrawMagnifier = true;
}

//--------------------------------------------------------------
void testApp::mouseReleased(){
	bDrawMagnifier = false;
}
