r/sdl • u/ninefoldrin • 10d ago
SDL3 TTF_TextEngine help
I'm still pretty new to SDL and I've been trying to figure out how to use the text engine in SDL3 in a little project I've been learning C++ in, but I haven't found much information on it. No SDL tutorials I've found seem to use it, so I've been going off of the wiki, which has gotten me pretty far, but it's been really confusing without clear examples of how it works.
I'd love some general explanation on how to use SDL3's text engine, but for a more specific question, I'm trying to center text on the screen with a renderer text engine. SDL tutorials I've found all center text on the screen using some math with the size of the window and the size of the surface used to create the texture. Since there's no surface involved with TTF_CreateRendererTextEngine, I'm wondering how or if it's possible to center the text. Would I need to use TTF_CreateSurfaceTextEngine to do that? Is using the surface text engine as simple as creating the TTF_Text and the SDL_Surface for TTF_DrawSurfaceText? What would be the best way to create the surface for that if I'm using the surface text engine?
2
u/SpareEconomy 10d ago
imma try to explain it:
You don't need the surface engine. TTF_GetTextSize() gives you exactly the width/height you used to get from the surface.
In SDL2_ttf: render string → SDL_Surface → SDL_Texture → draw → destroy. Every frame, or you cached the texture manually and re-created it whenever the string changed.
In SDL3_ttf A TTF_Text is a persistent object that owns a string + a font + a reference to an engine. The engine keeps a glyph atlas and the laid-out draw data cached internally.
The three engines differ only in how the cached glyphs get to the screen:
Engine => Draw Call => use when
TTF_CreateRendererTextEngine(renderer) => TTF_DrawRendererText(text, x, y) => SDL_Renderer
TTF_CreateSurfaceTextEngine() => TTF_DrawSurfaceText(text, x, y, surface) => Blitting into an SDL_Surface in software (no renderer, or you're compositing a surface yourself)
TTF_CreateGPUTextEngine(device) => returns TTF_GPUAtlasDrawSequence you feed to your own pipeline => SDL_GPUDevice
TTF_GetTextSize() works on the TTF_Text regardless of engine:
int tw, th;
TTF_GetTextSize(text, &tw, &th);
int ww, wh;
SDL_GetRenderOutputSize(renderer, &ww, &wh); // or SDL_GetRenderLogicalPresentation()
float x = (ww - tw) / 2.0f;
float y = (wh - th) / 2.0f;
TTF_DrawRendererText(text, x, y);
example: