Message Board


Message Board > C/C++ > SDL colourkeys don't work! (c++)

April 10, 2007, 21:38
OScoder
None
1338 posts
Hi,
I am currently writing an SDL wrapper in c++, but am having problems getting colour keying to work. When the draw() method is called, is the object is drawn, but with its background colour still there. Can anyone see anything wrong with the following code? I'll post some more later if need be.

Code:
bool c_object::set_colourkey(unsigned char r, unsigned char g, unsigned char b)
{
 /*Update our properties:*/
    colourkey.r=r;
    colourkey.g=g;
    colourkey.b=b;
 /*Update our surface, and return:*/
    return(surface_set_colourkey(surface,
                                SDL_MapRGB(surface->format,colourkey.r,
                                                            colourkey.g,
                                                            colourkey.b)));
}


Code:
void c_object::draw()
{
 c_region *p_region;
    if(surface!=NULL)
    {
    /*Apply our surface:*/
     apply_surface(x,y,surface,screen::surface);
    }
}


Code:
bool surface_set_colourkey(SDL_Surface *source, unsigned long colourkey)
{
 return(SDL_SetColorKey(source, SDL_RLEACCEL | SDL_SRCCOLORKEY, colourkey));
}


Thanks,
OScoder

[Edited on April 10, 2007 by OScoder]
____________
om
#
April 10, 2007, 22:20
Htbaa
Perl
368 posts

I see you're using LazyFoo.net as well (apply_surface :-)).
SDL_Surface *surface; // << This is your surface
Uint32 colorkey = SDL_MapRGB(surface->format, 0xFF, 0x6B, 0xFA );
//Set all pixels of color R 0, G 0xFF, B 0xFF to be transparent
SDL_SetColorKey( surface, SDL_RLEACCEL | SDL_SRCCOLORKEY, colorkey );

Using the Uint32 type for storing a colorkey is the way I learned it. Why are you using a unsigned long?
____________
blog.htbaa.com
#
April 11, 2007, 11:05
OScoder
None
1338 posts
I think they are the same size - long is 32 bits. Anyway, it is now fixed! However I don't quite understand why. All I did was to optimise the object's surface, like so:
Code:
    surface = SDL_DisplayFormat(temp_surface);
    SDL_FreeSurface(temp_surface);


I don't see exactly why this should have helped though. Here's the function as a whole:
Code:
bool c_object::set_graph(RESOURCE_ID image_id)
{
 SDL_Surface *source_surface;
 SDL_Surface *temp_surface;
 /*Check we have been given a valid image id:*/
    if(get_resource_type(image_id)!=RESOURCE_TYPE_IMAGE || get_resource_offset(image_id)==0)
     return(1);
 /*Free the old surface:*/
    if(surface != NULL)
     SDL_FreeSurface(surface);
 /*Take the parameters of the new one:*/
    source_surface = (SDL_Surface *)get_resource_offset(image_id);
    width = source_surface->w;
    height = source_surface->h;
    surface_flags = SDL_SRCCOLORKEY | SDL_SWSURFACE;
    surface_format = source_surface->format;
 /*Create and blit new surface:*/
    temp_surface=SDL_CreateRGBSurface(SDL_SRCCOLORKEY | SDL_SWSURFACE, width, height, 32,
                            rmask, gmask, bmask, amask);
    apply_surface(0, 0, source_surface, temp_surface);
 /*Optimise image:*/
    surface = SDL_DisplayFormat(temp_surface);
    SDL_FreeSurface(temp_surface);
 /*Update properties:*/
    graph=image_id;
 return(0);   
}


But at least it works!
____________
om
#

Message Board > C/C++ > SDL colourkeys don't work! (c++)

Quick reply


You must log in or register to post.
Copyright © 2005 Booleansoup.com
Questions? Comments? Bug reports? Contact us!