一、下载辅助库SDL_image
使用时确保已经安装了SDL1.2.15库
官网地址:http://www.libsdl.org/projects/SDL_image/release-1.2.html
下载地址:http://www.libsdl.org/projects/SDL_image/release/SDL_image-1.2.12.tar.gz
二、解压编译
tar xvf SDL_image-1.2.12.tar.gz
./configure
make
make install
三、测试样例代码:
#include <stdio.h>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
SDL_Surface *screen = NULL;
const int SCREEN_BPP = 32;
void display_png(char* file_name)
{
SDL_Surface* image = NULL;
/* Load the BMP file into a surface */
//image = SDL_LoadBMP(file_name);
//image = IMG_Load(file_name);
//if (image == NULL) {
// fprintf(stderr, "Couldn't load %s: %s\n", file_name, SDL_GetError());
// return;
//}
/* 转换SDL页面,得到一个像素格式和screen相同的页面 */
//SDL_Surface *skill_surface = SDL_DisplayFormat(temp);
//SDL_FreeSurface(temp);
/* 在screen的中间选定一个矩形区域来显示图像 */
//SDL_Rect dest_rect = {(screen->w - 200)/2, (screen->h - 125)/2, 200, 125};
//SDL_BlitSurface(skill_surface, NULL, screen, &dest_rect);
//SDL_Flip(screen);
/* Fill screen with some color */
SDL_FillRect(screen, NULL, 0xff009900);
/* Load PNG and Display */
SDL_Surface *temp = IMG_Load(file_name);
SDL_SetColorKey(temp, SDL_SRCCOLORKEY|SDL_RLEACCEL, 0x00000000);
SDL_Surface *skill_surface = SDL_DisplayFormat(temp);
SDL_FreeSurface(temp);
//SDL_Rect dest_rect = {(screen->w - 200)/2, (screen->h - 125)/2, 200, 125};
SDL_BlitSurface(skill_surface, NULL, screen, NULL);
SDL_Flip(screen);
}
void display_bmp(char* file_name)
{
SDL_Surface* image = NULL;
/* Load the BMP file into a surface */
image = SDL_LoadBMP(file_name);
if (image == NULL) {
fprintf(stderr, "Couldn't load %s: %s\n", file_name, SDL_GetError());
return;
}
/*
* Palettized screen modes will have a default palette (a standard
* 8*8*4 colour cube), but if the image is palettized as well we can
* use that palette for a nicer colour matching
*/
if (image->format->palette && screen->format->palette) {
SDL_SetColors(screen, image->format->palette->colors, 0, image->format->palette->ncolors);
}
/* Blit onto the screen surface */
if (SDL_BlitSurface(image, NULL, screen, NULL) < 0)
fprintf(stderr, "BlitSurface error: %s\n", SDL_GetError());
/* Update the modified region of the screen */
SDL_UpdateRect(screen, 0, 0, image->w, image->h);
/* Free the allocated BMP surface */
SDL_FreeSurface(image);
}
int main(int argc, char **argv)
{
SDL_Init(SDL_INIT_EVERYTHING);
screen = SDL_SetVideoMode( 600 , 480 , SCREEN_BPP, SDL_SWSURFACE );
if (screen == NULL)
return -1;
SDL_Event event;
bool gameRunning = true;
while (gameRunning)
{
if (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
{
gameRunning = false;
}
}
else
{
//display_bmp("output.png");
system("mv output.png output1.png");
display_png("output1.png");
}
}
SDL_Quit();
//getchar();
return 0;
}