0

I'm trying to get a transparent SDL_Surface (to blit translucent rectangles on it), and here's what I've done:

surface := SDL_CreateRGBSurface(SDL_SWSURFACE, XMAX*TILE_SIZE,
YMAX*TILE_SIZE, BPP, 0, 0, 0, SDL_ALPHA_TRANSPARENT);
SDL_FillRect(surface, nil, SDL_MapRGBA(surface^.format, 0, 0, 0, 0));

This still doesn't work, the surface I get is black instead of being transparent. Any idea?

BONUS: To avoid the blitting of multiple translucide rectangles on top of each other, is there any way I can remove these rectangles then blit the new one (without blitting again the background ...) ?

halflings
  • 1,487
  • 1
  • 13
  • 33

1 Answers1

0

From looking at the docs for SDL_CreateRGBSurface, I think you need to replace SDL_SWSURFACE with SDL_SRCALPHA.

For the bonus part, you might try first to get hardware acceleration working... But if that's not an option and you're willing to spend some memory:

  • First, before drawing a rectangle, make a copy of the pixels underneath it.

  • To erase the topmost rectangle you simply re-blit the stored background image.

  • To erase any other rectangle, you re-blit its background, then redraw the overlapping portions of the rectangles on top of it from back to front.

tangentstorm
  • 6,851
  • 2
  • 26
  • 37
  • Mmh ... Even if this flag seems necessary to have a transparent surface (as it's insinuated in the documentation), it still doesn't fix my problem :/. Actually, it doesn't change anything ... When I use SDL_FillRect with RGBA pixels (and alpha value different from 255) I still see them in full opacity. For my "bonus" part, the problem is that in this case my background would be ... nothing. Or to be more precise: Transparent pixels. I don't know if blitting transparent pixels over an already blitted surface would do anything ? – halflings May 05 '12 at 21:59
  • Wait. Do you want the surface to be transparent, or do you want the _window_ to be transparent? If it's the window, you need to talk to the operating system, not SDL itself. On Win32, see [this question](http://stackoverflow.com/questions/3970066/creating-a-transparent-window-in-c-win32) - it's for c++ but you can talk to the same dll's in delphi... You may actually have to dig into the SDL source code to get it working - (maybe not. I've never tried it. – tangentstorm May 06 '12 at 08:32
  • As for blitting transparent pixels: why not? Just disable blending first. Then re-enable it to stamp the overlays. (And if you can't get the transparent window stuff working, you can also ask the operating system to just give you a screenshot and copy pixels from there -- that used to be the only way to get transparent windows in Java. See [this old article](http://onjava.com/onjava/excerpt/swinghks_hack41/index.html) for details.) – tangentstorm May 06 '12 at 08:36
  • No, I don't want transparent windows ! Just a transparent surface. Do you have any queue why the surface wouldn't be transparent even with all what I did (the SDL_SRCALPHA thing, filling with RGBA pixels, etc.) ? – halflings May 06 '12 at 08:45