SDL2 & SDL_ttf Best Practice


I’m still very new to SDL and I’m just learning as I go along. I have some basics set up in my game but I came to a point where I want to include font rendering (initially for debug output to the screen).

Looking at various tutorials and the SDL_ttf library I require clarification on something. The SDL_ttf will give you an SDL_Surface which you can then create a texture from to allow for hardware rendering. The whole process though requires a font file loading. My main query is - Is it safe to leave the font file open until the font is no longer needed (could be exiting the app).

I saw a tutorial which closed the font file immediately after creating the SDL_Texture. My thoughts were if this was used for frequently updated text e.g. a frame rate counter, this would be highly inefficient and it would be better to hold the font file open (assuming there is no negative impact). An the follow up from this would be if the font file is held open, would it be locked so no other font could use it?


Yeah, I’ve never had problems with keeping a font open on either Windows or Linux.------------------------
Nate Fries

After some more research I have found that

doesn’t hold the file on disk open. Therefore once this is called the font is then in memory to be used as required and the file on disk is free for use again. This negates the need for constant reloading if coded appropriately.

The font is then just released from memory with

when it is no longer needed.