• Announcements

Archived

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

How do I center text?

Recommended Posts

How do I center text, so that it is completely centered, and not just left indented?

Share on other sites
Kinda hard to tell you how to do something with out knowing how your are doing it, there are many ways to do text, depending on what you are doing it will probobly be as simple as glTranslateing (the_size_of_the_text)/2 to the left. but more info will be nessisary to provide a more detailed answer

Share on other sites
if you are talking about a console app you have to subtract the length of your text from the line width of the console and divide that number by 2. add that many 'space' characters to the beginning of your string and it'll be centered.

int numSpaces = (int) ((LINE_LENGTH - stringLength) * 0.5f);

-me

[edited by - Palidine on March 19, 2004 6:08:53 PM]

Share on other sites
Console app? Do you mean that stuff you do in Dos? I'm talking about OpenGL bitmap or outline fonts. I want buttons with centered text. I tried doing this once with an outline font, using the GLYPHMETRICSFLOAT structure (a 256 array of them, no less) to find the width of the characters. I don't know why the example I got this from used an array of 256 GLYPHMETRICSFLOATs, because I debugged it, and found that all the character widths were the same. the example was from OpenGL game programming book. They did something like this:

GLYPHMETRICSFLOAT gmf[256];			// holds orientation and placement									// info for display lists	SelectObject(g_HDC, hFont);	wglUseFontOutlines(g_HDC, 0, 255, base, 0.0f, depth, WGL_FONT_POLYGONS, gmf);	// center the text	for (unsigned int loop=0;loop<(strlen(str));loop++)	// find length of text	{		length+=gmf[str[loop]].gmfCellIncX;		        // increase length by character's width	}	glTranslatef(-length/2,0.0f,0.0f);                 //translate to center text

I did something like that, but only ended up with left-indented text

[edited by - EGD Eric on March 19, 2004 10:41:50 PM]

Edit: Okay, I went to the original example and found that the GLYPHMETRICSFLOAT gmf[256] has differening character widths, but in my program they're all the same width no matter what. Curious.

[edited by - EGD Eric on March 19, 2004 11:38:36 PM]

• Forum Statistics

• Total Topics
627774
• Total Posts
2979012

• 11
• 10
• 10
• 23
• 9