Sign in to follow this  
J2902

FTGL Text Align

Recommended Posts

I've been trying to figure out how to center text with FTGL in C++. I noticed that under the namespace FTGL there is an enum called TextAlign. I've been searching for awhile and have been unable to find some sort of reference on how to use it. Any ideas? Thanks.

Share this post


Link to post
Share on other sites
Do you have to center the text through FTGL? I'm unfamiliar with FTGL, so I'm not sure what your setup is like.

In any case, if FTGL can provide you with the width of the text as it will appear on-screen, and if you know the width of the box that you want to center the text in (such as the screen's resolution dimensions), you can do the centering yourself. Code would look like this:

int center_pos = (screen_width - text_width) / 2;

The resulting center_pos variable would provide you with the x-coordinate for the left-hand side of the text.

Share this post


Link to post
Share on other sites
Yea, the only problem with that is I don't know how to get the length of a string. >.>

(Also, centering text inside a box is not the problem: the problem is rotating the text such that the rotation is around the center.)

Currently, the setup is really flexible. If need be, I can change to a different library in a cinch. (Heck, it was designed for that.)

Share this post


Link to post
Share on other sites
Quote:
Original post by J2902
Yea, the only problem with that is I don't know how to get the length of a string.

What type of strings are you using? Character arrays or the string class?

For character arrays, you can get the length by using the strlen() method. For standard strings, use the .length() member function.

Share this post


Link to post
Share on other sites
What I meant was the length of the string as it appeared on the screen. Which, I discovered, you can get using this.


string str = "hello world";

// I don't know if the abs is required because I don't know if lower is always on the bottom right and upper is always on the top left
abs((m_font->BBox(str.c_str()).Upper().X()) - m_font->BBox(str.c_str()).Lower().X());



That effectively solves the issue, but I would still like to know how to use TextAlign.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this