The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
Poor font quality with SDL_ttf regardless of antialiasing
mkutr


Joined: 30 Jun 2015
Posts: 2
When rendering text using SDL_ttf, the result is always awful-looking, even when I use the TTF...Blended() functions (I'm running Windows 8.1, if that matters).
I've tried with 6+ different fonts and none work well. The text is rendered to a 640x480 texture, which is then rendered to a window of the same resolution. Why is the text quality turning out so poor?

Example screenshot with the GNU free fonts (24 pt font):
(Fast = solid mode, Quality = blended mode)



Relevant code:

Code:

void renderText() {
   int step = 64;    // space out the lines
   mc.textureLoader.drawTextFast("GNU FreeMono Fast.", 0, 0, mono, fontColor);//format("%s", worldClock.checkSeconds()) ~ "s", 0, 0, font, fontColor);
   mc.textureLoader.drawTextQuality("GNU FreeMono Quality.", 0, step, mono, fontColor);//format("%s", worldClock.checkSeconds()) ~ "s", 0, 0, font, fontColor);
   mc.textureLoader.drawTextFast("GNU FreeSans Fast.", 0, step*2, sans, fontColor);
   mc.textureLoader.drawTextQuality("GNU FreeSans Quality.", 0, step*3, sans, fontColor);
   mc.textureLoader.drawTextFast("GNU FreeSerif Fast.", 0, step*4, serif, fontColor);
   mc.textureLoader.drawTextQuality("GNU FreeSerif Quality.", 0, step*5, serif, fontColor);
}

void drawTextFast(string text, int x, int y, TTF_Font* font, SDL_Color fg) {
   if (font is null)
      return;
   SDL_Surface* surf = TTF_RenderText_Solid(font, text.toStringz(), fg);
   SDL_Texture* tex = SDL_CreateTextureFromSurface(mc.rend, surf);
   drawTexture(tex, x, y);
   SDL_FreeSurface(surf);
   SDL_DestroyTexture(tex);
   return;
}

void drawTextQuality(string text, int x, int y, TTF_Font* font, SDL_Color fg) {
   if (font is null)
      return;
   SDL_Surface* surf = TTF_RenderText_Blended(font, text.toStringz(), fg);
   SDL_Texture* tex = SDL_CreateTextureFromSurface(mc.rend, surf);
   drawTexture(tex, x, y);
   SDL_FreeSurface(surf);
   SDL_DestroyTexture(tex);
   return;
}

void drawTexture(SDL_Texture* img, int x, int y) {
   SDL_Rect pos;
   pos.x = x;
   pos.y = y;
   SDL_QueryTexture(img, null, null, &pos.w, &pos.h);
   SDL_RenderCopy(mc.rend, img, null, &pos);
   return;
}
MrTAToad


Joined: 13 Feb 2014
Posts: 205
Location: Chichester, England
Shouldn't the second load parameter for TTFLoadBlended be the font size in points, and not a colour? It does give very good results, so will need to check against my loading and printing system.
mkutr


Joined: 30 Jun 2015
Posts: 2
Well, this is embarassing. Embarrassed

Turns out the texture I rendered to was 640x640, instead of 640x480. When it got stretched to the window, the text was cut off and distorted in wierd ways. I fixed it and now the text looks fine:



Thanks anyway!
MrTAToad


Joined: 13 Feb 2014
Posts: 205
Location: Chichester, England
No problem!