libSDL--初步使用

  1. #include <stdlib.h>
  2. #include <SDL/SDL.h>
  3. #include <SDL/SDL_image.h>
  4. #define MOVE_STEP 5
  5. #define SCREEN_LENGTH 640
  6. #define SCREEN_WIDTH 480
  7. #define SCREEN_DIPTH 24
  8. unsigned int global_pos_x = 0;
  9. void move_bmp(SDL_Surface* screen, SDL_Rect* dest, SDL_Surface* bmp, SDL_Rect* bmp_rect)
  10. {
  11.   if (global_pos_x >= bmp->w+SCREEN_LENGTH)
  12.     {
  13.       global_pos_x = 0;
  14.     }
  15.   global_pos_x += MOVE_STEP; 
  16.  
  17.   if (global_pos_x <= bmp->w)
  18.     {
  19.       //screen draw rect
  20.       dest->x = 0;
  21.       dest->y = 0;
  22.       dest->w = global_pos_x;
  23.       dest->h = bmp->h;
  24.       //bmp rect to draw
  25.       bmp_rect->x = bmp->w - global_pos_x;   
  26.     }
  27.   else if (global_pos_x < SCREEN_LENGTH)
  28.     {
  29.       dest->x = global_pos_x - bmp->w;
  30.       dest->y = 0;
  31.       dest->w = bmp->w;
  32.       dest->h = bmp->h;
  33.       bmp_rect->x = 0;
  34.     } 
  35.   else
  36.     {
  37.       dest->x = global_pos_x - bmp->w;
  38.       dest->y = 0;
  39.       dest->w = bmp->w + SCREEN_LENGTH - global_pos_x;
  40.       dest->h = bmp->h;
  41.       bmp_rect->x = 0;
  42.     }
  43.   bmp_rect->y = 0;
  44.   bmp_rect->w = dest->w;
  45.   bmp_rect->h = bmp->h;
  46.   //use back color clear screen
  47.   if ( SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0)) < 0)
  48.     {
  49.       printf("clear screen failure:%s/n", SDL_GetError());
  50.       exit(1);
  51.     }
  52.   SDL_BlitSurface(bmp, bmp_rect, screen, dest);
  53.   //must be filp screen, can not UpdateRect, or the front rect will left
  54.   SDL_Flip(screen);
  55. }
  56. int main(int argc, char* argv[])
  57. {
  58.   if (SDL_Init(SDL_INIT_VIDEO) < 0)
  59.     {
  60.       printf("can not init sdl:%s/n", SDL_GetError());
  61.       exit(1);
  62.     }
  63.   atexit(SDL_Quit);
  64.  
  65.   SDL_Surface* screen = SDL_SetVideoMode(SCREEN_LENGTH, SCREEN_WIDTH, SCREEN_DIPTH, SDL_SWSURFACE|SDL_NOFRAME);
  66.   if (screen == NULL)
  67.     {
  68.       printf("can not setvidemode:%s/n", SDL_GetError());
  69.       exit(1);
  70.     }
  71.   SDL_Surface* bmp = SDL_LoadBMP("test.bmp");
  72.   if (bmp == NULL)
  73.     {
  74.       printf("can not load test.bmp:%s/n", SDL_GetError());
  75.       exit(1);
  76.     }
  77.   SDL_WM_SetCaption("yijunjun""very good");
  78.   SDL_Rect dest;
  79.   SDL_Rect bmp_rect;
  80.   SDL_Event event;
  81.   SDL_Surface* jpg = IMG_Load("my_self.jpg");
  82.   if (jpg == NULL)
  83.     {
  84.       printf("img load failure:%s/n", SDL_GetError());
  85.       exit(1);
  86.     }
  87.   SDL_Rect jpg_rect;
  88.   while(1)
  89.     {
  90.       //check have exit message
  91.       while (SDL_PollEvent(&event))
  92.     if (event.type == SDL_QUIT)
  93.       exit(0);
  94.       move_bmp(screen, &dest, jpg, &jpg_rect);
  95.       SDL_Delay(18);
  96.     }
  97.   return 0;
  98. }
  99.  
  100.  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值