问题
I have a menu where there is a lot of text being rendered that can change in size/color/position, so I made two functions in my menu class...:
void drawText(string text,int text_size,int x,int y, Uint8 r,Uint8 g,Uint8 b);
void updateTexts();
The updateTexts() function sits in the game loop and contains many drawText functions, When I launch the program I notice the programs memory gradually increase from 4mb about 1gb (it should stay at 4mb) then it crashes. I assume the problem exists because TTF_OpenFont" is run constantly though I needed a way to be able to create new font sizes on the fly as my menu changes based on the users input.
Is there a better way to do this?
code for the two functions:
void Menu::drawText(string text,int text_size,int x,int y, Uint8 r,Uint8 g,Uint8 b)
{
TTF_Font* arial = TTF_OpenFont("arial.ttf",text_size);
if(arial == NULL)
{
printf("TTF_OpenFont: %s\n",TTF_GetError());
}
SDL_Color textColor = {r,g,b};
SDL_Surface* surfaceMessage = TTF_RenderText_Solid(arial,text.c_str(),textColor);
if(surfaceMessage == NULL)
{
printf("Unable to render text surface: %s\n",TTF_GetError());
}
SDL_Texture* message = SDL_CreateTextureFromSurface(renderer,surfaceMessage);
SDL_FreeSurface(surfaceMessage);
int text_width = surfaceMessage->w;
int text_height = surfaceMessage->h;
SDL_Rect textRect{x,y,text_width,text_height};
SDL_RenderCopy(renderer,message,NULL,&textRect);
}
void Menu::updateTexts()
{
drawText("Item menu selection",50,330,16,0,0,0);
drawText("click a menu item:",15,232,82,0,0,0);
drawText("item one",15,59,123,0,0,0);
drawText("item two",15,249,123,0,0,0);
drawText("item three",15,439,123,0,0,0);
drawText("item four",15,629,123,0,0,0);
}
回答1:
Each opened font, created surface and created texture uses memory.
If collection of different resources that you require is limited, e.g. only 3 different text_size
, it's better to create them once and then reuse.
For example by storing them in some kind of cache:
std::map<int, TTF_Font*> fonts_cache_;
TTF_Font * Menu::get_font(int text_size) const
{
if (fonts_cache_.find(text_size) != fonts_cache_.end())
{
// Font not yet opened. Open and store it.
fonts_cache_[text_size] = TTF_OpenFont("arial.ttf",text_size);
// TODO: error checking...
}
return fonts_cache_[text_size];
}
void Menu::drawText(string text,int text_size,int x,int y, Uint8 r,Uint8 g,Uint8 b)
{
TTF_Font* arial = get_font(text_size)
...
}
Menu::~Menu()
{
// Release memory used by fonts
for (auto pair : fonts_cache_)
TTF_CloseFont(pair.second);
...
}
For dynamic resources that should be allocated on each method invocation you should not forget to free them. Currently you are not releasing memory of TTF_Font* arial
, SDL_Texture* message
; do:
void Menu::drawText(string text,int text_size,int x,int y, Uint8 r,Uint8 g,Uint8 b)
{
...
TTF_CloseFont(arial);
SDL_DestroyTexture(message);
}
来源:https://stackoverflow.com/questions/29003216/sdl2-rendering-text-issues