Jump to content
  • Advertisement
Sign in to follow this  
Spippo

Making a transparent image in SDL

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a little problem with an image: I have an opend window (created with SDL) and the window has a background picture, that can variate. On this background, I want to place an other picture (a black dot). So I now I want to know if it is possible to set a transparent collor? Because it only has to show the dot, and not the other pixels around it. Here's how I try to do it:
SDL_Surface *screen      // The screen surface (fully init)
SDL_Surface *background  // The background picture
SDL_Surface *ball        // The black dot

background = SDL_LoadBMP("images/bg.bmp");
ball = SDL_LoadBMP("images/ball.bmp");

SDL_Rect rball = {160,160, 10, 10};

SDL_BlitSurface(background, NULL, screen, NULL);
SDl_BlitSurface(ball, NULL, screen, &rball);
SDl_Flip(screen);
Now I see the dot on the background, but also the surounding pixels from the ball.bmp.

Share this post


Link to post
Share on other sites
Advertisement
Can you give an example on how it works?
Because the Reference is not very clear to me (I'm not english)

Share this post


Link to post
Share on other sites
The first parameter is the bitmap surface that you want to set the color key for.

The second parameter is for flags. This is set to SDL_SRCCOLORKEY.

The third parameter is an RGB value. use the SDL_MapRGB function to obtain a value for this parameter. The first parameter of the SDL_MapRGB function is the format. I would use the format member of the surface being used. the next three parameters are values for R, G, and B respectively.

In the end, it should look like this:

SDL_SetColorKey( ball, SDL_SRCCOLORKEY, SDL_MapRGB ( ball->format, 0x00, 0x00, 0x00));

Note: made major edits.
Further note: I used all 0 values for the color key, you should use whatever floats your boat.

Share this post


Link to post
Share on other sites
The basic example looks like:

SDL_Surface * surface = /* get a SDL_Surface somehow */;
SDL_SetColorKey(surface, SDL_SRCCOLORKEY, SDL_MapRGB(surface->format, red, green, blue));


You can skip the SDL_MapRGB() if you already have the color you want to use as a transparent color in a the correct format for the surface.

Share this post


Link to post
Share on other sites
hmmm, link doesn't work for me.

i've tried this:


SDL_Surface *screen // The screen surface (fully init)
SDL_Surface *background // The background picture
SDL_Surface *ball // The black dot

background = SDL_LoadBMP("images/bg.bmp");
ball = SDL_LoadBMP("images/ball.bmp");

SDL_Rect rball = {160,160, 10, 10};

SDL_Color color = {225,128,64};
int SDL_SetColorKey(screen, 0, color);

SDL_BlitSurface(background, NULL, screen, NULL);
SDl_BlitSurface(ball, NULL, screen, &rball);
SDl_Flip(screen);


And it gives me this error:

Quote:

initializer list being treated as compound expression

Share this post


Link to post
Share on other sites
if I use
Quote:

SDL_SetColorKey(scherm, SDL_SRCCOLORKEY, SDL_MapRGB(scherm->format, 225, 128, 64));


It gives me no error, but I can still see the color that shoud be transparrent

Share this post


Link to post
Share on other sites
SDL_SetColorKey should be applied to the surface that you want to make transparent. In your case, it looks like you should ball instead of screen.

Share this post


Link to post
Share on other sites
To Clarify:
The surface you set the color key for should be the bitmap surface you are blitting to the screen.
Also:
I saw a stray 'int' in the line calling SDL_SetColorKey in your previous post.

Share this post


Link to post
Share on other sites
Yeah, thanx, I should have used the ball surface, not the screen surface. (It works now).

And for the 'int': I looked at the sdl reference, and it sais:
Quote:

#include "SDL.h"
int SDL_SetColorKey(SDL_Surface *surface, Uint32 flag, Uint32 key);


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!