Transparency on 16bit screens

I’ve just upped the color depth of my game from 8bit to 16bit to allow for
some better backgrounds. Only problem is tht I have now lost the
transparency of the sprites and everything else. All the images now have
their black background surrounding them.

It happens for images that are 4bit (16 color BMPs) or PNG/JPG/GIF images.

Any suggestions?

could you show us your loading/blitting code?

-Jim> ----- Original Message -----

From: sweenes@fnb.co.uk (Steven Sweeney))
To:
Sent: Monday, April 22, 2002 6:39 AM
Subject: [SDL] Transparency on 16bit screens

I’ve just upped the color depth of my game from 8bit to 16bit to allow for
some better backgrounds. Only problem is tht I have now lost the
transparency of the sprites and everything else. All the images now have
their black background surrounding them.

It happens for images that are 4bit (16 color BMPs) or PNG/JPG/GIF images.

Any suggestions?


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

// ============== Loading Code =================

SDL_Surface *setTransparent(SDL_Surface sprite)
{
/
Set transparent pixel as the pixel at (0,0) */
if (sprite->format->palette ) {
SDL_SetColorKey(sprite, (SDL_SRCCOLORKEY|SDL_RLEACCEL),
*(Uint8 *)sprite->pixels);
}

return sprite;

}

SDL_Surface *loadImage(char *file_name)
{
SDL_Surface *image, *newImage;

/* Load the BMP file into a surface */
image = IMG_Load(file_name);

//image = SDL_DisplayFormat(screen);
if (image == NULL) {
    fprintf(stderr, "Couldn't load %s: %s\n", file_name,

SDL_GetError());
exit(1);
}

newImage = SDL_DisplayFormat(image);

SDL_FreeSurface(image);

return setTransparent(newImage);

}

// ============== Blitting Code ================

void blit(SDL_Surface *image, int x, int y)
{
// Set up a rectangle to draw to
SDL_Rect area;
area.x = x;
area.y = y;
area.w = image->w;
area.h = image->h;

/* Blit onto the screen surface */
 if(SDL_BlitSurface(image, NULL, globals.screen, &area) < 0)
	  fprintf(stderr, "BlitSurface error: %s\n",

SDL_GetError());

//SDL_UpdateRect(screen, x, y, image->w, image->h);

}> -----Original Message-----

From: Steven James Stapleton [SMTP:stapleton.41 at osu.edu]
Sent: Monday, April 22, 2002 12:16 PM
To: sdl at libsdl.org
Subject: Re: [SDL] Transparency on 16bit screens

could you show us your loading/blitting code?

-Jim

----- Original Message -----
From: “Sweeney, Steven (FNB)”
To:
Sent: Monday, April 22, 2002 6:39 AM
Subject: [SDL] Transparency on 16bit screens

I’ve just upped the color depth of my game from 8bit to 16bit to allow
for
some better backgrounds. Only problem is tht I have now lost the
transparency of the sprites and everything else. All the images now have
their black background surrounding them.

It happens for images that are 4bit (16 color BMPs) or PNG/JPG/GIF
images.

Any suggestions?


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

As you setting the colorkey to black? Furthermore, is the black background
really black, or just a really dim shade of gray?

-JohnOn Mon, Apr 22, 2002 at 11:39:31AM +0100, Sweeney, Steven (FNB) wrote:

I’ve just upped the color depth of my game from 8bit to 16bit to allow for
some better backgrounds. Only problem is tht I have now lost the
transparency of the sprites and everything else. All the images now have
their black background surrounding them.

It happens for images that are 4bit (16 color BMPs) or PNG/JPG/GIF images.


John R. Hall - KG4RUO - Stranded in the Sol System
Student, Georgia Tech; Author, Programming Linux Games

Shouldn’t that be *(UInt16 *)sprite->pixels if you’re now in 16 bit mode?
Also, you should lock the surface before accessing its pixels directly:

UInt16 colorkey;

SDL_LockSurface(sprite);
colorkey = *(UInt16 *)sprite->pixels;
SDL_UnlockSurface(sprite);

SDL_SetColorKey(sprite, SDL_SRCCOLORKEY | SDL_RLEACCEL, colorkey);

Locking probably won’t matter in this case, but it might be necessary
on some platforms. Don’t call SDL_SetColorKey inside the lock.

Although there’s nothing wrong with using the upper left pixel as a
colorkey, it may be better to use a pixel value for a particular RGB color
as retrieved by SDL_MapRGB (forgive me if I’ve bungled the arguments of MapRGB;
I don’t have time to look it up right now):

SDL_SetColorKey(sprite, SDL_SRCCOLORKEY | SDL_RLEACCEL, SDL_MapRGB(sprite->format, 0, 0, 0));

-JohnOn Mon, Apr 22, 2002 at 01:10:01PM +0100, Sweeney, Steven (FNB) wrote:

// ============== Loading Code =================

SDL_Surface *setTransparent(SDL_Surface sprite)
{
/
Set transparent pixel as the pixel at (0,0) */
if (sprite->format->palette ) {
SDL_SetColorKey(sprite, (SDL_SRCCOLORKEY|SDL_RLEACCEL),
*(Uint8 *)sprite->pixels);
}

return sprite;
}


John R. Hall - KG4RUO - Stranded in the Sol System
Student, Georgia Tech; Author, Programming Linux Games

// ============== Loading Code =================

SDL_Surface *setTransparent(SDL_Surface sprite)
{
/
Set transparent pixel as the pixel at (0,0) */

Along with the advice given here…

I might be way off here, but why are you checking for a palette here and
only setting the transparency/colorkey if you do have a palette?
There is no palette if you are rendering greater than 8 bits, i.e. 16,24,
32.>From: overcode at overcode.net (John R. Hall)

Reply-To: sdl at libsdl.org
To: sdl at libsdl.org
Subject: Re: [SDL] Transparency on 16bit screens
Date: Mon, 22 Apr 2002 10:10:44 -0400
On Mon, Apr 22, 2002 at 01:10:01PM +0100, Sweeney, Steven (FNB) wrote:

if (sprite->format->palette ) {
  SDL_SetColorKey(sprite, (SDL_SRCCOLORKEY|SDL_RLEACCEL),

*(Uint8 *)sprite->pixels);
}

return sprite;

}

Shouldn’t that be *(UInt16 *)sprite->pixels if you’re now in 16 bit mode?
Also, you should lock the surface before accessing its pixels directly:

UInt16 colorkey;

SDL_LockSurface(sprite);
colorkey = *(UInt16 *)sprite->pixels;
SDL_UnlockSurface(sprite);

SDL_SetColorKey(sprite, SDL_SRCCOLORKEY | SDL_RLEACCEL, colorkey);

Locking probably won’t matter in this case, but it might be necessary
on some platforms. Don’t call SDL_SetColorKey inside the lock.

Although there’s nothing wrong with using the upper left pixel as a
colorkey, it may be better to use a pixel value for a particular RGB color
as retrieved by SDL_MapRGB (forgive me if I’ve bungled the arguments of
MapRGB;
I don’t have time to look it up right now):

SDL_SetColorKey(sprite, SDL_SRCCOLORKEY | SDL_RLEACCEL,
SDL_MapRGB(sprite->format, 0, 0, 0));

-John


John R. Hall - KG4RUO - Stranded in the Sol System
Student, Georgia Tech; Author, Programming Linux Games


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


MSN Photos is the easiest way to share and print your photos:
http://photos.msn.com/support/worldwide.aspx

Yeah… it probably should be :slight_smile: Shows what happens when you cut and paste
code from tutorials, eh? :slight_smile:

Thank You.> -----Original Message-----

From: overcode at overcode.net [SMTP:overcode at overcode.net]
Sent: Monday, April 22, 2002 3:11 PM
To: sdl at libsdl.org
Subject: Re: [SDL] Transparency on 16bit screens

On Mon, Apr 22, 2002 at 01:10:01PM +0100, Sweeney, Steven (FNB) wrote:

// ============== Loading Code =================

SDL_Surface *setTransparent(SDL_Surface sprite)
{
/
Set transparent pixel as the pixel at (0,0) */
if (sprite->format->palette ) {
SDL_SetColorKey(sprite, (SDL_SRCCOLORKEY|SDL_RLEACCEL),
*(Uint8 *)sprite->pixels);
}

return sprite;

}

Shouldn’t that be *(UInt16 *)sprite->pixels if you’re now in 16 bit mode?
Also, you should lock the surface before accessing its pixels directly:

UInt16 colorkey;

SDL_LockSurface(sprite);
colorkey = *(UInt16 *)sprite->pixels;
SDL_UnlockSurface(sprite);

SDL_SetColorKey(sprite, SDL_SRCCOLORKEY | SDL_RLEACCEL, colorkey);

Locking probably won’t matter in this case, but it might be necessary
on some platforms. Don’t call SDL_SetColorKey inside the lock.

Although there’s nothing wrong with using the upper left pixel as a
colorkey, it may be better to use a pixel value for a particular RGB color
as retrieved by SDL_MapRGB (forgive me if I’ve bungled the arguments of
MapRGB;
I don’t have time to look it up right now):

SDL_SetColorKey(sprite, SDL_SRCCOLORKEY | SDL_RLEACCEL,
SDL_MapRGB(sprite->format, 0, 0, 0));

-John


John R. Hall - KG4RUO - Stranded in the Sol System
Student, Georgia Tech; Author, Programming Linux Games


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

I must confess, I’m not sure… seems my entire game is running on functions
that I have butchered from example code! Just trying to set the black around
my sprite images to transparent, so I don’t have an image with a block of
black zooming around the screen. There’s probably better ways of doing all
this, but I’m slightly stuck in that case :|> -----Original Message-----

From: Calvin Harrigan [SMTP:charrig at hotmail.com]
Sent: Monday, April 22, 2002 3:55 PM
To: sdl at libsdl.org
Subject: Re: [SDL] Transparency on 16bit screens

From: overcode at overcode.net (John R. Hall)
Reply-To: sdl at libsdl.org
To: sdl at libsdl.org
Subject: Re: [SDL] Transparency on 16bit screens
Date: Mon, 22 Apr 2002 10:10:44 -0400

On Mon, Apr 22, 2002 at 01:10:01PM +0100, Sweeney, Steven (FNB) wrote:

// ============== Loading Code =================

SDL_Surface *setTransparent(SDL_Surface sprite)
{
/
Set transparent pixel as the pixel at (0,0) */

Along with the advice given here…

I might be way off here, but why are you checking for a palette here and
only setting the transparency/colorkey if you do have a palette?
There is no palette if you are rendering greater than 8 bits, i.e. 16,24,
32.

if (sprite->format->palette ) {
  SDL_SetColorKey(sprite, (SDL_SRCCOLORKEY|SDL_RLEACCEL),

*(Uint8 *)sprite->pixels);
}

return sprite;

}

Shouldn’t that be *(UInt16 *)sprite->pixels if you’re now in 16 bit mode?
Also, you should lock the surface before accessing its pixels directly:

UInt16 colorkey;

SDL_LockSurface(sprite);
colorkey = *(UInt16 *)sprite->pixels;
SDL_UnlockSurface(sprite);

SDL_SetColorKey(sprite, SDL_SRCCOLORKEY | SDL_RLEACCEL, colorkey);

Locking probably won’t matter in this case, but it might be necessary
on some platforms. Don’t call SDL_SetColorKey inside the lock.

Although there’s nothing wrong with using the upper left pixel as a
colorkey, it may be better to use a pixel value for a particular RGB
color
as retrieved by SDL_MapRGB (forgive me if I’ve bungled the arguments of
MapRGB;
I don’t have time to look it up right now):

SDL_SetColorKey(sprite, SDL_SRCCOLORKEY | SDL_RLEACCEL,
SDL_MapRGB(sprite->format, 0, 0, 0));

-John


John R. Hall - KG4RUO - Stranded in the Sol System
Student, Georgia Tech; Author, Programming Linux Games


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


MSN Photos is the easiest way to share and print your photos:
http://photos.msn.com/support/worldwide.aspx


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Sounds similar to what happened to me once, but it was actually a png file
with an alpha channel, which I thought was color keyed. Anyway, make sure
you set the color key I’d say, if it should be using a color key. Or if
it’s an alpha channel, make sure you are preserving it.
SDL_DisplaySurface() might be losing this information on you. Look at the
surface attributes of your final surface before a blit and see what you
really have and if it’s what you think you should have. I’d guess that it’s
probably not. Next you just have to figure out where along the line you are
losing the information.> ----- Original Message -----

From: sweenes@fnb.co.uk (Steven Sweeney))
To:
Sent: Monday, April 22, 2002 6:39 AM
Subject: [SDL] Transparency on 16bit screens

I’ve just upped the color depth of my game from 8bit to 16bit to allow for
some better backgrounds. Only problem is tht I have now lost the
transparency of the sprites and everything else. All the images now have
their black background surrounding them.

It happens for images that are 4bit (16 color BMPs) or PNG/JPG/GIF images.

Any suggestions?


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

I’ve got it sorted now :slight_smile: Thanks for all the help :))> -----Original Message-----

From: Jason Hoffoss [SMTP:jason at hoffoss.com]
Sent: Tuesday, April 23, 2002 2:13 AM
To: sdl at libsdl.org
Subject: Re: [SDL] Transparency on 16bit screens

Sounds similar to what happened to me once, but it was actually a png file
with an alpha channel, which I thought was color keyed. Anyway, make sure
you set the color key I’d say, if it should be using a color key. Or if
it’s an alpha channel, make sure you are preserving it.
SDL_DisplaySurface() might be losing this information on you. Look at the
surface attributes of your final surface before a blit and see what you
really have and if it’s what you think you should have. I’d guess that
it’s
probably not. Next you just have to figure out where along the line you
are
losing the information.

----- Original Message -----
From: “Sweeney, Steven (FNB)”
To:
Sent: Monday, April 22, 2002 6:39 AM
Subject: [SDL] Transparency on 16bit screens

I’ve just upped the color depth of my game from 8bit to 16bit to allow
for
some better backgrounds. Only problem is tht I have now lost the
transparency of the sprites and everything else. All the images now have
their black background surrounding them.

It happens for images that are 4bit (16 color BMPs) or PNG/JPG/GIF
images.

Any suggestions?


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl