Sign in to follow this  
thejahooli

Semi-Transparent Rectangles

Recommended Posts

I'm trying to be able to create a rectangle with an alpha value which I can then draw to my screen using SDL. I tried doing this
SDL_FillRect(screen, rect, SDL_MapRGBA(screen->format, 255, 0, 0, 150));

where screen is the surface set as the window and rect is the destination rectangle, but it just draws a fully opaque rectangle in the position. My bits per pixel is 32. Does anyone know how I would be able to do this.

Share this post


Link to post
Share on other sites
Are you sure? Did you draw anything behind it to make sure you can see the transparency?

also make sure the numbers arent supposed to be from like 0-1 or something. instead of 255

Share this post


Link to post
Share on other sites
Quote:
Original post by thejahooli
I'm trying to be able to create a rectangle with an alpha value which I can then draw to my screen using SDL. I tried doing this

*** Source Snippet Removed ***

where screen is the surface set as the window and rect is the destination rectangle, but it just draws a fully opaque rectangle in the position. My bits per pixel is 32. Does anyone know how I would be able to do this.

I'm not sure about the most recent versions of SDL, but as I recall, you have to make your own function to do this. Something like this should work:


//Returns 0 on success, -1 on failure. Call SDL_GetError to get the error messages.
int SDL_FillAlphaRect(SDL_Surface *destination, SDL_Rect *rect, SDL_Color color)
{
if(destination == NULL)
{
SDL_SetError("SDL_FillAlphaRect: 'destination' is NULL");
return -1;
}
if(rect == NULL)
{
SDL_SetError("SDL_FillAlphaRect: 'rect' is NULL");
return -1;
}

SDL_Surface *temporarySurface = SDL_CreateRGBSurface(SDL_SWSURFACE, rect->w, rect->h, destination->format->BitsPerPixel,
destination->format->Rmask, destination->format->Gmask,
destination->format->Bmask, destination->format->Amask);
if(temporarySurface == NULL)
return -1; //We don't want to overwrite CreateRGBSurface's error message, so we just return.

if(SDL_FillRect(screen, NULL, SDL_MapRGB(temporarySurface->format, color.r, color.g, color.b)) == -1)
return -1;


if(SDL_SetAlpha(temporarySurface, SDL_SRCALPHA, color.a) == -1)
return -1;

if(SDL_BlitSurface(temporarySurface, NULL, destination, rect) == -1)
return -1;

SDL_FreeSurface(temporarySurface);

return 0;
}





Warning: Since I'm creating and then destroying a surface every function call, it wont be super fast, but it should work for casual usage (a few GUI windows, as opposed to a couple hundred partical effects or something). Also, I didn't bother testing the function (just typed it straight into the post), but it *should* work.

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