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



#include "mygl.h"

/* ----- GLOBAL VARIABLES -------------------------------------------------- */

// Current position, and direction (used for 'Turtle graphics')
GLintPoint currentPosition  = {0, 0};
GLint      currentDirection = 0;


// My Colours
int              penColour = 0;
GLfloatRGBColour colour[8] = { {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 1.0f},
                                  {0.0f, 1.0f, 0.0f}, {1.0f, 0.0f, 0.0f},
                               {0.0f, 1.0f, 1.0f}, {1.0f, 0.0f, 1.0f},
                               {1.0f, 1.0f, 0.0f}, {1.0f, 1.0f, 1.0f}};



/*
** Utility functions
*/

float distance(int x1, int y1, int x2, int y2)
{
  return  sqrt ((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
}

void clearScreen(void)
{
  glClear(GL_COLOR_BUFFER_BIT);
}

void showScreen(void)
{
  glFlush();
  glutSwapBuffers();
}

void setPenColour(GLfloatRGBColour newColour)
{
  glColor3f(newColour.r, newColour.g, newColour.b);
}

void setPenSize(int newsize)
{
  glPointSize(newsize);
}


/*
** Basic drawing functions
*/
void drawPoint(GLint x, GLint y)
{
  glBegin(GL_POINTS);
  glVertex2i(x, y);
  glEnd();
}

void drawLine(GLint x1, GLint y1, GLint x2, GLint y2)
{
  glBegin(GL_LINES);
  glVertex2i(x1, y1);
  glVertex2i(x2, y2);
  glEnd();
}


void moveTo(GLint x, GLint y)
{
  currentPosition.x = x;
  currentPosition.y = y;
}

void lineTo(GLint x, GLint y)
{
  drawLine(currentPosition.x, currentPosition.y, x, y);
  moveTo(x, y);
}

void moveRel(GLint dx, GLint dy)
{
  moveTo(currentPosition.x + dx, currentPosition.y + dy);
}

void lineRel(GLint dx, GLint dy)
{
  lineTo(currentPosition.x + dx, currentPosition.y + dy);
  
}


void turnTo(GLint angle)
{
  currentDirection = angle;
}

void turn(GLint angle)
{
  currentDirection += angle;
}

void forward(GLint distance, int isVisible)
{
  GLfloat RadPerDegree = 0.017453393;
  GLint dx = (GLint) (distance* cos(RadPerDegree * currentDirection));
  GLint dy = (GLint) (distance* sin(RadPerDegree * currentDirection));

  if (isVisible)
    lineRel(dx, dy);
  else
    moveRel(dx, dy);
}


/*
** Initialisers
*/

void setWindow(GLfloat left, GLfloat right, GLfloat bottom, GLfloat top)
{
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluOrtho2D(left, right, bottom, top);
}

void setViewport(GLint left, GLint right, GLint bottom, GLint top)
{
  glViewport(left, bottom, right - left, top - bottom);
}


James Little