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);
}