This section of the archives stores flipcode's complete Developer Toolbox collection,
featuring a variety of mini-articles and source code contributions from our readers.

Fontmap Rendering Class
Submitted by

During the developement of my engine there were sometimes situations,
when I wanted a bit more textual output than by a console.
Ideally some information appers naerby to the object it's related to. To
abstract the process of rendering texture mapped fonts using OpenGL
primitives I wrote the CFontmap class.
To use it include just cfontmap.h and make a instance of CFontmap like
CFontmap fontmap;
Then you may create a font by calling Fontmap::create(char *fontname,
int resolution), where resolution specifies the height of a char in
pixels within the font texture map.
The text will be printed from the Origin, normal OpenGL transformation
will apply. However if you want to set an offset in the XY plane you can
do that using
CFontmap::set_pos(X, Y);
Since the offsets are changed internally during rendering you'll have to
do this everytime before you print text with it.
Here is a small example programm using GLUT, that increases a counter
everytime when the window is about to be redrawn and shows the counter
content using CFontmap

/*
* make_fontmap(..)
* creates a new font texture map that can be used by CFontmap from the scratch
* given a fontname the resolution and the other stuff.
* Also parameterizes a given CFontmap in the right way.
* NOTE: The iResolution parameter sets the height of a character box in the texture,
* but the case is, that the texture size is choosen on the iResolution parameter
* and the font height ist scaled to fit. Since texture sizes must be powers of
* two (2^x) all Resolutions in [2^x;2^(x+1)] have the same effect.
* e.g. [1,1], [2,4], [5,16], [17,32] ...
* Mipmapping is done by createing a smaller font for each level to take care of
* - a good look
* - special font behaviours on smaller sizes if there
*/bool make_fontmap(char *csFontname, int iResolution, unsignedint uiChars, unsignedint uiColumns, unsignedint uiRows, CFontmap *pFontmap)
#ifdef _WIN32
// The Windoze stuff . . .
// CreateContext, some error? ( retry? oh please! ), CreateFont, SelectObject some error? ( retry? oh please! ), DeleteFont, DeleteContext
{
printf("make_fontmap(%s, %i, %i, %i, %i, %p)\n", csFontname, iResolution, uiChars, uiColumns, uiRows, pFontmap);

// First estimate the next integer exponent for 2^x the calc it
// Bit shift is a trick to get power of 2 values quickly (integers only)
int iTexWidth=(1L<<(int)ceil(LOG(uiColumns*iResolution,2)))*2;
int iTexHeight=(1L<<(int)ceil(LOG(uiRows*iResolution,2)))*2;

SelectObject(hdc, hbitmapOld);
DeleteObject(hbitmap);
DeleteDC(hdc);
returntrue;
// That was it, not very long or complex but tricky, whew!!!
}
#else// I's there really an other real GUI system as X11 on the earth?!?
// Normally I'm using Linux and my so loved X11. But in the moment my XFree4.0
// server doesn't work correctly. So I wasn't able to implement XFonts 8-(
{
printf(stderr, "make_fontmap(...) sorry, not implemented for X11. xet\n");
returnfalse;
}
#endif