
/*
 * lines.c -- Program to debug the problems reported in CS433 grid drawing
*/

#define THE_WAY_THAT_FAILS
/*
*/

#define EXTRA (0.5)

#include <stdio.h>       
#include <stdlib.h>       


/*
// Sometimes glu.h includes glut.h--sometimes not.
*/
#ifdef MAC_OSX
#    include <OpenGL/glu.h>
#    include <GLUT/glut.h>
#else 
#    include <GL/glu.h>       
#    include <GL/glut.h>       
#endif



/*
// Some debugging macros. Normally I make macros upper case; these are
// exceptions. 
*/

#define dbc(X)                                                                 \
    if (debug_level >= 1)                                                      \
    {                                                                          \
        fprintf(stderr,                                                        \
                "dbc: "#X" is ->%c<- (0x%x, %d) on source line %d of %s\n",    \
                (int)(X), (int)(X), (int)(X), __LINE__, __FILE__);             \
    }   

#define dbp(X) \
    if (debug_level >= 1)                                                      \
    {                                                                          \
        fprintf(stderr, "%s\n", (X));                                          \
    }   

#define dbi(X)                                                                 \
    if (debug_level >= 1)                                                      \
    {                                                                          \
        fprintf(stderr,                                                        \
                "dbi: "#X" is %ld on source line %d of %s\n", (long)(X),       \
               __LINE__, __FILE__);                                            \
    }   

#define  dbw()                                                                 \
    if (debug_level >= 1)                                                      \
    {                                                                          \
        fprintf(stderr,                                                        \
                "At line %d of %s\n", __LINE__, __FILE__);                     \
    }   


/* -------------------------------------------------------------------------- */


/* -------------------------------------------------------------------------- */


/* -------------------------------------------------------------------------- */

/* 
// Some prefer to write their programs backwards, so we don't need to declare
// functions up front--I prefer to write top down, so I have added function
// declarations to the original program. 
*/

static void display_CB(void);            /* Called whenever redisplay needed */
static void key_CB(unsigned char key, int x, int y);     /* Called on key press */

/* -------------------------------------------------------------------------- */

static int debug_level = 1;  /* Used by themacros above. */

/* -------------------------------------------------------------------------- */

int main(int argc, char *argv[])
{
    int win;


    glutInit(&argc, argv);                /* initialize GLUT system */

    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
    glutInitWindowSize(400,400);  
    
    win = glutCreateWindow("Lines");   /* create window */

    /* From this point on the current window is win */

    /* set background to black */
    glClearColor((GLclampf)0.0,(GLclampf)0.0,(GLclampf)0.0,(GLclampf)0.0);  

#ifdef FIX_NUMBER_ONE
    gluOrtho2D(0.0,401.0,0.0,401.0); /* how object is mapped to window */
#else 
    gluOrtho2D(0.0,400.0,0.0,400.0); /* how object is mapped to window */
#endif 

    glutDisplayFunc(display_CB);     /* set window's display callback */
    glutKeyboardFunc(key_CB);        /* set window's key callback */

    glutMainLoop();                       /* start processing events... */

    /* Execution never reaches this point, so return value is failure */

    return EXIT_FAILURE;
}

/*  /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\   */

/*
// Function called whenever redisplay needed (try hiding the window and then
// exposing it. The debug macro should print something.)
*/
static void display_CB(void)
{
    int i;


    glClear(GL_COLOR_BUFFER_BIT);         /* clear the display */

    /* set current color */
    glColor3d(250, 250, 250);

    for (i = 0; i < 100; i++)
    {
        glBegin(GL_LINES);
#ifdef THE_WAY_THAT_FAILS
        glVertex2i(3 * i, 0);
        glVertex2i(3 * i, 300);
#else
        glVertex2d(EXTRA + (double)(3 * i) , 0.0);
        glVertex2d(EXTRA + (double)(3 * i) , 300.0);
#endif 
        glEnd(); 
    }

    /* Shift by one. */
    for (i = 0; i < 100; i++)
    {
        glBegin(GL_LINES);
#ifdef THE_WAY_THAT_FAILS
        glVertex2i(49 + 3 * i, 100);
        glVertex2i(49 + 3 * i, 400);
#else
        glVertex2d(EXTRA + (double)(49 + 3 * i) , 100.0);
        glVertex2d(EXTRA + (double)(49 + 3 * i) , 400.0);
#endif 
        glEnd(); 
    }

    /* Shift by two. */
    for (i = 0; i < 100; i++)
    {
        glBegin(GL_LINES);
#ifdef THE_WAY_THAT_FAILS
        glVertex2i(98 + 3 * i, 200);
        glVertex2i(98 + 3 * i, 500);
#else
        glVertex2d(EXTRA + (double)(98 + 3 * i) , 200.0);
        glVertex2d(EXTRA + (double)(98 + 3 * i) , 500.0);
#endif 
        glEnd(); 
    }


    /* draw filled triangle */

    /*
     * This demonstrates the differences between lines and poly's, especially
     * when stretches. Interestingly, stretching works differently if only lines
     * are defined. I am not sure why. 
    */
    glBegin(GL_POLYGON);

    glVertex2d(200.5, 125.5); 
    glVertex2d(100.5, 375.5); 
    glVertex2d(300.5, 375.5); 

    glEnd();           /* OpenGL draws the filled triangle */

    glFlush();         /* Complete any pending operations */
    glutSwapBuffers(); /* Make the drawing buffer the frame buffer 
                          and vice versa */
}
/*  /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\   */

/* Function called on key press */
static void key_CB(unsigned char key, int x, int y) 
{
    dbc(key);   /* Print debugging stuff on stderr. */
    dbi(x);     /* Print debugging stuff on stderr. */
    dbi(y);     /* Print debugging stuff on stderr. */

    if( key == 'q' ) exit(0);
}

/*  /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\   */


