# Order of Surface "Filling" and transparant color setting

Hello!

I’m new to SDL so this might be a stupid question. The background is that I
didn’t get transparancy when blitting even though I had set the transparancy
colour for the source surface. My initial algorithm was like this:

1. Create an empty surface of correct size.
2. Set transparancy color
3. Fill surface with data from pcx file using the “putpixel” in the SDl
documentation.

Then I changed place of step 2 and 3 and viola! Then it worked (blit is
transparent). Why is this? An explanation would be helpful to avoid
misstakes in the future.

Thanks

/ Niklas_________________________________________________________________
Chat with friends online, try MSN Messenger: http://messenger.msn.com

1. Create an empty surface of correct size.
2. Set transparancy color
3. Fill surface with data from pcx file using the “putpixel” in the SDl
documentation.

It’s much easier to use SDL_image library… you can download it from libsdl.org
What “putpixel” are you talking about? You mean direct pixel access?

I agree that it’s easier to use the SDL-Image library. But it also nice to
now how to fiddle with direct access to pixels. This can be useful in other
applications. The “putpixel” I refered to is

/*

• Set the pixel at (x, y) to the given value

• NOTE: The surface MUST be locked before calling this!
*/
void BitMap::putpixel(int x, int y, Uint32 pixel)
{
int bpp = rep -> format -> BytesPerPixel;

/* Here p is the address to the pixel we want to set */
Uint8 *p = (Uint8 *) rep -> pixels + y * rep -> pitch + x * bpp;

switch(bpp) {
case 1:
*p = pixel;
break;

case 2:
*(Uint16 *)p = pixel;
break;

case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
p[0] = (pixel >> 16) & 0xff;
p[1] = (pixel >> 8) & 0xff;
p[2] = pixel & 0xff;
} else {
p[0] = pixel & 0xff;
p[1] = (pixel >> 8) & 0xff;
p[2] = (pixel >> 16) & 0xff;
}
break;

case 4:
*(Uint32 *)p = pixel;
break;
}
}

To: sdl at libsdl.org
Subject: Re: [SDL] Order of Surface “Filling” and transparant color
setting.
Date: Sun, 02 Jun 2002 12:09:47 +0200

1. Create an empty surface of correct size.
2. Set transparancy color
3. Fill surface with data from pcx file using the “putpixel” in the SDl
documentation.

It’s much easier to use SDL_image library… you can download it from
libsdl.org
What “putpixel” are you talking about? You mean direct pixel access?

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

Join the world?s largest e-mail service with MSN Hotmail.
http://www.hotmail.com

Hello!

I’m new to SDL so this might be a stupid question. The background is
that I didn’t get transparancy when blitting even though I had set the
transparancy colour for the source surface. My initial algorithm was
like this:

1. Create an empty surface of correct size.
2. Set transparancy color
3. Fill surface with data from pcx file using the “putpixel” in the SDl
documentation.

Then I changed place of step 2 and 3 and viola! Then it worked (blit is
transparent). Why is this? An explanation would be helpful to avoid
misstakes in the future.

sounds like you were using colorkey, which bases it’s work on the current image…
so if you change the image after setting a color key, the alpha channel is not re-keyed for you,
you would have to setup the colorkey after setting the surface image up completely.–
-==-
Jon Atkins
http://jcatki.2y.net/

Thanks, but this means that SDL_SetColorKey changes more than just the
"colorkey" value in the SDL_PixelFormat struct, right?
Does it recode the information in any other way?

A related question, I get a segmentation fault in SDL_DisplayFormat. This is
related because the segmentation fault disappears if I do not set the
transparancy color. The code below have the error.

stderr:

Initializing Simple DirectMedia Layer
Done…
ColorKey returned: 0
main. Convert to display format
Fatal signal: Segmentation Fault (SDL Parachute Deployed)

code:

//
// Initialize SDL library
//
fprintf(stderr, “Initializing Simple DirectMedia Layer\n”);

//initialize systems
SDL_Init ( SDL_INIT_VIDEO | SDL_INIT_TIMER );

//set our at exit function
atexit ( SDL_Quit ) ;

fprintf(stderr, “Done…\n”);

//
// Set 320 x 200 x 16bit
//
SDL_Surface *sp = SDL_SetVideoMode(320, 200, 16, SDL_SWSURFACE |
SDL_DOUBLEBUF | SDL_FULLSCREEN);

SDL_Surface *s = 0;

if ( s == NULL )
{
throw int(-1);
}

//
// Convert to display format.
//
Uint16 trans = SDL_MapRGB(SDL_GetVideoSurface() -> format, 255, 0, 255);
int res = SDL_SetColorKey(s, SDL_SRCCOLORKEY, trans);
fprintf(stderr, “ColorKey returned: %d\n”, res);

fprintf(stderr, “main. Convert to display format\n”);
SDL_Surface *s2 = SDL_DisplayFormat(s);
fprintf(stderr, “Done\n”);

if ( s2 == NULL )
throw int(-1);

SDL_FreeSurface(s);

exit(0);

/ Niklas>From: Jonathan Atkins

To: sdl at libsdl.org
Subject: Re: [SDL] Order of Surface “Filling” and transparant color
setting.
Date: Sun, 02 Jun 2002 11:38:02 -0500

Hello!

I’m new to SDL so this might be a stupid question. The background is that
I didn’t get transparancy when blitting even though I had set the
transparancy colour for the source surface. My initial algorithm was like
this:

1. Create an empty surface of correct size.
2. Set transparancy color
3. Fill surface with data from pcx file using the “putpixel” in the SDl
documentation.

Then I changed place of step 2 and 3 and viola! Then it worked (blit is
transparent). Why is this? An explanation would be helpful to avoid
misstakes in the future.

sounds like you were using colorkey, which bases it’s work on the current
image…
so if you change the image after setting a color key, the alpha channel is
not re-keyed for you,
you would have to setup the colorkey after setting the surface image up
completely.

-==-
Jon Atkins
http://jcatki.2y.net/

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