Open GL Super Bible

Previous Table of Contents Next


Before you create the font bitmaps, you need to decide on a character set. Normally you’ll use the ANSI or UNICODE character sets. The ANSI character set (ANSI_CHARSET) provides the standard 7-bit ASCII character set. To support international characters and diacritical marks, use the UNICODE character set instead (UNICODE_CHARSET). Some fonts use special character sets. The Symbol font, for example, provides Greek letters and many scientific symbols.

For this simple implementation, we will set the character set to ANSI_CHARSET for normal fonts, and SYMBOL_FONTSET for the Symbol font. See Listing 11-3.

Listing 11-3 Continuation of the FontCreateBitmaps function

   if (stricmp(typeface, "symbol") == 0)
     font = CreateFont(height, 0, 0, 0, weight, italic, FALSE, FALSE,
                       SYMBOL_CHARSET, OUT_TT_PRECIS,
                       CLIP_DEFAULT_PRECIS, DRAFT_QUALITY,
                       DEFAULT_PITCH, typeface);
   else
     font = CreateFont(height, 0, 0, 0, weight, italic, FALSE, FALSE,
                       ANSI_CHARSET, OUT_TT_PRECIS,
                       CLIP_DEFAULT_PRECIS, DRAFT_QUALITY,
                       DEFAULT_PITCH, typeface);

   SelectObject(hdc, font);

   wglUseFontBitmaps(hdc, 32, 96, base);

   return (base);
   }

If you need to use international characters, change the “normal” character set to UNICODE_CHARSET, and define 224 characters (256 minus 32), as shown here:

  else

    font = CreateFont(height, 0, 0, 0, weight, italic, FALSE, FALSE,

                      UNICODE_CHARSET, OUT_TT_PRECIS,

                      CLIP_DEFAULT_PRECIS, DRAFT_QUALITY,

                      DEFAULT_PITCH, typeface);



  SelectObject(hdc, font);



  wglUseFontBitmaps(hdc, 32, 224, base);

To complement FontCreateBitmaps you’ll need a font deletion function (Listing 11-4). Here the glDeleteLists function simply deletes the specified display lists, in this case our font bitmaps. As with the FontCreateBitmaps function, to make this function work with international character sets you need to change the number of display lists from 96 to 224.

Listing 11-4 FontDelete function

void
FontDelete(GLuint font) /* I - Font to delete */
{
  if (font == 0)
    return;

  glDeleteLists(font, 96);
}

Finally, to make drawing character strings easier, you can make put-string and printf-string functions. FontPuts (Listing 11-5) uses the glPushAttrib and glPopAttrib functions to save and restore the current display list base ID. If you forget to do this, you might inadvertently affect your other drawing code that uses display lists!

Listing 11-5 FontPuts function

void
FontPuts(GLuint font, /* I - Font to use */
         char   *s)   /* I - String to display */
{
  if (font == 0)
    return;

  if (s == NULL)
    return;

  glPushAttrib(GL_LIST_BIT);
    glListBase(font - 32);
    glCallLists(strlen(s), GL_UNSIGNED_BYTE, s);
  glPopAttrib();
}


A Note About glCallLists and Strings:  
It is important to remember that glCallLists and the font functions presented here do not handle control characters such as tab and newline. If you include control characters in the string you display, other display lists may be called that affect your final output. This behavior can be controlled by parsing the incoming string prior to using glCallLists. Newline and tab functionality can be simulated using the glBitmap technique outlined in the previous note, “A Note About the Current Raster Position,” along with a call to glGetIntegerv (described in Chapter 14).

The FontPrintf function (Listing 11-6) uses the <stdarg.h> header file to manage the variable number of arguments needed for vsprintf, which formats the string to be drawn.

Listing 11-6 FontPrintf function

#define MAX_STRING 1024

void
FontPrintf(GLuint font,       /* I <?> - Font to use */
           char   *format,    /* I - printf() style format string */
           …)/* I - Other arguments as necessary */
{
  va_list      ap;               /* Argument pointer */
  char         s[MAX_STRING + 1]; /* Output string */

  if (format == NULL)
    return;

  va_start(ap, format);       /* Start variable argument processing */
  vsprintf(s, format, ap);    /* Format the text into our output string */
  va_end(ap);                 /* End variable argument processing */

  FontPuts(font, s);
}

The complete code for FontCreate, FontDelete, FontPuts, and FontPrintf can be found in the CH11\FONT.C file. Prototypes are in the CH11\FONT.H file on the source code CD-ROM.

Pixmaps: Bitmaps with Color

Images with more than two colors are usually called pixmaps (short for pixel maps) and are used as background images or textures (covered in Chapter 12). In OpenGL, pixmaps are generally either 8-bit color index images or 24-bit RGB images.


Previous Table of Contents Next