#if defined(__APPLE__) && defined (__MACH__)
#   include <GLUT/glut.h>
#else
#   include <GL/glut.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#define DEG_TO_RAD 0.017453

GLfloat deg = 0.0;
float time = 0.0;

void display()
{
  glClear(GL_COLOR_BUFFER_BIT);
  GLfloat sinval = sin(DEG_TO_RAD*deg);
  GLfloat cosval = cos(DEG_TO_RAD*deg);
  glBegin(GL_POLYGON);
    glVertex2f(cosval, sinval);
    glVertex2f(-sinval, cosval);
    glVertex2f(-cosval, -sinval);
    glVertex2f(sinval, -cosval);
  glEnd();
  glutSwapBuffers();
}

void idle()
{
  float new_time = glutGet(GLUT_ELAPSED_TIME);

  if((new_time - time)/1000.0 >= 0.01) {
    deg += ((new_time - time) * 360.0/5.0)/1000.0;
    if(deg > 360.0)
      deg -= 360.0;
    time = new_time; 
    glutPostRedisplay();
  }
}

void reshape(GLsizei w, GLsizei h)
{
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();

  if(w <= h)
    gluOrtho2D(-2.0, 2.0, -2.0 * (GLfloat) h / (GLfloat) w, 
	       2.0 * (GLfloat) h / (GLfloat) w);
  else
    gluOrtho2D(-2.0 * (GLfloat) w / (GLfloat) h, 
	       2.0 * (GLfloat) w / (GLfloat) h,
	       -2.0, 2.0);
  glMatrixMode(GL_MODELVIEW);

  glViewport(0, 0, w, h);
}

void init()
{
  glClearColor(1.0, 0.0, 0.0, 0.0);
  glColor3f(0.0, 0.0, 1.0);

  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluOrtho2D(-2.0, 2.0, -2.0, 2.0);
}

void keyboard(unsigned char key, int x, int y)
{
  if(key == 'q' || key == 'Q' || key == 27)
    exit(0);
}

int main(int argc, char** argv)
{
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  glutCreateWindow("Test 1");
  glutDisplayFunc(display);
  glutReshapeFunc(reshape);
  glutKeyboardFunc(keyboard);
  glutIdleFunc(idle);
  init();
  glutMainLoop();
  return 0;
}

