Getting SDL_ttf to play nice with SDL2

4

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?

c++
sdl
sdl-ttf
asked on Stack Overflow Jul 9, 2013 by B. Elliott • edited Jul 9, 2013 by genpfault

1 Answer

3

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.

answered on Stack Overflow Jul 10, 2013 by emartel

User contributions licensed under CC BY-SA 3.0