Sign in to follow this  
silverphyre673

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

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 this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
You 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 this post


Link to post
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 =(

Thanks for your response.

Share this post


Link to post
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.

[edit]
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]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this