织梦CMS - 轻松建站从此开始!

罗索

使用SDL库编程

落鹤生 发布于 2010-03-20 21:16 点击:次 
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.
TAG:

什么是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文档,你可以修改一下就可以用了。至于更详细的内容建议找本关于Makefile的书读一读。我推荐一本:O'Reilly Taiwan公司出版的《GNU Make 项目管理》(东南大学出版社)

  • SDL程序框架:

首先看下面的代码:

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程序:

这个程序就在前面的框架上进行扩展,我们首先用一种方式向那个设置好的屏幕输出一张图片看看能否在那上面显示:

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);
}
(秩名)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201003/8814.html]
本文出处:网络博客 作者:秩名
顶一下
(7)
87.5%
踩一下
(1)
12.5%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容