我正在开发的游戏中遇到严重的 ram 使用情况。我让它运行并看到它使用了超过 1.5 GB 的内存,从最初的 30 MB 使用。我已经确定了 2 个函数的主要原因,这些函数粘贴在下面。通过研究这个问题,我得出结论,其中一个问题是使用 TTF_RenderText_Blended 创建一个 TTF 表面会创建一个新的表面,而该表面没有被释放。我该如何释放这个自动制作的表面?我不能使用 SDL_FreeSurface,因为我没有表面名称。
我对第二个函数中发生的事情有点困惑。我假设正在发生类似的事情(一个函数被调用并创建了第二个没有被释放的表面),但我不确定是什么原因造成的。这不是一个问题,因为我计划实现此功能的不同版本,它不需要创建任何表面..
TTF 功能:
void speak(int id, string message, SDL_Renderer *ren, TTF_Font *font, SDL_Color color, int x, int y)
{
SDL_Surface *surf = TTF_RenderText_Blended(font, message.c_str(), color);
if(surf == nullptr) cout << "surf error";
SDL_Texture *texture = SDL_CreateTextureFromSurface(ren, surf);
SDL_FreeSurface(surf);
if(texture == nullptr) cout << "tex error";
SDL_Rect rect;
SDL_QueryTexture(texture, NULL, NULL, &rect.w, &rect.h);
rect.x = x-rect.w/2 - camera_xpos*10;
rect.y = y+sprite_set_array[idList[id].sprite].y_offset - rect.h - camera_ypos*10;
SDL_RenderCopy(ren, texture, NULL, &rect);
}
第二个功能(用于显示精灵):
void displayAtPos(int id, int sprite_num, int x, int y, SDL_Window *win, SDL_Renderer *ren)
{
if(idList[id].type == 5) sprite_num = 11;
string sprite = sprite_set_array[sprite_num].getString(idList[id].facing, idList[id].sprite_counter);
SDL_Texture *texture = nullptr;
SDL_Surface *bmp = SDL_LoadBMP(sprite.c_str());
if(bmp==nullptr) {bmp = SDL_LoadBMP("junk.bmp");}
SDL_SetColorKey( bmp, SDL_TRUE, SDL_MapRGB(bmp->format, 255, 0, 255) );
SDL_Texture *tex = SDL_CreateTextureFromSurface(ren, bmp);
SDL_FreeSurface(bmp);
if (tex == nullptr) { cout << "SDL_CreateTextureFromSurface Error: " << SDL_GetError() << endl; }
SDL_Rect rect;
rect.x = x;
rect.y = y+sprite_set_array[idList[id].sprite].y_offset;
SDL_QueryTexture(tex, NULL, NULL, &rect.w, &rect.h);
SDL_RenderCopy(ren, tex, NULL, &rect);
if(idList[id].active_effect > 0 && idList[id].active_effect_counter > 0) //Display a texture from the status effects array.
{
SDL_RenderCopy(ren, effect_array[idList[id].active_effect_counter%3], NULL, &rect);
idList[id].active_effect_counter--;
}
}