PSP编程开发学习笔记(二):SDL配置



掌叔
2009-02-18 09:34:30

摘自:[url]http://blog.sina.com.cn/jesil[/url]
作者:嫣雨华源

我是打算在VS2008里调用SDL,所以只翻译了LAZY FOO's Productions下VS.net 2005/2008部分,如果有想要使用VC6平台的同志,可以去[url=http://lazyfoo.net/SDL_tutorials/index.php]SDL英文教程[/url]去查询VC6的安装SDL方法,这里就不另述了。这个网址下还有许多SDL的教程,希望对大家有所帮助。

首先去[url=http://www.libsdl.org/download-1.2.php]这里[/url]下载SDL相关资源,下载下图红箭头所指部分。之后解压下载文件包,并存放在你觉得适当的路径下,我的是"C:PSP移植SDL"的目录下。

[attach]849[/attach]

1,启动VS,进入工具菜单栏的选项。

[attach]850[/attach]

2,到项目和解决方案下的VC++目录里,分别在显示以下内容的目录的选项里选择包含文件(include)和库文件(lib),将刚刚解压的文件添加进去。

[attach]851[/attach]

[attach]852[/attach]

[attach]853[/attach]

[attach]854[/attach]

[attach]855[/attach]

3,将刚解压的lib文件夹里的SDL.dll复制到"盘符:windowssystem32"里去,这样每次的SDL APP都可以找到SDL.dll,不用特意把SDL.dll放到工程目录里去。不过这样做有一个弊端,就是SDL.dll与SDL版本不一样的话,可能会发生问题。所以一般推荐是将SDL.dll文件放到编译的工程目录里

4,之后就可以正常的建立项目了,不过建立项目后需要进行几个设置。进入项目工具栏里的属性。

[attach]856[/attach]

在C/C++的常规里,把检测64位可移植性问题选否。

[attach]857[/attach]

在代码生成里的运行时库改为多线程dll

[attach]858[/attach]

在链接器里的输入中附加依赖项里添加SDL.lib和SDLmain.lib

[attach]859[/attach]

最后在链接器里的系统里,在子系统里选择Windows

[attach]860[/attach]

5,最后附加一个测试代码:

[code=c]#include "SDL.h"
#include

//The attributes of the screen
const int SCREEN_WIDTH = 480;
const int SCREEN_HEIGHT = 272;
const int SCREEN_BPP = 32;

//The surfaces that will be used
SDL_Surface *message = NULL;
SDL_Surface *background = NULL;
SDL_Surface *screen = NULL;
SDL_Surface *load_image( std::string filename );
void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination );
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 );
//Load the images
//message = load_image( "hello_world.bmp" );
background = load_image( "hello_world.bmp" );
//Apply the background to the screen
apply_surface( 0, 0, background, screen );
//Update the screen
if( SDL_Flip( screen ) == -1 )
{
return 1;
}
//Wait 2 seconds
SDL_Delay( 2000 );
//Free the surfaces
// SDL_FreeSurface( message );
SDL_FreeSurface( background );

//Quit SDL
SDL_Quit();

//Return
return 0;
}

//load_image 读取位图并转换成32位图,好与显示器上的屏幕位图匹配
SDL_Surface *load_image( std::string filename )
{
//Temporary storage for the image that's loaded
SDL_Surface* loadedImage = NULL;

//The optimized image that will be used
SDL_Surface* optimizedImage = NULL;
//Load the image
loadedImage = SDL_LoadBMP( filename.c_str() );
//If nothing went wrong in loading the image
if( loadedImage != NULL )
{
//Create an optimized image
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 );
}[/code]

注意,必须将一个名为hello_world.bmp的文件拷贝到项目文件夹下才行。以后每次新建一个工程必须重复第四步。