#include "SDL/SDL.h"
#include <string>
//屏幕属性
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;
//能被使用到的面
SDL_Surface *message = NULL;
SDL_Surface *background = NULL;
SDL_Surface *screen = NULL;
SDL_Surface *load_image( std::string filename )
{
//临时存储图像
SDL_Surface* loadedImage = NULL;
//存储优化的图像
SDL_Surface* optimizedImage = NULL;
//加载图片
loadedImage = SDL_LoadBMP( filename.c_str() );
//如果在加载图像时候没有错误发生
if( loadedImage != NULL )
{
//创建一个优化的图像
optimizedImage = SDL_DisplayFormat( loadedImage );
//释放旧图像
SDL_FreeSurface( loadedImage );
}
//返回优化图像
return optimizedImage;
}
void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination )
{
//建立一个零时的矩阵设置显示的坐标
SDL_Rect offset;
//设置矩形的坐标
offset.x = x;
offset.y = y;
//显示图像
SDL_BlitSurface( source, NULL, destination, &offset );
}
int main( int argc, char* args[] )
{
//初始化所有SDL子系统
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
{
return 1;
}
//建立一个屏幕
screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
//如果在设计屏幕的时候有错误
if( screen == NULL )
{
return 1;
}
//设置窗口标题
SDL_WM_SetCaption( "Hello World", NULL );
//加载图片
message = load_image( "hello.bmp" );
background = load_image( "background.bmp" );
//将背景面应用到屏幕上
apply_surface( 0, 0, background, screen );
apply_surface( 320, 0, background, screen );
apply_surface( 0, 240, background, screen );
apply_surface( 320, 240, background, screen );
//将加载图片的面应用到屏幕上
apply_surface( 180, 140, message, screen );
//更新屏幕
if( SDL_Flip( screen ) == -1 )
{
return 1;
}
//等待2秒
SDL_Delay( 2000 );
//释放所有的面
SDL_FreeSurface( message );
SDL_FreeSurface( background );
//退出SDL
SDL_Quit();
return 0;
}
转载于:https://my.oschina.net/u/1271540/blog/492054