Sign in to follow this  

SDL ColorKey

This topic is 4104 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, Im onto lesson 5 of The lazy foo productions, and I need some help. How do I have the color key for a particular color, lets say hot pink. If i want hot pink to be transparent in my program, how do I find this? In the tut, it gives how to do it for the lite blue color, but it dosnt explain how to get any other color. Note: I have question within the code. Please help! my version on the lazy foo source:
#include <string>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>

using namespace std;

// screen size
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;

// images
SDL_Surface * backGround;
SDL_Surface * man;
SDL_Surface * screen;

// handles events
SDL_Event event;

SDL_Surface * loadImage( string filename )
{
    // temp storage
    SDL_Surface * loadedImage = NULL;
    
    SDL_Surface * optImage = NULL;
    
    loadedImage = IMG_Load( filename.c_str() );
    
    if( loadedImage != NULL ) {
        optImage = SDL_DisplayFormat( loadedImage );
        
        SDL_FreeSurface( loadedImage );
        
        if( optImage != NULL ) {
            // map the color key
            // whats Uint32 mean??
            Uint32 colorKey = SDL_MapRGB( optImage->format, 0, 0xFF, 0xFF ); // how do I find the colors RGB code thingy?
            
            // set all pixels of R 0, G 0xFF, and B 0xFF to be transparent
            SDL_SetColorKey( optImage, SDL_RLEACCEL | SDL_SRCCOLORKEY, colorKey ); // can someone clarify more on RLEACCEL please?
        }
        
        return optImage;
    }
}

void Draw( int x, int y, SDL_Surface * picture, SDL_Surface * dest )
{
    SDL_Rect offset;
    
    offset.x = x;
    offset.y = y;
    
    SDL_BlitSurface( picture, NULL, dest, &offset );
}

int main( int argc, char* args[] )
{
    bool quit = false;
    
    if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 ) {
        return 1;
    }
    
    screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
    
    SDL_WM_SetCaption( "Man on Hill!", NULL );
    
    backGround = loadImage( "background.png" );
    man = loadImage( "foo.png" );
    
    Draw( 0, 0, backGround, screen );
    Draw( 300, 300, man, screen );
    
    if( SDL_Flip( screen ) == -1 ) {
        return 1;
    }
    
    while( !quit ) {
        while( SDL_PollEvent( &event ) ) {
            if( event.type == SDL_QUIT ) {
                quit = true;
            }
        }
    }
    
    SDL_FreeSurface( backGround );
    SDL_FreeSurface( man );
    
    SDL_Quit();
    
    return 0;
}

Share this post


Link to post
Share on other sites
The really easy, but not necesarily portable way is to do:

SDL_SetColorKey( optImage, SDL_RLEACCEL | SDL_SRCCOLORKEY, 0xff00ff);

Note the 0xff00ff. Not all colors will be portable, IIRC, to big-endian processors, though. Better way is to do like so:

SDL_Color key;
key.r = 255;
key.g = 0;
key.b = 255;
key.unused = 255;

SDL_SetColorKey( optImage, SDL_RLEACCEL | SDL_SRCCOLORKEY, key);


Edit: Blast it! Wrong again. Ignore the second part, I will fix it shortly.
Edit: You had it right in the first place, anyhow. :) I guess I missread you.

To find color codes, the easiest way is to open paint, make your own color, and copy it's red green and blue values into your program.

Share this post


Link to post
Share on other sites
Quote:
Original post by NUCLEAR RABBIT
Thanks for you quick help Ezbez! Worked great! [smile]

Just one more thing. Whats Uint32 mean/for/do?


It's an:

Unsigned (meaning it's always postive)
Integer that's
32 bits in size.

Ever do HTML? you know how you set colors like 255, 255, 255? well those 255s are numbers so it only makes sense that a color is a number.

Share this post


Link to post
Share on other sites
Quote:
Original post by Lazy Foo
Quote:
Original post by NUCLEAR RABBIT
Thanks for you quick help Ezbez! Worked great! [smile]

Just one more thing. Whats Uint32 mean/for/do?


It's an:

Unsigned (meaning it's always postive)
Integer that's
32 bits in size.

Ever do HTML? you know how you set colors like 255, 255, 255? well those 255s are numbers so it only makes sense that a color is a number.


roger that! thanx for the help. [smile]

Share this post


Link to post
Share on other sites

This topic is 4104 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this