问题描述
我最近开始从使用SDL(1.2.15)迁移到SDL2(2.0.0),并且以前依赖于使用扩展库SDL_ttf(2.0.11)来呈现字体.当我尝试使用与版本SDL和SDL2(公认尚未正式发布)相同的文本库时,它可以很好地编译.但是,当我运行可执行文件时(此刻我正在使用VS2012 for Desktop),出现以下错误:
I've recently started migrating from using SDL (1.2.15) to SDL2 (2.0.0), and had previously depended on using the extension library SDL_ttf (2.0.11) to render fonts. When I try to use the same text library I used with version one SDL with SDL2 (admittedly not yet officially released), it compiles just fine. When I run the executable, though (I'm using VS2012 for Desktop at the moment), I get the following error:
Unhandled exception at 0x6C7D8C24 (SDL2.dll) in test.exe: 0xC0000005: Access violation reading location 0x00000045.
据我所知,这是由于以下代码段所致.我创建了一个Window类来封装一些常用的SDL函数:
From what I can gather, this is due to the following bits of code. I have created a Window class to encapsulate some usual SDL functions:
window.cpp:
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_Surface转换为SDL_Texture时,它将翻转.
line, where the created SDL_Surface is incompatible with SDL2's definition of Surfaces, and so when it tries to convert the SDL_Surface into an SDL_Texture, it flips out.
我不认为我是唯一遇到此问题的人,所以是否有解决此问题的SDL_ttf解决方案/更新版本,还是应该推迟到SDL2,直到我可以使用字体为止?
I don't think I'm the only one to have run into this issue, so is there a workaround/updated version of SDL_ttf that fixes this, or should I hold off on moving to SDL2 until I can get fonts working?
推荐答案
您是否签出了 Mercurial存储库 SDL_TTF?它似乎已经更新了SDL2
,同步最新代码并进行构建绝对是值得的.
这篇关于使SDL_ttf与SDL2配合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!