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
Weird black pixels around TTF text
luca00555


Joined: 04 Jan 2015
Posts: 33
Location: The Netherlands
The title says it all, here is an example:


And here is the code that I'm using:
Code:
SDL_Texture* loadText(std::string textureText[], SDL_Color textColor, int tTC, int width, int height)
{
   int texty = 0;
   SDL_Texture* textTexture = NULL;
   SDL_Surface* textSurface = NULL;
   textSurface = SDL_CreateRGBSurface(0,width,height,32,0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);
   SDL_Rect rect;
   for(int i = 0; i < tTC; i++)
   {
      SDL_Surface* textSurfaceTemp = TTF_RenderText_Blended( gFont, textureText[i].c_str(), textColor );
      if(textSurfaceTemp != NULL)
      {
         rect.x = 0;
         rect.y = texty;
         rect.h = textSurfaceTemp->h * SCREEN_HEIGHT / 1080.f;
         rect.w = textSurfaceTemp->w * SCREEN_WIDTH / 1920.f;
         texty += textSurfaceTemp->h * SCREEN_HEIGHT / 1080.f;
         SDL_BlitSurface(textSurfaceTemp, NULL, textSurface, &rect);
         SDL_FreeSurface(textSurfaceTemp);
         
      }
   }
   textTexture = SDL_CreateTextureFromSurface(gRenderer, textSurface);
   SDL_FreeSurface(textSurface);
   textSurface = NULL;
   return textTexture;
}

The color used for textColor = 255,255,255. tTC is the amount of strings the array textureText contains.
Does anyone know why this causes weird black pixels around the text? Thanks in advance.
MikeyPro


Joined: 14 Feb 2015
Posts: 15
Either 1) the surface you create has the wrong format, or 2) The font you're using is causing it.

I would first try simplifying your method, liike my code below:

Code:
   
TTF_Font* txtfont = GetFont(ResourcePool[Font]);
   Texture = SDL_CreateTextureFromSurface(Renderer,
      TTF_RenderText_Blended(txtfont, Text, color(255,255,255,255)));
   SDL_QueryTexture(Texture, NULL, NULL, &Width, &Height);
   SrcRect = rect(0,0,Width,Height);
   DestRect = rect((int)fX, (int)fY, Width, Height);


Secondly, if that doesn't solve it, try a different font. Try a couple different fonts.
luca00555


Joined: 04 Jan 2015
Posts: 33
Location: The Netherlands
Will do, another thing I've been thinking about, you could also make the text non anti aliased and 8x bigger than requested format. That way, you'll size it down and texture filtering will do the smoothing.
luca00555


Joined: 04 Jan 2015
Posts: 33
Location: The Netherlands
I have found the problem, when directly converting the text surface into a texture everything seems okay.
When doing it my way, SDL_BlitSurface(textSurfaceTemp, NULL, textSurface, &rect); causes alpha value of letter edges to turn dark.
How can I fix this or do it without SDL_BlitSurface();?
luca00555


Joined: 04 Jan 2015
Posts: 33
Location: The Netherlands
I fixed it by putting SDL_FillRect(textSurface, NULL, SDL_MapRGBA(textSurface->format, textColor.r, textColor.g, textColor.b, 0)); after
textSurface = SDL_CreateRGBSurface(0,width,height,32,0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);
luca00555


Joined: 04 Jan 2015
Posts: 33
Location: The Netherlands
Final result:
Code:
SDL_Texture* loadText(std::string textureText[], SDL_Color textColor, int tTC, int width, int height)
{
   int texty = 0;
   int w = 0;
   int h = 0;
   SDL_Texture* textTexture = NULL;
   SDL_Surface* textSurface = NULL;
   textSurface = SDL_CreateRGBSurface(0,width,height,32,0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);
   SDL_FillRect(textSurface, NULL, SDL_MapRGBA(textSurface->format, textColor.r, textColor.g, textColor.b, 0));
   SDL_Rect rect;
   for(int i = 0; i < tTC; i++)
   {
      TTF_SizeText(gFont, textureText[i].c_str(), &w, &h);
      rect.x = 0;
      rect.y = texty;
      rect.h = h * SCREEN_HEIGHT / 1080.f;
      rect.w = w * SCREEN_WIDTH / 1920.f;
      texty += h * SCREEN_HEIGHT / 1080.f;
      SDL_BlitSurface(TTF_RenderText_Blended(gFont, textureText[i].c_str(), textColor), NULL, textSurface, &rect);
   }
   textTexture = SDL_CreateTextureFromSurface(gRenderer, textSurface);
   SDL_FreeSurface(textSurface);
   textSurface = NULL;
   return textTexture;
}
MikeyPro


Joined: 14 Feb 2015
Posts: 15
Have you tried using TTF_SetFontHinting???