0

所以我想在 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。

4

1 回答 1

1

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

这是可用于 Visual Studio 的 SDL_TTF SDL2 版本。 http://hg.libsdl.org/SDL_ttf/file/62fc3433538d/VisualC

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

于 2016-04-03T17:10:03.050 回答