SDL学习入门

来源:互联网 发布:赛诺数据官网 编辑:程序博客网 时间:2024/06/04 23:20

1、搭建开发平台
    (1)sudo apt-get install libsdl1.2-dev
        最基本的开发包
    (2)sudo apt-get install libsdl-image1.2-dev
        关于图像的开发包
    (3)sudo apt-get install libsdl-mixer1.2-dev
        关于音频的开发包
    (4)sudo apt-get install libsdl-ttf2.0-dev
        关于文字的开发包
    安装好以上四个开发包,开发平台算是搭建好了
    
2、最有代表性的简单程序,包括图像,文字,音乐,注释如下:
[cpp] view plaincopyprint?
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <SDL/SDL.h>  
  4. #include <SDL/SDL_image.h>  
  5. #include <SDL/SDL_mixer.h>  
  6. #include <SDL/SDL_ttf.h>  
  7.   
  8. static SDL_Surface* screen;//SDL窗口  
  9.   
  10. int main(int argc, char *argv[])  
  11. {  
  12.   
  13.     int quit = 0;  
  14.     SDL_Surface *text_sur;//文字容器  
  15.     SDL_Surface *background;//图像容器  
  16.     SDL_Event event;  
  17.     SDL_Color color;  
  18.     SDL_Rect srect, drect;  
  19.     Mix_Music *bgsound;  
  20.     TTF_Font *font;  
  21.       
  22.     //初始化SDL  
  23.     if (SDL_Init(SDL_INIT_VIDEO||SDL_INIT_AUDIO) < 0)  
  24.     {  
  25.         fprintf(stderr, "SDL init error:%s\n", SDL_GetError());  
  26.         exit(0);  
  27.     }  
  28.     atexit(SDL_Quit);//注册退出时调用的操作  
  29.       
  30.       
  31.     //设置SDL屏幕大小  
  32.     screen = SDL_SetVideoMode(600, 400, 24, SDL_HWSURFACE);  
  33.     if (screen == NULL)  
  34.     {  
  35.         fprintf(stderr, "Set video mode failure:%s\n", SDL_GetError());  
  36.         exit(0);  
  37.     }  
  38.       
  39.     //设置SDL窗口标题  
  40.     SDL_WM_SetCaption("test", NULL);  
  41.    
  42.     /*显示图像*/  
  43.     background = IMG_Load("background.jpg");//导入图像文件,并将图像放入文字容器  
  44.     srect.x = 0;  
  45.     srect.y = 0;  
  46.     srect.w = background->w;  
  47.     srect.h = background->h;  
  48.     drect = srect;//设置截取范围  
  49.     SDL_BlitSurface(background, &srect, screen, &drect);//将图像容器放入SDL窗口  
  50.   
  51.     /*显示文字*/  
  52.     //初始化TTF  
  53.     if (TTF_Init() < 0)  
  54.     {  
  55.         fprintf(stderr, "TTF init error:%s\n", SDL_GetError());  
  56.         return;  
  57.     }  
  58.       
  59.     font = TTF_OpenFont("test.ttf", 40);//导入字体文件  
  60.     color.r = 255;  
  61.     color.g = 0;  
  62.     color.b = 0;//设置文字颜色  
  63.     text_sur=TTF_RenderText_Solid(font, "Hello, Welcome to GAME!!", color);//将文字放入文字容器  
  64.       
  65.     srect.x = 0;  
  66.     srect.y = 0;  
  67.     srect.w = text_sur->w;  
  68.     srect.h = text_sur->h;  
  69.       
  70.     drect.x = (600 - text_sur->w) / 2;  
  71.     drect.y = (400 - text_sur->h) / 2;  
  72.     drect.w = text_sur->w;  
  73.     drect.h = text_sur->h;//设置截取范围  
  74.     SDL_BlitSurface(text_sur, &srect, screen, &drect);//将文字容器放入SDL窗口  
  75.       
  76.     SDL_UpdateRect(screen, 0, 0, 600, 400);//更新SDL窗口,让新添加的容器显示  
  77.   
  78.     /*播放声音*/  
  79.     Mix_OpenAudio(44100, AUDIO_S16, 2, 4096);//打开音频  
  80.     bgsound = Mix_LoadMUS("bgsound.mp3");//导入声音文件  
  81.     Mix_PlayMusic(bgsound, -1);//播放音频  
  82.     while (quit == 0)  
  83.     {  
  84.         while (SDL_PollEvent(&event))  
  85.         {  
  86.             switch (event.type)  
  87.             {  
  88.              case SDL_QUIT:  
  89.                 Mix_CloseAudio();//关闭音频  
  90.                 quit = 1;  
  91.                 break;  
  92.              default:  
  93.                 break;  
  94.             }  
  95.         }  
  96.         SDL_Delay(100);  
  97.     }  
  98.       
  99.     return 0;  
  100. }  



问题:gcc main.c -o main编译时可能会出现如下错误:
[cpp] view plaincopyprint?
  1. main.c:(.text+0x19): undefined reference to `SDL_Init'  
  2. main.c:(.text+0x22): undefined reference to `SDL_GetError'  
  3. main.c:(.text+0x50): undefined reference to `SDL_Quit'  
  4. main.c:(.text+0x79): undefined reference to `SDL_SetVideoMode'  
  5. main.c:(.text+0x8c): undefined reference to `SDL_GetError'  
  6. main.c:(.text+0xc7): undefined reference to `SDL_WM_SetCaption'  
  7. main.c:(.text+0xd3): undefined reference to `IMG_Load'  
  8. main.c:(.text+0x132): undefined reference to `SDL_UpperBlit'  
  9. main.c:(.text+0x137): undefined reference to `TTF_Init'  
  10. main.c:(.text+0x140): undefined reference to `SDL_GetError'  
  11. main.c:(.text+0x174): undefined reference to `TTF_OpenFont'  
  12. main.c:(.text+0x1a3): undefined reference to `TTF_RenderText_Solid'  
  13. main.c:(.text+0x24c): undefined reference to `SDL_UpperBlit'  
  14. main.c:(.text+0x279): undefined reference to `SDL_UpdateRect'  
  15. main.c:(.text+0x29d): undefined reference to `Mix_OpenAudio'  
  16. main.c:(.text+0x2a9): undefined reference to `Mix_LoadMUS'  
  17. main.c:(.text+0x2c1): undefined reference to `Mix_PlayMusic'  
  18. main.c:(.text+0x2d5): undefined reference to `Mix_CloseAudio'  
  19. main.c:(.text+0x2ec): undefined reference to `SDL_PollEvent'  
  20. main.c:(.text+0x2fc): undefined reference to `SDL_Delay'  
  21. collect2: ld returned 1 exit status  
原因:那是因为该程序用到四个静态库,分别为:
(1)SDL
(2)SDL_image
(3)SDL_ttf
(4)SDL_mixer
需用-l参数连起来才能编译得过,如:gcc main.c -lSDL -lSDL_image -lSDL_ttf -lSDL_mixer -o main
0 0
原创粉丝点击