IMG_Load prepares the image such that it is ready to blit. So you don't need to do anything special to get alpha transparency here. However, if you want the fastest blit, you might still want to (correctly) use SDL_DisplayFormatAlpha().
Anyway, that is the reason I suggested you try doing nothing to the image first. However I don't understand the behaviour you are describing. I have created a simple test program:
#include <iostream>
#include <cstdlib>
#include "SDL.h"
#include "SDL_image.h"
int main(int, char**)
{
if(SDL_Init(SDL_INIT_VIDEO) < 0)
{
std::cerr << "Failed to initialise SDL: " << SDL_GetError() << '\n';
return 1;
}
std::atexit(&SDL_Quit);
SDL_Surface *screen = SDL_SetVideoMode(800, 600, 0, SDL_SWSURFACE);
if(!screen)
{
std::cerr << "Failed to set video mode: " << SDL_GetError() << '\n';
return 1;
}
SDL_Surface *image = IMG_Load("foo.png");
if(!image)
{
std::cerr << "Failed to load imae: " << IMG_GetError() << '\n';
return 1;
}
#if 1
SDL_Surface *temp = SDL_DisplayFormatAlpha(image);
if(temp)
{
std::swap(temp, image);
SDL_FreeSurface(temp);
}
else
{
std::cerr << "Failed to format image to display: " << SDL_GetError() << '\n';
return 1;
}
#if 1
int result = SDL_SetAlpha(image, SDL_SRCALPHA, SDL_ALPHA_TRANSPARENT);
if(result < 0)
{
std::cerr << "Failed to set image alpha: " << SDL_GetError() << '\n';
return 1;
}
#endif
#endif
int x = 0;
int y = 0;
int vx = 1;
int vy = 1;
bool running = true;
while(running)
{
SDL_Event event;
while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
{
running = false;
}
else if(event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE)
{
running = false;
}
}
x += vx;
y += vy;
if(x < 0 || x + image->w > screen->w)
{
vx *= -1;
}
if(y < 0 || y + image->h > screen->h)
{
vy *= -1;
}
SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0x00, 0x00, 0xff));
SDL_Rect dest = { x, y };
SDL_BlitSurface(image, NULL, screen, &dest);
SDL_Flip(screen);
}
return 0;
}
You can use the preprocessor to enable the call to SDL_DisplayFormatAlpha(), and SDL_SetAlpha(). On my system, it looks the same regardess of whether none, one or both are enabled.