How do I do a semi-transparent fill with SDL_FillRect?

I’ve tiled an image on the screen surface to create a background. I’d like to
fill a rectangular area on the screen with a color but I’d like it to be
semi-transparent so some of the background shows through.

I’ve tried the following:

color = SDL_MapRGBA(screen->format, 0, 0, 255, 128);
SDL_FillRect(play_area, &play_area_rect, color);
SDL_UpdateRect(screen, 0, 0, 0, 0);

but the rectangular area is a solid color with no transparency.

Thanks,

Jason Bodnar–
Open WebMail Project (http://openwebmail.org)

Jason Bodnar wrote:

SDL_FillRect(play_area, &play_area_rect, color);

SDL_FillRect doesn’t do alpha transparency…it just fills with the pixel value you give it.
You would have to make your own routine to do this.–
-==-
Jon Atkins
http://jcatki.2y.net/

“Jason Bodnar” wrote:

I’ve tiled an image on the screen surface to create a background. I’d like to
fill a rectangular area on the screen with a color but I’d like it to be
semi-transparent so some of the background shows through.

You cannot use FillRect for this; the pixels in the destination rectangle
are always replaced with the pixel value you specified. This is documented.

Try instead creating an auxiliary surface and fill it with an opaque colour,
call SetAlpha to set its opacity (alpha=128 is a good choice since it’s
likely to be fast), and then blit this surface to its location on the screen

You are actually writing that color to the screen surface, which
does’nt have alpha support, if you want it to be transparent create a
second surface, do the fill rect on it, then blit that second surface
to the screen.

Jason Bodnar wrote:

I’ve tiled an image on the screen surface to create a background. I’d like
to> fill a rectangular area on the screen with a color but I’d like it to be
semi-transparent so some of the background shows through.

I’ve tried the following:

color = SDL_MapRGBA(screen->format, 0, 0, 255, 128);
SDL_FillRect(play_area, &play_area_rect, color);
SDL_UpdateRect(screen, 0, 0, 0, 0);

but the rectangular area is a solid color with no transparency.

Thanks,

Jason Bodnar


Open WebMail Project (http://openwebmail.org)


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl