Segmentation Fault hunting: reusing SDL_Texture o SDL_Surface

Hi All,

this is part of the Segmentation Fault hunting: SDL_Rect on heap or stack for SDL_RenderCopy? so I will avoid the introduction.

In our codebase that is at GitHub - kismet/tetris-2024 we defined a “cache” system for avoiding to reload several times the same Font / Image. I’m wondering if it safe to store SDL_Texture o SDL_Surface for reause or if each SDL_RenderCopy / SDL_RenderPresent it consumes the Texture / Surface

As you can see from the code in easy_sdl.h and easy_sdl.cpp we are using

typedef struct Image_Asset {
    uint16_t height;
    uint16_t width;
    SDL_Texture* texture;
    SDL_Surface* surface;
} Image_Asset_t;

typedef struct Font_Asset {
    int size;
    bool monospaced;
    TTF_Font* font;
} Font_Asset_t;

for cache the loaded Image / Font and we use the loadAsset(char *) for loading from a file (and, at the moment, the path is used as key for avoiding to loading the same file several times)

Best Regards,
Stefano

It is highly recommended to pre-load textures etc at the start of the application/game and then reuse/re-render them every frame.
SDL_RenderCopy and other SDL-specific functions doesn’t consume or destroys the data for you, so you can safely render an SDL_Texture how many times you want.
One exception is the SDL_DestroyTexture function, which will indeed destroy the texture and its data.

For all the textures, fonts and other data that you create at the start of the application/game, remember to destroy those objects with corresponding Destroy* function.

1 Like

Yes, this is safe and should always be done for static textures. In other words, regardless of whether it is a texture, font, sound or any other data, if their content does not change during the game session, such data should be loaded only once and at such a moment that it does not interfere with the game’s operation (e.g. does not lower the framerate 'at).

Why did you decide on the caching system? Tetris is a relatively simple game, so unless you have several gigabytes of assets, such a system becomes unnecessary complexity. If a game takes up less than a gigabyte of memory after loading absolutely all of its data, then it should load everything at startup — once, without reloading it later. Unless it is intended for very low-end devices (e.g. a laptop with 2 gigs of RAM) or those that have very limited resources (like basic Raspberry Pi).

Great!

Actually, the project has two main goal:

  1. The creation of Tetris kind of game which was achived by my student :slight_smile:
  2. The creation of EasySDL library which I could use the next year for teaching game creation during the computer science class at the high school, and of course other people may use it too. This gool is definitely WIP

In the context of point 2, caching was more an excercise for me rather then an actual need. The real requirement for EasySDL (as the name states) is to simplify the usage for most of the common action (i.e. drawAsset or drawText which hide all the SDL details)

Ciao,
Stefano

P.S.: As report on the another thread the segmentation fault was found and the student could present the project see Segmentation Fault found - Thank you!

1 Like