I would like to use SDL_ttf to draw lettering over a potentially complex image, such that the lettering is opaque but no background box is drawn; the background is transparent. Is this possible? Here’s my attempt; I had hoped that by making bgColor transparent I might achieve this result. But no, my lettering still appears surrounded by black boxes.
(I am aware of this answer but at 14 years old it looks radically out of date. Have things been made better in SDL2?)
void DrawText(int x, int y, const char* text, SDL_Color textColor, TTF_Font *font) {
int width, height;
SDL_Color bgColor = { 0, 0, 0, 0};
SDL_Surface* textSurface =
TTF_RenderText_Shaded(font, text, textColor, bgColor);
if( textSurface == NULL )
printf( "Unable to render text surface! SDL_ttf Error: %sn", TTF_GetError() );
else {
//Create texture from surface pixels
SDL_Texture *mTexture = SDL_CreateTextureFromSurface( gRenderer, textSurface );
if( mTexture == NULL ) printf( "Unable to create texture from rendered text! SDL Error: %sn", SDL_GetError() );
else {
//Get image dimensions
width = textSurface->w;
height = textSurface->h;
}
//Get rid of old surface
SDL_FreeSurface( textSurface );
//Render to screen
int descent = TTF_FontDescent(gFont);
SDL_Rect renderQuad = { x, y-height-descent, width, height };
SDL_RenderCopy( gRenderer, mTexture, NULL, &renderQuad);
SDL_DestroyTexture(mTexture);
}
}