Archived

This topic is now archived and is closed to further replies.

pauljg

Reading Surfaces (SDL) with OGL

Recommended Posts

Hi, Not strictly an OGL question, but its connected !. I''ve comeup against a little problem with my project, I''m trying to display sprites using textures on quads. I know I''ve got to use the Alpha channel for masking them. At the mo, I''m loading a BMP image and then converting the surface over to a new surface with RGBA support. (using: alphaimage = SDL_DisplayFormatAlpha(image) I want to go through my image, and see if the values of R,G,B are equal to my mask colour. If they are, put a mask value in Alpha channel. But how can I do that ?, I''ve gotta get at the R,G,B components.. and then write to A !!! I can''t find any SDL instructions for helping me. Please Help !.

Share this post


Link to post
Share on other sites
It seems to me that you just have to loop through the (alphaimage.w x alphaimage.h) pixels, compare the rgb for a specific value and then fill in the alpha.

Assuming the surface is in 32 bit RGBA mode (8,8,8,8), this should work:

  

if ( SDL_MUSTLOCK(alphaimage) )
SDL_LockSurface(alphaimage);

Uint8 *currentRow = alphaimage.pixels;
for (Uint32 y=0; y<alphaimage.h; ++y)
{
for (Uint32 x=0; x<alphaimage.w; ++x)
{
Uint8 *currentPixel = currentRow + 4*x;

if ( currentPixel[0] == colorkey.red &&
currentPixel[1] == colorkey.red &&
currentPixel[2] == colorkey.red && )
currentPixel[3] = 0;
else
currentPixel[3] = 255;
}
currentRow += alphaimage.pitch;
}

if ( SDL_MUSTLOCK(alphaimage) )
SDL_UnlockSurface(alphaimage);

Share this post


Link to post
Share on other sites
Sorry but I made a little error. (Can you spot it?)






OH well I''ll tell you. Instead of .red .red .red it should be .red .green .blue of course.

Share this post


Link to post
Share on other sites
Yeah, I noticed it !

I had to change quite a bit of the code for it to compile, not sure if your running a different version of SDL to me - but I finally got it working.



Edited by - pauljg on February 21, 2002 3:48:55 PM

Share this post


Link to post
Share on other sites