[SDL] SDL/SDL_Image : DIsplay transparent PNG

Vincent R. forumer at smartmobili.com
Fri Jun 19 09:47:00 PDT 2009


Hi,

IS there anything special to do to display a PNG with alpha channel ?
I have a background and I am displaying a png that is supposed to have some
transparency but it doesn't seem to work :

/* Initialize SDL */
	if (SDL_Init(SDL_INIT_VIDEO) < 0) 
	{
		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
		return(1);
	}

	info = SDL_GetVideoInfo();
	if ( info->vfmt->BitsPerPixel > 8 ) {
		video_bpp = info->vfmt->BitsPerPixel;
	} else {
		video_bpp = 16;
fprintf(stderr, "forced 16 bpp mode\n");
	}
	videoflags = SDL_SWSURFACE;

	/* Set video mode */
	if
((screen=SDL_SetVideoMode(info->current_w,info->current_h,video_bpp,videoflags))
== NULL)
	{
		fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n",
						w, h, video_bpp, SDL_GetError());
		quit(2);
	}

	//We must first initialize the SDL video component, and check for success
	if (SDL_Init(SDL_INIT_VIDEO) != 0)
	{
		printf("Unable to initialize SDL: %s\n", SDL_GetError());
		return 1;
	}
	
	//When this program exits, SDL_Quit must be called
	atexit(SDL_Quit);
	
	SDL_Surface *font;
	SDL_Surface *temp;	//This pointer will temporarily reference our bitmap
sprite
	SDL_Rect rcSrc, rcDest;	//These rectangles will describe the source and
destination regions of our blit

	temp = IMG_Load(std::string(sAppPath +
std::string("BG_pink_680x680_32bits.png")).c_str());
	image_font = SDL_DisplayFormat(temp);
	SDL_FreeSurface(temp);

	rcSrc.x = 0;
	rcSrc.y = 0;
	rcSrc.w = image_font->w;
	rcSrc.h = image_font->h;

	SDL_BlitSurface(image_font, NULL, screen, &rcSrc);
	temp = IMG_Load(std::string(sAppPath + images_min[0]).c_str());
	//sAppPath += sImgPath1;
	//temp = IMG_Load(sAppPath.c_str());
	if (temp == NULL) {
		printf("Unable to load bitmap: %s\n", SDL_GetError());
		return 33;
	}
	
	//Convert the surface to the appropriate display format
	image = SDL_DisplayFormat(temp);
	
	//Release the temporary surface
	SDL_FreeSurface(temp);
	
	//Construct the source rectangle for our blit
	rcSrc.x = 0;
	rcSrc.y = 0;
	rcSrc.w = image->w;	//Use image->w to display the entire width of the
image
	rcSrc.h = image->h;	//Use image->h to display the entire height of the
image

	//Blit the image to the backbuffer
	//resizeImage(image, rcDest.w, rcDest.h);
	SDL_BlitSurface(image, NULL, screen, &rcSrc);
	//SDL_StretchSurfaceRect(image,  &rcSrc, screen, &rcDest);

	//Flip the backbuffer to the primary
	SDL_Flip(screen);



More information about the SDL mailing list