让SDL_ttf与SDL2兼容

4

最近我开始从使用SDL (1.2.15)迁移到SDL2 (2.0.0),之前我依赖于使用扩展库SDL_ttf (2.0.11)来渲染字体。当我尝试使用同样的文本库与SDL2(尽管它还没有正式发布)一起使用时,编译通过了。但是当我运行可执行文件时(目前我使用VS2012 for Desktop),出现以下错误:

Unhandled exception at 0x6C7D8C24 (SDL2.dll) in test.exe: 0xC0000005: Access violation reading location 0x00000045.

据我所了解,这归因于以下代码片段。我创建了一个Window类来封装一些常用的SDL函数:
window.cpp:
SDL_Texture* Window::RenderText(const std::string &message, const std::string &fontFile, SDL_Color color, int fontSize){
//Open the font
TTF_Font *font = nullptr;
font = TTF_OpenFont(fontFile.c_str(), fontSize);
if (font == nullptr)
    throw std::runtime_error("Failed to load font: " + fontFile + TTF_GetError());

//Render the message to an SDL_Surface, as that's what TTF_RenderText_X returns
SDL_Surface *surf = TTF_RenderText_Blended(font, message.c_str(), color);
SDL_Texture *texture = SDL_CreateTextureFromSurface(mRenderer.get(), surf);
//Clean up unneeded stuff
SDL_FreeSurface(surf);
TTF_CloseFont(font);

return texture;
}

它被这个问题绊住了。
SDL_Texture *texture = SDL_CreateTextureFromSurface(mRenderer.get(), surf);

在这里,创建的SDL_Surface与SDL2对Surface的定义不兼容,因此当它尝试将SDL_Surface转换为SDL_Texture时,它会失控。

我认为我不是唯一遇到这个问题的人,那么是否有解决方法/更新版本的SDL_ttf来解决这个问题?或者我应该暂时不要转移到SDL2,直到我能让字体工作?

1个回答

3
你查看过SDL_TTF的Mercurial Repository了吗?它似乎已经更新到SDL2,同步最新代码并构建会非常值得。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接