Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do line-breaks and line-wrapping with SDL-TTF?

Tags:

sdl

sdl-ttf

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)?
like image 469
mpen Avatar asked Jul 25 '13 02:07

mpen


2 Answers

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.

like image 122
Mars Avatar answered Sep 17 '22 09:09

Mars


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.

like image 33
parkydr Avatar answered Sep 19 '22 09:09

parkydr