I want to render text. My problem is that I don't know how to use the char field text of SDL's SDL_TextInputEvent. According to the docs it's a "null-terminated input text in UTF-8 encoding". As far as I understand UTF8 takes 8 bits and a char has more or less 8 bits. But why does SDL offer me an array, size of 32, so 8 bits or 1 byte * 32 = 32 bytes if UTF8 only need 1 byte?
I need to figure out the value so that I can parse it to FreeType to get the glyph's index. What's the "layout" of that char field?
Best How To :
UTF-8 is called UTF-8 because it encodes text as a sequence of 8-bit units. Each Unicode character (code point) will be encoded as 1, 2, 3, or 4 bytes. Likewise, UTF-16 encodes characters (code points) in 16-bit units, each character will take 16 bits or 32 bits. There are text encodings where each character is exactly 8 bits, such as ASCII1 or the various Latin encodings, but they are obsolete and you should avoid them unless you have text already encoded in those encodings.
It is also possible for multiple characters to result from the user pressing a key on the keyboard, which explains why the field is so large.
In order to render UTF-8 text properly using FreeType, load the UTF-8 text into a HarfBuzz buffer, which will run a shaping algorithm and give you a list of glyph indexes and glyph locations.
If you don't want to use HarfBuzz (maybe you only care about English), then you can do it yourself. You will have to read the UTF-8 text as a sequence of Unicode code points, and pass those code points to the Unicode character map in your font using FreeType, and then draw the resulting glyphs. This will often give you incorrect results, and the results may not look as nice, but it is usually good enough for English text.
The Wikipedia article on UTF-8 describes how to decode it. Please ignore the mention of 5-byte or 6-byte characters, which are not permitted in UTF-8.
1: Actually 7 bits.