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


#include "myglut.h"

/*
**  ----------------------------
**  Routines for Array of Points
**  ----------------------------
*/

/*
** LINE DRAWING FUNCTIONS
*/
void drawPolyLine(GLintPointArray poly, int closed)
{
  int i;
  glBegin(closed ? GL_LINE_LOOP : GL_LINE_STRIP);
  for (i = 0; i < poly.num; i++)
    glVertex2i(poly.pt[i].x, poly.pt[i].y);
  glEnd();
}

/*
**  The C version to read a file of lines
**
**  NOTE:  This is *not* good code!!
*/
void drawPolyLineFile(char * filename)
{
  int i, j, numpolys, numlines;
  GLint x, y;
  char* line;
  FILE *inputFile;

  if ((inputFile = fopen(filename, "r")) == NULL)
      exit(1); // Can't open the file!!

  // We read a string (upto 10 charcters) from the file, and make it an int
  fgets(line, 10, inputFile);
  numpolys = atoi(line);

  for (j = 0; j < numpolys; j++)
  {
    fgets(line, 10, inputFile);
    numlines = atoi(line);
    glBegin(GL_LINE_STRIP);
    for (i = 0; i < numlines; i++)
    {
      fgets(line, 10, inputFile);
      sscanf(line, "%d %d", &x, &y);// scans the string 'line' for two ints
      glVertex2i(x,y);
    }
    glEnd();
  }
  fclose(inputFile);
  showScreen();
}

/* The C++ Version of drawing from the file
**
**  NOTE:  This is *not* good code!!
**  needs the extra include:

#include <fstream.h>

void drawPolyLineFile(char * filename)
{
  fstream inStream;
  inStream.open(filename, ios ::in);
  if (inStream.fail())
    return;

  GLint numpolys, numlines, x, y;
  inStream >> numpolys;
  for (int j = 0; j < numpolys; j++)
  {
    inStream>> numlines;
    glBegin(GL_LINE_STRIP);
    for (int i = 0; i < numlines; i++)
    {
      inStream >> x >> y;
      glVertex2i(x,y);
    }
    glEnd();
  }
  inStream.close();
  showScreen();
}
*/
/* End of the C++ version */


/*
**  ------------------------
**  Routines for Linked List
**  ------------------------
*/

/*
** Remove all the points in the list, and free up the space
*/
GLintPointPtr* clearPoints(GLintPointPtr* points)
{
  GLintPointPtr  *tempptr;
  while (points != NULL)
  {
    tempptr = points;
    points  = points->next;
    free(tempptr);
  }
  return NULL;
}


/*
** Find a point in the list
*/
GLintPointPtr* findPoint(GLintPointPtr* points, int x, int y)
{
  GLintPointPtr *tempptr = points;

  while (tempptr != NULL)
  {
    if (distance(tempptr->point.x, tempptr->point.y, x, y) < 5.0)
    {
      return tempptr;
    }
    tempptr = tempptr->next;
  }
  return NULL; 
}


James Little