# Filling an area with color, using partial transparency, in SDL

This topic is 4887 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm working on a simple GUI framework, and I'm just trying to get a very basic background working. It needs to be able to be partially transparent when I'm blitting the surface to the main video surface. I can easily use SDL_FillRect() to fill with color, but there isn't a way to include what transparency you want with this function. Basically I'm asking for a substitute that will allow me to fill an area with color, and utilizing an alpha channel. Thanks!

##### Share on other sites
Check out the SDL_gfx library.

##### Share on other sites
You can try using the SDL_SetAlpha() function.

##### Share on other sites
Quote:
 Original post by SiCraneYou can try using the SDL_SetAlpha() function.

Yeap, that will do the trick. I just tried it out myself. If by chance you are also looking for some Fade In/Out code, take a look here. I also utilized the SetAlpha function.

##### Share on other sites
Well, I've done some fade in/out stuff before, and my hackish solution was to load up a black bitmap image, set the alpha for that, and then repeatedly blit it across the whole screen. So this wouldn't work to change the transparency of the whole screen?:

SDL_SetAlpha(SDL_GetVideoSurface(), SDL_SRCALPHA, alpha);

I tried it and nothing happened =(

##### Share on other sites
You can use the functions in the SDL_gfx library to draw a semi transparent rectangle over the entire screen and gradually increase the opacity of the rectangle.
int boxRGBA(SDL_Surface * dst, Sint16 x1, Sint16 y1,         Sint16 x2, Sint16 y2, Uint8 r, Uint8 g, Uint8 b, Uint8 a);

If I recall correctly the SDL_SetAlpha function will set the alpha on a source surface and blend it with the destination surfaces pixels (not sure if it ignores destination alpha or not). Since you were trying to set the alpha of the screen or destination surface in this situation you probably saw no effect. If this is the way you want to go, then creating a black/blank surface the size of the screen and increasing the alpha value on the surface via SDL_SetAlpha then blitting to the screen over a second or two will more than likely do what you want.

The reason I suggested the SDL_gfx library is because it does exactly what you described in your first post.
Quote:
 I can easily use SDL_FillRect() to fill with color, but there isn't a way to include what transparency you want with this function. Basically I'm asking for a substitute that will allow me to fill an area with color, and utilizing an alpha channel.

Utilizing the alpha component of boxRGBA, you can draw semi transparent boxes quite easily. Not to mention that unless you want to get into assembly programming, and even then, you probably won't be able to get better performance than the routines in the SDL_gfx library as far as alpha blending goes without utilizing a 3D API such as OpenGL.

The long winded paragraph about creating a blank/black surface is the same as what Drew posted in his link above only his example is much more informative :)

I do however still recomend the SDL_gfx library until you move into a 3D API. The line and circle drawing functions alone are worth it.
[/edit]

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 9
• 11
• 9
• 9
• ### Forum Statistics

• Total Topics
634136
• Total Posts
3015755
×