//The headers
#include<SDL/SDL.h>
#include<stdio.h> //the attributes of the screen
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32; //the surfaces that will be used /*这些是将要使用的3个图片的声明。"background"明显将作为背景图片,
"message" 是一个显示“Hello”的位图,而 "screen"明显是一个屏幕。
牢记:如果指针没有指向目标时,最好的方法是让它们指向NULL。*/ SDL_Surface *message = NULL;
SDL_Surface *background = NULL;
SDL_Surface *Screen = NULL; SDL_Surface *load_image(char *filename)
{
//临时储存载入图片
SDL_Surface *loadedImage = NULL;
//将使用的优化的图片
SDL_Surface *optimizedImage = NULL;
/*这里,我们使用图片加载函数。这个函数用来载入图片,返回一个指针,用
来优化加载图像版本。参数"filename"是图片加载的路径,"loadedImage"是我
们加载图片时的画面。"optimizedImage"是我们将要使用的画面。*/ //load the image
loadedImage = SDL_LoadBMP("1.BMP"); //if nothing went wrong in loading the image
if(loadedImage != NULL)
{
//creat an optimizedImage
optimizedImage = SDL_DisplayFormat(loadedImage); //free the old image
SDL_FreeSurface(loadedImage);
}
//return the optimized image
return optimizedImage;
} void apply_surface(int x,int y,SDL_Surface *source,SDL_Surface *destination)
{
//make a temporary rectangle to hold the offsets
SDL_Rect offset; //give the offsets to the rectangle
offset.x = x;
offset.y = y; //blit the surface
SDL_BlitSurface(source,NULL,destination,&offset); return;
} int main(int argc,char *args[])
{
//Initialize all SDL subsystems
if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
{
return 1;
} //set up the screen
Screen = SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP,SDL_SWSURFACE); //if there was an error in setting up the screen
if(Screen == NULL)
{
return 1;
}
//set the window caption
SDL_WM_SetCaption("Hello World",NULL); //loade the images
message = load_image("1.bmp");
background = load_image("1.bmp"); //apply the background to the screen
apply_surface(0,0,background,Screen); //wait 2 seconds
SDL_Delay(8000); //free the surfaces
SDL_FreeSurface(message);
SDL_FreeSurface(background); //quit SDL
SDL_Quit(); //return
return 0;
}
#include<SDL/SDL.h>
#include<stdio.h> //the attributes of the screen
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32; //the surfaces that will be used /*这些是将要使用的3个图片的声明。"background"明显将作为背景图片,
"message" 是一个显示“Hello”的位图,而 "screen"明显是一个屏幕。
牢记:如果指针没有指向目标时,最好的方法是让它们指向NULL。*/ SDL_Surface *message = NULL;
SDL_Surface *background = NULL;
SDL_Surface *Screen = NULL; SDL_Surface *load_image(char *filename)
{
//临时储存载入图片
SDL_Surface *loadedImage = NULL;
//将使用的优化的图片
SDL_Surface *optimizedImage = NULL;
/*这里,我们使用图片加载函数。这个函数用来载入图片,返回一个指针,用
来优化加载图像版本。参数"filename"是图片加载的路径,"loadedImage"是我
们加载图片时的画面。"optimizedImage"是我们将要使用的画面。*/ //load the image
loadedImage = SDL_LoadBMP("1.BMP"); //if nothing went wrong in loading the image
if(loadedImage != NULL)
{
//creat an optimizedImage
optimizedImage = SDL_DisplayFormat(loadedImage); //free the old image
SDL_FreeSurface(loadedImage);
}
//return the optimized image
return optimizedImage;
} void apply_surface(int x,int y,SDL_Surface *source,SDL_Surface *destination)
{
//make a temporary rectangle to hold the offsets
SDL_Rect offset; //give the offsets to the rectangle
offset.x = x;
offset.y = y; //blit the surface
SDL_BlitSurface(source,NULL,destination,&offset); return;
} int main(int argc,char *args[])
{
//Initialize all SDL subsystems
if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
{
return 1;
} //set up the screen
Screen = SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP,SDL_SWSURFACE); //if there was an error in setting up the screen
if(Screen == NULL)
{
return 1;
}
//set the window caption
SDL_WM_SetCaption("Hello World",NULL); //loade the images
message = load_image("1.bmp");
background = load_image("1.bmp"); //apply the background to the screen
apply_surface(0,0,background,Screen); //wait 2 seconds
SDL_Delay(8000); //free the surfaces
SDL_FreeSurface(message);
SDL_FreeSurface(background); //quit SDL
SDL_Quit(); //return
return 0;
}