Advertisement

Problem with bitmap fonts!!!!

Started by February 19, 2003 11:24 AM
2 comments, last by SteRo 22 years ago
I using bitmap fonts in my GL program. I want to center the text. To center the text I need to know how is text width (in float)!!! (I don''t know if you understand, because I don''t speak English very well)
post the code in the buildfont() or such..
---sorry for not using proper english , I'm from latin roots
Advertisement
--This build code I use--

GLvoid CFonts::Build(int size,const char *type)
{
HFONT font;
base = glGenLists(256); font = CreateFont(-size,0,0,0, FW_BOLD, FALSE, FALSE, FALSE, ANSI_CHARSET, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS,
ANTIALIASED_QUALITY,
FF_DONTCARE|DEFAULT_PITCH,
type);
SelectObject(hDC, font);
wglUseFontBitmaps(hDC, 0, 255, base);
}

--I want to know if It is possible to know how much is text long (in float)--
--Then I want to know "how"--
--In my opinion I think It is not possible--(It is my opinion)
Directly from NeHe Tutorial 14:

Thanks to Jim Williams for suggesting the code below. I was centering the text manually. His method works alot better

We start off by making a loop that goes through all the text character by character. strlen(text) gives us the length of our text. After we''ve set up the loop, we will increase the value of length by the width of each character. When we are done the value stored in length will be the width of our entire string. So if we were printing "hello" and by some fluke each character was exactly 10 units wide, we''d increase the value of length by the width of the first letter 10. Then we''d check the width of the second letter. The width would also be 10, so length would become 10+10 (20). By the time we were done checking all 5 letters length would equal 50 (5*10).

The code that gives us the width of each character is gmf[text[loop]].gmfCellIncX. remember that gmf stores information out each display list. If loop is equal to 0 text[loop] will be the first character in our string. If loop is equal to 1 text[loop] will be the second character in our string. gmfCellIncX tells us how wide the selected character is. gmfCellIncX is actually the distance that our display moves to the right after the character has been drawn so that each character isn''t drawn on top of eachother. Just so happens that distance is our width You can also find out the character height with the command gmfCellIncY. This might come in handy if you''re drawing text vertically on the screen instead of horizontally.

NOTE: Curt Werline adds - When you calculate the dimensions of the text string, you are using gmfCellIncX for the width and you suggest using gmfCellIncY for the height. These values are offsets and not true dimensions. To get the true dimensions you should use gmfBlackBoxX and gmfBlackBoxY.

for (unsigned int loop=0;loop<(strlen(text));loop++) // Loop To Find Text Length
{
length+=gmf[text[loop]].gmfCellIncX; // Increase Length By Each Characters Width
}

This topic is closed to new replies.

Advertisement