#include <sdl.h>
#include <sdl_image.h>
void applySurface(SDL_Surface* source, SDL_Surface* destination, int x, int y)
{
SDL_Rect offset;
offset.x = x;
offset.y = y;
SDL_BlitSurface(source, NULL, destination, &offset);
}
SDL_Surface* loadImage(std::string file)
{
SDL_Surface* temp = IMG_Load(file.c_str());
SDL_Surface* image = SDL_DisplayFormatAlpha(temp);
SDL_FreeSurface(temp);
return(image);
}
int main(int argc, char* argv[])
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Surface* screen(nullptr);
screen = SDL_SetVideoMode(800, 600, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
SDL_ShowCursor(SDL_DISABLE);
SDL_Surface* bg = loadImage("data/bg.png");
SDL_Surface* img1 = loadImage("data/img1.png");
bool done(false);
SDL_Event event;
while(!done)
{
while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT || (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE))
{
done = true;
}
}
applySurface(bg, screen, 0, 0);
static Uint8 alpha(0);
for(int y(0); y < img1->h; ++y)
{
for(int x(0); x < img1->w; ++x)
{
Uint32* pixel = (Uint32*)img1->pixels + (x + y * img1->w);
Uint8 r;
Uint8 g;
Uint8 b;
Uint8 a;
SDL_GetRGBA(*pixel, img1->format, &r, &g, &b, &a);
*pixel = SDL_MapRGBA(screen->format, r, g, b, alpha);
}
}
++alpha;
applySurface(img1, screen, 0, 0);
SDL_Flip(screen);
}
SDL_Quit();
return(0);
}
I've tried several different ways of changing the alpha value, but all with the same result.