首页 文章

让SDL_ttf与SDL2一起玩得很好

提问于
浏览
4

我最近开始从使用SDL(1.2.15)迁移到SDL2(2.0.0),之前依赖于使用扩展库SDL_ttf(2.0.11)来渲染字体 . 当我尝试使用与SDL2的第一版SDL相同的文本库时(虽然尚未正式发布),它编译得很好 . 当我运行可执行文件时(我目前正在使用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的Surfaces定义不兼容,因此当它试图将SDL_Surface转换为SDL_Texture时,它会翻转掉 .

我不认为我是唯一一个遇到这个问题的人,所以SDL_ttf的解决方法/更新版本是否可以解决这个问题,或者我是否应该继续转移到SDL2直到我能够使字体工作?

1 回答

  • 3

    您是否查看了Mercurial Repository for SDL_TTF?它似乎已经为 SDL2 更新了,它绝对值得同步最新代码并构建它 .

相关问题