#### Archived

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

# Fonts.... Again

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

## Recommended Posts

Hi. I''ve been doing a font class (again) based on NeHe''s tutorial on Bitmap Fonts. It looks fine (almost the same as NeHe''s code), it even compiles without any error or warning messages, but the font doesn''t display. CAn anyone see anything i can''t? Heres my code:

class CFont
{
public:
void Write(float x, float y, char* text, ...);
void DestroyFont();
void CreateBMFont(HDC& hdc, char* FontName, int size, int weight, DWORD Italic, DWORD Underline, DWORD Strikeout);
CFont();
virtual ~CFont();

private:
UINT FontInd;
};

void CFont::CreateBMFont(HDC& hdc, char* FontName, int size, int weight, DWORD Italic, DWORD Underline, DWORD Strikeout)
{
HFONT hFont;					//Font handle

HFONT hOldFont;					//Apparently for good house keeping

FontInd = glGenLists(256);		//Generate font lists

hFont = CreateFont(	size,			//Set up font

0,
0,
0,
weight,
Italic,
Underline,
Strikeout,
ANSI_CHARSET,
OUT_TT_PRECIS,
CLIP_DEFAULT_PRECIS,
ANTIALIASED_QUALITY,
FF_DONTCARE|DEFAULT_PITCH,
FontName
);

hOldFont = (HFONT)SelectObject(hdc, hFont);		// Selects The Font We Want

wglUseFontBitmaps(hdc, 32, 96, FontInd);			// Builds 96 Characters Starting At Character 32

SelectObject(hdc, hOldFont);					// Selects The Font We Want

DeleteObject(hFont);							// Delete The Font

}

void CFont::DestroyFont()
{
glDeleteLists(FontInd, 96);						//Delete the 96 chars in the dis lists.

}

void CFont::Write(float x, float y, char* text, ...)
{
if(text == NULL)					//If there''s no text then return

return;

va_list pargs;						//Argument list pointer

char string[256];					//Final output string

glRasterPos2f(x,y);					//Position text (in 2D) with users x,y coord''s

va_start(pargs, text);							// Parses The String For Variables

vsprintf(string, text, pargs);				// And Converts Symbols To Actual Numbers

va_end(pargs);									// Results Are Stored In Text

glPushAttrib(GL_LIST_BIT);						// Pushes The Display List Bits

glListBase(FontInd - 32);						// Sets The Base Character to 32

glCallLists(strlen(string), GL_UNSIGNED_BYTE, string);		// Draws The Display List Text

glPopAttrib();												// Pops The Display List Bits

}

thx, -J

##### Share on other sites
you dont'' have any error checking code whatsoever. so the error could be anywhere. You should try writing your own texture based font renderer peice by peice. It isn''t that hard. And that way you will see yourself where you go wrong. Texture based fonts also look much better, and are much faster than the fonts you are trying to use.

##### Share on other sites
quote:
Original post by RipTorn
you dont'' have any error checking code whatsoever. so the error could be anywhere.

Yep. Use GetLastError, FormatMessage, and glGetError to check for errors.
quote:

You should try writing your own texture based font renderer peice by peice. It isn''t that hard.

It might be quite hard and intimidating for a newbie. I know it was for me.
quote:

And that way you will see yourself where you go wrong.

I have to disagree with this: finding a weird bug in 600 lines of code is most certainly harder than doing so in 6 lines.
quote:

Texture based fonts also look much better, and are much faster than the fonts you are trying to use.

I think he is trying to use texture based fonts. The biggest problem with wgl font functions is that they render text by single characters, which kills performance. I don''t see, however, how writing your own font engine will give you better output quality.

##### Share on other sites
Can you give me some info on how to use those error functions please?. Also, is being a newbie (at open GL at least) for about a year a bad thing? I can''t do most the stuff from memory still (like textures, font stuff), and I can''t code a engine from skratch either (without looking at other peoples code/tutorials/books). Is this bad?

thx

-J

##### Share on other sites
quote:
Original post by Jason2Jason
Can you give me some info on how to use those error functions please?

Write two small macros to save typing:

#define CHECKWIN { int e = GetLastError(); if (e) DebugBreak(); }
#define CHECKGL { int e = glGetError(); if (e != GL_NO_ERROR) DebugBreak(); }

Then, after each Win32 call such as CreateFont, insert:

CHECKWIN;

and after GL calls, such as wglUseFontBitmaps, insert:

CHECGL;

If an error is detected, you''ll be placed in a debugger where you can view the value of variable ''e'' (error code). Hit Alt-3 or Alt-4 in MSVC.
quote:

Also, is being a newbie (at open GL at least) for about a year a bad thing?

It''s not a bad thing. As long as you''re satisfied with the progress you''re making, you''re ok. If you aren''t satisfied, then you should work harder until you are. Not knowing how to do something is certainly not a bad thing, so don''t be hard on yourself.