huzzm
huzzm

Reputation: 567

Rendering font with UTF8 in SDL_ttf

I am trying to render characters using the TTF_RenderUTF8_Blended method provided by the SDL_ttf library. I implemented user input (keyboard) and pressing 'ä' or 'ß' for example works fine. These are special characters of the German language. In this case, they are even in the extended ASCII 8-bit code, but even when I copy and paste some Greek letters for example, the fonts get rendered correctly using UTF8. (However not all the UNICODE glyphs you can find here (http://unicode-table.com/) am I able to render as I recognized during testing but I guess that is normal because the Arial font might not have every single glyph. Anyways most of the UNICODE glyphs work fine.)

My problem is that passing strings (parameter as const char*) the additional characters (to ASCII) aren't rendered correctly. So entering 'Ä', 'ß', or some other UNICODE chars with the keyboard at runtime works but passing them as a parameter to get - let's say a title for my game - inside the code like this does not work:

font_srf = TTF_RenderUTF8_Blended(font, "Hällö", font_clr);

I don't really understand why this is happening. What I get on the screen is:

H_ll_
And I am using _ to represent the typical vertical rectangle that the guy who gave the following speech used as a funny way of an introduction: https://www.youtube.com/watch?v=MW884pluTw8

Ironically, when I use TTF_RenderText_Blended(font, "Hällö", font_clr); it works because 'ä' and 'ö' are 8-bit extended ASCII encoded, but what I want is UNICODE support, so that does not help.

Edit & Semi-Solution

I kind of (not really good) fixed the problem, Because my input works fine, I just checked what values I get as input when I press 'ä', 'ß', ... on my keyboard using the following code:

const char* c = input.c_str();

for (int i = 0; i < input.length(); i++)
{
    std::cout << int(c[i]) << " ";
}

Then I printed those characters in the following way:

const char char_array[] = {-61, -74, -61, -97, '\0'};
const char* char_pointer = char_array;

-61, -74 is 'ö' and -61, -97 is 'ß'. This does fit the UTF8 encoding right?

This code works fine as well in case some of you were wondering. And I think this is what I will keep doing for now. Looking up the Hex-code for some Unicode glyphs isn't that hard.

But what I still can't figure out is how to get to the extended ASCII integer value of 246. Plus, isn't there a more human-friendly solution to my problem?

Upvotes: 2

Views: 5720

Answers (1)

Peter Stock
Peter Stock

Reputation: 450

If you have non-ASCII characters in a source file, the character encoding of that source code file matters. So in your text editor or IDE, you need to set the character set (e.g. UTF-8) when you save it.

Alternatively, you can use the \x... or \u.... format to specify non-ASCII characters using only ASCII characters, so source file encoding doesn't matter.

Microsoft doc, but not MS-specific:

https://msdn.microsoft.com/en-us/library/6aw8xdf2.aspx

Upvotes: 2

Related Questions