【发布时间】:2015-03-12 14:48:43
【问题描述】:
我有一个菜单,其中有很多可以改变大小/颜色/位置的文本,所以我在菜单类中创建了两个函数...:
void drawText(string text,int text_size,int x,int y, Uint8 r,Uint8 g,Uint8 b);
void updateTexts();
updateTexts() 函数位于游戏循环中并包含许多 drawText 函数,当我启动程序时,我注意到程序内存逐渐从 4mb 增加到 1gb(应该保持在 4mb)然后它崩溃了。我认为问题存在是因为 TTF_OpenFont" 一直在运行,尽管我需要一种能够在我的菜单根据用户输入更改时动态创建新字体大小的方法。
有更好的方法吗?
两个函数的代码:
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);
}
【问题讨论】: