While normal ttf render functions take const char* for the text they're gonna render TTF_RenderUNICODE_Solid() function takes const Uint*.
I used this structure to create textures from ttf surfaces while working with ASCII characters:
Text = TTF_RenderText_Solid(times, title.c_str(), MakeColor(255, 0, 255));
ButtonTexture = SDL_CreateTextureFromSurface(renderer, Text);
.
And when I wanted to work with unicode I tried this:
Text = TTF_RenderUNICODE_Solid(times, title.c_str(), MakeColor(255, 0, 255));
ButtonTexture = SDL_CreateTextureFromSurface(renderer, Text);
.
Because title.c_str() is const char* and the function wants const Uint16 I can't create the texture.
This is how I pass the title:
MenuButtons[0] = new CreateButton("TEXT");
void CreateButton(string title)
{
Text = TTF_RenderText_Solid(times, title.c_str(), MakeColor(255, 0, 255));
ButtonTexture = SDL_CreateTextureFromSurface(renderer, Text);
//or
Text = TTF_RenderUNICODE_Solid(times, title.c_str(), MakeColor(255, 0, 255));
ButtonTexture = SDL_CreateTextureFromSurface(renderer, Text);
}
Question: How can I convert my string to Uint16?
I have seen two versions of TTF_RenderText_Solid
. One supports utf-8
, one supports latin1
. When your version supports utf-8
you simply need a string where the text is encoded in this format. utf-8
and latin-1
use a simple char
as storage unit so you need to look that up in the documentation to know that. Let's assume your version supports latin1
then it covers more characters than your expected ascii
character range.
However, that is still not what you want then. So when you want to use TTF_RenderUNICODE_Solid
your text-producer must deliver UTF-16
characters. So you need to know where the content from title
comes from and how it's encoded.
For a fast example you can try static text (with a c++11 compiler):
const Uint16 text[]=u"Hello World! \u20AC";
This should also help: The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)