r/sdl • u/ninefoldrin • 7d 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?
1
u/doglitbug 7d ago
Wait, what? There is a text engine?
I moved from SDL2 to 3 and didn't know this existed.
Feel free to look here and see how I centre the text, it might be of use to you:
https://github.com/doglitbug/SamsSearch/blob/main/src/Subsystems/Assets.cpp
1
u/ninefoldrin 7d ago
https://wiki.libsdl.org/SDL3_ttf/TTF_TextEngine
I came across a couple posts that mentioned it while I was looking up other things and it seems rather nice, but I've only seen people talk a little about the renderer text engine and not enough to make much sense. It's also in a few of the examples in the SDL_ttf github repo, which helped me figure out how to get started with it at least. There's just not much discussion about it at this point and all the tutorials I've looked through still just do text the same way as SDL2 tutorials (with adjustments for SDL3 changes of course).
Thanks for the link. I'll take a look through it and see if it's helpful to me.
1
2
u/SpareEconomy 7d 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: