TTF_RenderText_Solid 使我的 ram 增加到 2GB 并返回 NULL
TTF_RenderText_Solid makes my ram's increase until 2GB and returned NULL
我正在 C/C++ 上使用 SDL 库,并且
我有这种在屏幕上写文字的方法:
//string, pos.x and pos.y
void writeText(int x, int y, char *str)
{
SDL_Surface* textSurface;
SDL_Texture *mTexture;
SDL_Color textColor = { 0XFF, 0XFF, 0XFF };
SDL_Rect src, dst;
SDL_Renderer* gRenderer = getRenderer();
textSurface = TTF_RenderText_Solid(font, str, textColor);
mTexture = SDL_CreateTextureFromSurface(gRenderer, textSurface);
src.x = 0; dst.x = x;
src.y = 0; dst.y = y;
src.w = dst.w = textSurface->w;
src.h = dst.h = textSurface->h;
SDL_RenderCopy(gRenderer, mTexture, &src, &dst);
}
我在 do-while 的 main 函数上使用这个方法来编写玩家 SCORE。即,使内存从 20MB(不使用此方法)增加到 2024MB(使用此方法),然后返回 "read access violation"。我认为问题在于此方法在每次 do-while 迭代中创建一个对象,并且不受控制地增加内存。
我怎样才能绕过这个?
我是 C 指针的新手,我尝试了类似这样的方法来重用 textSurface 而无需在每次迭代时创建新对象:
SDL_Surface textSurfaceCreate() {
SDL_Surface textSurface;
return textSurface;
}
在 Main 上,调用
SDL_Surface textSurface = textSurfaceCreate();
并在 do-while 中:
writeText(SDLSurface* textSurface, int x, int y, char *str);
但是当我编译时,Visual Studio 给我一些错误。
你能展示你在 Visual Studio 中的错误吗?
您实际上已经提供了自己的解决方案。如果您对原始代码块进行了这些更改,那么您不会在每次调用此函数时创建新的 SDL_Texture 和 SDL_Surface(我想每帧一次)那么您就不会没有体验到内存的大量增加。您可以简单地在函数之外的其他地方设置表面和纹理,然后将它们传递给函数,例如:
SDL_Surface* textSurface = nullptr;
SDL_Texture* mTexture = nullptr;
void writeText(SDL_Surface* surface, SDL_Texture* texture, int x, int y, char *str)
{
SDL_Color textColor = { 0XFF, 0XFF, 0XFF };
SDL_Rect src, dst;
SDL_Renderer* gRenderer = getRenderer();
surface = TTF_RenderText_Solid(font, str, textColor);
texture = SDL_CreateTextureFromSurface(gRenderer, surface);
src.x = 0; dst.x = x;
src.y = 0; dst.y = y;
src.w = dst.w = surface->w;
src.h = dst.h = surface->h;
SDL_RenderCopy(gRenderer, texture, &src, &dst);
}
如果您不想这样做,那么您可以在绘制调用之后在此函数的末尾创建和销毁纹理和表面。我相信该功能将类似于 SDL_DestroySurface() 和 SDL_DestroyTexture().
我正在 C/C++ 上使用 SDL 库,并且 我有这种在屏幕上写文字的方法:
//string, pos.x and pos.y
void writeText(int x, int y, char *str)
{
SDL_Surface* textSurface;
SDL_Texture *mTexture;
SDL_Color textColor = { 0XFF, 0XFF, 0XFF };
SDL_Rect src, dst;
SDL_Renderer* gRenderer = getRenderer();
textSurface = TTF_RenderText_Solid(font, str, textColor);
mTexture = SDL_CreateTextureFromSurface(gRenderer, textSurface);
src.x = 0; dst.x = x;
src.y = 0; dst.y = y;
src.w = dst.w = textSurface->w;
src.h = dst.h = textSurface->h;
SDL_RenderCopy(gRenderer, mTexture, &src, &dst);
}
我在 do-while 的 main 函数上使用这个方法来编写玩家 SCORE。即,使内存从 20MB(不使用此方法)增加到 2024MB(使用此方法),然后返回 "read access violation"。我认为问题在于此方法在每次 do-while 迭代中创建一个对象,并且不受控制地增加内存。
我怎样才能绕过这个?
我是 C 指针的新手,我尝试了类似这样的方法来重用 textSurface 而无需在每次迭代时创建新对象:
SDL_Surface textSurfaceCreate() {
SDL_Surface textSurface;
return textSurface;
}
在 Main 上,调用
SDL_Surface textSurface = textSurfaceCreate();
并在 do-while 中:
writeText(SDLSurface* textSurface, int x, int y, char *str);
但是当我编译时,Visual Studio 给我一些错误。
你能展示你在 Visual Studio 中的错误吗?
您实际上已经提供了自己的解决方案。如果您对原始代码块进行了这些更改,那么您不会在每次调用此函数时创建新的 SDL_Texture 和 SDL_Surface(我想每帧一次)那么您就不会没有体验到内存的大量增加。您可以简单地在函数之外的其他地方设置表面和纹理,然后将它们传递给函数,例如:
SDL_Surface* textSurface = nullptr;
SDL_Texture* mTexture = nullptr;
void writeText(SDL_Surface* surface, SDL_Texture* texture, int x, int y, char *str)
{
SDL_Color textColor = { 0XFF, 0XFF, 0XFF };
SDL_Rect src, dst;
SDL_Renderer* gRenderer = getRenderer();
surface = TTF_RenderText_Solid(font, str, textColor);
texture = SDL_CreateTextureFromSurface(gRenderer, surface);
src.x = 0; dst.x = x;
src.y = 0; dst.y = y;
src.w = dst.w = surface->w;
src.h = dst.h = surface->h;
SDL_RenderCopy(gRenderer, texture, &src, &dst);
}
如果您不想这样做,那么您可以在绘制调用之后在此函数的末尾创建和销毁纹理和表面。我相信该功能将类似于 SDL_DestroySurface() 和 SDL_DestroyTexture().