/*  drawmouse1.cpp
 *  This program demonstrates the drawing of rectangles entered with
 *  the mouse.  Use left button to input corners of rectangles, use
 *  middle button to clear rectangles, and use right button to exit.
 */
#include << X11/Xlib.h>
#include << fstream.h>
#include << math.h>
#include << GLUT/glut.h>
#include << OpenGL/gl.h>
#include << stdlib.h>
#include << stdio.h>

const int screenWidth = 640;
const int screenHeight = 480;        
class GLintPoint {
  public:
  GLint x, y;
};

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
   glMatrixMode (GL_PROJECTION);       // set "camera shape"
   glLoadIdentity ();                    // clear the matrix
           // viewing transformation 
   gluOrtho2D (0.0, (GLdouble)screenWidth, 0.0, (GLdouble)screenHeight);
           // set the Worlk Window
}

void myDisplay(void)
{
  glClear(GL_COLOR_BUFFER_BIT);     // clear the screen
 glFlush();
}

void myMouse(int button, int state, int x, int y)
{
 static GLintPoint corner[2];
 static int numCorners = 0;               // initial value is 0

  switch (button) {
     case GLUT_LEFT_BUTTON:
        if (state == GLUT_DOWN) {
            corner[numCorners].x = x;
            corner[numCorners].y = screenHeight - y;    // flip y coordinate
            numCorners++;      // have another point
            if(numCorners == 2) {
                glRecti(corner[0].x, corner[0].y, corner[1].x, corner[1].y);
                numCorners = 0;    // back to 0 corners
            }
        }
        break;
   case GLUT_MIDDLE_BUTTON:
        if (state == GLUT_DOWN)
            glClear(GL_COLOR_BUFFER_BIT);           // clear the window
        break;
   case GLUT_RIGHT_BUTTON:
        if (state == GLUT_DOWN)
           exit (-1);
        break;
   default:
        break;
   glFlush();
   }
}

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(myMouse);                  // register myMouse function
   glutMainLoop();                          // go into a perpetual loop
   return 0;
}