Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3964835
  • 博文数量: 366
  • 博客积分: 9916
  • 博客等级: 中将
  • 技术积分: 7195
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-29 23:27
个人简介

简单!

文章分类

全部博文(366)

文章存档

2013年(51)

2012年(269)

2011年(46)

分类: LINUX

2013-01-07 23:44:15


       在Ubuntu系统上是自带SDL库的,之所以要源码编译安装SDL库主要有以下两个原因:1.可以使用指定的SDL版本;2.编译出可debug的版本。


一、获取源码、编译、安装

SDL代码使用mercurial管理,先安装mercurial然后clone获取代码。

  1. $ sudo apt-get install aptitude autoconf
  2. $ sudo aptitude install mercurial
  3. $ hg clone -u SDL-1.2 http://hg.libsdl.org/SDL SDL-1.2
  4. $ cd SDL-1.2
    $ ./autogen.sh
    $ ./configure  --prefix=/usr/
    $ make
    $ sudo make install
这样安装后,将出现/usr/lib/libSDL.so等文件。



二、一个简单的测试程序

sdltest1.c源码:

  1. #include <SDL/SDL.h>
  2.   
  3. int main( int argc, char* argv[])
  4. {
  5.     // Fire up SDL, this starts all subsystems; audio video etc.
  6.      SDL_Init(SDL_INIT_EVERYTHING);
  7.      // Now Shut it down
  8.      SDL_Quit();
  9.   
  10.      return 0;
  11. }
编译测试源代码:

  1. $ gcc -o test sdltest1.c -lSDL -g
产生test可执行程序,并且具有可以debug需要的symbol。

查看测试代码使用了哪些so库:

  1. $ ldd test | grep SDL
  2. libSDL-1.2.so.0 => /usr/lib/i386-linux-gnu/libSDL-1.2.so.0 (0xb76ea000)
从结果来看,测试文件并没有使用我们编译好的动态库(安装的路径在/usr/lib/libSDL.so)。

在gcc前添加LD_LIBRARY_PATH环境变量:

  1. $ export LD_LIBRARY_PATH=/usr/lib
再次编译:

  1. $ gcc -o test sdltest1.c -lSDL -g
再次查看测试代码使用了哪些so库:

  1. $ ldd test | grep SDL
  2. libSDL-1.2.so.0 => /usr/lib/libSDL-1.2.so.0 (0xb76ec000)
gdb调试该程序:

  1. $ gdb
  2. (gdb) file test
  3. Reading symbols from ./test...done.
  4. (gdb) b main
  5. Breakpoint 1 at 0x8048575: file sdltest1.c, line 6.
  6. (gdb) b SDL_Init
  7. Breakpoint 2 at 0x8048460
  8. (gdb) run
  9. Starting program: ./test
  10. [Thread debugging using libthread_db enabled]
  11. Using host libthread_db library "/lib/i386-linux-gnu/libthread_db.so.1".

  12. Breakpoint 1, main (argc=1, argv=0xbffff2b4) at sdltest1.c:6
  13. 6        SDL_Init(SDL_INIT_EVERYTHING);
  14. (gdb) s

  15. Breakpoint 2, SDL_Init (flags=65535) at ./src/SDL.c:151
  16. 151    {
  17. (gdb)
首先装载test,然后设置了两个断点,分别在main()函数和SDL_Init()函数。



三、复杂一点的SDL测试程序

sdltest2.c源码:

  1. #include <stdio.h>
  2. #include <SDL/SDL.h>
  3.  
  4. #define WIDTH 640
  5. #define HEIGHT 480
  6. #define BPP 4
  7. #define DEPTH 32
  8.  
  9. void setpixel(SDL_Surface *screen, int x, int y, Uint8 r, Uint8 g, Uint8 b)
  10. {
  11.     Uint32 *pixmem32;
  12.     Uint32 colour;
  13.   
  14.     colour = SDL_MapRGB( screen->format, r, g, b );
  15.    
  16.     pixmem32 = (Uint32*) screen->pixels + y + x;
  17.     *pixmem32 = colour;
  18. }
  19.  
  20.  
  21. void DrawScreen(SDL_Surface* screen, int h)
  22. {
  23.     int x, y, ytimesw;
  24.    
  25.     if(SDL_MUSTLOCK(screen))
  26.     {
  27.         if(SDL_LockSurface(screen) < 0) return;
  28.     }
  29.  
  30.     for(y = 0; y < screen->h; y++ )
  31.     {
  32.         ytimesw = y*screen->pitch/BPP;
  33.         for( x = 0; x < screen->w; x++ )
  34.         {
  35.             setpixel(screen, x, ytimesw, (x*x)/256+3*y+h, (y*y)/256+x+h, h);
  36.         }
  37.     }
  38.  
  39.     if(SDL_MUSTLOCK(screen)) SDL_UnlockSurface(screen);
  40.    
  41.     SDL_Flip(screen);
  42. }
  43.  
  44.  
  45. int main(int argc, char* argv[])
  46. {
  47.     SDL_Surface *screen;
  48.     SDL_Event event;
  49.    
  50.     int keypress = 0;
  51.     int h=0;
  52.    
  53.     if (SDL_Init(SDL_INIT_VIDEO) < 0 ) return 1;
  54.     
  55.     if (!(screen = SDL_SetVideoMode(WIDTH, HEIGHT, DEPTH, SDL_HWSURFACE)))
  56.     {
  57.         SDL_Quit();
  58.         return 1;
  59.     }
  60.    
  61.     while(!keypress)
  62.     {
  63.          DrawScreen(screen,h++);
  64.          while(SDL_PollEvent(&event))
  65.          {
  66.               switch (event.type)
  67.               {
  68.                   case SDL_QUIT:
  69.                   keypress = 1;
  70.                   break;
  71.                   case SDL_KEYDOWN:
  72.                        keypress = 1;
  73.                        break;
  74.               }
  75.          }
  76.     }
  77.  
  78.     SDL_Quit();
  79.    
  80.     return 0;
  81. }
效果如下:





四、动态库搜索路径的搜索先后顺序

  1. 编译目标代码时指定的动态库搜索路径;
  2. 环境变量LD_LIBRARY_PATH指定的动态库搜索路径;
  3. 配置文件/etc/ld.so.conf中指定的动态库搜索路径;
  4. 默认的动态库搜索路径/lib;
  5. 默认的动态库搜索路径/usr/lib。
阅读(6041) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~