#include <GL/glut.h> // this includes <GL/gl.h>
#include <stdio.h>
#include <stdlib.h>


void
display()
{
    glClear(GL_COLOR_BUFFER_BIT) ;

    glFlush();

    glutReportErrors() ;
    return ;
}

void
keyboard(unsigned char key, int x, int y)
{
    switch (key) {
        // exit if the user hits the escape key or the 'q' key
        case 27:
        case 'q':
            exit(0);
            break;

        default:
            // print data to stderr so we know a key had been hit
            fprintf(stderr,"key %d hit. mouse at %d %d\n",key,x,y);
            break;
    };
}

void
mouse(int button, int state, int x, int y)
{
    switch (button) {

        case GLUT_LEFT_BUTTON:
            if (state == GLUT_DOWN) {
                fprintf(stderr,"left mouse button pressed\n");
            } else if (state == GLUT_UP) {
                fprintf(stderr,"left mouse button released\n");
            }
            break;

        case GLUT_MIDDLE_BUTTON:
            break;

        case GLUT_RIGHT_BUTTON:
            break;
    };
}

void
reshape(int width, int height)
{
    fprintf(stderr,"window reshaped to %d x %d\n",width,height);

    // We tell OpenGL the viewport size. Why?
    // It is possible, for instance, to use glViewport() to specify
    // a subrectangle of the window size. This will cause OpenGL  to
    // restrict its drawing to that subrectangle. There are other possible
    // uses for the viewport specification also. For instance, suppose we
    // wanted to draw a circle that was circular, no matter the shape of the
    // window. If we don't know the viewport dimensions, which tell us 
    // the aspect ratio (ratio of width/height) of the window, we can't
    // ensure our circle remains circular when the window changes its width or height.
    // 
    // In this case we do want to draw into the whole window, so that's
    // what we specify. This is also OpenGL's default, which is why it
    // wasn't needed in example-00.
    glViewport(0, 0, width, height);
}

int main(int argc, char **argv)
{
    glutInit( &argc, argv);
    glutInitWindowSize(800,600);
    glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);

    glutCreateWindow("example-01");

    glutDisplayFunc( display );

    // Ask GLUT to call the function named 'keyboard' when a key is pressed
    glutKeyboardFunc( keyboard );

    // Ask GLUT to call the function named 'mouse' when a mouse button is pressed
    glutMouseFunc( mouse );

    // Ask GLUT to call the function named 'reshape' when the window is resized
    glutReshapeFunc( reshape );

    // changing the color to medium blue just for fun
    glClearColor(0.0f, 0.0f, 0.5f, 1.0f) ;

    glutMainLoop() ;

    return(0) ;
}