#include "testApp.h"

//--------------------------------------------------------------
void testApp::setup(){	
	
	ofBackground(155,155,155);
	ofSetWindowTitle("HPP lattice gas model");
	ofSetFrameRate(1);
	
	w = 1024;
	h = 800;
	texGray.allocate(w,h,GL_LUMINANCE);
	
	pixelDirection		= new int [w*h];
	for (int i = 0; i < w*h; i++){
		float nr = ofRandomuf();
		if (nr < .25) pixelDirection[i] = 1;
		else if (nr < .5) pixelDirection[i] = 2;
		else if	(nr < .75) pixelDirection[i] = 4;
		else pixelDirection[i] = 8;
	}
	
	grayPixels			= new unsigned char [w*h];
	newgrayPixels		= new unsigned char [w*h];

	for (int i = 0; i < w*h; i++){
		float nr = ofRandomuf();
		if (nr < .5){
			grayPixels[i] = 0;
		} else {
			grayPixels[i] = 255;
		}
		
	}

	for (int i = 300*1024; i < 500*1024; i+=1024){
		for (int j = 412; j < 612; j++){
			grayPixels[i+j] = 0;
		}
	}
			
			
	texGray.loadData(grayPixels, w,h, GL_LUMINANCE); 
	
	
		
}

//--------------------------------------------------------------
void testApp::update(){

	//determine collisions
	for (int i = 0; i < w*h; i++){
		if (grayPixels[i] == 255){
			switch(pixelDirection[i]){
			case 1:
				try { 
					if (grayPixels[i-1025] == 255){
						pixelDirection[i] = 8;
					}
				}
					catch (int k){};
			case 2:
				try { 
					if (grayPixels[i-1023] == 255){
						pixelDirection[i] = 4;
					}
				}
					catch (int k){};
			case 4:
				try { 
					if (grayPixels[i+1023] == 255){
						pixelDirection[i] = 2;
					}
				}
					catch (int k){};
			case 8:
				try { 
					if (grayPixels[i+1025] == 255){
						pixelDirection[i] = 1;
					}
				}
					catch (int k){};
			}
		}
	}
		 
	//move pixels
	newgrayPixels = grayPixels;
	
	for (int i = 0; i < w*h; i++){
		if (grayPixels[i] == 255){
			switch(pixelDirection[i]){
				case 1:
					try { 
						if (grayPixels[i-1025] == 0){
							newgrayPixels[i] = 0;
							newgrayPixels[i-1025] = 255;
						}
					}
					catch (int k){};
				case 2:
					try { 
						if (grayPixels[i-1023] == 0){
							newgrayPixels[i] = 0;
							newgrayPixels[i-1023] = 255;
						}
					}
					catch (int k){};
				case 4:
					try { 
						if (grayPixels[i+1023] == 0){
							newgrayPixels[i] = 0;
							newgrayPixels[i+1023] = 255;
						}
					}
					catch (int k){};
				case 8:
					try { 
						if (grayPixels[i+1025] == 0){
							newgrayPixels[i] = 0;
							newgrayPixels[i+1025] = 255;
						}
					}
					catch (int k){};
			}
		}
	}
	
	grayPixels = newgrayPixels;
								
	texGray.loadData(grayPixels, w,h, GL_LUMINANCE); 

	
}

//--------------------------------------------------------------
void testApp::draw(){
	
	ofSetColor(0xffffff);
	texGray.draw(0,0,w,h);

	string info = "HPP lattice gas model";
	ofFill();
	ofSetColor(0xFFFFFF);
	ofRect(10,10,300,70);
	ofSetColor(0x000000);
	ofDrawBitmapString(info,30,30);	
}

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

//--------------------------------------------------------------
void testApp::keyReleased(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){
	
}

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

//--------------------------------------------------------------
void testApp::windowResized(int w, int h){
	
}

