# OpenGL Font Width Issues

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

## Recommended Posts

Hello, I'm trying my best to code a 1/2 decent Pong remake using OpenGL. I have an engine that is working pretty well, and I have a class for my Bitmap fonts. I have until now simply put the fonts on screen using passed variables (x and y). I would like to center the text on the screen, but I'm not sure how. I'm not sure how wide my fonts are. I have different sized fonts and whatnot, so being able to figure this out when the font is made would be best I'd think. This is my code for my Bitmap font class. Any help is welcome.
/*=================================================================
BitMapFont.cpp
Implementing the BitmapFont class.
==================================================================*/

#include "BitMapFont.h"

BitmapFont::BitmapFont(HDC hDC, int size, char* family)
{
// set font size for later reference when centering
fontSize = size;

if (hDC && family && strlen(family) > 0)
{
HFONT font;     //windows font id
HFONT oldFont;  // housekeeping
base = glGenLists(256);

font = CreateFont(  size * -1,                  //height of font
0,                          //width of font
0,                          //angle of escapement
0,                          //angle of orientation
FW_BOLD,                    //weight of font
FALSE,                      //italic
FALSE,                      //underline
FALSE,                      //strikeout
ANSI_CHARSET,               //character set
OUT_TT_PRECIS,              //output precision
CLIP_DEFAULT_PRECIS,        //clipping precision
ANTIALIASED_QUALITY,        //antialiase the font
FF_DONTCARE|DEFAULT_PITCH,  //Family And Pitch
family);                    //Font name

oldFont = (HFONT)SelectObject(hDC, font);
wglUseFontBitmaps(hDC, 0, 256, base);
SelectObject(hDC, oldFont);
DeleteObject(font);
}
}

BitmapFont::~BitmapFont()
{
glDeleteLists(base, 256);
}

bool BitmapFont::DrawString(int x, int y, const char *text, ...)
{

char    output[256];
va_list ap;

if (text == NULL)
return false;

va_start(ap, text);             // Parses The String For Variables
vsprintf(output, text, ap);     // And Converts Symbols To Actual Numbers
va_end(ap);

glRasterPos3f(x, y, 0.0f);

// Must save/restore the list base.
glPushAttrib(GL_LIST_BIT);
glListBase(base);
glCallLists(strlen(output), GL_UNSIGNED_BYTE, output);
glPopAttrib();

return true;
}

bool BitmapFont::DrawCentered(int y, const char *text, ...)
{

float length = 0;
char    output[256];
va_list ap;

if (text == NULL)
return false;

va_start(ap, text);                 // Parses The String For Variables
vsprintf(output, text, ap);         // And Converts Symbols To Actual Numbers
va_end(ap);

// calculate length of string
for(unsigned int i = 0; i < (strlen(output)); i++)
length += fontSize;

glRasterPos3f(SCREEN_WIDTH/2 - length/2, y, 0.0f);  // HELP!!

// Must save/restore the list base.
glPushAttrib(GL_LIST_BIT);
glListBase(base);
glCallLists(strlen(output), GL_UNSIGNED_BYTE, output);
glPopAttrib();

return true;
}



##### Share on other sites
For TrueType fonts:
GetCharABCWidths
For non TrueType fonts:
GetCharWidth32

##### Share on other sites
// Load the widths of the current font (our font)if( !GetCharWidth32(hdc, 32, 127, m_charWidths) ){	util::Message::print("IBitmapFont:: Unable to load character widths for font \"%s\"", name);	return false;	// FIXME: error can't get the width properly, so we assume that there is a problem}

Where m_charWidths is an array[96] of ints, because I believe that in the first set of ascii characters there are 96 printable characters (if I'm wrong please let me know, this would be something I would have to fix).

Then, to get the width of any particular character:
int getCharWidth(char c) const{	return m_charWidths[c-32];	// because we're not using the first 32 characters (non-printable)}

##### Share on other sites
Thanks for the help!!

How would you specify the font to use? I'm not sure exactly how to use this function to do what I need. I'm guessing you'd simply initialize the buffer array in the constructor and then utilize it afterwards when figuring out the length of the string. Correct?

Can anyone supply some more example code that uses the function or initializes it?

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 9
• 13
• 9
• 9
• 15
• ### Forum Statistics

• Total Topics
634073
• Total Posts
3015343
×

## Important Information

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!