Transparency on PNG

Hello there. Im trying to draw a tile with a transparency on it. The image is here:

[Image: http://i42.photobucket.com/albums/e310/KOUK2/tilescopy.png ]

As you can see, the first tile has a hole in it, thats supposed to be transparent. Im using this code:

Code:

SDL_Surface *temp = IMG_Load(“tilescopy.png”);

gTiles = SDL_DisplayFormat(temp);
SDL_FreeSurface(temp);
temp = NULL;

To load the images then this code

Code:

void drawtile(int x, int y, int tile)
{
// Lock surface if needed
if (SDL_MUSTLOCK(gTiles))
if (SDL_LockSurface(gTiles) < 0)
return;

int i, j;
for (i = 0; i < TILESIZE; i++)
{
int screenofs = x + (y + i) * PITCH;
int tileofs = (i + tile * TILESIZE) * (gTiles->pitch / 4);
for (j = 0; j < TILESIZE; j++)
{
((unsigned int*)gScreen->pixels)[screenofs] =
((unsigned int*)gTiles->pixels)[tileofs];
screenofs++;
tileofs++;
}
}

// Unlock if needed
if (SDL_MUSTLOCK(gTiles))
SDL_UnlockSurface(gTiles);
}

I call this function once with the 2nd tile, then again with the first one. So the overlap i should see the first tile and then the 2nd through the hole of the first one.

But im getting only a white hole where the tile was supposed to be transparent. I do realize i must be doing something horribly wrong but im just starting out so…halp? :frowning:

Change SDL_DisplayFormat(temp); to SDL_DisplayFormatAlpha(temp); and it should work. :slight_smile:

Stoney wrote:

Change in your first code snippet SDL_DisplayFormat(temp); to SDL_DisplayFormatAlpha(temp); and it should work. :slight_smile:

I tried that and it didnt work =/ any other ideas?

Wich graphic program are you using to set the alpha transparency?
I’m using GIMP and have not encountered any problems so far.

It might not be a good way, but you could call SDL_SetColorKey(…) before you the SDL_DisplayFormat/SDL_DisplayFormatAlpha call.

I call this function once with the 2nd tile, then again with the first one.
So the overlap i should see the first tile and then the 2nd through the hole
of the first one.

As far as I can see, you simply copy the pixels. So the pixeldata of the
second one simply overwrites the first data. They are not merged in your
code.

Use SDL_BlitSurface. That should handle the Alpha channel…Am Donnerstag, dem 19. Nov 2009 schrieb Tabris:


AKFoerster

Thanks for the help list, it worked wonder! I got a prb tho. From SDL_BlitSurface documentation:

int SDL_BlitSurface(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect);

Description

This performs a fast blit from the source surface to the destination surface.

The width and height in srcrect determine the size of the copied rectangle. Only the position is used in the dstrect (the width and height are ignored).

If srcrect is NULL, the entire surface is copied. If dstrect is NULL, then the destination position (upper left corner) is (0, 0).

So my prb is that im using 32 x 32 tiles. If i wanna draw, say, the 2nd time i would create a SDL_Rect with x = 0 and y = 32. If i give that SDL_Rect as a second argument to SDL_BlitSurface, its gonna draw everything y>32. How can i make it stop at y> 64 (so i get only one tile)?

Thanks in advance!

Nevermind, found out how to solve it. Apperantly the width and height are NOT! ignored, and they are used to crop the images…So yeah…Thanks everyone =D

Uint16 tileW, tileH;
tileW = tileH = 32;
// Drawing first tile:
SDL_Rect srcrect = {0, 0, tileW, tileH};
SDL_BlitSurface(tileSurface, &srcrect, screen, &dest);
// Drawing second tile:
srcrect.y = 32;
SDL_BlitSurface(tileSurface, &srcrect, screen, &dest);

Jonny DOn Sat, Nov 21, 2009 at 4:41 PM, Tabris wrote:

Thanks for the help list, it worked wonder! I got a prb tho. From
SDL_BlitSurface documentation:

Quote:

int SDL_BlitSurface(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst,
SDL_Rect *dstrect);

Description

This performs a fast blit from the source surface to the destination
surface.

The width and height in srcrect determine the size of the copied rectangle.
Only the position is used in the dstrect (the width and height are ignored).

If srcrect is NULL, the entire surface is copied. If dstrect is NULL, then
the destination position (upper left corner) is (0, 0).

So my prb is that im using 32 x 32 tiles. If i wanna draw, say, the 2nd
time i would create a SDL_Rect with x = 0 and y = 32. If i give that
SDL_Rect as a second argument to SDL_BlitSurface, its gonna draw everything
y>32. How can i make it stop at y> 64 (so i get only one tile)?

Thanks in advance!


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

They are ignored on the destination rect, though.

Jonny DOn Sat, Nov 21, 2009 at 4:49 PM, Tabris wrote:

Nevermind, found out how to solve it. Apperantly the width and height are
NOT! ignored, and they are used to crop the images…So yeah…Thanks
everyone =D


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org