How can I get a boolean representation of a surface?

I have a surface containing a certain sprite in my game.
I use the SDL_SetColorKey function in order to make parts of it transparent.
Now I want to be able to get a boolean representation of the sprite - for each pixel - is it transparent or opaque.
So, if for example my sprite is in the shape of a ball, I would get this:
000111000
001111100
001111100
000111000

Thanks ahead, Yoni.

There may be a more straightforward way using some SDL API that I’m not
familiar with, but if not, you could traverse the bitmap surface looking
for pixels which match the colorkey color. Use that to fill a matrix of
booleans.

e.g.:

for (y = 0; y < surf->h; y++)
{
for (x = 0; x < surf->w; x++)
{
Uint32 pix;
Uint8 r, g, b;

  pix = getpixel(surf, x, y);
  SDL_GetRGB(pix, surf->format, &r, &g, &b);

  if (r == key_r && g == key_g && b == key_b)
    mask[y][x] = 0;
  else
    mask[y][x] = 1;
}

}

Good luck!On Sat, Jun 10, 2006 at 02:39:48PM +0200, Yoni Levy wrote:

I have a surface containing a certain sprite in my game.
I use the SDL_SetColorKey function in order to make parts of it transparent.
Now I want to be able to get a boolean representation of the sprite - for each pixel - is it transparent or opaque.


-bill!
bill at newbreedsoftware.com
http://www.newbreedsoftware.com/

Thanks,
I suspect there really is no simpler solution…

Yoni.> ----- Original Message -----

From: nbs@sonic.net (Bill Kendrick)
To: "A list for developers using the SDL library. (includes SDL-announce)"

Sent: Monday, June 12, 2006 9:50 PM
Subject: Re: [SDL] How can I get a boolean representation of a surface?

On Sat, Jun 10, 2006 at 02:39:48PM +0200, Yoni Levy wrote:

I have a surface containing a certain sprite in my game.
I use the SDL_SetColorKey function in order to make parts of it
transparent.
Now I want to be able to get a boolean representation of the sprite - for
each pixel - is it transparent or opaque.

There may be a more straightforward way using some SDL API that I’m not
familiar with, but if not, you could traverse the bitmap surface looking
for pixels which match the colorkey color. Use that to fill a matrix of
booleans.

e.g.:

for (y = 0; y < surf->h; y++)
{
for (x = 0; x < surf->w; x++)
{
Uint32 pix;
Uint8 r, g, b;

 pix = getpixel(surf, x, y);
 SDL_GetRGB(pix, surf->format, &r, &g, &b);

 if (r == key_r && g == key_g && b == key_b)
   mask[y][x] = 0;
 else
   mask[y][x] = 1;

}
}

Good luck!


-bill!
bill at newbreedsoftware.com
http://www.newbreedsoftware.com/


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

Well, if the surface is RLE-encoded internally, it may be possible to
do it a little quicker, but damned if I know the details of how that works
internally, and how to take advantage of it. :^/

-bill!On Thu, Jun 15, 2006 at 09:04:02PM +0200, Yoni Levy wrote:

Thanks,
I suspect there really is no simpler solution…

Thanks,
I suspect there really is no simpler solution…
If it’s not so simple to keep track of the used colorkey, you could just
blit it to two surfaces, a black and a white one, and compare whether
they blit the same color. Then you don’t need to know what colorkey you
used. Imagine the beauty of the function
char** SDL_SurfaceGetMask(SDL_Surface * surface)
:)On Thu, 2006-06-15 at 12:13 -0700, Bill Kendrick wrote:
On Thu, Jun 15, 2006 at 09:04:02PM +0200, Yoni Levy wrote:

Well, if the surface is RLE-encoded internally, it may be possible to
do it a little quicker, but damned if I know the details of how that works
internally, and how to take advantage of it. :^/

-bill!


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