Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer 1

up vote 1 down vote accepted

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!

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.