I am loading PNG file (with some transparent places) into my SDL application.
Googling on how to do it provided me with this code sample:
SDL_Surface *LoadImage(std::string filename)
{
SDL_Surface* loaded_image = 0, compatible_image = 0;
if (!filename.c_str())
return 0;
loaded_image = IMG_Load(filename.c_str());
if (!loaded_image)
return 0;
compatible_image = SDL_DisplayFormat(loaded_image);
SDL_FreeSurface(loaded_image);
return compatible_image;
}
But when the line compatible_image = SDL_DisplayFormat(loaded_image); is reached, application halts with an uncatchable exception (even try { /* ... */ } catch (...) { /* ... */ } does not help). Replacing SDL_DisplayFormat() with SDL_DisplayFormatAlpha() did not help too. So, i just removed the exception-trowable lines and get this code working to load images:
SDL_Surface *LoadImage(std::string filename)
{
if (!filename.c_str())
return 0;
return IMG_Load(filename.c_str());
}
And i’ve found such unpleasant thing: when some sprite overlaps with transparent pieces of another one, artifacts appear. Something like this:


I am animating my “hero” with this simple algorithm:
// SDL_Surface sprite = LoadImage("hero.bmp");
// hero.bmp contains animation frames followed one-by-one in a single line
// spriteFrameCnt is a number of animation frames
// spriteWidth and spriteHeight contain single frame params
SDL_Rect srcRect;
srcRect.x = spriteFrame * spriteWidth;
srcRect.w = spriteWidth;
srcRect.y = 0;
srcRect.h = spriteHeight;
spriteFrame = ++spriteFrame % spriteFrameCnt;
SDL_BlitSurface(sprite, &srcRect, screen, &rcSprite);
How can this be explained and fixed?
Found a solution: when a tile with transparent pieces is drawn and another sprite is drawn on the top of the first one, artifacts appear. Sure they will, because nothing lies behind that first sprite!
The first and the most-quick fix is to fill the entire background with some color before any further blittings.
The second and the most beautiful way (in my case) is to draw some background image (grass as shown).
Here are the results:
As you can see, no artifacts appearing. Well done, me! =)