Search code examples
c++utf-8sdl-2

How to get utf-8 input from SDL_TEXTINPUT?


Here is a small code and when we press "é" key or special characters with for instance french keyboards, we get in the console weird characters such as "├®".

#include <iostream>
#include "SDL.h"
using namespace std;

int main(int argc, char** argv)
{
    SDL_Init(SDL_INIT_EVERYTHING);
    SDL_CreateWindow("test", 100, 100, 1920, 1080, SDL_WINDOW_RESIZABLE);
    bool opened = true;
    SDL_Event event;
    while(opened)
    {
        SDL_StartTextInput();
        SDL_WaitEvent(&event);
        switch(event.type)
        {
            case SDL_QUIT:
                {
                    opened = 0;
                    break;
                }
            case SDL_TEXTINPUT:
                {
                    cout << event.text.text << endl;
                    break;
                }
        }
    }
    return EXIT_SUCCESS;
}

PS: I am using SDL 2


Solution

  • Just have to use TTF_RenderUTF8_Blended for render (output in file was fine).