Skip to content

TTF_RenderGlyph_LCD doesn't work with transparent background #602

@leoncounter

Description

@leoncounter

I created a simple program to render the character 'A' in the top left corner of a window. I've set the foreground to be completely white and the background to be completely transparent. When using TTF_RenderGlyph_Solid, TTF_RenderGlyph_Shaded, and TTF_RenderGlyph_Blended, everything works correctly. When using TTF_RenderGlyph_LCD, the glyph appears to be completely transparent. If I use an opaque background, it renders correctly, but using the fully transparent background makes even the foreground transparent.

Result when using TTF_RenderGlyph_Shaded:

Image

Result when using TTF_RenderGlyph_LCD:

Image

Here's the code:

#define SDL_MAIN_USE_CALLBACKS 1
#include <SDL3/SDL.h>
#include <SDL3/SDL_main.h>
#include <SDL3_ttf/SDL_ttf.h>

static SDL_Window* window;
static SDL_Renderer* renderer;
static TTF_Font* font;
static SDL_Texture* glyph_texture;

SDL_AppResult SDL_AppInit(void** userdata, int ac, char** av)
{
	if (!SDL_Init(SDL_INIT_VIDEO))
	{
		SDL_Log("sdl init error: %s", SDL_GetError());
		return SDL_APP_FAILURE;
	}
	if (!SDL_CreateWindowAndRenderer("Glyph Test", 800, 600, 0, &window, &renderer))
	{
		SDL_Log("window/renderer creation error: %s", SDL_GetError());
		return SDL_APP_FAILURE;
	}
	if (!SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND))
	{
		SDL_Log("unable to set blend mode: %s", SDL_GetError());
		return SDL_APP_FAILURE;
	}
	SDL_SetRenderVSync(renderer, 1);

	TTF_Init();
	font = TTF_OpenFont("DejaVuSans.ttf", 128);
	if (!font)
	{
		SDL_Log("unable to open font: %s", SDL_GetError());
		return SDL_APP_FAILURE;
	}

	SDL_Color fg = {255, 255, 255, 255};
	SDL_Color bg = {0, 0, 0, 0};

	SDL_Surface* glyph_surface;
	//glyph_surface = TTF_RenderGlyph_Solid(font, 'A', fg);
	//glyph_surface = TTF_RenderGlyph_Shaded(font, 'A', fg, bg);
	//glyph_surface = TTF_RenderGlyph_Blended(font, 'A', fg);
	glyph_surface = TTF_RenderGlyph_LCD(font, 'A', fg, bg);

	if (!glyph_surface)
	{
		SDL_Log("unable to render glyph: %s", SDL_GetError());
		return SDL_APP_FAILURE;
	}

	glyph_texture = SDL_CreateTextureFromSurface(renderer, glyph_surface);
	if (!glyph_texture)
	{
		SDL_Log("unable to create texture: %s", SDL_GetError());
		return SDL_APP_FAILURE;
	}
	SDL_DestroySurface(glyph_surface);

	return SDL_APP_CONTINUE;
}

SDL_AppResult SDL_AppIterate(void* userdata)
{
	SDL_SetRenderDrawColor(renderer, 127, 127, 127, 255);
	SDL_RenderClear(renderer);
	float w, h;
	SDL_GetTextureSize(glyph_texture, &w, &h);
	SDL_FRect dst = {0, 0, w, h};
	SDL_RenderTexture(renderer, glyph_texture, NULL, &dst);
	SDL_RenderPresent(renderer);
	return SDL_APP_CONTINUE;
}

SDL_AppResult SDL_AppEvent(void* userdata, SDL_Event* e)
{
	switch(e->type)
	{
		case SDL_EVENT_QUIT: return SDL_APP_SUCCESS; break;
	}

	return SDL_APP_CONTINUE;
}

void SDL_AppQuit(void* data, SDL_AppResult result)
{
}

I've tested with other fonts and had the same result, but here's the ttf file used just in case.

font.zip

Currently using fully up-to-date Arch Linux, SDL 3.2.28, FreeType 2.14.1, and using Sway 1.11. GPU is AMD 6600 XT with Mesa 25.3.2.

I tried using SDL3_ttf-3.2.2 and building from main, but did not notice any changes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions