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

struct screen {

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


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



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;

float rgbcalcs[7];
#include "alpha.h"
int rec_center_x = 0;
int rec_center_y = 0;
int rec_width = 100;
int rec_length = 100;
int rec_rotation = 0;
int uneven = 50;
int K, J;
int X;
int Y;  
int RES;
int DEPTH = 3;


double x_coord = 0;

static void Animate(void)
{

	glClear(GL_COLOR_BUFFER_BIT);
	//glClear(GL_DEPTH_BUFFER_BIT);
	//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);			// Clear The Screen And The Depth Buffer
	//glLoadIdentity();							// Reset The Current Modelview Matrix

	//glLoadIdentity();

	//glLoadIdentity();
	//glBegin(GL_POINTS);
	//glColor3f(rgbcalcs[1], rgbcalcs[2], rgbcalcs[3]);
	//glVertex3f(x1+.5, y1+.5, 1); 
	//glEnd();

	//glColor4f(150.0, 150.0, 0.0, 255.0);
	//ls_cube(0, 0, 10, 100, 100, 5);

	glLoadIdentity();
	glColor4f(2.0, 0.0, 0.0, 2.0);
	glBegin(GL_POINTS);
	glVertex3f(50, 50, 1);
	glVertex3f(52, 51, 1);
	glVertex3f(53, 51, 1);
	glVertex3f(54, 51, 1);
	glEnd();

glColor3f( 0.0, 2.0, 2.0 );
glutSolidCube(2);

	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 == 5)
        {
                fps = time2 / time1;
                printf("fps: %lf - %lf frames in %lf seconds\n", fps, time2, time1);
                gettimeofday(&starttime, NULL);
                time2 = 0;

	}





}

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 = (screen *)realloc(mainwindow, sizeof(screen)*RES*3) ) ==NULL )
	{
		printf("Reallocation failed\n");
		exit(1);
	} 

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









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

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

                        mainwindow2[i].red = 0;  
                        mainwindow2[i].green = 0;
                        mainwindow2[i].blue = 0; 
                        mainwindow2[i].alpha = 0;

	}



	printf("res*depth %d\n", RES*DEPTH);

	//mainwindow[9437183].red = 255;
	//mainwindow[9437183].green = 255;
	//printf("red: %d\n", mainwindow[9437183].red);

//9437184

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


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



	return 0;
}
