SDL做的一個簡單Button

  1. #include "SDL.h" 
  2. #include "SDL_gfxPrimitives.h" 
  3.  
  4. #define BMP_NAME "icon.bmp" 
  5. #define ICON "sample.bmp" 
  6. #define TITLE_NAME "TestSDL" 
  7. #define ICON_NAME  "My WinFrame"        //最小化icon的name 
  8. #define WIDTH  640 
  9. #define HEIGTH 480 
  10.  
  11. static SDL_Surface *screen = NULL; 
  12.  
  13. int Init()                      //初始化SDL  
  14.     if(SDL_Init(SDL_INIT_VIDEO) == -1) 
  15.     { 
  16.         fprintf(stderr,"SDL init error:%s",SDL_GetError()); 
  17.         return -1; 
  18.     } 
  19.     return 0; 
  20.  
  21. SDL_Surface *loadBMP(char *fileName)            //加載一個bmp位圖  
  22.     SDL_Surface *bmp; 
  23.     bmp = SDL_LoadBMP(fileName); 
  24.     if(bmp == NULL) 
  25.     { 
  26.         fprintf(stderr,"Could not load %s: %s\n",fileName,SDL_GetError()); 
  27.         exit(1); 
  28.     } 
  29.     return bmp; 
  30.  
  31. void creatScreen(int width , int height , int bpp , Uint32 flags)       //創建一個VideoMode 
  32.     screen = SDL_SetVideoMode(width , height,  bpp , flags); 
  33.     if(screen == NULL) 
  34.     { 
  35.         fprintf(stderr,"Could not Creat a Screen!:%s\n",SDL_GetError()); 
  36.         exit(1); 
  37.     } 
  38.     return ; 
  39.  
  40.  
  41. int Destory(SDL_Surface *file)                  //釋放空間  
  42.     SDL_FreeSurface( file ); 
  43.     return 0; 
  44.  
  45. void show_bmp(SDL_Surface *bmp ,SDL_Rect *rect) //顯示bmp圖  
  46. {    
  47.      
  48.     SDL_BlitSurface(bmp , NULL , screen , rect); 
  49.     SDL_UpdateRect(screen , 0 , 0 , 0 , 0 ); 
  50.     return ; 
  51.  
  52. void draw_button(SDL_Rect *button_rect , int flag)  //繪出BUTTON  
  53.     boxColor(screen, button_rect->x , button_rect->y ,button_rect->x + button_rect->w , button_rect->y + button_rect->h, 0xffffffff); 
  54.     if(flag == 1) 
  55.     { 
  56.         hlineColor( screen, button_rect->x , button_rect->x + button_rect->w ,button_rect->y + button_rect->h , 0x000000ff); 
  57.         vlineColor( screen, button_rect->x + button_rect->w , button_rect->y , button_rect->y + button_rect->h , 0x000000ff); 
  58.     } 
  59.      
  60.     show_bmp( screen , &(screen->clip_rect) );   
  61.     return ; 
  62.  
  63. int main(int argc,char **argv) 
  64. {    
  65.  
  66.     const SDL_VideoInfo *info = NULL; 
  67.     int flag = 1; 
  68.     int width = WIDTH; 
  69.     int heigth = HEIGTH; 
  70.     int bpp = 0; 
  71.      
  72.     SDL_Rect button_rect; 
  73.          
  74.     Init(); 
  75.     creatScreen(width , heigth, bpp , SDL_SWSURFACE); 
  76.      
  77.     button_rect.x = ( screen->w / 2 )- 20; 
  78.     button_rect.y = ( screen->h / 2 )- 20; 
  79.     button_rect.w = 40; 
  80.     button_rect.h = 40; 
  81.      
  82.     info = SDL_GetVideoInfo(); 
  83.     if(info == NULL) 
  84.     { 
  85.         fprintf( stderr, "Video query failed: %s\n",SDL_GetError( ) ); 
  86.         exit(1); 
  87.  
  88.     } 
  89.     bpp = info->vfmt->BitsPerPixel ;            //獲得VideoMode的bpp  
  90.  
  91.     SDL_WM_SetCaption(TITLE_NAME, ICON_NAME);               //設置窗體的NAME和ICON的NAME  
  92.     SDL_WM_SetIcon(loadBMP(ICON) , NULL);                       //加載ICON     
  93.     SDL_FillRect(screen, &(screen->clip_rect) , 0xaac9ff); 
  94.     draw_button(&button_rect , 0); 
  95.      
  96.     SDL_Event event; 
  97.     while(flag) 
  98.     { 
  99.         while(SDL_PollEvent(&event))            //把事件加入事件隊列  
  100.         {    
  101.             switch(event.type) 
  102.             { 
  103.                 case SDL_KEYDOWN: 
  104.                     if(event.key.keysym.sym == SDLK_ESCAPE) 
  105.                     { 
  106.                         flag = 0; 
  107.                     }    
  108.  
  109.                     SDL_FillRect(screen, &(screen->clip_rect) , 0x00);          //填充screen               
  110.                     SDL_UpdateRect(screen , 0 , 0 , 0 , 0 ); 
  111.                     break
  112.                 case SDL_KEYUP: 
  113.                     printf("Key up......\n");                    
  114.                     break
  115.                 case SDL_MOUSEMOTION: 
  116.                  
  117.                     break
  118.                 case SDL_MOUSEBUTTONDOWN: 
  119.                     printf("mouse: x = %d , y = %d\n",event.button.x,event.button.y); 
  120.                     if((event.button.x >= button_rect.x && event.button.x <= (button_rect.x + button_rect.w)) && (event.button.y >= button_rect.y && event.button.y <= ( button_rect.y + button_rect.h ))) 
  121.                     { 
  122.                         draw_button(&button_rect , 1); 
  123.                     } 
  124.                      
  125.                     break
  126.                 case SDL_MOUSEBUTTONUP:  
  127.                      
  128.                     draw_button(&button_rect , 0); 
  129.                     break
  130.                 case SDL_QUIT: 
  131.                     printf("quit\n"); 
  132.                     flag = 0; 
  133.                     break
  134.             } 
  135.         } 
  136.     } 
  137.          
  138.     Destory(screen); 
  139.     SDL_Quit(); 
  140.     return 0; 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章