/*  tiling1.cpp
 *  This program demonstrates the tiling of the screen windoe with
 *  copies of the dinosaur.  The data file is "dino.dat".
 *  Click the right button to exit.
 */
#include << X11/Xlib.h>
#include << fstream.h>
#include << math.h>
#include << GL/glut.h>
#include << GL/gl.h>
#include << stdlib.h>
#include << stdio.h>

const int screenWidth = 640;
const int screenHeight = 480;        

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

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

void myInit(void) 
{
   glClearColor (1.0, 1.0, 1.0, 0.0);   // set black background color
   glColor3f (0.0f, 0.0f, 0.0f);       // set the drawing color
   glPointSize (2.0);                  // set dot size 2 x 2
}

void drawPolylineFile(char *fileName)
{
int i,j;

//   fstream inStream;
//   inStream.open(fileName, ios::in); // open the file
//   if (inStream.fail())
//      return;
   ifstream inStream(fileName);
   GLint numpolys, numLines, x, y;
   inStream >> numpolys;             // read the number of polylines
   for (j = 0; j < numpolys; j++)
   {
      inStream >> numLines;
      glBegin(GL_LINE_STRIP);        // draw the next polyline
          for (i = 0; i < numLines; i++)
          {
              inStream >> x >> y;    // read the next x, y pair
              glVertex2i (x, y);
          }
      glEnd();
   }
   glFlush ();                       // send all out to display
   inStream.close();
}

void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);     // clear the screen
setWindow(0.0, 640.0, 0.0, 480.0);             // set the window
for (int i=0; i < 10; i++)
    for (int j=0; j < 11; j++) {
        setViewport(i*64, i*64 + 64, j*44, j*44 + 44);
        drawPolylineFile("dino.dat");
    }
}

void myMouse(int button, int state, int x, int y)
{
  switch (button) {
     case GLUT_RIGHT_BUTTON:
        if (state == GLUT_DOWN)
           exit (-1);
        break;
     default:
        break;
  }
}

void myMouse1(int button, int state, int x, int y){}

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

   glutInit(&argc, argv);                            // initialize the toolkit
   glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);     // set display mode
   glutInitWindowSize (screenWidth, screenHeight);   // set screen window size
   glutInitWindowPosition (100, 150);       // set window position on screen
   glutCreateWindow (argv[0]);              // open the screen window
   myInit ();
   glutDisplayFunc(myDisplay);              // register redraw function
   glutMouseFunc(myMouse1);                  // register myMouse function
   glutMouseFunc(myMouse);                  // register myMouse function
   glutMainLoop();                          // go into a perpetual loop
   return 0;
}