SDL_SetColorKey(...) help

hi,

I have this problem, I have an image with jsut 2 colors,
black as white, and I want to make all the white
transparent.

int SDL_SetColorKey(SDL_Surface *surface, Uint32 flag, Uint32 key);

should do, but I don’t know how to work with this, because
de concepts of flag and key are strange to me ( newbie writing ).

Could someone give me an example, and a small explanation?
I have consulted SDL documentation, but, since I don’t know
what key and flag stands for, it did not help me mutch…

well, tankz for nay answer.
bye
->Pedro Santos_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com

‘flag’ is an integer where each bit has a special meaning. There are
defines in the SDL headers that assign sensible names to bits and
patterns of bits - you should alwmays use these and not worry about
which actual bits you’re dealing with.

In your case, you should pass ‘SDL_SRCCOLORKEY’ for the ‘flag’ argument.

For performance in software rendering, you could also add ‘SDL_RLEACCEL’,
which encodes the surface in a way that allows a faster software blitter
to be used. To combine flag defines, use the ‘|’ (logic OR) operator:
‘SDL_SCRCOLORKEY | SDL_RLEACCEL’.

‘key’ is the pixel value of the color you want to be transparent. Note
that it’s NOT an 8:8:8 RGB value, unless your surface happens to be in
that format! You should construct this value using SDL_MapRGB(), passing
the pixel format struct from your surface as the ‘fmt’ argument, and your
RGB color as the r, g and b args. (range: 0…255.)

In short:

SDL_SetColorKey(your_surface, SDL_SRCCOLORKEY | SDL_RLEACCEL,
		SDL_MapRGB(your_surface->format, 255,255,255));

Do note that the “white” color of your image must have exactly the
value of 255,255,255 (0xffffff), or the image will remain opaque!

(I’ve noticed that both GIMP and PhotoShop have rounding errors in the
indexed<->RGB conversion code, so 255 becomes 254 if you convert back and
forth.)

//David Olofson — Programmer, Reologica Instruments AB

.- M A I A -------------------------------------------------.
| Multimedia Application Integration Architecture |
| A Free/Open Source Plugin API for Professional Multimedia |
----------------------------> http://www.linuxdj.com/maia -' .- David Olofson -------------------------------------------. | Audio Hacker - Open Source Advocate - Singer - Songwriter |--------------------------------------> david at linuxdj.com -'On Thursday 13 September 2001 19:15, Pedro Santos wrote:

hi,

I have this problem, I have an image with jsut 2 colors,
black as white, and I want to make all the white
transparent.

int SDL_SetColorKey(SDL_Surface *surface, Uint32 flag, Uint32 key);

should do, but I don’t know how to work with this, because
de concepts of flag and key are strange to me ( newbie writing ).

At 06:15 PM 9/13/2001 +0100, you wrote:

here is a little sample code that might help …

the key is the value that you want to use for the transparency … here i
use the top left pixels value …

and the flags are for the type of key you are creating … in this case i
am causing all of the pixels on the bitmap that match the upper left corner
pixel to be transparent.

have fun …

sdl_sprite = SDL_LoadBMP(“sprite.bmp”);
if (sdl_sprite == NULL) return GFX_ERROR_LOAD_SPRITE;

     SDL_DisplayFormat(sdl_sprite);

     SDL_LockSurface(sdl_sprite);

     int colorkey = getpixel(sdl_sprite,0,0);

     SDL_UnlockSurface(sdl_sprite);

     SDL_SetColorKey(sdl_sprite,SDL_SRCCOLORKEY,colorkey);>hi,

I have this problem, I have an image with jsut 2 colors,
black as white, and I want to make all the white
transparent.

int SDL_SetColorKey(SDL_Surface *surface, Uint32 flag, Uint32 key);

should do, but I don’t know how to work with this, because
de concepts of flag and key are strange to me ( newbie writing ).

Could someone give me an example, and a small explanation?
I have consulted SDL documentation, but, since I don’t know
what key and flag stands for, it did not help me mutch…

well, tankz for nay answer.
bye
->Pedro Santos


Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com


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

In the event that you still have questions, here’s something a bit more
disected yet interestingly vague:

#include
#include <SDL/SDL.h>

Uint8 transR = 0x00;
Uint8 transG = 0xff;
Uint8 transB = 0xff;
// this makes an obnoxious bright blue transparent.

Uint32 transRGB;

image1 = SDL_LoadBMP(“deliciouswheelofcheese.bmp”);
if (image1 == NULL )
{
fprintf(stderr, “%s\n”, SDL_GetError());
HonorableDeath();
}

transRGB = SDL_MapRGB(image1->format, transR, transG, transB);
SDL_SetColorKey(image1, SDL_SRCCOLORKEY, transRGB);

Be sure to look up SDL_MapRGB, SDL_DisplayFormat and related links at
libsdl.org.

thanks for the explanation, now I understand that concepts.

but… when I use

(…)

SDL_Surface *surface = SDL_LoadBMP( “file.bmp” );

SDL_SetColorKey(surface , SDL_SRCCOLORKEY | SDL_RLEACCEL,
SDL_MapRGB(surface->format, 255,255,255));

(…)

I get
Fatal signal: Segmentation Fault (SDL Parachute Deployed)

the BMP file is loaded without error, and the app
goes down no SDL_SetColorKey(…)

( onde question, it’s the same to use 16 color BitMap,
256 Color BitMap and 24 Bit BitMap? )

I also tried the Vince’s code (witch does the same thing
in my opinion) , and Shane?s code,
and it went down…

any ideas?

Pedro Santos_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com

my code went down huh :slight_smile: please send me the file you are working with …
i’ve been working with that code for about 6 months with no problem at all
… on BeOS and Win9x/Win2k

At 06:23 PM 9/14/2001 +0100, you wrote:>thanks for the explanation, now I understand that concepts.

but… when I use

(…)

SDL_Surface *surface = SDL_LoadBMP( “file.bmp” );

SDL_SetColorKey(surface , SDL_SRCCOLORKEY | SDL_RLEACCEL,
SDL_MapRGB(surface->format, 255,255,255));

(…)

I get
Fatal signal: Segmentation Fault (SDL Parachute Deployed)

the BMP file is loaded without error, and the app
goes down no SDL_SetColorKey(…)

( onde question, it’s the same to use 16 color BitMap,
256 Color BitMap and 24 Bit BitMap? )

I also tried the Vince’s code (witch does the same thing
in my opinion) , and Shane?s code,
and it went down…

any ideas?

Pedro Santos


Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com


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

SDL_Surface *surface = SDL_LoadBMP( “file.bmp” );

SDL_SetColorKey(surface , SDL_SRCCOLORKEY | SDL_RLEACCEL,
SDL_MapRGB(surface->format, 255,255,255));

[…]

( onde question, it’s the same to use 16 color BitMap,
256 Color BitMap and 24 Bit BitMap? )

Yes. In indexed color (256) modes, SDL_MapRGB() actually looks up the
palette entry with the best match to the RGB you specify. (Don’t ask me
exactly how it’s done, though; there are several popular methods. :slight_smile:

I also tried the Vince’s code (witch does the same thing
in my opinion) , and Shane?s code,
and it went down…

Weird. (Or I’m just too tired to spot the obvious! :slight_smile:

Try putting an exit() right after SDL_SetColorKey() call, and see if it
terminates, or if it still segfaults. Or try the debugger…

//David Olofson — Programmer, Reologica Instruments AB

.- M A I A -------------------------------------------------.
| Multimedia Application Integration Architecture |
| A Free/Open Source Plugin API for Professional Multimedia |
----------------------------> http://www.linuxdj.com/maia -' .- David Olofson -------------------------------------------. | Audio Hacker - Open Source Advocate - Singer - Songwriter |--------------------------------------> david at linuxdj.com -'On Friday 14 September 2001 19:23, Pedro Santos wrote:

The entire work that the code I posted was taken from is at
http://neen.par1.net/~corwin/udp
The files template.c and yinyan.bmp should be all you need. I am curious if
you get errors compiling it as it stands.

Vince~> ----- Original Message -----

From: pedro_dp_santos@yahoo.com (Pedro Santos)
To:
Sent: Friday, September 14, 2001 12:23 PM
Subject: RE: [SDL] SDL_SetColorKey(…) help

thanks for the explanation, now I understand that concepts.

but… when I use

(…)

SDL_Surface *surface = SDL_LoadBMP( “file.bmp” );

SDL_SetColorKey(surface , SDL_SRCCOLORKEY | SDL_RLEACCEL,
SDL_MapRGB(surface->format, 255,255,255));

(…)

I get
Fatal signal: Segmentation Fault (SDL Parachute Deployed)

the BMP file is loaded without error, and the app
goes down no SDL_SetColorKey(…)

( onde question, it’s the same to use 16 color BitMap,
256 Color BitMap and 24 Bit BitMap? )

I also tried the Vince’s code (witch does the same thing
in my opinion) , and Shane?s code,
and it went down…

any ideas?

Pedro Santos


Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com


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

I goi it to compile and run with absolutely no changes to the code.

At 03:34 PM 9/14/2001 -0500, you wrote:>The entire work that the code I posted was taken from is at

http://neen.par1.net/~corwin/udp
The files template.c and yinyan.bmp should be all you need. I am curious if
you get errors compiling it as it stands.

Vince~

----- Original Message -----
From: Pedro Santos <pedro_dp_santos at yahoo.com>
To:
Sent: Friday, September 14, 2001 12:23 PM
Subject: RE: [SDL] SDL_SetColorKey(…) help

thanks for the explanation, now I understand that concepts.

but… when I use

(…)

SDL_Surface *surface = SDL_LoadBMP( “file.bmp” );

SDL_SetColorKey(surface , SDL_SRCCOLORKEY | SDL_RLEACCEL,
SDL_MapRGB(surface->format, 255,255,255));

(…)

I get
Fatal signal: Segmentation Fault (SDL Parachute Deployed)

the BMP file is loaded without error, and the app
goes down no SDL_SetColorKey(…)

( onde question, it’s the same to use 16 color BitMap,
256 Color BitMap and 24 Bit BitMap? )

I also tried the Vince’s code (witch does the same thing
in my opinion) , and Shane?s code,
and it went down…

any ideas?

Pedro Santos


Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com


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

I’ve been doing it that way in Project Spitfire for a good while too, and
now in Kobo Deluxe. I think we’re innocent. :slight_smile:

//David Olofson — Programmer, Reologica Instruments AB

.- M A I A -------------------------------------------------.
| Multimedia Application Integration Architecture |
| A Free/Open Source Plugin API for Professional Multimedia |
----------------------------> http://www.linuxdj.com/maia -' .- David Olofson -------------------------------------------. | Audio Hacker - Open Source Advocate - Singer - Songwriter |--------------------------------------> david at linuxdj.com -'On Friday 14 September 2001 22:06, Shane Fausett wrote:

my code went down huh :slight_smile:

Same here; no problems whatsoever…

Did you check your DLL/so versions? Might be some old version lying
around… Or you have some build that can start up even if some library -
that you’ll need later - isn’t present.

//David Olofson — Programmer, Reologica Instruments AB

.- M A I A -------------------------------------------------.
| Multimedia Application Integration Architecture |
| A Free/Open Source Plugin API for Professional Multimedia |
----------------------------> http://www.linuxdj.com/maia -' .- David Olofson -------------------------------------------. | Audio Hacker - Open Source Advocate - Singer - Songwriter |--------------------------------------> david at linuxdj.com -'On Friday 14 September 2001 22:57, Shane Fausett wrote:

I goi it to compile and run with absolutely no changes to the code.

wow… i did not said that the examples that you guys gave were wrong…
I only said that when I used them my program went down…

well, I have done a small teste program and… it worked!
So the error was in my own code…

I found it, it was a small detail, that I have not payed attencion,
and after all, the image was not loaded rigth :slight_smile:

sorry all the trouble I gave you… :slight_smile:

now, one more question, how do I make that close button in SDL
window… work? :slight_smile:

and, I know this is OT, but can anyone direct me to some
documentation on how to… (hum… I can’t express my self)
do a Zoom on an SDL surface? Like put an image smaller.

Pedro Santos_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com

now, one more question, how do I make that close button in SDL
window… work? :slight_smile:

Hum, the SDL_QUIT event ?----------------
Gloire ? mon saigneur Arioch
Murlock (http://www.murlock.org)

now, one more question, how do I make that close button in SDL
window… work? :slight_smile:

The close button sends an SDL_QUIT event.

and, I know this is OT, but can anyone direct me to some
documentation on how to… (hum… I can’t express my self)
do a Zoom on an SDL surface? Like put an image smaller.

Try this library:
http://www.ferzkopp.net/Software/SDL_rotozoom/

See ya,
-Sam Lantinga, Software Engineer, Blizzard Entertainment