Search code examples
c++sdlpixeltruetypesdl-ttf

TTF_RenderTextSolid() returns a Surface with 1 byte per pixel?


This code:

TTF_Font * titania = TTF_OpenFont( "chintzy.ttf",28);
SDL_Color textColor = {255,255,0};
SDL_Surface * textSurface = TTF_RenderText_Solid(titania,"Its Working!",textColor);
std::cout << (int)textSurface->format->BytesPerPixel;

Prints the number one, meaning that the surface returned by TTF_RenderTextSolid has one byte per pixel. If I am correct it should be 4 bytes per pixel. Does anyone know why this is happening?


Solution

  • Looks like it's doing exactly what the documentation says it ought to:

    Solid

    Create an 8-bit palettized surface and render the given text at fast quality with the given font and color. The pixel value of 0 is the colorkey, giving a transparent background when blitted. Pixel and colormap value 1 is set to the text foreground color. This allows you to change the color without having to render the text again. Palette index 0 is of course not drawn when blitted to another surface, since it is the colorkey, and thus transparent, though its actual color is 255 minus each of the RGB components of the foreground color. This is the fastest rendering speed of all the rendering modes. This results in no box around the text, but the text is not as smooth. The resulting surface should blit faster than the Blended one. Use this mode for FPS and other fast changing updating text displays.

    If you want 32bpp you need to use the *_Blended() variants:

    Blended

    Create a 32-bit ARGB surface and render the given text at high quality, using alpha blending to dither the font with the given color. This results in a surface with alpha transparency, so you don't have a solid colored box around the text. The text is antialiased. This will render slower than Solid, but in about the same time as Shaded mode. The resulting surface will blit slower than if you had used Solid or Shaded. Use this when you want high quality, and the text isn't changing too fast.