Linux下的SDL图片浏览

http://blog.csdn.net/tody_guo/article/details/7452071

 

#include "SDL.h" 

  1. //#include "SDL_ttf.h" 
  2. #include "SDL_rotozoom.h" 
  3. #include "SDL_image.h" 
  4. #include "math.h" 
  5. #include "string.h" 
  6.  
  7. #define MAX  16 
  8. #define BMP_NAME "icon.bmp" 
  9. #define ICON "sample.bmp" 
  10. #define TITLE_NAME "TestSDL" 
  11. #define ICON_NAME  "My WinFrame"        //窗体最小化name 
  12. //#define PNG_NAME "Bliss.png"          //背景图片 
  13.  
  14. #define TEXT "欢迎光临"             //文字设置 
  15. #define TEXT_SIZE 50                //设置字体大小 
  16.  
  17. #define WIDTH  640 
  18. #define HEIGTH 480 
  19.  
  20. char *picture[] = { 
  21.           "1.jpg"  ,  "2.jpg"  ,  "3.jpg"  ,  "4.jpg" ,  
  22.           "5.jpg"  ,  "6.jpg"  ,  "7.jpg"  ,  "8.jpg"
  23.           "9.jpg"  ,  "10.jpg""11.jpg""12.jpg"
  24.           "13.png""14.jpg""15.jpg""16.jpg" 
  25.           }; 
  26. static SDL_Surface *screen = NULL; 
  27.  
  28. int Init()                      //初始化SDL 
  29.     if((SDL_Init(SDL_INIT_VIDEO)&&IMG_Init(IMG_INIT_PNG)) == -1) 
  30.     { 
  31.         fprintf(stderr,"SDL init error:%s",SDL_GetError()); 
  32.         return -1; 
  33.     } 
  34.     return 0; 
  35.  
  36. SDL_Surface *loadBMP(char *fileName)            //加载bmp图片 
  37.     SDL_Surface *bmp; 
  38.     bmp = SDL_LoadBMP(fileName); 
  39.     if(bmp == NULL) 
  40.     { 
  41.         fprintf(stderr,"Could not load %s: %s",fileName,SDL_GetError()); 
  42.         exit(1); 
  43.     } 
  44.     return bmp; 
  45.  
  46. void creatScreen(int width , int height , int bpp , Uint32 flags)       //创建一个VideoMode 
  47.     screen = SDL_SetVideoMode(width , height,  bpp , flags); 
  48.     if(screen == NULL) 
  49.     { 
  50.         fprintf(stderr,"Could not Creat a Screen!:%s",SDL_GetError()); 
  51.         exit(1); 
  52.     } 
  53.     return
  54. int Destory(SDL_Surface *file)                  //释放空间 
  55.     SDL_FreeSurface( file );    return 0; 
  56. void show_Pic(SDL_Surface *bmp ,SDL_Rect *rect) //显示bmp图片 
  57.     SDL_BlitSurface(bmp , NULL , screen , rect); 
  58.     SDL_UpdateRect(screen , 0 , 0 , 0 , 0 ); 
  59.     return
  60.  
  61. int getRightPic(int i) 
  62.      
  63.     i++; 
  64.     if(i % 15 == 0) 
  65.     { 
  66.         i = 0; 
  67.     } 
  68.     return i;  
  69. int  getLeftPic(int i) 
  70.     i--; 
  71.     if(i < 0) 
  72.     { 
  73.         i = 15; 
  74.     } 
  75.     return i;  
  76. void zoomBig(double *a) 
  77. {    
  78.     *a -= 0.25; 
  79.     if(*a <= 1.5) 
  80.     { 
  81.         *a = 1.5; 
  82.     } 
  83.      
  84. void zoomSmall(double *a) 
  85.  
  86.     *a += 0.25;  
  87.     if(*a >= 5) 
  88.     { 
  89.         *a = 5; 
  90.     }    
  91.  
  92. void draw_button(char *str , int flag) 
  93.     if(strcmp(str , "left") == 0) 
  94.     { 
  95.         boxColor(screen, 160, 420 , 200, 460, 0xf3f5ffff); 
  96.         if(flag == 1) 
  97.         { 
  98. hlineColor( screen, 160 , 200 ,460 , 0x000000ff);
  99. vlineColor( screen, 200 , 420 , 460 , 0x000000ff);
  100.         } 
  101.      
  102.         show_Pic( screen , &(screen->clip_rect) );    
  103.     } 
  104.     if(strcmp(str , "right") == 0) 
  105.     { 
  106.         boxColor(screen, 438, 420 , 478, 460, 0xf3f5ffff); 
  107.         if(flag == 1) 
  108.         { 
  109.             hlineColor( screen, 438 , 478 ,  460, 0x000000ff); 
  110.             vlineColor( screen, 478 , 420 , 460 , 0x000000ff); 
  111.         } 
  112.         show_Pic( screen , &(screen->clip_rect) );    
  113.     } 
  114.     return
  115. void button(char *str) 
  116.     SDL_Rect rect; 
  117.     int x,y; 
  118.     if(strcmp(str , "left") == 0) 
  119.     { 
  120.         boxColor(screen, 160, 420 , 200, 460, 0xf3f5ffff); 
  121.         SDL_UpdateRect(screen , 0 , 0 , 0 , 0 ); 
  122.     } 
  123.     if(strcmp(str , "right") == 0) 
  124.     { 
  125.         boxColor(screen, 438, 420 , 478, 460, 0xf3f5ffff); 
  126.         SDL_UpdateRect(screen , 0 , 0 , 0 , 0 ); 
  127.     } 
  128. int main(int argc,char **argv) 
  129. {    
  130.  
  131.     const SDL_VideoInfo *info = NULL; 
  132.     int flag = 1; 
  133.     int i = 0; 
  134.     int width = WIDTH; 
  135.     int heigth = HEIGTH; 
  136.     int bpp = 0; 
  137.     double zoom_x,zoom_y; 
  138.     SDL_Surface *backpng = NULL; 
  139.     Init(); 
  140.     creatScreen(width , heigth, bpp , SDL_SWSURFACE); 
  141.  
  142.     info = SDL_GetVideoInfo(); 
  143.     if(info == NULL) 
  144.     { 
  145.         fprintf( stderr, "Video query failed: %s\n",SDL_GetError( ) ); 
  146.         exit(1); 
  147.  
  148.     } 
  149.     bpp = info->vfmt->BitsPerPixel ;          //得到VideoMode的色度bpp 
  150.      
  151.     backpng = IMG_Load(picture[i]); 
  152.     if(!backpng) 
  153.     { 
  154.         fprintf(stderr,"Could not load %s: %s\n",picture[i],SDL_GetError()); 
  155.         exit(1);     
  156.     } 
  157.  
  158.     zoom_x = (double)((screen->w) / (double)(backpng->w)); 
  159.     zoom_y = (double)((screen->h) / (double)(backpng->h)); 
  160.      
  161.     backpng = zoomSurface(backpng , zoom_x , zoom_y , 1); 
  162.     show_Pic(backpng ,&(screen->clip_rect)); 
  163.     SDL_Rect fillRect; 
  164.      
  165.     fillRect.x = 107; 
  166.     fillRect.y = 80; 
  167.     fillRect.w = 426; 
  168.     fillRect.h = 320; 
  169.      
  170.      
  171.     SDL_WM_SetCaption(TITLE_NAME, ICON_NAME);               //设置窗口标题 
  172.     SDL_WM_SetIcon(loadBMP(ICON) , NULL);                       //设置图标   
  173.     static double zoomXY = 1.5; 
  174.     SDL_Event event; 
  175.     while(flag) 
  176.     { 
  177.         while(SDL_PollEvent(&event))            //将事件加入事件队列队列 
  178.         {    
  179.             switch(event.type) 
  180.             { 
  181.                 case SDL_KEYDOWN: 
  182.                     printf("Key Down......\n"); 
  183.                     if(event.key.keysym.sym == SDLK_ESCAPE) 
  184.                     { 
  185.                         flag = 0; 
  186.                     } 
  187.                     if(event.key.keysym.sym == SDLK_SPACE) 
  188.                     { 
  189.                         backpng = IMG_Load(picture[0]); 
  190.                         if(!backpng) 
  191.                         { 
  192.                             fprintf(stderr,"Could not load %s: %s\n",picture[i],SDL_GetError()); 
  193.                             exit(1);     
  194.                         } 
  195.  
  196.                         zoom_x = (double)((screen->w) / (double)(backpng->w)); 
  197.                         zoom_y = (double)((screen->h) / (double)(backpng->h)); 
  198.      
  199.                         backpng = zoomSurface(backpng , zoom_x , zoom_y , 1); 
  200.                         show_Pic(backpng ,&(screen->clip_rect)); 
  201.                         Destory(backpng); 
  202.                         backpng = NULL; 
  203.                     }    
  204.                     break
  205.                 case SDL_KEYUP: 
  206.                     /*
  207.                     *keyborad test
  208.                     */ 
  209.                     printf("Key up......\n"); 
  210.                     printf("key : %d\n" , event.key.keysym.sym ); 
  211.                     if(event.key.keysym.sym == 269) 
  212.                     { 
  213.                         zoomSmall(&zoomXY); 
  214.                     } 
  215.                     if(event.key.keysym.sym == 270) 
  216.                     { 
  217.                         zoomBig(&zoomXY); 
  218.                     } 
  219.                     break
  220.                 case SDL_MOUSEMOTION: 
  221.                     /*
  222.                     *mouse motton test
  223.                     */ 
  224.                     printf("mouse motton : x = %d , y = %d\n" ,event.button.x , event.button.y); 
  225.                     break
  226.                 case SDL_MOUSEBUTTONDOWN: 
  227.                     draw_button("left" , 0) ; 
  228.                     draw_button("right" , 0) ;  
  229.                     if(((event.button.x >= 160) && (event.button.x <= 200 )) && ((event.button.y >= 420) && (event.button.y <= 460 ))) 
  230.                     { 
  231.                         draw_button("left" , 1) ; 
  232.                         i = getLeftPic(i);   
  233.                         backpng = IMG_Load(picture[i]); 
  234.                         if(!backpng) 
  235.                         { 
  236.                             fprintf(stderr,"Could not load %s: %s\n",picture[i],SDL_GetError()); 
  237.                             exit(1);     
  238.                         } 
  239.                         zoom_x = (double)((screen->w) / (double)(backpng->w)); 
  240.                         zoom_y = (double)((screen->h) / (double)(backpng->h)); 
  241.      
  242.                         backpng = zoomSurface(backpng , (zoom_x / zoomXY), (zoom_y / zoomXY) , 1); 
  243.                         SDL_Rect rect; 
  244.                         rect.x =  (screen->w - backpng->w ) / 2; 
  245.                         rect.y =  (screen->h - backpng->h ) / 2; 
  246.                      
  247.                      
  248.                         rect.w = backpng->w; 
  249.                         rect.h = backpng->h; 
  250.                         printf("%d , %d , %d , %d\n" , rect.x, rect.y, rect.w , rect.h ); 
  251.                         SDL_FillRect(screen ,&fillRect , 0xffffffff); 
  252.                         SDL_UpdateRect(screen , 0 , 0 , 0 , 0 ); 
  253.                         show_Pic(backpng , &rect); 
  254.                         Destory(backpng); 
  255.                         backpng = NULL; 
  256.                          
  257.                     } 
  258.                     if(  (event.button.x >= 438 && event.button.x <= 478 ) && (event.button.y >= 420 && event.button.y <= 460 ) ) 
  259.                     { 
  260.                         draw_button("right" , 1) ;  
  261.                         i = getRightPic(i);  
  262.                         backpng = IMG_Load(picture[i]); 
  263.                         if(!backpng) 
  264.                         { 
  265.                             fprintf(stderr,"Could not load %s: %s\n",picture[i],SDL_GetError()); 
  266.                             exit(1);     
  267.                         } 
  268.                         zoom_x = (double)((screen->w) / (double)(backpng->w)); 
  269.                         zoom_y = (double)((screen->h) / (double)(backpng->h)); 
  270.      
  271.                         backpng = zoomSurface(backpng , (zoom_x / zoomXY), (zoom_y / zoomXY) , 1); 
  272.                         SDL_Rect rect; 
  273.                         rect.x =  (screen->w - backpng->w ) / 2; 
  274.                         rect.y =  (screen->h - backpng->h ) / 2; 
  275.                      
  276.                      
  277.                         rect.w = backpng->w; 
  278.                         rect.h = backpng->h; 
  279.                         printf("%d , %d , %d , %d\n" , rect.x, rect.y, rect.w , rect.h ); 
  280.                         SDL_FillRect(screen ,&fillRect , 0xffffffff); 
  281.                         SDL_UpdateRect(screen , 0 , 0 , 0 , 0 ); 
  282.                         show_Pic(backpng , &rect); 
  283.                         Destory(backpng); 
  284.                         backpng = NULL; 
  285.                     } 
  286.                     break
  287.                 case SDL_MOUSEBUTTONUP:  
  288.                     draw_button("left" , 0) ; 
  289.                     draw_button("right" , 0) ; 
  290.                     break
  291.                 case SDL_QUIT: 
  292.                     printf("quit\n"); 
  293.                     flag = 0; 
  294.                     break
  295.             } 
  296.         } 
  297.     } 
  298.  
  299.  
  300.     Destory(backpng); 
  301.     Destory(screen); 
  302.     //TTF_Quit(); 
  303.     SDL_Quit(); 
  304.     return 0; 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值