turtle.c
contents ::
  Makefile
  mygl.c
  mygl.h
  myglut.c
  myglut.h
  turtle.c

/*
** Includes
*/
#include "mygl.h"
#include "myglut.h"


GLint windowWidth  = 640;
GLint windowHeight = 480;

/*
** Headers
*/
void myInit(void);
void myDisplay(void);
void polySpiral(GLint direction);
void myMouse(int button, int state, int x, int y);
void myKeyboard(unsigned char theKey, int mouseX, int mouseY);
int  main (int argc,  char** argv);


/*
** Initialisers
*/
void myInit(void)
{
  // Here's something to work out the size of the computer screen
  // and the size of the window we've created.
  GLfloat screenWidth, screenHeight;
  screenWidth  = glutGet(GLUT_SCREEN_WIDTH);
  screenHeight = glutGet(GLUT_SCREEN_HEIGHT);
  windowWidth  = glutGet(GLUT_WINDOW_WIDTH);
  windowHeight = glutGet(GLUT_WINDOW_HEIGHT);
  printf("\nInitial values are: Screen (%.1f, %.1f) && Window (%d.0, %d.0)\n",
          screenWidth, screenHeight, windowWidth, windowHeight);
  // now let's return to what we want to do..

  glClearColor(1.0, 1.0, 1.0, 0.0);
  setPenColour(colour[0]);
  setPenSize  (2.0);
  setWindow  (0, 640, 0, 480);
  setViewport(0, 640, 0, 480);
}


/*
** Main Drawing Routine
*/

void myDisplay(void)
{
  int i;
  static int startDirection = 0;

  for (i = 0; i < 1; i++)
  {
    clearScreen();
    polySpiral(startDirection);
    startDirection += 5;
    showScreen();
  }
}

void polySpiral(GLint direction)
{
  GLint length = 0;
  GLint angle = 89;
  GLint increment = 1;
  int i;

  moveTo(0, 0);
  turnTo(direction);
  for (i = 0; i < 1000; i++)
  {
    forward(length, 1);
    turn(angle);
    length += increment;
    if ((length < 0) || (length > windowHeight)) break;
  }
}


/*
**  Change the mouse (Viewport) position to World coordinates
*/
void  fixMousePosition(GLint *x, GLint *y)
{
  *y = (GLint) windowHeight - *y - 1;
}


/*
**  Mouse Listener
*/
void myMouse(int button, int state, int x, int y)
{
  // no Mouse Listener today
}

/*
**  Key Listener
*/
void myKeyboard(unsigned char theKey, int mouseX, int mouseY)
{
  fixMousePosition(&mouseX, &mouseY);

  switch (theKey)
  {
    case 'p':
    case 'P': drawPoint(mouseX, mouseY);
              showScreen();
               break;

    case 'c':
    case 'C': penColour = (++penColour)%7;
              setPenColour(colour[penColour]);
              break;

    case 'r':
    case 'R': myDisplay();
              break;

    case 'w':
    case 'W': clearScreen();
              showScreen();
              break;

    case 'q':
    case 'Q': exit(0);

    default: break;
  }
}


/*
**  Main
*/
int main (int argc,  char** argv)
{
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  //glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  glutInitWindowSize(640, 480);
  glutInitWindowPosition(100, 150);
  glutCreateWindow("Turtle");

  glutDisplayFunc(myDisplay);
  glutMouseFunc(myMouse);
  glutKeyboardFunc(myKeyboard);

  myInit();
  glutMainLoop();
}


James Little