Getting SDL_ttf to play nice with SDL2

耗尽温柔 提交于 2019-12-07 08:20:49

问题


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.

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:

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;
}

It's getting tripped up by the

SDL_Texture *texture = SDL_CreateTextureFromSurface(mRenderer.get(), surf);

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.

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?


回答1:


Did you check out the Mercurial Repository for SDL_TTF? It seems to have been updated for SDL2, it would definitely be worth it to synch the latest code and build that.



来源:https://stackoverflow.com/questions/17557474/getting-sdl-ttf-to-play-nice-with-sdl2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!