Archived

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

Jason2Jason

A Font Class Gone Wrong!

Recommended Posts

Hi guys. I've been working on a class to handle fonts in open gl based on NeHe's tutorial 13 (Bitmap Fonts). It compiles fine, but doesn't display the text on screen. Heres the class code:

class CFont  
{
public:
	void put(const char *fmt, ...);
	void DestroyFont();
	void SetFont(HDC& thehdc, char* typeface, int height, int weight, bool italic, bool underlined, bool striked);
	CFont();
	~CFont();

private:
	HFONT oldfont;
	HFONT font;
	int base;
};

   

CFont::~CFont()
{
	DestroyFont();
}

void CFont::SetFont(HDC& thehdc, char* typeface, int height, int weight, bool italic, bool underlined, bool striked)
{
	base = glGenLists(96);								// Storage For 96 Characters

	font = CreateFont(	height,							// Height Of Font
						0,								// Width Of Font
						0,								// Angle Of Escapement
						0,								// Orientation Angle
						weight,						// Font Weight
						italic,							// Italic
						underlined,							// Underline
						striked,							// 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
						typeface);					// Font Name

	oldfont = (HFONT)SelectObject(thehdc, font);           // Selects The Font We Want
	wglUseFontBitmaps(thehdc, 32, 96, base);				// Builds 96 Characters Starting At Character 32
	SelectObject(thehdc, oldfont);							// Selects The Font We Want
	DeleteObject(font);									// Delete The
}

void CFont::DestroyFont()
{
	glDeleteLists(base, 96);						// Delete All 96 Characters 
}

void CFont:: Put(const char *fmt, ...)
{
		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
}

   
As you can see its nearly the same as in the turtorial, just in a class so its contained. In my twisted world, I'm thinking I can change the font by calling DestroyFont(), then SetFont() again with new font settings. At the moment SetFont() takes a reference to the DC, is that where its going wrong? It compiles with no errors or warnings, and idears? Just in case I'll leave my drawing test code here:

int DrawGLScene(GLvoid)									// Here's Where We Do All The Drawing
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	// Clear Screen And Depth Buffer
	glLoadIdentity();									// Reset The Current Modelview Matrix

	glTranslatef(0.0,0.0,-1.0);
	glRasterPos2f(0.0,0.0);

	glColor3f(1.0,1.0,1.0);

	font.put("Hello World!");

	return TRUE;										// Everything Went OK
}

   
Thx, -J PS: how do u make those wite scroll boxes of code again? [edited by - jason2jason on May 6, 2002 8:45:02 AM] [edited by - jason2jason on May 6, 2002 6:06:21 PM]

Share this post


Link to post
Share on other sites
can some one pls have a look, I know I posted this during game dev''s quite time of the day, but hey, just thought I''d make this at the top

thx
-J

Share this post


Link to post
Share on other sites
Hi there!

I'm not going to look too much at your code, however I looked a little at it and I couldnt exactly find any errors(I'm not any professional by any means Anyways, the reason I post here is that I've also made a Font class, also based on NeHe's tutorials.

Here's my class:

      void CFont::Init( char *fonttype )
{
HFONT font;
base = glGenLists(96);

font = CreateFont( -15,
0,
0,
0,
FW_BOLD,
FALSE,
FALSE,
FALSE,
ANSI_CHARSET,
OUT_TT_PRECIS,
CLIP_DEFAULT_PRECIS,
ANTIALIASED_QUALITY,
FF_DONTCARE|DEFAULT_PITCH,
fonttype);

SelectObject(hDC, font);
wglUseFontBitmaps(hDC, 32, 96, base);
}


void CFont::Print(float x, float y, const char *fmt, ...)
{
if (Initialised){
char text[256];
va_list ap;

if (fmt == NULL)
return;

va_start(ap, fmt);
vsprintf(text, fmt, ap);
va_end(ap);

glDisable(GL_DEPTH_TEST);
glDisable(GL_TEXTURE_2D);
glRasterPos2f( x, y );
glPushAttrib(GL_LIST_BIT);
glListBase(base - 32);
glCallLists(strlen(text), GL_UNSIGNED_BYTE, text);
glPopAttrib();
}
else{
MessageBox(NULL,"You have not Initialised the fonts!","ERROR: Initialisation",MB_OK | MB_ICONINFORMATION);
Exit = true;
}
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
}


PS: if you want your code to be in those boxes you can find all the information you need in the forum faq. Or I can give you it right away.. Just type "source" and "/source" in two []'s

oh, and just to clear out things again.. I'm not a VERY experienced coder so my code might suck...?

Kenneth Wilhelmsen
Try my little 3D Engine project, and mail me the FPS. WEngine
Or just visit my site
--------------------------
He who joyfully marches to music in rank and file has already earned my contempt. He has
been given a large brain by mistake, since for him the spinal cord would fully suffice. This
disgrace to civilization should be done away with at once. Heroism at command, senseless
brutality, deplorable love-of-country stance, how violently I hate all this, how despicable and ignoble war is; I would rather be torn to shreds than be a part of so base an action! It is my conviction that killing under the cloak of war is nothing but an act of murder

[edited by - kenwi on May 7, 2002 4:35:20 AM]

Share this post


Link to post
Share on other sites