/*==============================*/
/*  File:    gString.c          */
/*  Author:  Bary W Pollack     */
/*  Updated: Feb. 21, 2005      */
/*                              */
/*  Demonstrate GLUT text       */
/*==============================*/

#include <GL/glut.h>

/*==============================*/

GLsizei wh = 500, ww = 500;     /* initial window size */

/*==============================*/

void myinit(void)
{
    glViewport (0, 0, ww, wh);

    /* Pick 2D clipping window to match size of X window 
    This choice avoids having to scale object coordinates
    each time window is resized */

    glMatrixMode (GL_PROJECTION);
    glLoadIdentity (); 
    glOrtho (0.0, (GLdouble) ww, 0.0, (GLdouble) wh, -1.0, 1.0);
    glClearColor (0.8, 0.8, 0.8, 1.0);
    glClear (GL_COLOR_BUFFER_BIT);
    glFlush ();
}


/*=====  these are the predefined GLUT fonts
GLUT_BITMAP_9_BY_15
GLUT_BITMAP_8_BY_13
GLUT_BITMAP_TIMES_ROMAN_10
GLUT_BITMAP_TIMES_ROMAN_24
GLUT_BITMAP_HELVETICA_10
GLUT_BITMAP_HELVETICA_12
GLUT_BITMAP_HELVETICA_18
=====*/

/*==============================*/
/*  The gString subsystem       */

static void *_font;     /* the GLUT font to use           */
static int  _gx, _gy;   /* initial (_gx, _gy) coordinates */

void gInitText (void *font, int x, int y)   /* initialize */
{
    _font = font;  _gx = x;  _gy = y;
}

void gText (char c)         /* draw a character */
{
    static int shift = 0;
    if (c == '\0')
        shift = 0;
    else
    {
        glRasterPos2i (_gx + shift, _gy);
        glutBitmapCharacter (_font, c);
        shift += glutBitmapWidth (_font, c);
    }
}

void gString (char *s)      /* draw a string */
{
    gText ('\0');
    while (*s)
        gText (*s++);
}

/*==============================*/
/*  display callback routine    */

void display (void)
{
    char  *sMessage = "ABCDE 12345";

    glClearColor (0.8, 0.8, 0.8, 1.0);
    glClear (GL_COLOR_BUFFER_BIT);
    glColor3f (0.0, 0.0, 0.0);

    gInitText (GLUT_BITMAP_TIMES_ROMAN_24, 2*ww/5, wh-ww/11 - 0*25);
    gString (sMessage);

    gInitText (GLUT_BITMAP_TIMES_ROMAN_10, 2*ww/5, wh-ww/11 - 1*25);
    gString (sMessage);

    gInitText (GLUT_BITMAP_HELVETICA_18, 2*ww/5, wh-ww/11 - 2*25);
    gString (sMessage);

    gInitText (GLUT_BITMAP_HELVETICA_12, 2*ww/5, wh-ww/11 - 3*25);
    gString (sMessage);

    gInitText (GLUT_BITMAP_HELVETICA_10, 2*ww/5, wh-ww/11 - 4*25);
    gString (sMessage);

    gInitText (GLUT_BITMAP_9_BY_15, 2*ww/5, wh-ww/11 - 5*25);
    gString (sMessage);

    gInitText (GLUT_BITMAP_8_BY_13, 2*ww/5, wh-ww/11 - 6*25);
    gString (sMessage);

    glFlush ();
}

/*==============================*/

int main(int argc, char** argv)
{
    glutInit (&argc, argv);
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize (500, 500);
    glutCreateWindow ("GLUT Text");
    glutDisplayFunc (display);

    myinit ();

    glutMainLoop();
}


/*==============================*/
