Archived

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

CreateFont and GetTextExtentPoint32 (in opengl)

Recommended Posts

Using Nehe's truetype font build and print functions from lesson#13 I've come upon a problem where some text is written where the mouse cursor is pointed and follows it around... I then need the width of the string to determine how much I have to push it back along the x-axis so the text does not draw off the screen... You get the width in pixels with GetTextExtentPoint32 (under 2k/xp) yes? but I can't quite make it work... It returns wrong meassure... No matter what fontsize I type under the CreateFont call a string ie "hello" will be exactly the same number of pixels under the same font... that I don't get... here's a dumbed down test-version of my code that's supposed to do the text-pushing...
	char tempchar[] = "ZZZZxxXXxxXXxxZZZZzzZZzzZZzzzX");
SIZE size1;
GetTextExtentPoint32(hDC, tempchar, strlen(tempchar), &size1);
if ( size1.cx + mouse.x > 640 )
{
xpos = size1.cx + mouse.x - 640 ;
}
glRasterPos2f( (float) mouse.x - xpos, (float) 480 - mouse.y );
glPrint("%s", tempchar);

I used a Arial truetype font here at size "-14" in a 640*480 orthogonal screen. I don't think I need to explain anything else? mouse.x and y holds the cursor position... there's nothing added to the y coordinate of the text position because that's not really necessary at the moment, only horizontally. It's supposed to work so that the very last character in that string "X" will be perfectly aligned with the right side of the screen but in most cases (different strings) it isn't. Also the longer the string is, the more wrong, which is why I made it so long and certain characters as well makes it more evident... Been searching the net up and down but can't for the life of it find of a solution... so... Can anyone help? edit: bad engrish

Share on other sites
no reply and I''ve been messing with this for hours now, both today and last night... and I''m gonna go nuts soon...

does ANYONE use this GetTextExtentPoint32 or GetCharacterWidths (exact same result) or similar to find the width of strings or characters at all??

How come the size of the font, (height and width) set in the CreateFont call, doesn''t apply when using for instance the GetTextExtentPoint32 ???
No matter what size you set it at the gettext... call returns the same value...

puh-leaaaaaaseee help

Share on other sites
Did you use SelectObject, to select the created font for the HDC?

And no it doesn't return pixels, it returns logical units.

[edited by - fredizzimo on July 4, 2003 6:03:25 PM]

Share on other sites
edit: I take it back... seems I was too fast there..

yeah I'm using SelectObject at the end of the build of the font as nehe's lesson13 does it and nothing else is ever selected into the dc and reselecting doesn't seem to help...
it should register what fontsize I'm using yes?

using MulDiv I can convert logical units to pixels right?

[edited by - wamingo on July 4, 2003 9:42:50 PM]

Share on other sites
I took a look at the nehe code for BuildFont. And there''s two SelectObject calls. First it selects the newly created font, then it builds the bitmaps, and then finally it selects the old font into the hdc and deletets the font you created.

So in fact when you are trying to print the string, the default font is always selected.

A not so efficient way is to create the font again, and select it, before meassuring the lenght. But better and much more efficient would be to save the created font handles, so that you only need SelectObject, to select it to the DC and meassure it. But remember to delete those handles when you no longer need it. And also to remeove the DeleteObject from the CreateFont function.

Share on other sites
you are right!
I think it works now by selecting the "font" instead of the "oldfont"... it definately registers the font-size now...

although I thought that this...
oldfont = (HFONT)SelectObject(hDC, font);
...would make "oldfont" a copy of "font" and then use...
SelectObject(hDC, oldfont);
...to load the font into the dc, but that''s not the same?

Do you have an idea why nehe, and hence I, load oldfont at all? nehe doesn''t explain this part very well. He just says: "for good housekeeping". whatever that means...
Maybe I should ask, what exactly does oldfont contain?

By the way, thanks a lot for helping me out

• Forum Statistics

• Total Topics
628312
• Total Posts
2981996

• 9
• 9
• 13
• 11
• 13