Reading Surfaces (SDL) with OGL

Started by
4 comments, last by pauljg 22 years, 1 month ago
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 !.
Advertisement
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);  
Dirk =[Scarab]= Gerrits
Thankyou !!!! you''ve saved my project from going under
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.
Dirk =[Scarab]= Gerrits
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
Well I didn''t copy this out of my own code. I just made it up when replying. I glad you were able to make it work though.
Dirk =[Scarab]= Gerrits

This topic is closed to new replies.

Advertisement