How to make line breaks and line wrapping using SDL-TTF?

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

  • It does not support \n ; he simply creates space instead of going down the line. Is there any way to add support for this? In particular, using the correct text height rather than multiple RenderText calls in different Y coordinates.
  • Given the X, Y coordinate and width, how can I automatically lower it along the line when this width is reached (gap between words)?
+6
source share
2 answers

Instead of TTF_RenderText_Blended use TTF_RenderText_Blended_Wrapped . An additional parameter is required: the width in pixels, after which the text will be split into the next line.

+18
source

SDL_TTF does not wrap, you must write your own.

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

gives the width and height of the line.

+2
source

Source: https://habr.com/ru/post/950222/


All Articles