所以我想在 SDL2 中制作一个文本渲染器。当我在游戏的每一帧更新文本时,最终它给了我这个错误:
Unhandled exception at 0x6C7B543D (SDL2.dll) in Games.exe: 0xC0000005: Access violation reading location 0x00000004.
这很奇怪,因为它不会立即给我异常,只是在一段时间后(这个时间似乎有所不同,而且我认为是 SDL_Mixer 导致它)当我“打破”异常时,我似乎是导致错误的行(当我从表面创建纹理时):
void Text::SetText(SDL_Renderer* rend, std::string message)
{
SDL_Color textCol = { Col.Red, Col.Green, Col.Blue, Col.Alpha };
//Load image at specified path
SDL_Surface* loadedSurface = TTF_RenderText_Solid(font, message.c_str(), textCol);
if (loadedSurface == NULL) Debug::Fatal("Could not load text");
//Create texture from surface pixels
/*EXCEPTION ON THIS LINE --->*/ SDL_Texture* newTexture = SDL_CreateTextureFromSurface(rend, loadedSurface);
if (newTexture == NULL) Debug::Fatal("Could not create texture from text");
Scale.x = loadedSurface->w;
Scale.y = loadedSurface->h;
//Get rid of old loaded surface
SDL_FreeSurface(loadedSurface);
Texture = newTexture;
}
我正在使用 32 位 Visual Studio 版本的 SDL_TTF。
如果您能帮助修复此错误,我们将不胜感激。谢谢你。
期望的行为是它可以加载文本,而最终不会给出异常。
其他人有这个错误,但解决方案没有帮助(因为我在更新的 repo 上找不到 Visual Studio 版本):Getting SDL_ttf to play nice with SDL2
编辑1:
更新到稍新版本的 SDL_TTF 2 后错误仍未修复,有什么建议吗?
编辑2:
调用后:
SDL_GetError()
IMG_GetError()
TTF_GetEror()
这是控制台中的输出:
CreateTexture(): UNKNOWN
CreateTexture(): UNKNOWN
CreateTexture(): UNKNOWN
似乎“TTF_RenderText_Solid”正在返回 NULL。顺便说一句,我使用的是最新版本的 SDL_TTF。