Making a transparent image in SDL

Started by
26 comments, last by Spippo 19 years, 4 months ago
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.
Spippo is magic,is magic!!!!!Oh ho hoo!!!!Spippo is magic
Advertisement
Did you try SDL_SetColorKey()?
Can you give an example on how it works?
Because the Reference is not very clear to me (I'm not english)
Spippo is magic,is magic!!!!!Oh ho hoo!!!!Spippo is magic
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.
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.
hmmm, link doesn't work for me.

i've tried this:

SDL_Surface *screen      // The screen surface (fully init)SDL_Surface *background  // The background pictureSDL_Surface *ball        // The black dotbackground = 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
Spippo is magic,is magic!!!!!Oh ho hoo!!!!Spippo is magic
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
Spippo is magic,is magic!!!!!Oh ho hoo!!!!Spippo is magic
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.
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.
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);


Spippo is magic,is magic!!!!!Oh ho hoo!!!!Spippo is magic

This topic is closed to new replies.

Advertisement