SDL_TTF和SDL 2不在一起工作.给出未处理的异常

SDL_TTF and SDL 2 not working together. Giving Unhandled Exception

本文关键字:SDL 未处理 异常 工作 在一起 TTF      更新时间:2023-10-16

所以我试图在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;
    }

我使用的是SDL_TTF的32位visual studio版本。

如果你能帮助修复这个错误,我们将不胜感激。非常感谢。

所需的行为是,它可以加载文本,而不会最终给出异常。

其他人也有这个错误,但解决方案没有帮助(因为我在更新的repo上找不到visual studio版本):让SDL_ttf在SDL2 中发挥作用

编辑1:

在更新到SDL_TTF 2的稍新版本后,错误仍未修复,有什么建议吗?

编辑2:

呼叫后:

SDL_GetError()
IMG_GetError()
TTF_GetEror()

这是控制台中的输出:

CreateTexture(): UNKNOWN
CreateTexture(): UNKNOWN
CreateTexture(): UNKNOWN

似乎"TTF_RenderText_Solid"正在返回NULL。顺便说一下,我使用的是SDL_TTF的最新版本。

正如您引用的帖子中所建议的,您应该更新SDL_ttf版本,使其能够与SDL2一起使用。

以下是可用于visual studio的SDL_TTF SDL2版本。http://hg.libsdl.org/SDL_ttf/file/62fc3433538d/VisualC

您将找到允许您构建SDL_ttf库的SDL_ttf.sln;)。