I’m using SDL2
and SDL2_ttf
libs in the code below. It runs OK for now but I’m anxious for leaking memory. Do I need to uncomment the commented lines and free
the surface & texture pointers each time the loop calls the function? Do TTF_RenderText_Blended
& SDL_CreateTextureFromSurface
functions allocate new memory space each time the loop calls the renderText
function?
...
...
...
typedef struct tSomeStruct {
SDL_Window *sdlWind;
SDL_Renderer *sdlRend;
SDL_Surface *sdlSurf;
SDL_Texture *sdlText;
...
...
...
} tSomeStruct;
tSomeStruct vars;
void textRender(
char *someText,
char *otherText
) {
vars.sdlSurf = TTF_RenderText_Blended(vars.font, someText, (SDL_Color) { 0, 127, 191, 255 });
vars.sdlText = SDL_CreateTextureFromSurface(vars.sdlRend, vars.sdlSurf);
SDL_RenderCopy(vars.sdlRend, vars.sdlText, NULL, &(SDL_Rect){1125, 100, 150, 32});
// SDL_FreeSurface(vars.sdlSurf);
// SDL_DestroyTexture(vars.sdlText);
vars.sdlSurf = TTF_RenderText_Blended(vars.font, otherText, (SDL_Color) { 0, 127, 191, 255 });
vars.sdlText = SDL_CreateTextureFromSurface(vars.sdlRend, vars.sdlSurf);
SDL_RenderCopy(vars.sdlRend, vars.sdlText, NULL, &(SDL_Rect){1125, 300, 150, 32});
// SDL_FreeSurface(vars.sdlSurf);
// SDL_DestroyTexture(vars.sdlText);
return;
}
...
...
...
int main(
int argc,
char *argv[]
) {
if (sdlInit() == SUCCESS) {
varsInit();
while (running) {
eventEvaluate();
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xff);
SDL_RenderClear(renderer);
textRender();
...
...
...
SDL_RenderPresent(renderer);
SDL_Delay(timeDelay);
}
sdlQuit();
}
return(0);
}
2