• 9
• 10
• 9
• 10
• 10

# Problem with Png and SDL

This topic is 3977 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 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 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 on other sites
if your image's alpha channels are setup correctly all you should have to do is

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

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

##### 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 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;