0
\$\begingroup\$

I'm trying to add text to my game using SDL_TTF, and it's working pretty well.

Except for one thing :

My text's SDL_Surface needs a width and a height, if I don't give it one it will automatically set it to 0, making the text invisible.

However, if I insert a value like 100 width and 100 height, it will stretch the image.

I need to know how I would convert ANY font size on the font arial to pixels in width and height.

\$\endgroup\$
  • \$\begingroup\$ The size of each font is different, I guess you only option is to try a size, get its width and height with TTF_SizeText() and then reduce it or increase it repeatedly \$\endgroup\$ – Xriuk Mar 18 '16 at 20:17
  • \$\begingroup\$ @Xriuk That would not give me a good high and width if the font size were to change during runtime though, right? \$\endgroup\$ – BiiX Mar 18 '16 at 20:19
  • \$\begingroup\$ Are you using surfaces or textures for rendering? \$\endgroup\$ – Tyyppi_77 Mar 18 '16 at 21:22
  • \$\begingroup\$ @BiiX You could implement this into a function, and then call it whenever your font changes size \$\endgroup\$ – Xriuk Mar 19 '16 at 13:35
  • \$\begingroup\$ @Tyyppi_77 I'm using textures. \$\endgroup\$ – BiiX Mar 19 '16 at 18:15
1
\$\begingroup\$

TTF_RenderText_Solid(font, const char* text, {r, g, b}); renders a surface with text on it. You could just use something like

SDL_Surface* text = TTF_RenderText_Solid(arial, "hi", {r, g, b});
int textWidth = text->w;
int textHeight = text->h;

You don't even need to make a blank surface for this method.

\$\endgroup\$
  • \$\begingroup\$ That does not work, it gives me a height and width of 0. \$\endgroup\$ – BiiX Mar 19 '16 at 18:14
  • 1
    \$\begingroup\$ Nevermind, it did work. I tried it before I asked the question and it didn't work... I must have done something really stupid. lol \$\endgroup\$ – BiiX Mar 19 '16 at 18:26
  • \$\begingroup\$ No problem. Good luck with the project! \$\endgroup\$ – Paolo Mazzon Mar 20 '16 at 0:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.