/***********************************
 * assignment 1 shell              *
 * CS 488 - Spring 2003            *
 * Written 8/00 by Andrew Johnson  *
 ***********************************/
 
#include <stdlib.h>
#include <stdio.h>

//most systems include GL/glut.h others just glut.h
#include <glut.h>


// Set up the logical borders
/////////////////////////////

#define LEFT_EDGE -200
#define RIGHT_EDGE 200
#define TOP_EDGE -200
#define BOTTOM_EDGE 200

// here are some global variables (nasty)
/////////////////////////////////////////

float x=15; // location of the left corner of the triangle
float y=10;

int direction =1;	// direction the triangle is moving
			// + to the right
			// - to the left


/*************************************************************
/* output code by Mark Kilgard */

void output(int x, int y, char *string)
{
  int len, i;

  glRasterPos2f(x, y);
  len = (int) strlen(string);
  for (i = 0; i < len; i++) {
    glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, string[i]);
  }
}

/*************************************************************
 *
 * keyboard
 * 
 * deal with the keyboard input to the program.  
 * 
 *************************************************************/

void keyboard(unsigned char c, int x, int y)
    {
        // escape key exits the program
      if (c == 27)
	exit(0);

        // if the user presses the space bar then
	// change the direction the triangle is moving

	if (c == ' ')
		direction = -direction;
    }

 
/*************************************************************
 *
 * update
 * 
 * update everything that needs to be updated in the scene - this
 * function should be called via a timer to ensure that updates 
 * happen at a regular rate, independent of the graphics update
 * rate
 *
 *************************************************************/

void update(int value)
    {
   
    /* at the end of this function
       reinstate the timer callback */
    /********************************/

	// set the location of the triangle to bounce back
	// and forth across the window

	if ((direction == 1) && (x > (RIGHT_EDGE-50)))
		direction = - direction;
	if ((direction == -1) && (x < LEFT_EDGE))
		direction = - direction;

	x += direction;


    glutTimerFunc(50, update, 1);
    } 
   
/*************************************************************
 *
 * display
 * 
 * this function is called each time the screen is to be drawn
 *************************************************************/

void display(void)
    {

    /* clear the screen to the clear colour */
    /****************************************/

    glClear(GL_COLOR_BUFFER_BIT);

    /* draw everything */
    /*******************/
        

	// draw a triangle with a white, red, and blue corner
	// at the current location specified by x,y
	/////////////////////////////////////////////////////

	glBegin(GL_POLYGON);   
		glColor3f(1.0, 1.0, 1.0);
		glVertex2f(x, y+40);
		glColor3f(1.0, 0.0, 0.0);
		glVertex2f(x+50, y+60);
		glColor3f(0.0, 1.0, 1.0);
		glVertex2f(x+50, y);
	glEnd();
                 
	glColor3f(1.0, 1.0, 1.0);
	output(0, 100, "Hi There");


    /* swap buffers */
    /****************/
        
    glutSwapBuffers();
    }

/*************************************************************
 *
 * dealWithParameters
 * 
 * here you could put some code to deal with the command-line
 * parameters for the program
 *************************************************************/
 
void dealWithParameters(int argc, char **argv)
    {
    }

/*************************************************************
 *
 * main
 * 
 *************************************************************/

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

    /* deal with any GLUT command Line options */
    /*******************************************/
        
    glutInit(&argc, argv);
    
    /* deal with any command line options for this program */
    /*******************************************************/
    
    dealWithParameters(argc, argv);

    /* create an output window */
    /***************************/

    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);

	// set up the physical window size

    glutInitWindowSize(500, 500);

	// set the name of the window and try to create it

    glutCreateWindow("CS 488 - HW 1");

    if (glutGet(GLUT_WINDOW_COLORMAP_SIZE) != 0)
	{
	printf("FAIL: bad RGBA colormap size\n");
	exit(1);
	}

    /* set up for accepting keyboard commands */
    /******************************************/

    glutKeyboardFunc(keyboard);

    /* set up the logical graphics space */
    /*************************************/
    
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(LEFT_EDGE, RIGHT_EDGE, BOTTOM_EDGE, TOP_EDGE);
    glClearColor(0.0, 0.0, 0.0, 1.0);

    /* assign the display function */
    /*******************************/
    
    glutDisplayFunc(display);

    /* assign the idle function */
    /****************************/

    glutIdleFunc(display);

    /* set up a timer to regularly update the scene */
    /************************************************/

    glutTimerFunc(50, update, 1);

    /* set everything going */
    /************************/
    
    glutMainLoop();
  
  return 0;  
}
