Set transparency with palette

Hi,

So I’m loading my tileset from a binary file to a surface and then creating a texture from the surface. My surface uses a palette and the pixel depth is set to 8bits.
The tiles have a transparency color which corresponds to the index 255 in my palette.
How do I set the color key using this index?
I tried to use :SDL_SetColorKey(pSurface_, SDL_TRUE, SDL_MapRGB(pSurface_->format, 255, 255, 255)) (255, 255, 255) is the color at the index 255.
But it doesn’t seem to work.
Should I use SDL_SetColorKey ? Or should I work with the texture itself using another function?

Thanks for your help!

Kinda weird that it doesn’t let you just specify a specific index to be transparent.

Instead, make index 255 be a unique color that isn’t used anywhere else, which is what it seems like SDL is expecting you to do (and was pretty commonly done back in the days when 8-bit paletted textures were the norm). Otherwise, if you have white (255, 255, 255) somewhere else in the palette SDL may be trying to use that instead.

Hi,

When you say it’s weird, do you mean that sdl should provide an API for setting an index directly instead of a color?
In my case, yes the white color is only used as the color key.
My only option I believe is to convert my 8bits surface to a RGBA surface. Do you know any alternative?

Just do the following:

SDL_SetColorKey(surface, SDL_TRUE, 255);

The docs say the last argument is a pixel in the surface’s format as returned by SDL_MapRGBA(), but it’s an indexed format so of course you can just directly specify the color index.

I’m embarrassed that didn’t occurr to me already :man_facepalming:

It works! Thanks for your help!