#include <psychlops.h>
using namespace Psychlops;

void psychlops_main() {
	Canvas cnvs(1024,768,Canvas::window);
	//Canvas cnvs(Canvas::fullscreen);
	cnvs.watchFPS();
	cnvs.showFPS();
	Mouse::show();

	const int N = 2500;
	Figures::ShaderGaborAlpha *plaid;
	plaid = new Figures::ShaderGaborAlpha[N];
	for(int y=0; y<N; y++) {
		plaid[y].phase = random(2*PI);
		plaid[y].contrast = random(0.5);
		plaid[y].wavelength = 5+random(10.0);
//		plaid[y].phase2 = random(2*PI);
//		plaid[y].contrast2 = random(0.5);
//		plaid[y].wavelength2 = 10+random(20.0);
	}

	double sigma = 6.0;
	int n = 100.0;
	Widgets::Dial dial;
	dial.link(sigma, 2).set(50).shift(100,100);
	Widgets::Slider slider;
	slider.link(n, Interval(0,2500), 4).set(200, 30).centering();

	while(!Keyboard::esc.pushed()) {
		cnvs.clear(Color::gray);
		if(slider.changed()) for(int i=0; i<n; i++) plaid[i].centering(Psychlops::random(cnvs.getWidth()), Psychlops::random(cnvs.getHeight()));
		for(int i=0; i<n; i++) {
			plaid[i].resize(sigma*8, sigma*8);
			plaid[i].phase += PI/6;
			plaid[i].wavelength = plaid[i].wavelength + .5*sin(plaid[i].phase);
			plaid[i].orientation = -atan2(plaid[i].getCenter().x-cnvs.mouse().x, plaid[i].getCenter().y-cnvs.mouse().y);
//			plaid[i].phase2 += PI/6;
//			plaid[i].wavelength2 = plaid[i].wavelength2 + .5*sin(plaid[i].phase2);
//			plaid[i].orientation2 = -PI/2-atan2(plaid[i].getCenter().x-cnvs.mouse().x, plaid[i].getCenter().y-cnvs.mouse().y);
			plaid[i].alpha = 0.5;
			plaid[i].draw();
		}
		dial.draw();
		slider.draw();
		cnvs.var(sigma*8, dial.getDatum().x, dial.getDatum().y);
		cnvs.var(n, slider.getDatum().x, slider.getDatum().y);
		cnvs.var(pow(floor(sigma), 2)*n, 200, 50, Color::green);
		cnvs.flip();
	}
	delete [] plaid;
}

