让SDL_ttf与SDL2玩得很好
Getting SDL_ttf to play nice with SDL2
我最近开始从使用SDL(1.2.15)迁移到SDL2(2.0.0),并且以前依赖于使用扩展库SDL_ttf(2.0.11)来渲染字体。当我尝试使用与SDL2版本一SDL相同的文本库(诚然,尚未正式发布)时,它编译得很好。然而,当我运行可执行文件时(我目前正在使用VS2012作为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的Surfaces定义不兼容,因此当它试图将SDL_Surfaces转换为SDL_Texture时,它会翻转出来。
我不认为我是唯一一个遇到这个问题的人,所以有没有SDL_ttf的变通方法/更新版本可以解决这个问题,或者我应该推迟到SDL2,直到我可以让字体正常工作?
您检查了SDL_TTF的Mercurial存储库吗?它似乎已经针对SDL2
进行了更新,同步最新的代码并构建它肯定是值得的。
相关文章:
- 我编写了代码将十进制分数转换为其二进制等效数.它编译得很好,但在执行时挂起
- 很好的语法来获取对向量/数组数据的大小引用?
- 我认为我的代码很好,但它在 cin a 之后停止并且没有进一步?
- 尽管一切看起来都很好,但值不会交换
- 为什么不同类型层次结构的指针之间的dynamic_cast定义得很好?
- 在 C++11 中利用 int*_t、int_fast*_t 和 int_least*_t 之间的差异的一个很好的例子是
- 对于短字符串来说,这是一个很好的哈希函数吗?
- 为什么指标有时效果很好,有时效果不佳?写下霍夫曼代码
- Red Hat:使用<atomic>编译很好,但链接器找不到__atomic_store_16;什么库?
- 使用移位的无符号数字作为数组的索引号是一种很好的做法
- 是否很好地使用状态模式来维护当前选定的对象?
- C++|以一种很好的方式将树(不一定是二进制的)打印到stdout
- 静态库单独使用很好,但在引用时会抛出错误
- 如果验证容器的大小并在同一条件语句下访问元素,这是很好的做法吗?
- 可变参数函数模板不能很好地使用 std::function 作为参数
- 有没有办法关闭文件? fclose 不能很好地工作
- 引用的静态强制转换强制模板实例化,其中不完整的类型很好
- libc++:为什么流关闭后仍然很好
- SDL2在Fedora 21中调整大小bug,但在窗口中很好
- 让SDL_ttf与SDL2玩得很好