什么是SDL,它能做什么?
Simple Directmedia Layer is a cross-platform multimedia library designed to provide low level access to audio, keyboard, mouse, joystick, 3D hardware via OpenGL, and 2D video framebuffer. It is used by MPEG playback software, emulators, and many popular games, including the award winning Linux port of "Civilization: Call To Power."
SDL supports Linux, Windows, Windows CE, BeOS, MacOS, Mac OS X, FreeBSD, NetBSD, OpenBSD, BSD/OS, Solaris, IRIX, and QNX. The code contains support for AmigaOS, Dreamcast, Atari, AIX, OSF/Tru64, RISC OS, SymbianOS, and OS/2, but these are not officially supported.
SDL is written in C, but works with C++ natively, and has bindings to several other languages, including Ada, C#, D, Eiffel, Erlang, Euphoria, Guile, Haskell, Java, Lisp, Lua, ML, Objective C, Pascal, Perl, PHP, Pike, Pliant, Python, Ruby, Smalltalk, and Tcl.
SDL is distributed under GNU LGPL version 2. This license allows you to use SDL freely in commercial programs as long as you link with the dynamic library.
如何使用SDL编写程序?
任何开发环境的配置都是千篇一律的,安装相应的SDL开发包,如果是在FreeBSD上,你就编译SDL的相关Port至于它们在哪里,怎么编译,不罗嗦 了。安装好以后,你可以得到一个叫sdl-config的东西,它会帮助你找到你所需要包含的头文件和所要链接的动态链接库。它们的参数分别是:"-- cflags"和"--libs"
这里有一个我写的Makefile文档,你可以修改一下就可以用了。至于更详细的内容建议找本关于Makefile的书读一读。我推荐一本:O'Reilly Taiwan公司出版的《GNU Make 项目管理》(东南大学出版社)
首先看下面的代码:
SDL_Surface *screen;
int main(int argc, char *argv[])
{
srand(time(NULL));
if ( SDL_Init(SDL_INIT_VIDEO) < 0 )
{
fprintf(stderr,
"Couldn't initialize SDL:%s\n",
SDL_GetError());
exit(1);
}
screen = SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);
if ( screen == NULL )
{
fprintf(stderr,
"Couldn't set display mode:%s\n",
SDL_GetError());
SDL_Quit();
exit(5);
}
// add codes in here.
SDL_Quit();
exit(0);
}
这是一个SDL程序的基本框架。它完成了SDL库的初始化,然后什么都没干就退出了。虽然它很简单,但是我们毕竟迈出了第一步。下面我们来看看它每是怎么初始化的。
首先我们定义了一个SDL_Surface指针,我们可以将它理解为用来显示我们的程序的所有输出的"屏幕"。SDL_Init函数用来开启SDL模式, 这里的参数是SDL_INIT_VIDEO我们开启的SDL视频模式,除了SDL视频模式,SDL还支持四种其它的模式。当我们成功开启了SDL视频模式 后,我们需要作一些必要的设置,我们将输出模式设置为640x480的屏幕大小(象不象以前玩的游戏的标准模式的屏幕大小?)但这一切参数都设置好了以 后,我们就可以把它当做一个通道或者屏幕,然后向它输出点什么了!现在什么都没有做,然后调用SDL_Quit()函数退出了SDL模式。
这个程序就在前面的框架上进行扩展,我们首先用一种方式向那个设置好的屏幕输出一张图片看看能否在那上面显示:
SDL_Surface *screen;
SDL_Surface *img_hander;
SDL_Surface * load_image(char *filename )
{
SDL_Surface *loadedImage = NULL;
SDL_Surface *optimizedImage = NULL;
loadedImage = IMG_Load(filename);
if ( loadedImage != NULL )
{
optimizedImage = SDL_DisplayFormat(loadedImage);
SDL_FreeSurface( loadedImage );
}
return optimizedImage;
}
void apply_surface(SDL_Surface *source,
SDL_Surface *destinaion,
int posx,
int posy)
{
SDL_Rect rect;
rect.x = posx;
rect.y = posy;
SDL_BlitSurface(source,NULL,destinaion, & rect);
}
int main(int argc, char *argv[])
{
srand(time(NULL));
if ( SDL_Init(SDL_INIT_VIDEO) < 0 )
{
fprintf(stderr,
"Couldn't initialize SDL:%s\n",
SDL_GetError());
exit(1);
}
screen = SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);
if ( screen == NULL )
{
fprintf(stderr,
"Couldn't set display mode:%s\n",
SDL_GetError());
SDL_Quit();
exit(5);
}
img_hander = load_image( "flag.jpg" );
apply_surface(screen,img_hander,0,0);
if(SDL_Flip(screen) ==- 1 )
return - 1 ;
SDL_FreeSurface(img_hander);
SDL_Delay( 2000 );
SDL_Quit();
exit(0);
}
(秩名) |