Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        3年前关闭。
                                                                                            
                
        
因此,我试图在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版本。

请如果您可以帮助解决此错误,将不胜感激。谢谢。

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

其他人有此错误,但解决方案无济于事(因为我无法在更新的存储库中找到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。

最佳答案

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

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

您会找到SDL_ttf.sln,您可以通过它来构建SDL_ttf库;)。

10-04 14:44
查看更多