http://hi.baidu.com/guzhou_diaoke/item/236f5337bc9417fd2784f415
/*
* sdl image
* guzhoudiaoke@126.com
* 2011-11-22 22:19
*/
#include <stdio.h>
#include <SDL/SDL.h>
int main(int argc, char **argv)
{
SDL_Surface *screen, *image1, *image2;
SDL_Rect location1, location2;
int delay = 4;
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
perror("SDLInit");
return 1;
}
printf("SDL initialized.\n");
screen = SDL_SetVideoMode(800, 600, 0, 0);
if (screen == NULL) {
printf("Problem: %s\n", SDL_GetError());
return 1;
}
printf("Video mode set.\n");
SDL_WM_SetCaption("SDL test image", "testing");
image1 = SDL_LoadBMP("image1.bmp");
if (image1 == NULL) {
printf("Problem: %s\n", SDL_GetError());
return 1;
}
printf("image1 loaded.\n");
image2 = SDL_LoadBMP("image2.bmp");
if (image2 == NULL) {
printf("Problem: %s\n", SDL_GetError());
return 1;
}
printf("image2 loaded.\n");
location1.x = (screen->w - image1->w) / 2;
location1.y = (screen->h - image1->h) / 2;
location1.w = image1->w;
location1.h = image1->h;
location2.x = (screen->w - image2->w) / 2;
location2.y = (screen->h - image2->h) / 2;
location2.w = image2->w;
location2.h = image2->h;
if (SDL_BlitSurface(image1, NULL, screen, &location1) < 0) {
printf("Problem: %s\n", SDL_GetError());
return 1;
}
SDL_UpdateRects(screen, 1, &location1);
SDL_Delay(delay * 1000);
if (SDL_BlitSurface(image2, NULL, screen, &location2) < 0) {
printf("Problem: %s\n", SDL_GetError());
return 1;
}
SDL_UpdateRects(screen, 1, &location2);
SDL_Delay(delay * 1000);
SDL_Quit();
return 0;
}
Makefile:
1 all : sdl_image
2
3 sdl_image: sdl_image.c
4 g++ -I/usr/include/SDL -o $@ $< -L/usr/lib -lSDL
![](https://i-blog.csdnimg.cn/blog_migrate/ee679a43deb6c0a05ebacbe8fdc11263.jpeg)