1
\$\begingroup\$

In my game, there are many units and for each units there's an information widget attached next to them. I use SDL as the game rendering engine.

I currently want to display their ID for debugging purposes.

However, for each unit, I create a texture with their ID, I render it, then I destroy it.

Needless to say that when there are about 40 units on my screen, the game gets pretty laggy (if its any indication, my game currenly weights 25 MB)

How can I get over this?

\$\endgroup\$
1
\$\begingroup\$

There are a few ways to do this.

You could render a texture with SDL_TTF that contains all the characters you want to be able to display, and then use that texture as a spritesheet, and pick characters from there using source rectangles, rendering them next to each other. This is a bit more work since you need to calculate correct character offsets etc.

Another alternative would be to simply cache the textures you render with SDL_TTF. Something like an LRU-cache with enough space to fit everything rendered during a single frame would do the job. So you would render your texture, save it in the cache, and if it's needed every frame, it won't be destroyed from the cache, until the text is not rendered anymore, and other textures take its place.

I have personally used both approaches, and while the latter one is simpler to implement, the first one has better performance.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.