Sign in to follow this  
flipper83

Problem with Png and SDL

Recommended Posts

I have a problem painting a Surface with alpha blending. If I load a BMP everything goes fine, but if I load a Png with SDL_Image it doesn't work. <code> Surface *sprite = IMG_Load("fichasbase.png"); //... SDL_SetAlpha(sprite,SDL_SRCALPHA|SDL_RLEACCEL,255); SDL_Rect rect = {x, y, 0, 0}; SDL_BlitSurface(sprite, &clip, screen, &rect); </code> why?? any work around? pleaseeeeeeeee. i'm sorry for my english

Share this post


Link to post
Share on other sites
you might try converting the image to SDL format

SDL_Surface *ConvertSDLformat( std::string filename )
{
SDL_Surface* image = NULL;
SDL_Surface* newimage = NULL;
image = IMG_Load( filename.c_str() );
if ( image != NULL )
{
newimage = SDL_DisplayFormat( image );
SDL_FreeSurface( image );
}
return newimage;
}

That bit of code is very similar to LazyFoo's tutorials.
I dont know if that will work, but it is an idea.

Share this post


Link to post
Share on other sites
What do you mean "it doesn't work"?

If your image doesn't appear to be transparent, it's probably because you're passing 255 to SDL_SetAlpha(), which AFAIK is opaque. Try passing 128 and see if it helps. If not, the SDL_SetAlpha(3) manpage says that per-pixel alpha is used if available, per-surface alpha notwhistanding. I believe this is only for RGBA surface to RGBA surface blits, but if passing 128 to SDL_SetAlpha() doesn't help, I'd suggest making sure that your surface doesn't have an alpha channel.

As RPG_Fanatic said, converting to the current display format is strongly advised for various reasons. However, for images with alpha informations, you'll have to use SDL_DisplayFormatAlpha(), not SDL_DisplayFormat().

If you mean something else, you'll need to provide more informations, like what the problem actually is.

Also, use [code]...[/code] to format your code, not <code>...</code>. [smile]

Hope this helps.

Share this post


Link to post
Share on other sites
if your image's alpha channels are setup correctly all you should have to do is

[QUOTE]
Surface *sprite = IMG_Load("fichasbase.png");

SDL_Rect rect = {x, y, 0, 0};
SDL_BlitSurface(sprite, &clip, screen, &rect);
[QUOTE]

assuming that clip is correct
you don't need SDL_SetAlpha(sprite,SDL_SRCALPHA|SDL_RLEACCEL,255);
can you post the png

Share this post


Link to post
Share on other sites
thanks for all.

i use RPG_Fanatic's method, and everything works fine, but the alpha chanel doesn't work, :S. I've tried using SDL_DisplayFormatAlpha() instead of SDL_DisplayFormat() and then the alpha works fine but the alpha blending doesn't.

Im sorry for the copy-paste errors, the clip is ok, and the 255 is a error, y use 80.

any ideas?

Share this post


Link to post
Share on other sites
i have found a work around. Now i save the PNG image without alpha capability and i put a background color (0xff00ff). Use on code :

SDL_SetColorKey(newimage, SDL_SRCCOLORKEY|SDL_RLEACCEL, SDL_MapRGB(newimage->format,255,0,255));

and now all works fine. Thanks for all, if something have another idea i would like to listen it;

Share this post


Link to post
Share on other sites

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