Archived

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

Taralieth

Need help with fonts

Recommended Posts

Hey. I''m having problems with fonts in opengl. I read through the 2d fonts tutorial at www.gametutorials.com and i created a small class that i can use in other projects. But for some reason, the text doesn''t show up in one of my program when i use the class i created, but in another one of my program, i use the exact same font class and it works fine. So can anyone look through the font class code and see if anything is wrong? Maybe even test it out if it works for u?
class CFont {
private:
	UINT m_FontListID; // holds the ID for the display list used to create the font 

	HFONT m_hOldFont; // holds the old font so that there''s no memory leaks

	int m_Height; // the height of the font


public:
	CFont(); // initializes the variables

	~CFont(); // cleans up the class


        // creates the font

	void CreateOpenGLFont(HDC hDC, LPSTR fontName, int height=32, BOOL underlined=FALSE, BOOL italicized=FALSE);

        // positions the text, used in the WriteText method

	void PositionText(int x, int y, int screenHeight);

        // writes the text in position (x,y), screen height is the height of the window in pixels

	void WriteText(int x, int y, int screenHeight, const char *string, ...);

        // destroys the font

	void DestroyFont(HDC hDC);

};
//-------------------------------------------------

// The Constructor and destructor

//-------------------------------------------------

CFont::CFont() {
	m_hOldFont = NULL;
	m_FontListID = 0;
}

CFont::~CFont() {
	m_hOldFont = NULL;
	m_FontListID = 0;
}

//-------------------------------------------------

// Creates the Font

//-------------------------------------------------

void CFont::CreateOpenGLFont(HDC hDC, LPSTR fontName, int height, BOOL underlined, BOOL italicized) {
	HFONT hFont;
	if(hDC == NULL) {
		return;
	}

	m_Height = height;

	m_FontListID = glGenLists(MAX_CHARS);
	hFont = CreateFont(-height,
					   0,
					   0,
					   0,
					   FW_BOLD,
					   italicized,
					   underlined,
					   FALSE,
					   ANSI_CHARSET,
				       OUT_TT_PRECIS,
					   CLIP_DEFAULT_PRECIS,
					   ANTIALIASED_QUALITY,
					   FF_DONTCARE|DEFAULT_PITCH,
					   fontName);

	m_hOldFont = (HFONT)SelectObject(hDC,hFont);
	
	wglUseFontBitmaps(hDC,0,MAX_CHARS-1, m_FontListID);
}

//--------------------------------------------------

// Position the text

//--------------------------------------------------

void CFont::PositionText(int x, int y, int screenHeight) {
	glPushAttrib(GL_TRANSFORM_BIT | GL_VIEWPORT_BIT);

	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
	glLoadIdentity();

	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();
	glLoadIdentity();

	// makes it so that y = 0 is at the top of the screen, not the bottom

	y = screenHeight - m_Height - y;

	glViewport(x-1,y-1,0,0);

	glRasterPos4f(0,0,0,1);

	glPopMatrix();

	glMatrixMode(GL_PROJECTION);
	glPopMatrix();

	glPopAttrib();
}

//-------------------------------------------------

// writes teh text on the screen

//-------------------------------------------------

void CFont::WriteText(int x, int y, int screenHeight, const char *string,...) {
	char text[256];
	va_list argumentPtr;

	if(string == NULL) {
		return;
	}

	va_start(argumentPtr,string);
		vsprintf(text,string,argumentPtr);
	va_end(argumentPtr);

	PositionText(x,y,screenHeight);

	glPushAttrib(GL_LIST_BIT);
		glListBase(m_FontListID);
		glCallLists(strlen(text),GL_UNSIGNED_BYTE,text);
	glPopAttrib();
}

//-------------------------------------------------

// destroys the fonts and cleans up everything

//-------------------------------------------------

void CFont::DestroyFont(HDC hDC) {
	glDeleteLists(m_FontListID,MAX_CHARS);
	SelectObject(hDC,m_hOldFont);
}

Share this post


Link to post
Share on other sites
=/ i''m stumped, no matter how much i look over it, i can''t find any errors. Are there any states that has to be enabled or disabled for the fonts to work?

Share this post


Link to post
Share on other sites
It''s more than likely something you''re not enabling...

Make sure you have

glEnable(GL_TEXTURE_2D);

... that''s about all that I can think of that would bite you in the ass..

I would also change your text buffer to look like this

char* text = (char*)calloc(strlen(string)+1);
//blah Blah blah
free(text);

-=|Mr.Oreo|=-
Code Monkey, Serpent Engine

Share this post


Link to post
Share on other sites
Hmm...i think you are right that i''m not enabling something. I already enabled GL_TEXTURE_2D. Does anyone know what states need to be enabled and/or disabled for 2d fonts (the one from www.gametutorials.com, probably also the one from nehe since the tutorial also mentions nehe''s tutorial) to work? Any and all help is greatly appreciated.

Share this post


Link to post
Share on other sites
First, function PositionText is totally bad, because it doesn''t do anything what we want. That problem is in projection matrix. You NEED to do that:


glPrintText(x,y,char*str)
{
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0,X_WINDOW_SIZE,Y_WINDOW_SIZE,0,-1,1); //!!!first problem
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glRasterPos2i(x,y);
glCallLists(......font etc....);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
}


possible will be better...









...::why don''t we have >assembler >forum?::...

Share this post


Link to post
Share on other sites