/*
    This file is part of lspong.
    lspong is a loose representation of the old pong game we all remember and love.
    Copyright (C) 2009  sterling pickens

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

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

void OpenGLInit(void);

static Animate(void );
static void ResizeWindow(int w, int h);
struct timespec ts;
//void keyboard (unsigned char key, int x, int y);
//void keyboardUp (unsigned char key, int x, int y);
//bool ignoreRepeats = false;
        int i;
        int coord1 = 0;
        int direction = 1;
	int coord2 = 0;
	int coord3 = -25;
	int coord4 = -23;
	int coord5 = 0;
	int coord6 = -25;
	int angle1 = 0;

void special (unsigned char key, int x, int y)
{

  switch (key) { 
    case GLUT_KEY_UP:
      break;

    case GLUT_KEY_DOWN:
      break;

    case GLUT_KEY_RIGHT:
	coord5--;
      break;

    case GLUT_KEY_LEFT:
	//printf("left key pressed\n");
	coord5++;
      break;

 }

}
        
void keyboard(unsigned char key, int x, int y)
{
        if (key==27)
        {
                        //27 is the ascii code for the ESC key
        exit (0); //end the program
        }else if (key==13){
		angle1=0;
	}

}


void keyboardUp (unsigned char key, int x, int y)
{

    
}


int main( int argc, char** argv )   
{
	//int i;
        //int coord1 = 0;
        //int direction = 1;   
	int angle1 = 0;
        glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );

        // Create and position the graphics window
    glutInitWindowPosition( 0, 0 );
    glutInitWindowSize( 1600, 1200 );
    glutCreateWindow( "lspong v0.1.0" );

//glutIgnoreKeyRepeat(ignoreRepeats);
//glutKeyboardUpFunc (keyboardUp);

//glutKeyboardFunc (keyboard);

        // Initialize OpenGL.
    OpenGLInit();
        // Set up the callback function for resizing windows
    glutReshapeFunc( ResizeWindow );
 
        // Callback for graphics image redrawing
    glutDisplayFunc( Animate);

glutKeyboardFunc (keyboard);
glutSpecialFunc(special);

//glutKeyboardFunc (SpecialKeyboardChars);
for (i=0; i<150; i++) {
        // Start the main loop.  glutMainLoop never returns.
        glutMainLoop(  );
}
   exit(1);
}



static Animate(void)
{
	//int i;
	//int coord1 = 0;
	//int direction = 1;
	int empty;
	//int angle1 = 0;
	//char keyboard[3];
//	for (i=0; i<150; i++) {
/*
	if(coord1 > 0 && coord1 < 25 && coord1+1 == lastcoord ||coord1 == -25)
	{
		coord1++;
	}else{ 
		if (coord1 == 25 || (coord1 > -25 && coord1 < 0) || coord1+1 == lastcoord )
		{
			coord1--;
		}
	}
	lastcoord = coord1;
*/
//glutKeyboardFunc (keyboard);
//glutKeyboardUpFunc (keyboardUp);
//	glutKeyboardFunc (keyboard);
//printf("%s\n", key);
/*	if (coord1 == 21)
	{
		direction = 0;
	}
	if (coord1 == -21)
	{
		direction = 1;
	}
	if(direction == 1)
	{
		coord1++;
	}else{
		coord1--;
	}
				
*/

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        if(angle1 != 6)
        {
                coord2 = coord2 + angle1;
//        }else{
//                printf("lose\n");
//        }


if(coord1 == -21)
{
	if(coord2 == coord5)
	{
		angle1 = 0;
		//coord2 = coord2*angle1;
	}else if(coord2 == coord5-2){
		//implement 45 degree downward trajectory
		angle1 = -2;
	}else if(coord2 == coord5-1){
		//implement 22.5 degree downward trajectory
		angle1 = -1;
	}else if(coord2 == coord5+2){
		//implement 45 degree upward trajectory	
		angle1 = 2;
	}else if(coord2 == coord5+1){
		angle1 = 1;
	}else{
		//game over
		angle1 = 6;
	}
}
//	if(angle1 != 6)
//	{
//		coord2 = coord2 + angle1;
//	}else{
//		printf("lose\n");
//	} 


	if(coord2 == 16)
	{
		angle1 = angle1 * -1;
	}
	if(coord2 == -16)
	{
		angle1 = abs(angle1); 
	}

        if (coord1 == 21)   
        {
                direction = 0;
        }
        if (coord1 == -21)
        {
                direction = 1;
        }
        if(direction == 1)
        {
                coord1++;
        }else{   
                coord1--;
        }
}else{

	glLoadIdentity();
	glTranslatef ( 0.0, 0.0, -25 );
	//glRasterPos2f(float x, float y);
	//glRasterPos3f(0, 0, -25);
	//glColor3f( 1.0, 1.0, 0.0 );
//glColor3f(0.0, 1.0, 0.0); 

/*
char s[15] = "You Lose!";
void * font = GLUT_BITMAP_9_BY_15;
for (string::iterator i = s.begin(); i != s.end(); ++i)
{
    char c = *i;
    glutBitmapCharacter(font, c);
}
glMatrixMode(GL_MODELVIEW);
glPopMatrix();

glMatrixMode(GL_PROJECTION);
glPopMatrix();
*/

  glColor3f(1.0, 0.0, 0.0);//print timer in red
  glRasterPos2f(0.0, 0.0);

//glRasterPos3f(0, 0, -25);
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18,'Y');
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18,'o');
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18,'u');
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18,' ');
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18,'L');
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18,'o');
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18,'s');
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18,'e');
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18,'!');
glRasterPos2f(-1.0, -1.0);
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18,'E');
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18,'n');
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18,'t');
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18,'e');
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18,'r');
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18,' ');
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18,'t');
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18,'o');
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18,' ');
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18,'C');
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18,'o');
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18,'n');
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18,'t');
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18,'i');
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18,'n');
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18,'u');
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18,'e');


	//glRasterPos3f(0, 0, -25);
//renderBitmapString(0, 0, -25, GLUT_BITMAP_HELVETICA_10,'You Lose!');
}
        // Clear the redering window
        //glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        // Clear the current matrix (Modelview)
         
        glLoadIdentity();    
        // Back off eight units to be able to view from the origin.
                
        glTranslatef ( coord1, coord2, coord3 );


        // Rotate the plane of the elliptic
        // (rotate the model's plane about the x axis by fifteen degrees)
        //glRotatef( 15.0, 1.0, 0.0, 0.0 );
        
    // Draw the sun     -- as a yellow, wireframe sphere
        glColor3f( 1.0, 1.0, 0.0 );
    //glutWireSphere( 4.0, 80, 80 );

glutSolidSphere( 0.50, 300, 300 );

//glutSolidCube(1.0);
//glutSolidCube(1.0);
//empty = 0;
//tv_usec
        /* Delay for a bit */
        ts.tv_sec = 0;
        ts.tv_nsec = 100000000;
        nanosleep (&ts, NULL);

glLoadIdentity();

glTranslatef ( coord4, coord5, coord6 );
//printf("coord2 %d\n", coord2);

glColor3f( 2.0, 2.0, 0.0 );
//glutSolidCube(1.0);
glRectf(-1.00f,2.00f, 1.00f, -2.00f);


//nanosleep (100, empty);
//nanosleep(tv_usec, empty);
    glFlush();
    glutSwapBuffers();
glutPostRedisplay();            // Request a re-draw for animation purposes

//}

}


void renderBitmapString(
		float x, 
		float y, 
		float z, 
		void *font, 
		char *string) {  
  char *c;
  glRasterPos3f(x, y,z);
//glColor3f( 1.0, 1.0, 0.0 );
  for (c=string; *c != '\0'; c++) {
    glutBitmapCharacter(font, *c);
  }
//return (0);
}



void OpenGLInit(void)
{       
    glShadeModel( GL_FLAT );
    glClearColor( 0.0, 0.0, 0.0, 0.0 );
    glClearDepth( 1.0 );
    glEnable( 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;
        
        // Set up the projection view matrix (not very well!)
    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    gluPerspective( 70.0, aspectRatio, 1.0, 30.0 );
        
        // Select the Modelview matrix
    glMatrixMode( GL_MODELVIEW );
}

