I would like to write a program that fade in a PNG file in main SDL screen with
a background image.
Currently it works fine when PNG file has non-transparent background, but when
PNG file has transparent background, nothing happens.
I have tried lots combination of
SDL_DisplayFormat/SDL_DisplayFormatAlpha/SDL_BlitSurface, but I can’t get both
kind PNG file work correctly.
The following is my source code (It’s written in D Programming Language, but it
looks like C a lot and SDL API is same), where should I check?
================== Start of Source Code ========================
module fade;
import derelict.sdl.sdl;
import derelict.sdl.image;
import tango.io.Stdout;
void main ()
{
DerelictSDL.load();
DerelictSDLImage.load();
if ( SDL_Init(SDL_INIT_VIDEO) != 0 ) {
Stdout ("Error init.").newline;
}
SDL_Surface * screen = SDL_SetVideoMode (800,600,32,SDL_HWSURFACE);
SDL_Surface * background = IMG_Load ("../image/background.jpg");
SDL_Surface * image = IMG_Load ("../image/image.png");
if (image is null ) {
Stdout ("Error").newline;
}
// Copy a blank screen.
SDL_Surface * back = SDL_DisplayFormatAlpha(screen);
// Place Position
SDL_Rect rect2;
rect2.x = 100;
rect2.y = 100;
// Place Size
SDL_Rect rect;
rect.w = image.w;
rect.h = image.h;
// Fade in
for (int i = 0; i < 255; i = i+5) {
// Clear screen.
SDL_BlitSurface (background, null, screen, null);
// Set alpha for PNG image
SDL_SetAlpha (image, SDL_SRCALPHA, i);
/*************************************************************
* Works fine when PNG file has no transparent background,
* but when PNG file has transparent background, nothing
* happend.
************************************************************/
//! SDL_Surface * temp = SDL_DisplayFormatAlpha(image);
//! SDL_BlitSurface (temp, &rect, screen, &rect2);
/*************************************************************
* Both PNG file fade in normally, but the transparent
* background in PNG will become white.
************************************************************/
//! SDL_Surface * temp = SDL_DisplayFormat(image);
//! SDL_BlitSurface (temp, &rect, screen, &rect2);
/*************************************************************
* Works fine when PNG file has no transparent background,
* but when PNG file has transparent background, nothing
* happend.*
* PS:Seems slower.
************************************************************/
//! SDL_BlitSurface (image, &rect, screen, &rect2);
SDL_Flip(screen);
}
}
================== End of Source Code ========================