Advertisement

Problem using transparency

Started by October 25, 2011 06:47 AM
1 comment, last by IronGryphon 13 years, 3 months ago


int apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination)
{
SDL_Rect rect;

rect.x = x;
rect.y = y;

//Tryck på ytan
SDL_BlitSurface(source, NULL, destination, &rect);
return 0;
}

int main ( int argc, char** argv )
{
SDL_Init(SDL_INIT_VIDEO);
SDL_Init(SDL_INIT_TIMER);


SDL_Surface* screen = SDL_SetVideoMode( 720, 480, 24, SDL_SWSURFACE );

SDL_Surface* player = SDL_LoadBMP("/home/mattias/Pictures/GIMPED/Tennsoldat från sidan.bmp"); //Loads a sprite with 0 255 255 background colour

SDL_Surface* background = SDL_LoadBMP("/home/mattias/CodeBlocks/Spelet/sprites/background3.bmp");

player = SDL_DisplayFormat(player); //Makes the player have the same properties as the screen, not really needed

Uint32 alpha = SDL_MapRGB( player->format, 0, 255, 255 ); //Specifies the colour which is going to be interpreted as alpha colour

SDL_SetColorKey( player, SDL_SRCCOLORKEY, alpha ); //Actually sets the pixel with this colour to alpha


apply_surface(0,0,background,screen);
apply_surface(720/2,480-64,player,screen);
SDL_Flip(screen);
SDL_Delay(3000);

SDL_Quit();

}


This is a modified extraction from my game. Despite having done the SetColorKey thing I still get my blue(0 255 255) border around my sprite. But if I set the bits per pixel to 24 it works. The sprite is in 24 bits I think and the colour is correct. In another part of the code I create a surface within SDL and fill this with alpha and it becomes transparent even with 32 BPP. Over at the SDL channel on Freenode they say that my VideoMode should have 32 bits so I would like to keep it that way. Why doesn't this work as it is?

I've attached the sprite and the background to this post. Just replace the file locations and you should be able to run this.
Hejsan!

I don't know why but if I change SDL_LoadBMP to IMG_Load it works. IMG_Load is part of SDL_image and supports more image formats.

I'm quite sure you shouldn't call SDL_Init more than once. Instead pass all the flags in the same call like this
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
Advertisement
On the topic of:

Over at the SDL channel on Freenode they say that my VideoMode should have 32 bits so I would like to keep it that way.[/quote]

It looks like SDL 1.3 is changing things up a little on the video mode and window creation:

LINK
SDBradley
CGP
"A person who won't read has no advantage over one who can't read." ~Mark Twain

This topic is closed to new replies.

Advertisement