Chroma Key

Ok, So I been searching up on how to make a Chroma Key but can't find any good documentation that actually explains it in a code format related to c++.

I'm looking at using one for a game I'm wanting to make to remove certain parts of a picture when needed. Just wondering if anyone has any good documentation on them or even better. Documentation on using them with C++ and SDL/openGL.

So any documentation or examples would be great.

Thanks,
Myth.
Well, I'm not sure about this, but isn't Chroma Key a color value to be made transparent within a given surface? If so, you can use this neat SDL function
int SDL_SetColorKey(SDL_Surface *surface, Uint32 flag, Uint32 key)
You can find more info about this function here: http://sdl.beuc.net/sdl.wiki/SDL_SetColorKey;

Here's some example code just for you to see how it works. Actually, this is the only piece of code which is used in all my SDL projects.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
SDL_Surface* Surface::load(char* filePath)
{
    //Used to load the suface and check for errors
    SDL_Surface* temp;
    //Use to store optimized version of the temp surface
    SDL_Surface* ret;
    //Load surface using SDL_image's IMG_Load
    temp = IMG_Load(filePath);
    //Error loading, return NULL
    if(!temp)
        return NULL;
    //Set transparency color
    SDL_SetColorKey(temp, SDL_SRCCOLORKEY, SDL_MapRGB(temp->format, 50, 50, 50));
    //Optimize
    ret = SDL_DisplayFormat(temp);
    //Get rid of unused temp surface
    SDL_FreeSurface(temp);
    //Error optimizing return NULL, else return the surface
    if(!ret)
        return NULL;
    else
        return ret;

}


And that's that. I hope this helps you :)

Happy programming,
~Deimos
That's purely for loading an image though. Makes sense and what not. But I was going to be using mine in a situation of a game like Worms/Gunbound - Destroying the map and making certain parts not viewable or solid.

Cheers though, Having a look at the documentation now.
So, you mean making the image transparent at run time?
If so, I think you'd need to access the surface's pixel array and modify it according to your needs....

I guess you could load a surface with a given color key, and when part of such surface is destroyed (mine, rocket, dinamite etc...) you would go to that surface's "pixels" member and using some sort of algorithm you'd get a circular area of pixels, then it'd be a matter of setting their colors to the colorkey value.

Please note that this is pure speculation, I haven't realy tested anything yet.

Cheers :)
Last edited on
Makes sense what your saying. I guess it's just how to attack the situation like that as well though. Hmm
Topic archived. No new replies allowed.