Search code examples
c++unicodesdl-ttfsdl-2

Can't see my text using SDL TTF for SDL 2


I guess I do all right, but anyway I don't see my text. Official docs are poor (or obsolete for SDL 2).

if (TTF_Init())
    printf("%s\n", TTF_GetError());
font = TTF_OpenFont("Fonts/DinCompRegular.ttf", 24);
if (!font)
    printf("%s\n", TTF_GetError());
SDL_Color fontColor = { 255, 0, 0, 255 };
u16 text[] = { '2', 's', 'ä', 'Щ' };
SDL_Surface *info = TTF_RenderUNICODE_Solid(font, text, fontColor);
SDL_Rect rect = { info->w, info->h, 0, 0 };
SDL_Surface *place = SDL_CreateRGBSurface(0, info->w, info->h, 32, 0, 0, 0, 0);

And in main loop:

    SDL_BlitSurface(info, 0, place, &rect);
    SDL_FreeSurface(info);
    SDL_RenderPresent(renderer);

Solution

  • I solved it by myself:

    u16 text[] = { '2', L'Щ', L'ä', L'Ø', '\0' };
    SDL_Surface *info = TTF_RenderUNICODE_Blended(font, text, fontColor);
    

    Loop:

        SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, info);
        SDL_Rect src = { 0, 0, info->w, info->h };
        SDL_Rect dst = { 0, 0, info->w, info->h };
        SDL_RenderCopy(renderer, texture, &src, &dst);
        SDL_DestroyTexture(texture);