Simon666
November 24th, 2003, 08:21 AM
Hello, I'm using OpenGL to draw in my application. I want to use multiple fonts. I found the functions below on NeHe to use fonts. The problem is : you only can declare one font, because when you call BuildFont more than once there is a memory leak which after time will give you a warning that you don't have enough virtual memory. using KillFont seems to make the program crash except when used in the destructor of your view. How can I use multiple fonts or one font with different sizes?
GLuint base;
GLvoid BuildFont(int Size, CDC* pDC) // Build Our Bitmap Font
{
HFONT font; // Windows Font ID
HFONT oldfont; // Used For Good House Keeping
base = glGenLists(96); // Storage For 96 Characters
font = CreateFont( -Size, // Height Of Font
0, // Width Of Font
0, // Angle Of Escapement
0, // Orientation Angle
FW_BOLD, // Font Weight
FALSE, // Italic
FALSE, // Underline
FALSE, // Strikeout
ANSI_CHARSET, // Character Set Identifier
OUT_TT_PRECIS, // Output Precision
CLIP_DEFAULT_PRECIS, // Clipping Precision
ANTIALIASED_QUALITY, // Output Quality
FF_DONTCARE|DEFAULT_PITCH, // Family And Pitch
"Courier New"); // Font Name
HDC hDC = pDC->GetSafeHdc();
oldfont = (HFONT)SelectObject(hDC, font); // Selects The Font We Want
wglUseFontBitmaps(hDC, 32, 96, base); // Builds 96 Characters Starting At Character 32
SelectObject(hDC, oldfont); // Selects The Font We Want
DeleteObject(font); // Delete The Font
hDC = NULL;
}
GLvoid KillFont(GLvoid) // Delete The Font List
{
glDeleteLists(base, 96); // Delete All 96 Characters
}
GLvoid glPrint(const char *fmt, ...) // Custom GL "Print" Routine
{
char text[256]; // Holds Our String
va_list ap; // Pointer To List Of Arguments
if (fmt == NULL) // If There's No Text
return; // Do Nothing
va_start(ap, fmt); // Parses The String For Variables
vsprintf(text, fmt, ap); // And Converts Symbols To Actual Numbers
va_end(ap); // Results Are Stored In Text
glPushAttrib(GL_LIST_BIT); // Pushes The Display List Bits
glListBase(base - 32); // Sets The Base Character to 32
glCallLists(strlen(text), GL_UNSIGNED_BYTE, text); // Draws The Display List Text
glPopAttrib(); // Pops The Display List Bits
}
void DrawAxes1(CDC* pDC)
{
glColor3d(0.45,0.50,0.75);
glBegin(GL_LINES);
glVertex3d( 0.00, 0.00, 0.00);
glVertex3d( 0.10, 0.00, 0.00);
glVertex3d( 0.00, 0.00, 0.00);
glVertex3d( 0.00, 0.10, 0.00);
glVertex3d( 0.00, 0.00, 0.00);
glVertex3d( 0.00, 0.00, 0.10);
glEnd();
glBegin(GL_LINES);
glVertex3d( 0.100,-0.002, 0.000);
glVertex3d( 0.115, 0.000, 0.000);
glVertex3d( 0.100, 0.002, 0.000);
glVertex3d( 0.115, 0.000, 0.000);
glVertex3d( 0.100,-0.002, 0.000);
glVertex3d( 0.100, 0.002, 0.000);
glEnd();
glBegin(GL_LINES);
glVertex3d(-0.002, 0.100, 0.000);
glVertex3d( 0.000, 0.115, 0.000);
glVertex3d( 0.002, 0.100, 0.000);
glVertex3d( 0.000, 0.115, 0.000);
glVertex3d(-0.002, 0.100, 0.000);
glVertex3d( 0.002, 0.100, 0.000);
glEnd();
glBegin(GL_LINES);
glVertex3d( 0.000,-0.002, 0.100);
glVertex3d( 0.000, 0.000, 0.115);
glVertex3d( 0.000, 0.002, 0.100);
glVertex3d( 0.000, 0.000, 0.115);
glVertex3d( 0.000,-0.002, 0.100);
glVertex3d( 0.000, 0.002, 0.100);
glEnd();
BuildFont(12,pDC);
glRasterPos3d( 0.125, 0.000, 0.000 );
glPrint("X");
glRasterPos3d( 0.000, 0.125, 0.000 );
glPrint("Y");
glRasterPos3d( 0.000, 0.000, 0.125 );
glPrint("Z");
}
GLuint base;
GLvoid BuildFont(int Size, CDC* pDC) // Build Our Bitmap Font
{
HFONT font; // Windows Font ID
HFONT oldfont; // Used For Good House Keeping
base = glGenLists(96); // Storage For 96 Characters
font = CreateFont( -Size, // Height Of Font
0, // Width Of Font
0, // Angle Of Escapement
0, // Orientation Angle
FW_BOLD, // Font Weight
FALSE, // Italic
FALSE, // Underline
FALSE, // Strikeout
ANSI_CHARSET, // Character Set Identifier
OUT_TT_PRECIS, // Output Precision
CLIP_DEFAULT_PRECIS, // Clipping Precision
ANTIALIASED_QUALITY, // Output Quality
FF_DONTCARE|DEFAULT_PITCH, // Family And Pitch
"Courier New"); // Font Name
HDC hDC = pDC->GetSafeHdc();
oldfont = (HFONT)SelectObject(hDC, font); // Selects The Font We Want
wglUseFontBitmaps(hDC, 32, 96, base); // Builds 96 Characters Starting At Character 32
SelectObject(hDC, oldfont); // Selects The Font We Want
DeleteObject(font); // Delete The Font
hDC = NULL;
}
GLvoid KillFont(GLvoid) // Delete The Font List
{
glDeleteLists(base, 96); // Delete All 96 Characters
}
GLvoid glPrint(const char *fmt, ...) // Custom GL "Print" Routine
{
char text[256]; // Holds Our String
va_list ap; // Pointer To List Of Arguments
if (fmt == NULL) // If There's No Text
return; // Do Nothing
va_start(ap, fmt); // Parses The String For Variables
vsprintf(text, fmt, ap); // And Converts Symbols To Actual Numbers
va_end(ap); // Results Are Stored In Text
glPushAttrib(GL_LIST_BIT); // Pushes The Display List Bits
glListBase(base - 32); // Sets The Base Character to 32
glCallLists(strlen(text), GL_UNSIGNED_BYTE, text); // Draws The Display List Text
glPopAttrib(); // Pops The Display List Bits
}
void DrawAxes1(CDC* pDC)
{
glColor3d(0.45,0.50,0.75);
glBegin(GL_LINES);
glVertex3d( 0.00, 0.00, 0.00);
glVertex3d( 0.10, 0.00, 0.00);
glVertex3d( 0.00, 0.00, 0.00);
glVertex3d( 0.00, 0.10, 0.00);
glVertex3d( 0.00, 0.00, 0.00);
glVertex3d( 0.00, 0.00, 0.10);
glEnd();
glBegin(GL_LINES);
glVertex3d( 0.100,-0.002, 0.000);
glVertex3d( 0.115, 0.000, 0.000);
glVertex3d( 0.100, 0.002, 0.000);
glVertex3d( 0.115, 0.000, 0.000);
glVertex3d( 0.100,-0.002, 0.000);
glVertex3d( 0.100, 0.002, 0.000);
glEnd();
glBegin(GL_LINES);
glVertex3d(-0.002, 0.100, 0.000);
glVertex3d( 0.000, 0.115, 0.000);
glVertex3d( 0.002, 0.100, 0.000);
glVertex3d( 0.000, 0.115, 0.000);
glVertex3d(-0.002, 0.100, 0.000);
glVertex3d( 0.002, 0.100, 0.000);
glEnd();
glBegin(GL_LINES);
glVertex3d( 0.000,-0.002, 0.100);
glVertex3d( 0.000, 0.000, 0.115);
glVertex3d( 0.000, 0.002, 0.100);
glVertex3d( 0.000, 0.000, 0.115);
glVertex3d( 0.000,-0.002, 0.100);
glVertex3d( 0.000, 0.002, 0.100);
glEnd();
BuildFont(12,pDC);
glRasterPos3d( 0.125, 0.000, 0.000 );
glPrint("X");
glRasterPos3d( 0.000, 0.125, 0.000 );
glPrint("Y");
glRasterPos3d( 0.000, 0.000, 0.125 );
glPrint("Z");
}