#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <sys/time.h>


void OpenGLInit(void);
static void Animate(void );
static void ResizeWindow(int w, int h);
struct timespec ts;
struct timeval starttime,endtime;

double fps = 0;
double time1 = 0;
double time2 = 0;


int rec_center_x = 0;
int rec_center_y = 0;
int rec_width = 100;
int rec_length = 100;
int rec_rotation = 0;
int K, J;
int X;
int Y;  
int RES;



struct screen {

	unsigned char red;
	unsigned char green;
	unsigned char blue;
	unsigned char alpha;	
};


struct screen *mainwindow=( (struct screen *) malloc(sizeof(struct screen)*5) );


static void Animate(void)
{
	int i;
	int x1 = 0;
	int y1 = 0;

	glClear(GL_COLOR_BUFFER_BIT);

	x1 = 0;
	y1 = 0;

	for(i=0; i < RES; i+=X) {
		glBegin(GL_POINTS);
		

		
		for(x1=0; x1<X; x1++) {
			glColor4f( mainwindow[x1+(y1*X)].red, mainwindow[x1+(y1*X)].green, mainwindow[x1+(y1*X)].blue, 
                            mainwindow[x1+(y1*X)].alpha );
			glVertex2f(x1, y1);

		}
		y1++;		


		glEnd();


	}
	glFlush();
	glutSwapBuffers();
	glutPostRedisplay();

	time2++;

	gettimeofday(&endtime, NULL);
	time1=((double)(endtime.tv_sec*1000000-starttime.tv_sec*1000000+endtime.tv_usec-starttime.tv_usec))/1000000;

        if (time2 == 10)
        {
                fps = time2 / time1;
                printf("fps: %lf - %lf frames in %lf seconds\n", fps, time2, time1);
                gettimeofday(&starttime, NULL);
                time2 = 0;

	}

		K = rec_center_x;
		J = rec_center_y;

		while(J < (rec_center_y + rec_length ) ) {
			while(K < (rec_center_x + rec_width ) ) {
				mainwindow[K+(X*J)].red = 0;				
				mainwindow[K+(X*J)].green = 0;
				mainwindow[K+(X*J)].blue = 255;
				mainwindow[K+(X*J)].alpha = 0;
				K++;
			}
			J++;
			K = rec_center_x;
		}

		if(rec_center_x < 500) {
			rec_center_x+=25;
		} else {
			rec_center_x=0;
		}

                if(rec_center_y < 500) {
                        rec_center_y+=25;
                } else {
                        rec_center_y=0;
                }  




                K = rec_center_x;
                J = rec_center_y;
                while(J < (rec_center_y + rec_length) ) {
                        while(K < (rec_center_x + rec_width) ) {
                                mainwindow[K+(X*J)].red = 255;
                                mainwindow[K+(X*J)].green = 0;
                                mainwindow[K+(X*J)].blue = 0;
				mainwindow[K+(X*J)].alpha = 1;
                                K++;
                        }
                        J++;
                        K = rec_center_x;
                }


}

void OpenGLInit(void)
{
	glShadeModel( GL_FLAT );
	glClearColor( 0.0, 0.0, 0.0, 0.0 );
	glClear(GL_COLOR_BUFFER_BIT);
	glDisable( GL_DEPTH_TEST );
}       
        

static void ResizeWindow(int w, int h)
{  
	float aspectRatio;
        h = (h == 0) ? 1 : h;
        w = (w == 0) ? 1 : w;
        glViewport( 0, 0, w, h );       // View port uses whole window
        aspectRatio = (float)w/(float)h;

	glMatrixMode( GL_PROJECTION );
	glLoadIdentity();
	glOrtho (0, w, h, 0, 0, 1);
	glMatrixMode( GL_MODELVIEW );
}




int main(int argc, char** argv) {

        glutInit(&argc,argv);
        glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
        glutInitWindowPosition( 0, 0 );

	Y = glutGet(GLUT_SCREEN_HEIGHT); 
	X = glutGet(GLUT_SCREEN_WIDTH);
	RES = X*Y;

	printf("res: %d x: %d y: %d\n", RES, X, Y);

	if( (mainwindow = (struct screen *)realloc(mainwindow, sizeof(struct screen)*RES) ) ==NULL )
	{
		printf("Reallocation failed\n");
		exit(1);
	} 










	int i;
	for(i=0; i < RES; i++) {

                        mainwindow[i].red = 0;
                        mainwindow[i].green = 0;
                        mainwindow[i].blue = 255;
			mainwindow[i].alpha = 1;
	}


	printf("test %d\n", mainwindow[0].alpha);


	glutInitWindowSize( X, Y );
	glutCreateWindow( "lsrender test" );


	OpenGLInit();
	glutReshapeFunc( ResizeWindow );
	glutDisplayFunc( Animate );
	glutMainLoop(  );



	return 0;
}
