Lost transparency in SDL surfaces drawn manually

Posted by Christian Ivicevic on Game Development See other posts from Game Development or by Christian Ivicevic
Published on 2012-09-10T18:06:07Z Indexed on 2012/09/10 21:50 UTC
Read the original article Hit count: 466

Filed under:
|
|
|

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 =
    SDL_CreateRGBSurface(
        SDL_HWSURFACE | SDL_SRCALPHA,
        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?

© Game Development or respective owner

Related posts about rendering

Related posts about sdl