[SDL] Sorry to bother you

Jonathan Dearborn grimfang4 at hotmail.com
Sat Dec 22 16:48:20 PST 2007


Does colorkeying definitely work with SDL_SRCALPHA?

Jonny D

From: madsj_6 at hotmail.com
To: sdl at lists.libsdl.org
Date: Sat, 22 Dec 2007 13:48:33 +0000
Subject: [SDL] Sorry to bother you

Hello. I have a very stupid problem. I have tried for several days to get it working, but i can't seem to get it right.
I am actually trying to do the simple thing to make a empty surface, and then draw another image into it. My code is the following (c++):

//code for drawing from one surface to another:

void apply_surface( float x, float y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL )
		//Holds offsets
		SDL_Rect offset;
		//Get offsets
		offset.x = x;
		offset.y = y;
		SDL_BlitSurface( source, clip, destination, &offset );

//code for loading an image from memory:
SDL_Surface *load_image( string filename ) 
		//The image that's loaded
		SDL_Surface* loadedImage = NULL;
		//The optimized surface that will be used
		SDL_Surface* optimizedImage = NULL;
		//Load the image
		loadedImage = IMG_Load( filename.c_str() );
		//If the image loaded
		if( loadedImage != NULL )			
		  SDL_SetColorKey( loadedImage, SDL_SRCCOLORKEY, SDL_MapRGB( loadedImage->format, 255, 0, 255 ) );
			//Create an optimized surface
			optimizedImage = SDL_DisplayFormatAlpha( loadedImage );
			//Free the old surface
			SDL_FreeSurface( loadedImage );
		//Return the optimized surface
		return optimizedImage;

//the code for making the empty surface(tempframe) and applying the appropriate sprite from the sprite sheet
// loaded from memory:
//create an empty surface
SDL_Surface* tempframe = SDL_CreateRGBSurface( SDL_SWSURFACE|SDL_SRCALPHA,
//fill it with the colorkey color       
SDL_FillRect(tempframe, NULL,SDL_MapRGB(tempframe->format, 255, 0, 255));

//assaign the colorkey                                
SDL_SetColorKey(tempframe, SDL_SRCCOLORKEY , SDL_MapRGB(tempframe->format, 255, 0, 255))   ;
//draw the current frame from the gense spritesheet  

the result is the following (look at the sprite on the left with the pink rectangle around it, it is supposed to be completely transparent)

Thanks for your time,

Kombiner det bedste fra Windows og nettet med Windows Live Hent det nu!

i’m is proud to present Cause Effect, a series about real people making a difference.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20071222/174da942/attachment-0008.htm>

More information about the SDL mailing list