#include <stdio.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <math.h>
#include <stdbool.h>

#define RECT_SIDE 0.1
#define SPEED_X_INC 0.05

float x = 0, c = 0;
bool reverse = false;

void reshape(int w,int h){
printf( "event: reshaped to %d x %d\n", w, h );
}


/*
void tastiera(unsigned char k, int x, int y)
{
printf( "tasto premuto %c\n",k );
}
*/

void redraw() {
	//glClearColor(0.0,0.0,0.0,1.0); glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_POLYGON); glColor3f(0.0,1.0,0.0); glVertex2f(0.8,0.2); glColor3f(1.0,1.0,0.0); glVertex2f(0.7,0.5);
	//glColor3f(0.0,0.0,1.0); glVertex2f(-0.5,0.6); glVertex2f(-0.7,0.0); glColor3f(1.0,1.0,0.0); glVertex2f(-0.5,-0.6); glColor3f(1.0,1.0,1.0); glVertex2f(0.5,-0.5); glEnd();glFlush();

	float anim_x, anim_y;

	glClear(GL_COLOR_BUFFER_BIT);

	// Linear, sinusoid
	//anim_x = sin(x) * 0.8;
	//x = x + SPEED_X_INC;
	
	// Linear, linear
	if (reverse)
	{
		x = x + SPEED_X_INC;
	} else {
		x = x - SPEED_X_INC;
	}
	if (x < -1 || x > 1)
	{
		reverse = !reverse;
	}
	anim_x = x;
	 
	// Circle, linear
	// anim_x = cos(x) * 0.8;
	// ani m_y = tan(x) * 0.8;
	// x = x + SPEED_X_INC;
	c = c + 0.03;

	glColor3f(sin(c),sin(c+(M_PI/3)),sin(c+(2*(M_PI/3))));
	// glRectf(-RECT_SIDE/2+anim_x, RECT_SIDE/2, RECT_SIDE/2+anim_x, -RECT_SIDE/2);
	glRectf(-RECT_SIDE/2+anim_x, RECT_SIDE/2+anim_y, RECT_SIDE/2+anim_x, -RECT_SIDE/2+anim_y);
	// glFlush();
	glutSwapBuffers();

}

int main( int argc, char** argv ){
glutInit(&argc, argv);
glutInitDisplayMode( GLUT_RGB);
glutInitWindowPosition(300, 100 );
glutInitWindowSize(500,500);
glutCreateWindow(argv[0]);
glutReshapeFunc(reshape);
glutDisplayFunc(redraw);
glutIdleFunc(redraw);
//glutKeyboardFunc(tastiera);
	glClearColor(0.0, 0.0, 0.0, 1.0);
	glClear(GL_COLOR_BUFFER_BIT);

glutMainLoop();
}
