Jump to content
  • Advertisement
Sign in to follow this  
Halsafar

Center a D3DFont -- Find its width on screen

This topic is 4837 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 am labelling a few things with D3D Font and I need to center the font. Its all in screen space. I am not quite sure how to determine the exact width of a word of text given how many letters are in and its size. It is being aligned within a screen quad.

Share this post


Link to post
Share on other sites
Advertisement
To center your text horizontally, you'd supply to the DrawText method (ID3DXFont interface) a rectangle (parameter pRect) as large as the screen's width, and then use the flag DT_CENTER (paremeter Format) to specify that it should be horizontally centered.
A better example (assuming a 640x480 resolution) :

RECT rt = { h, 0, h + 50, 640 };
pFont->DrawText(..., ..., ..., &rt, DT_CENTER, ...);

To center the text vertically :

RECT rt = { w, 0, w + 100, 480 };
pFont->DrawText(..., ..., ..., &rt, DT_SINGLELINE | DT_VCENTER, ...);

Share this post


Link to post
Share on other sites
Well thats not working -- now I just see nothing.

Okay I'll be more clear.
I have a quad at position x,y,w,h == 15, 400, 150, 50
I need to center text within the quad and also make sure the width of the quad changes given the length of the text is larger than 150.

So what values should I feed the RECT?

Share this post


Link to post
Share on other sites
You might want to check the Win32 API function GetTextExtentPoint32, which retrieves the width in pixels of a given string, using the font selected in an HDC that's passed to the function.

Share this post


Link to post
Share on other sites
Quote:
Original post by Coder
You might want to check the Win32 API function GetTextExtentPoint32, which retrieves the width in pixels of a given string, using the font selected in an HDC that's passed to the function.


The GDI functions will give similar but different results for some strings, so if you're using D3DXFont, use the CALCRECT flag to get the size. If you're going to use GDI rendering, use the GDI calls.

Share this post


Link to post
Share on other sites
I use DT_CALCRECT, then take the resulting rect struct and subtract half its width from its X coordinate, and draw with the new rect struct. Works just fine.

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!