Transparent

Hi guys,
I have some trouble with the transparent colors.

I do the following:

ImageLoad:---------------------
img->data = (SDL_Surface **) malloc(sizeof(SDL_Surface *)*img->num);

where data is a SDL_Surface** inside the img struct.

After that I do:

tmp = SDL_DisplayFormat(tmp);
colorkey = SDL_MapRGB(tmp->format, sdf.rgb.r, sdf.rgb.g, sdf.rgb.b);
SDL_SetColorKey(tmp, SDL_RLEACCEL | SDL_SRCCOLORKEY, colorkey);

for (i = 0; i < img->num; i++)
{
img->data[i] = SDL_CreateRGBSurface(sf, img->rect.w, img->rect.h,
DEPTH, RMASK, GMASK, BMASK, AMASK);
img->data[i] = SDL_DisplayFormat(img->data[i]);
img->rect.x = img->rect.w * i;

SDL_BlitSurface(tmp, &img->rect, img->data[i], NULL);
}

So I come back to my main function and blit the surfaces on the screen.

The Image A is shawn on the background and the color is not trasparent
but it’s black.

Does anyone say me why?

I didn’t look too closely, but perhaps you want to be calling
"SDL_DisplayFormatAlpha()" rather than simply “SDL_DisplayFormat()”…?

-bill!On Sun, May 14, 2006 at 02:16:21PM +0200, Salvatore Di Fazio wrote:

Hi guys,
I have some trouble with the transparent colors.

Bill Kendrick ha scritto:

I didn’t look too closely, but perhaps you want to be calling
"SDL_DisplayFormatAlpha()" rather than simply “SDL_DisplayFormat()”…?

-bill!

It’s not enough.
With this change I can’t show anything :-\

I past my SetImage function hoping that it is helpfully to understand
what I do----------------------
void SetImage(char *dir, sImage *img, int sf)
{
char imagepath[LINE];
int colorkey;
int i;
SDL_Surface *tmp;
sSdfImage sdf;

if (img == NULL)
{
EMSG(“img == NULL”);
abort();
}

if (dir == NULL)
{
EMSG(“dir == NULL”);
abort();
}

LoadImageSdf(dir, &sdf);

memset(&imagepath[0], ‘\0’, LINE-1);
sprintf(imagepath, “data/%s/%s”, dir, sdf.filename);

tmp = IMG_Load(imagepath);
if(!tmp)
{
EMSG(IMG_GetError());
abort();
}

/* number of animation /
img->num = tmp->w / sdf.width;
/
alloc the space for the animation */
img->data = (SDL_Surface **) malloc(sizeof(SDL_Surface *)img->num);
/
set the rect that we use to blit the images */
img->rect.x = 0;
img->rect.y = 0;
img->rect.w = sdf.width;
img->rect.h = sdf.height;

/* the img is converted to screen format to obtain a better performance /
tmp = SDL_DisplayFormat(tmp);
colorkey = SDL_MapRGB(tmp->format, sdf.rgb.r, sdf.rgb.g, sdf.rgb.b);
/
set pixels of color rgb to be transparent */
SDL_SetColorKey(tmp, SDL_RLEACCEL | SDL_SRCCOLORKEY, colorkey);

/* blit all the images in the struct */
for (i = 0; i < img->num; i++)
{
img->data[i] = SDL_CreateRGBSurface(sf, img->rect.w, img->rect.h,
DEPTH, RMASK, GMASK, BMASK, AMASK);
img->data[i] = SDL_DisplayFormat(img->data[i]);

img->rect.x = img->rect.w * i;

SDL_BlitSurface(tmp, &img->rect, img->data[i], NULL);

}

img->rect.x = 0;
img->rect.y = 0;

SDL_FreeSurface(tmp);
}

in the main I wrote before SDL_Flip

SDL_BlitSurface(sprite.image.data[0], NULL, scr, &sprite.image.rect);

help please :slight_smile:

Excuse me to everybody,
I have just resolved

in this way---------------------------------------------------

/* the img is converted to screen format to obtain a better performance */
tmp = SDL_DisplayFormat(tmp);

/* blit all the images in the struct /
for (i = 0; i < img->num; i++)
{
img->data[i] = SDL_CreateRGBSurface(sf, img->rect.w, img->rect.h,
DEPTH, RMASK, GMASK, BMASK, AMASK);
img->data[i] = SDL_DisplayFormat(img->data[i]);
colorkey = SDL_MapRGB(img->data[i]->format, sdf.rgb.r, sdf.rgb.g,
sdf.rgb.b);
/
set pixels of color rgb to be transparent */
SDL_SetColorKey(img->data[i], SDL_RLEACCEL | SDL_SRCCOLORKEY, colorkey);

img->rect.x = img->rect.w * i;
SDL_BlitSurface(tmp, &img->rect, img->data[i], NULL);

}