Font problem (probably easy)

Started by
5 comments, last by smitty1276 21 years, 9 months ago
I really just started messing around with bitmap fonts and I''m having some trouble... First of all, spaces aren''t showing up as spaces. Instead, they are completely ignored and the words all run together. My second problem occurs when I try to create two instances of a simple font class that I created. The class just holds the HFONT handle and a variable for the display list base, and the constructor calls CreateFont(), SelectObject(), and wglUseFontBitmaps(). Then there is a Select() member func that selects it as the current font and a Print() member function that handles the printing. Like I said, everything works fine except for printing spaces UNTIL I try to create a second instance and print with it. It''s like it completely ignores the second font. Here''s sort of what the code that creates and uses it looks like (summarized)... // --- Global variables --- CGLFont font1("Arial Black", 14, FW_BOLD); CGLFont font2("Courier", 10, FW_BOLD); void MyRenderingFunc( void ) { // All my other OpenGL rendering type stuff font1.Select(); font1.Print("This is a test..."); font2.Select(); font2.Print("But it will be in Arial andwithnospaces."); //... }
Advertisement
quote:Original post by smitty1276
The class just holds the HFONT handle and a variable for the display list base, and the constructor calls CreateFont(), SelectObject(), and wglUseFontBitmaps().

What for? You only need an HFONT to create the display lists, it''s useless afterwards.
quote:
Then there is a Select() member func that selects it as the current font

Post its source.
quote:
and a Print() member function that handles the printing.

Post its source as well.

Need more code. Have you, btw, seen NeHe''s font tutorials?
---visit #directxdev on afternet <- not just for directx, despite the name
Actually, most of these functions are pretty much snippets from the OpenGL Game Programming book...

This is just an incredibly simple little class that doesn't even do enough to warrant being its own class (yet)...

Here is the whole thing at the moment...

class CGLFont
{
public:
HFONT m_hFont;
unsigned int m_nListBase;

//Constructor
CGLFont( char *name, int height, int weight);

void Select( HDC hDC ); //make it the current font
void Print ( char *text ); //Print a string...

};

CGLFont::CGLFont( char *name, int height, int weight)
{
m_nListBase = glGenLists(96);

m_hFont = CreateFont( height, 0, 0, 0, weight,
FALSE, FALSE, FALSE, ANSI_CHARSET,
OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY, FF_DONTCARE | DEFAULT_PITCH, name );
}


void CGLFont::Select( HDC hDC )
{
SelectObject(hDC, m_hFont);
wglUseFontBitmaps(hDC, 32, 96, m_nListBase);
}

void CGLFont:rint( char *text )
{
glPushAttrib(GL_LIST_BIT);

glListBase(m_nListBase - 32);
glCallLists(strlen(text), GL_UNSIGNED_BYTE, text);

glPopAttrib();
}






[edited by - smitty1276 on June 27, 2002 1:55:06 PM]
You're doing printing wrong. Select's contents should be in the constructor, you need to save return value of SelectFont and select it back, and you need to destroy the font you created with CreateFont. See NeHe's lesson 13 for the proper way of printing text. I also explained some font stuff here.

[edited by - IndirectX on June 27, 2002 2:29:28 PM]
---visit #directxdev on afternet <- not just for directx, despite the name
So, basically, once I use CreateFont(), make it current, and call wglUseFontBitmaps(), I can toss the HFONT variable...

Then I can just select the instance of CGLFont that I want to use by calling wglUseFontBitmaps()?
quote:Original post by smitty1276
So, basically, once I use CreateFont(), make it current, and call wglUseFontBitmaps(), I can toss the HFONT variable...

Don''t toss it. Call SelectObject to make the old font current (which you saved, right?) and delete the HFONT to avoid memory leaks.
quote:
Then I can just select the instance of CGLFont that I want to use by calling wglUseFontBitmaps()?

You only need to call wglUseFontBitmaps once. It generates the display lists for the font, and you only need to do this once. Once the display lists are generated, you can use them for drawing. You only need to call Print to draw text.
---visit #directxdev on afternet <- not just for directx, despite the name
Yesterday I used Nehe''s lesson 13 to make a font class, and it works good, but I was wondering, is there any way to get around using the hDC?

It seems unnecessary to create a font that I need the hDC

This topic is closed to new replies.

Advertisement