9

I just started using SDL2_ttf. I've figured out how to get some text on the screen with TTF_RenderText_Blended, but how do can I get it to do line-breaks and automatic wrapping?

  1. It doesn't seem to support \n; it just creates a space instead of going down a line. Is there a way to add support for this? Specifically, using the proper line-height of the text, not by multiple calls to RenderText at different Y coordinates.
  2. Given an X, Y coordinate and a width, how can I have it automatically go down a line whenever that width is reached (breaking between words)?
mpen
  • 272,448
  • 266
  • 850
  • 1,236

2 Answers2

30

Instead of using TTF_RenderText_Blended, use TTF_RenderText_Blended_Wrapped. It takes additional parameter: width in pixels after which the text will break into next line.

Mars
  • 867
  • 2
  • 13
  • 22
  • Is that part of the standard library? I can't find any documentation on it. If that exists, that's sweet. – mpen Aug 24 '13 at 19:40
  • 1
    It's within latest SDL_ttf. I cannot find proper documentation(dunno why there is no reference to it at all), but I can assure you that it works. – Mars Aug 24 '13 at 20:29
  • Must be in VCS then.....doesn't exist in the 2.0.11 sources. Hrm... Just downloaded 2.0.12 and it's there. Awesome. – mpen Aug 25 '13 at 18:26
  • 8
    To anyone else reading this: `\n` also works with the *_Wrapped functions. – mpen Aug 25 '13 at 19:42
  • 3
    There's a bug with this where if your text has no spaces it does not wrap. – Stanley Bak May 28 '16 at 21:58
  • Just wanna add more info. Mars is right, I checked via `nm -a /usr/local/lib/libSDL2_ttf.a` for symbol table; on macOS (after self compile and install, but should be similar location if use `brew`). There are `_TTF_RenderText_Blended_Wrapped`, `_TTF_RenderUNICODE_Blended_Wrapped`, `_TTF_RenderUTF8_Blended_Wrapped`, and there is no document whatsoever. Need to peek into source for function arguments. – haxpor Aug 24 '18 at 10:10
  • `TTF_RenderText_Blended_Wrapped` will only put text on a new line if the string contains whitespace, and is only capable breaking lines on whitespace characters. It will not automatically break adjacent alphanumeric characters onto separate lines. – Michael Lee Oct 01 '21 at 21:36
2

SDL_TTF does not do wrapping, you have to write your own.

TTF_Font* ttf;
TTF_SizeText(ttf, "Hello World", &w, &h);

gives you the width and height of a string.

parkydr
  • 7,596
  • 3
  • 32
  • 42
  • I was hoping for something a little more in-depth, but I guess I can write my own font-manager class. – mpen Jul 26 '13 at 17:04
  • I can have a go at some code if you like. C or C++? I've always just split any long messages into several short ones. – parkydr Jul 26 '13 at 17:14
  • C#, actually. I'm writing a wrapper around SDL2. You don't need to write any code, I just wanted to be aware of any pitfalls. The one thing I can't figure out how to do right now is kerning. – mpen Jul 26 '13 at 17:18
  • SDL-TTF should do that, it uses proportional fonts. I'd just use TTF_SizeText to get the legth of each string you add. – parkydr Jul 26 '13 at 17:24
  • Doesn't look like it from my tests. If you write something like "AVA" the "V" should come over top of the "A"s a little bit, but it doesn't look like they overlap at all. Maybe I was just using a bad font though... will need to experiment more. – mpen Jul 26 '13 at 17:32