Jump to content
  • Advertisement
Sign in to follow this  
MonkeyInBlack

Centering bitmap fonts

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been looking for a way to display bitmap fonts centered but it seems that i'm stuck. The problem i'm having with the fonts is that the different characters composing the font have variable widths. Therefor I can't find out the width of the entire string - and can't center it. I'm sure i must be missing some way to get the width of the different characters in the font. If anyone could help me out here that would make me really [smile]. PS : the code i'm using to build and display the font is pretty standard, but i thought i'd post it anyway.
// Building the font
base = glGenLists(256);
HFONT	font = CreateFont(height,
			0,
			0,
			0,
			FW_BOLD,
			FALSE,
			FALSE,
			FALSE,
			ANSI_CHARSET,
			OUT_TT_PRECIS,
			CLIP_DEFAULT_PRECIS,
			ANTIALIASED_QUALITY,
			FF_DONTCARE|DEFAULT_PITCH,
			fontfile);

SelectObject(hDC, font);

wglUseFontBitmaps(hDC, 32, 96, base);


// Displaying text
char		text[256];
va_list		ap;

va_start(ap, string);
    vsprintf(text, originalstring, ap);
va_end(ap);

glPushAttrib(GL_LIST_BIT);
glListBase(base-32);

glTranslated(x,y,0);
glRasterPos2f(0.0f,0.0f);
glCallLists(strlen(text),GL_UNSIGNED_BYTE,text);
glTranslated(-x,-y,0);

glPopAttrib();

Share this post


Link to post
Share on other sites
Advertisement
In standard Win32 you can find it out by having TEXTMETRICS defined and aquired when you print out the text or align it. You can probably find something similar with Win32 functions related to font, because I'm pretty much sure they have specified for width/height/ave for each size of the font.

Good luck.

Share this post


Link to post
Share on other sites
Quote:
Original post by Dave Hunt
Have a look at GetTextExtentPoint32. It will give you the width and height of a string based on the font currently selected into the DC.


That was exactly what i was looking for. A big 'thank you' to you both [smile] .

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!