• Advertisement

Archived

This topic is now archived and is closed to further replies.

Font problem (probably easy)

This topic is 5778 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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."); //... }

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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()?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

  • Advertisement