本文介绍了SDL2呈现文本问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个菜单,其中有很多文本正在呈现,可以改变大小/颜色/位置,所以我在我的菜单类中做了两个功能...:

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();

updateTexts()函数位于游戏循环中,包含许多drawText函数,当我启动程序我注意到程序内存从4mb约增加1gb(它应该保持在4mb),然后它崩溃。我假设这个问题存在,因为TTF_OpenFont不断运行,虽然我需要一种方法,以便能够创建新的字体大小在飞行,我的菜单根据用户输入更改。

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.

有更好的方法吗?

这两个函数的代码:

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);
}


推荐答案

并创建纹理使用内存。

如果收集您需要的不同资源有限,例如只有3个不同的 text_size ,最好先创建一次,然后重用。
例如,将它们存储在某种缓存中:

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);
  ...
}

方法调用,你不应该忘记释放它们。目前您不会释放 TTF_Font * arial SDL_Texture * message 的记忆; do:

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);
}

这篇关于SDL2呈现文本问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-28 12:46
查看更多