/****************************

	2D template

	sets up 2d opengl scene

	Dan Wilcox
	
	Art and Technology
	IT Univ of Goteborg

****************************/

#include "cross_platform.h"

#define FPS			29
#define NUM_POINTS 20

GLint winx = 1000;
GLint winy = 1000;

char *window_title = "ORIGAMI MATTER";

int fullScreen = false;
int current_point = 0;

struct point{
	int 	x;
	int 	y;
	GLubyte	R;
	GLubyte G;
	GLubyte B;
	bool display;
};

point points[NUM_POINTS];

void clear()
{
	for(int c = 0; c< NUM_POINTS; c++)
	{
		points[c].display = false;
	}
}

// initScene: initialize gl Scene
void initScene() 
{
	glShadeModel(GL_SMOOTH);
	clear();
}

// idle: gets called periodicaly
void idle()
{
	glutPostRedisplay(); // redraw the screen
}		

// keyboard: keyboard callback
void keyboard(unsigned char key, int x, int y)
{
	switch(key)
	{
		case 'c':
			clear();
			break;
		case ESCAPE:
			exit(0);		// exit good
	}  
}

/* mouse */
void mouse(int button, int state, int x, int y)
{

	if(state == GLUT_DOWN){
		if(button == GLUT_LEFT_BUTTON){
//			srand(5000);
			current_point++;
			if(current_point == NUM_POINTS) current_point = 0;
			points[current_point].x = x;
			points[current_point].y =winy- y;
			int r = rand()%256;
			points[current_point].R = r;
			points[current_point].G = r;
			points[current_point].B = r;
//			if(r == 0)  points[current_point].R = rand()%256;
//			if(r == 1)  points[current_point].G = rand()%256;
//			if(r == 2)  points[current_point].B = rand()%256;
			points[current_point].display = true;
			}

		else if(button == GLUT_RIGHT_BUTTON){
			current_point--;
			if(current_point == 0) current_point = 0;
			points[current_point].display = false;
	}
}}

//void mouseMoves(int x, int y)
//{

//}

// changeSize: recompute perspective
void changeSize(int w, int h) 
{
	glViewport((w-winx)/2,(h-winy)/2, winx, winy);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluOrtho2D(0, winx, 0, winy);
	glMatrixMode(GL_MODELVIEW);
	winx = w;
	winy = h;
}

// renderScene: draws the gl secene
void renderScene(void) 
{
	glClear(GL_COLOR_BUFFER_BIT);

	glBegin(GL_POLYGON);
		for(int c = 0; c < NUM_POINTS; c++){
			if(points[c].display = true){
				glVertex2i(points[c].x, points[c].y);
				glColor3ub(points[c].R, points[c].G, points[c].B);}}
	glEnd(); 

	// put draw code here

	_sleep(1000/FPS);
	glutSwapBuffers();
}

// main : initialize Glut window and create!
int main(int argc, char **argv)
{
	glutInit(&argc, argv);							// start glut
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);	// set gl display mode
	glutInitWindowPosition(0, 0);					// set window pos
	glutInitWindowSize(winx, winy);					// set window size!
	glutCreateWindow(window_title);					// start window!
	initScene();									// setup gl scene
	
	if(fullScreen)									// fullscreen?
		glutFullScreen();

	glutDisplayFunc(renderScene);					// display scene
	glutReshapeFunc(changeSize);					// set resize callback
	glutIdleFunc(renderScene);						// set idle callback
	glutKeyboardUpFunc(keyboard);                   // set keyboard callback
	glutMouseFunc(mouse);							// set mouse callback
//	glutMotionFunc(mouseMoves);
	glutMainLoop();									// loop display!

	return 0;										// exit good
}
