我正在制作使用SDL_TTF
和SDL_Image
的游戏。我有一个名为browseinventory
的函数,它允许您在将鼠标悬停在项目上时检查项目的统计信息。发生内存泄漏的地方。一旦你徘徊在一个项目槽上,程序就会检查所述槽中是否有任何内容,如果有,则Text
类对象设置了项目的参数然后再渲染。
我很确定错误在Text类中,因为一旦我在一个项目上盘旋,内存使用量会迅速增加。
Text
课程:
class Text
{
public:
Text() {w = 0; h = 0; saying = ""; Textthing = NULL;}
~Text(){destroy();}
void destroy(){SDL_DestroyTexture(Textthing); Textthing = 0; saying = "", w = 0, h = 0;}
void render(int, int, int, SDL_Renderer*);
void settext(string, SDL_Color);
void rendertest(int, int, SDL_Renderer*);
int rows;
private:
string saying;
SDL_Texture *Textthing;
int w, h;
unsigned int flag1 = 0, flag2, loops = 0;
string saying1;
unsigned int counter;
bool spaceflag = 0;
};
render()
:
void Text::render(int x, int y, int maxchars = 100, SDL_Renderer *Temp = Saviour)
{
rows = 0;
flag1 = 0, flag2 = maxchars, loops = 0;
int increment = maxchars;
if (resolution == "800x600")
{
flag2 = maxchars*0.8;
increment = maxchars*0.8;
}
spaceflag = 0;
do
{
spaceflag = 0;
for (counter = flag1; counter < flag2 && counter < saying.length(); counter++)
{
if (counter > flag2-10 && saying[counter] == ' ')
{
spaceflag = 1;
break;
}
saying1 += saying[counter];
}
asdfgfa1.settext(saying1);
asdfgfa1.rendertest(x, y + loops*(SCREEN_HEIGHT/19), Temp);
saying1 = "";
loops++;
rows++;
if (flag2 > saying.length())
break;
if (spaceflag == 0)
{
flag1 = flag2;
flag2 += increment;
}
else
{
flag1 = counter;
flag2 = counter + increment;
}
}while (1);
asdfgfa1.destroy();
SDL_DestroyTexture(Textthing);
Textthing = 0;
}
setText()
:
void Text::settext(string stuff, SDL_Color Colour = {255, 255, 255})
{
saying = stuff;
SDL_Surface *Surf = TTF_RenderText_Solid(Arial, stuff.c_str(), Colour);
w = Surf->w;
h = Surf->h;
Textthing = SDL_CreateTextureFromSurface(Saviour, Surf);
SDL_FreeSurface(Surf);
Surf = 0;
}