Skip to main content
added 169 characters in body
Source Link
Sciborg
  • 604
  • 4
  • 15

The problem is that you're calling TTF_OpenFont every frame, and you only need to load a font once. TTF_OpenFont is a very intensive function that unsurprisingly uses a lot of memory, because it's only designed for one use. It would be a good idea to read the documentation for more on how it's intended to be used. (Documentation link.)

The best approach here is to save your fonts somewhere as a field and only open them once at the initialization stage of your program, and then pass that font to your rendering functions each time you want to draw a text box with that font on the screen.

Like this:

TTF_Font* Font = TTF_OpenFont("Arial.ttf", 18);

And then when you need to use it to create a text surface object for rendering (SDL_Surface), you can simply pass in the initialized font object.

SDL_Surface* Surface = TTF_RenderText_Blended_Wrapped(Font...);

You'll also need to call TTF_CloseFont in your destructor to free up the memory when the font is no longer needed, depending on how you've implemented all of this.

The problem is that you're calling TTF_OpenFont every frame, and you only need to load a font once. TTF_OpenFont is a very intensive function that unsurprisingly uses a lot of memory, because it's only designed for one use. It would be a good idea to read the documentation for more on how it's intended to be used. (Documentation link.)

The best approach here is to save your fonts somewhere as a field and only open them once at the initialization stage of your program, and then pass that font to your rendering functions each time you want to draw a text box with that font on the screen.

Like this:

TTF_Font* Font = TTF_OpenFont("Arial.ttf", 18);

And then when you need to use it to create a text surface object for rendering (SDL_Surface), you can simply pass in the initialized font object.

SDL_Surface* Surface = TTF_RenderText_Blended_Wrapped(Font...);

The problem is that you're calling TTF_OpenFont every frame, and you only need to load a font once. TTF_OpenFont is a very intensive function that unsurprisingly uses a lot of memory, because it's only designed for one use. It would be a good idea to read the documentation for more on how it's intended to be used. (Documentation link.)

The best approach here is to save your fonts somewhere as a field and only open them once at the initialization stage of your program, and then pass that font to your rendering functions each time you want to draw a text box with that font on the screen.

Like this:

TTF_Font* Font = TTF_OpenFont("Arial.ttf", 18);

And then when you need to use it to create a text surface object for rendering (SDL_Surface), you can simply pass in the initialized font object.

SDL_Surface* Surface = TTF_RenderText_Blended_Wrapped(Font...);

You'll also need to call TTF_CloseFont in your destructor to free up the memory when the font is no longer needed, depending on how you've implemented all of this.

Source Link
Sciborg
  • 604
  • 4
  • 15

The problem is that you're calling TTF_OpenFont every frame, and you only need to load a font once. TTF_OpenFont is a very intensive function that unsurprisingly uses a lot of memory, because it's only designed for one use. It would be a good idea to read the documentation for more on how it's intended to be used. (Documentation link.)

The best approach here is to save your fonts somewhere as a field and only open them once at the initialization stage of your program, and then pass that font to your rendering functions each time you want to draw a text box with that font on the screen.

Like this:

TTF_Font* Font = TTF_OpenFont("Arial.ttf", 18);

And then when you need to use it to create a text surface object for rendering (SDL_Surface), you can simply pass in the initialized font object.

SDL_Surface* Surface = TTF_RenderText_Blended_Wrapped(Font...);