我有一个菜单,在这里有很多的文字渲染,可以在大小/颜色/位置变化的,所以我在菜单类所做的两个功能...:
void drawText(string text,int text_size,int x,int y, Uint8 r,Uint8 g,Uint8 b);
void updateTexts();
该updateTexts()函数坐落在游戏循环,并含有许多的drawText功能,当我启动该程序,我注意到程序内存约为1GB 4MB逐渐增加(它应该保持在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);
}