I want to create SDL_Surface objects for each layer of my 2d tile-based map so that I have to render only one surface per layer rather than too many tiles. With normal tiles which do not have transparent areas this works well, however I am not able to create a SDL_Surface with transparent pixels everywhere to be able to draw some tiles on specific parts which should be visible (I do NOT want the whole surface to appear with a specific opacity - I want to create overlaying tiles where one can look through).

Currently I am creating my layers like this to draw with SDL_BlitSurface on them:

SDL_Surface* layer =
        layerWidth, layerHeight, 32, 0, 0, 0, 0);

If you have a look at this screenshot I have provided here

Transparency Fail

you can see that the bottom layer with no transparent parts gets rendered correctly. However the overlay with the tree tile (which is transparent in the top left corner) is drawn own its own surface which is black and not transparent as expected. The expected result (concerning the transparency) can be seen here

Expected transparency

Can anyone explain me how to handle surfaces which are actually transparent rather than drawing all my overlay tiles separately?


1 Answer 1


After trying a few things in desperation I was able to fix this issue! First of all I have filled the SDL_Surface objects with magenta

SDL_FillRect(layer, NULL, SDL_MapRGB(layer->format, 255, 0, 255));

then I noticed I have tried to handle alpha while drawing to the layer but I forgot to think about the SDL_BlitSurface call to the screen! Just one line above this call I had to add

SDL_SetColorKey(layer, SDL_SRCCOLORKEY, SDL_MapRGB(layer->format, 255, 0, 255));

and that did it!


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .