96 lines
2.0 KiB
C
96 lines
2.0 KiB
C
/* Example of alpha blending with SDL. */
|
|
|
|
#include <SDL/SDL.h>
|
|
#include <SDL/SDL_image.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
int main()
|
|
{
|
|
SDL_Surface *screen;
|
|
SDL_Surface *background;
|
|
SDL_Surface *image_with_alpha;
|
|
SDL_Surface *image_without_alpha;
|
|
SDL_Rect src, dest;
|
|
|
|
/* init sdl and check for erro */
|
|
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
|
|
printf("Unable to init SDL: %s\n", SDL_GetError());
|
|
return 1;
|
|
}
|
|
|
|
atexit(SDL_Quit);
|
|
|
|
screen = SDL_SetVideoMode(800, 600, 16, 0);
|
|
if (screen == NULL) {
|
|
printf("Unable to set video mode: %s\n", SDL_GetError());
|
|
return 1;
|
|
}
|
|
|
|
/* Load image file */
|
|
image_with_alpha = IMG_Load("with-alpha.png");
|
|
if (image_with_alpha == NULL) {
|
|
printf("Unable to load image. \n");
|
|
return 1;
|
|
}
|
|
|
|
image_without_alpha = IMG_Load("without-alpha.png");
|
|
if (image_without_alpha == NULL) {
|
|
printf("Unable to load image\n");
|
|
return 1;
|
|
}
|
|
|
|
background = IMG_Load("background.png");
|
|
if (background == NULL) {
|
|
printf("Unable to load background.png.\n");
|
|
return 1;
|
|
}
|
|
|
|
/* Draw background. */
|
|
|
|
src.x = 0;
|
|
src.y = 0;
|
|
src.w = background->w;
|
|
src.h = background->h;
|
|
dest.x = 0;
|
|
dest.y = 0;
|
|
dest.w = background->w;
|
|
dest.h = background->h;
|
|
SDL_BlitSurface(background, &src, screen, &dest);
|
|
|
|
/* draw first image with alpha */
|
|
|
|
SDL_SetAlpha(image_with_alpha, SDL_SRCALPHA, 0);
|
|
src.w = image_with_alpha->w;
|
|
src.h = image_with_alpha->h;
|
|
dest.w = src.w;
|
|
dest.h = src.h;
|
|
dest.x = 40;
|
|
dest.y = 50;
|
|
SDL_BlitSurface(image_with_alpha, &src, screen, &dest);
|
|
|
|
/* Draw second image without alpha. Set to 50% transparence for entire surface.
|
|
*/
|
|
SDL_SetAlpha(image_without_alpha, SDL_SRCALPHA, 128);
|
|
src.w = image_without_alpha->w;
|
|
src.h = image_without_alpha->h;
|
|
dest.w = src.w;
|
|
dest.h = src.h;
|
|
dest.x = 180;
|
|
dest.y = 50;
|
|
SDL_BlitSurface(image_without_alpha, &src, screen, &dest);
|
|
|
|
/* Ask SDL to update entire screen */
|
|
SDL_UpdateRect(screen, 0, 0, 0, 0);
|
|
|
|
SDL_Delay(10000);
|
|
|
|
/* Free memory */
|
|
SDL_FreeSurface(background);
|
|
SDL_FreeSurface(image_with_alpha);
|
|
SDL_FreeSurface(image_without_alpha);
|
|
|
|
return 0;
|
|
}
|
|
|