ubadub
ubadub

Reputation: 3880

Why are my SDL_ttf fonts so pixelated and ugly? Are there better ways of rendering fonts?

Here is an example:

example Here is the corresponding code:

TTF_Font *Sans = TTF_OpenFont("resources/fonts/lato/Lato-Semibold.ttf", 36);

    if( Sans == NULL )
    {
        std::cout << "Failed to load font! SDL_ttf Error: " << TTF_GetError() << std::endl;
    }

    else
    {
        SDL_Color White = {255, 255, 255};

        SDL_Surface *surfaceMessage = TTF_RenderText_Blended(Sans, "GAME OVER", White);

        SDL_Texture *Message = SDL_CreateTextureFromSurface(renderer_, surfaceMessage);

        SDL_Rect Message_rect;
        Message_rect.x = 100;
        Message_rect.y = 100;
        Message_rect.w = 500;
        Message_rect.h = 500;

        SDL_RenderCopy(renderer_, Message, NULL, &Message_rect);
        SDL_RenderPresent(renderer_);

}

(yes, I free the surface later)

Upvotes: 2

Views: 3622

Answers (1)

keltar
keltar

Reputation: 18399

TTF_RenderText_Blended and other SDL_ttf font rendering functions produces surface based on font size specified when font was opened (along with font style itself and symbols in the text line). You rescale that result to 500x500 rectangle, which causes both proportions distortion and blurier image, as source image have different dimensions.

To avoid that you should avoid rescaling first - use surfaceMessage->w and surfaceMessage->h (or SDL_QueryTexture on Message) to get original dimensions, then RenderCopy to the same sized rectangle.

Of course if text is static and not localised pre-rendering this static image in some graphics editor is also a good option.

As a side note, it may be copy-paste oversimplification, but it appears you opening font and recreating your target texture on each frame. This is very slow and provides almost no benefits.

Upvotes: 6

Related Questions