/*
 *  lib_font.c
 *
 *  Draws some text in a bitmapped font.  Uses glBitmap() 
 *  and other pixel routines.  Also demonstrates use of 
 *  display lists.
 *
 *  This fixed-width font has WIDTH 8 and HEIGHT 13 pixels.
 */


#include <stdlib.h>
#include <string.h>
#include <iostream.h>

#ifdef GL
  #include <GL/gl.h>
  #include <GL/glu.h>
  #include <GL/glut.h>
#endif


void printString( char *s, float x, float y, float width, float height )

{
#ifdef GL
  int len;

  glMatrixMode(GL_PROJECTION);
  glPushMatrix();
  glLoadIdentity();
  glOrtho( 0, width, 0, height, 0.5, -0.5 );

  glMatrixMode(GL_MODELVIEW);
  glPushMatrix();
  glLoadIdentity();

  // Draw background (necessary due to Mesa bug)

  len = strlen(s);

  if (x<=0)
    x = 1;

  if (x+8*len+1 >= width) {
    x = width - 8*len - 1;
    if (x<=0)
      x = 1;
  }

  if (y<=3)
    y = 4;

  glColor3f(.5,.5,.5);
  glBegin( GL_POLYGON );
  glVertex2f(x-1,y-4);
  glVertex2f(x+8*len+1,y-4);
  glVertex2f(x+8*len+1,y+11);
  glVertex2f(x-1,y+11);
  glEnd();

  // Draw the characters

  glColor3f( 1, 1, 1 );

  glRasterPos2f( x, y );

  for (int i=0; i<len; i++)
    glutBitmapCharacter( GLUT_BITMAP_8_BY_13, (GLubyte) s[i] );

  glMatrixMode(GL_MODELVIEW);
  glPopMatrix();

  glMatrixMode(GL_PROJECTION);
  glPopMatrix();
#endif
}
