Hey all, I’m writing a program with a good deal of text in the GUI, and hitting a roadblock with the quality I’m getting out of SDL_ttf.
The problem seems to be that that TTF_RenderUTF8_Blended (and related functions, and the Shaded ones as well), render with a blurring edge size that doesn’t scale down adequately as the font size decreases. At 72pt it looks good enough, but at 12, text that should be white is more of a middle grey and very blurry.
I’m not scaling the text at all, the TTF file itself is fine – elsewhere it renders sharp, bright, and clear.
Is there just a limit to the quality achievable with SDL_ttf?
The attached screenshot compares the quality in my test program (all of which is below) to a TTF file preview provided by macos’s “font book.” Note that the text in my program should be pure white.
#include <stdio.h>
#include "SDL2/SDL.h"
#include "SDL2/SDL_ttf.h"
#define OPEN_SANS "/Users/charlievolow/Downloads/open-sans/OpenSans-Regular.ttf"
int main()
{
SDL_Init(SDL_INIT_VIDEO);
TTF_Init();
SDL_Window *win = SDL_CreateWindow("TTF Test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_RESIZABLE);
SDL_Renderer *rend = NULL;
Uint32 render_flags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC;
rend = SDL_CreateRenderer(win, -1, render_flags);
TTF_Font *fonts[8];
fonts[0] = TTF_OpenFont(OPEN_SANS, 12);
fonts[1] = TTF_OpenFont(OPEN_SANS, 14);
fonts[2] = TTF_OpenFont(OPEN_SANS, 16);
fonts[3] = TTF_OpenFont(OPEN_SANS, 20);
fonts[4] = TTF_OpenFont(OPEN_SANS, 24);
fonts[5] = TTF_OpenFont(OPEN_SANS, 30);
fonts[6] = TTF_OpenFont(OPEN_SANS, 36);
fonts[7] = TTF_OpenFont(OPEN_SANS, 42);
SDL_Color white = {255, 255, 255, 255};
SDL_Color grey = {40, 40, 40, 255};
int quit = 0;
while (!quit) {
SDL_Event e;
while (SDL_PollEvent(&e)) {
if (e.type == SDL_QUIT) {
quit = 1;
}
}
SDL_SetRenderDrawColor(rend, grey.r, grey.g, grey.b, 255);
SDL_RenderClear(rend);
SDL_Rect txt_box = {10, 10, 0, 0};
for (int i=0; i<8; i++) {
SDL_Surface *surface = TTF_RenderUTF8_Shaded(fonts[i], "I look meh.", white, grey);
SDL_Texture *texture = SDL_CreateTextureFromSurface(rend, surface);
SDL_QueryTexture(texture, NULL, NULL, &(txt_box.w), &(txt_box.h));
SDL_RenderCopy(rend, texture, NULL, &txt_box);
txt_box.y += txt_box.h + 5;
}
SDL_RenderPresent(rend);
SDL_Delay(10);
}
}