Getting SDL_ttf for playing with SDL2

I recently started porting using SDL (1.2.15) to SDL2 (2.0.0) and previously depended on using the SDL_ttf (2.0.11) extension library to render fonts. When I try to use the same text library that I used with SDL version 1 with SDL2 (admittedly not yet officially released), it compiles just fine. When I run the executable, though (I am using VS2012 for the 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 assemble, this is due to the following bits of code. I created the Window class to encapsulate some common 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 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; } 

Will work

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

where the created SDL_Surface is incompatible with the definition of Surface SDL2, and therefore, when it tries to convert SDL_Surface to SDL_Texture, it is inverted.

I don’t think that I am the only one who has encountered this problem, so there is a workaround / updated version of SDL_ttf that fixes this, or should I hold on when switching to SDL2 until I can get the fonts to work

+4
source share
1 answer

Have you checked the Mercurial Repository for SDL_TTF? It seems that it has been updated for SDL2 , it would definitely be worth synchronizing the latest code and building it.

+3
source

Source: https://habr.com/ru/post/1490567/


All Articles