我使用SDL_TTF发生某种奇怪的内存泄漏。多次调用我的调整大小函数时,我看到程序的RAM使用量从10mb增加到20mb,然后停留在那里。我可以再次调整我想要的大小,但是永远不会超出或缩小。
这是我的文字课:
#ifndef TEXT_H
#define TEXT_H
class Text
{
public:
Text(){
srcRect.x = 0;
srcRect.y = 0;
srcRect.w = 0;
srcRect.h = 0;
dstRect.x = 0;
dstRect.y = 0;
dstRect.w = 0;
dstRect.h = 0;
good = false;
visable = true;
sprite = NULL;
font = NULL;
ZeroMemory(text, sizeof(text));
ZeroMemory(fontPath, sizeof(fontPath));
}
~Text(){
// Something
}
void SetFont(const char* fontPath, int fontSize){
// Check if a font is already loaded
if(font != NULL) TTF_CloseFont(font);
// Reset the fontPath string
if(strcmp(this->fontPath, fontPath)){
ZeroMemory(this->fontPath, sizeof(this->fontPath));
strcpy(this->fontPath, fontPath);
}
// Set the new fontSize
this->fontSize = fontSize;
// Load the new fontface and size
font = TTF_OpenFont(fontPath, fontSize);
// Check for errors
if(font == NULL){
cout << "Failed loading font: " << fontPath << endl;
good = false;
return;
} else good = true;
// Set the new font
this->font = font;
}
void Initialize(int x, int y){
this->dstRect.x = x;
this->dstRect.y = y;
}
void Load(SDL_Renderer* renderer, const char* text, const char* fontPath, int fontSize, SDL_Color text_color){
// Check if a texture is already loaded
if(sprite != NULL) SDL_DestroyTexture(sprite);
// Set the new text
if(strcmp(text, this->text)){
ZeroMemory(this->text, sizeof(this->text));
strcpy(this->text, text);
}
// Set the color
this->text_color = text_color;
// Set the new font
SetFont(fontPath, fontSize);
// Load the new sprite
SDL_Surface* loader;
good = LoadSDLTexture(renderer, &loader, &sprite, TTF_RenderText_Blended, this->font, this->text, this->text_color);
// Set the new width and height
this->srcRect.w = loader->w;
this->srcRect.h = loader->h;
TTF_SizeText(this->font, this->text, &dstRect.w, &dstRect.h);
// Free the sprite
SDL_FreeSurface(loader);
}
void Draw(SDL_Renderer* renderer){
if(good && visable) SDL_RenderCopy(renderer, sprite, &srcRect, &dstRect);
else cout << "Text is not good!" << endl;
}
void ResizeFont(SDL_Renderer* renderer, int newSize){
Load(renderer, this->text, this->fontPath, newSize, text_color);
}
void ChangeFontFace(SDL_Renderer* renderer, const char* newPath){
if(!strcmp(this->fontPath, newPath)){
cout << "Cannot change old font to the same font!" << endl;
return;
}
Load(renderer, this->text, newPath, this->fontSize, this->text_color);
}
char fontPath[50] = {0};
TTF_Font* font;
int fontSize;
char text[50] = {0};
SDL_Color text_color;
SDL_Rect srcRect, dstRect;
SDL_Texture* sprite;
bool good, visable;
};
#endif
这是LoadSDLTexture函数:
bool LoadSDLTexture(SDL_Renderer* renderer, SDL_Surface** loader, SDL_Texture** sprite, SDL_Surface* loaderFunction(TTF_Font*, const char*, SDL_Color), TTF_Font* font, const char* text, SDL_Color text_color){
// Load the surface and texture
*loader = loaderFunction(font, text, text_color);
*sprite = SDL_CreateTextureFromSurface(renderer, *loader);
// Check for loading errors
if(*loader == NULL || *sprite == NULL){
color(12);
cout << "Failed loading text: \"";
color(14);
cout << text;
color(12);
cout << "\"";
color(7);
}
// Check for loader surface specific errors
if(!*loader){
cout << "\n\tLoader == NULL\n\t->" << TTF_GetError() << endl;
return false;
}
// Check for texture sprite specific errors
if(!*sprite){
cout << "\n\tSprite == NULL\n\t" << TTF_GetError() << endl;
return false;
}
// Print "Success" if everything is fine
color(10);
cout << "Successfully loaded text: \"";
color(11);
cout << text;
color(10);
cout << "\"\n";
color(7);
return true;
}
如果您看到此类的其他潜在问题,请随意说。