Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Centering text.

Credits go to 23yrold3yrold for helping out!
This thread is locked; no one can reply to it. rss feed Print
Centering text.
julian_boolean
Member #8,201
January 2007

I want to put text over a graphic, like a button or whatever, and have it centered. Right now I'm just doing it manually by adding onto the x position of the text until it looks good, which is a pain in the ass.

23yrold3yrold
Member #1,134
March 2001
avatar

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

julian_boolean
Member #8,201
January 2007

Omigod omigod omigod! Are you single?

SonShadowCat
Member #1,548
September 2001
avatar

Jonatan Hedborg
Member #4,886
July 2004
avatar

Please bookmark this: http://www.allegro.cc/manual/

Please. :)

(oh, and the alternative method would be to use text_length(font,"the text"), and subtract half that from the x value)

julian_boolean
Member #8,201
January 2007

Top right corner. The previous question was much more then a thank you ;) but thank you. I seen this function before but never really thought anything of it at the time because I didn't need it, then I forgot about it.

Edit:

That works.. But if I move the image the text was over then the text will remain in the same spot (because I'm no longer giving it the image's current x and y position.)

Nm I think.

textout_centre_ex(buffer, font, caption.c_str(), xPos + width / 2, yPos, WHITE, -1);

TeamTerradactyl
Member #7,733
September 2006
avatar

If you create a button, you know the button's coordinates. When you use textout_centre_ex, simply use the button's xPos,yPos coordinates, and the centered text should correctly align no matter where the button is located on the screen.

  textout_centre_ex(buffer, font, caption.c_str(), button_xPos + width / 2, button_yPos, WHITE, -1);

Go to: