home *** CD-ROM | disk | FTP | other *** search
/ Game Programming - All in One (3rd Edition) / game_prog_all_in_one_3rd_ed.iso / sources / chapter09 / animsprite / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  2006-09-13  |  1.6 KB  |  74 lines

  1. ////////////////////////////////////////////////////////////////
  2. // Game Programming All In One, Third Edition
  3. // Chapter 9 - AnimSprite
  4. ////////////////////////////////////////////////////////////////
  5.  
  6. #include <stdio.h>
  7. #include <allegro.h>
  8.  
  9. #define WHITE makecol(255,255,255)
  10. #define BLACK makecol(0,0,0)
  11.  
  12. BITMAP *kitty[7];
  13. char s[20];
  14. int curframe=0, framedelay=5, framecount=0;
  15. int x=100, y=200, n;
  16.  
  17. int main(void) 
  18. {
  19.     //initialize the program
  20.     allegro_init();
  21.     install_keyboard(); 
  22.     install_timer();
  23.     set_color_depth(16);
  24.     set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
  25.     textout_ex(screen, font, "AnimSprite Program (ESC to quit)",
  26.         0, 0, WHITE,0);
  27.  
  28.     //load the animated sprite
  29.     for (n=0; n<6; n++)
  30.     {
  31.         sprintf(s,"cat%d.bmp",n+1);
  32.         kitty[n] = load_bitmap(s, NULL);
  33.     }
  34.  
  35.     //main loop
  36.     while(!keypressed())
  37.     {
  38.         //erase the sprite
  39.         rectfill(screen, x, y, x+kitty[0]->w, y+kitty[0]->h, BLACK);
  40.  
  41.         //update the position
  42.         x += 5;
  43.         if (x > SCREEN_W - kitty[0]->w)
  44.             x = 0;
  45.  
  46.         //update the frame
  47.         if (framecount++ > framedelay)
  48.         {
  49.             framecount = 0;
  50.             curframe++;
  51.             if (curframe > 5)
  52.                 curframe = 0;
  53.         }
  54.  
  55.         acquire_screen();
  56.  
  57.         //draw the sprite
  58.         draw_sprite(screen, kitty[curframe], x, y);
  59.  
  60.         //display logistics
  61.         textprintf_ex(screen,font,0,20,WHITE,0,
  62.             "Sprite X,Y: %3d,%3d", x, y);
  63.         textprintf_ex(screen,font,0,40,WHITE,0,
  64.             "Frame,Count,Delay: %2d,%2d,%2d",
  65.             curframe,framecount,framedelay);
  66.  
  67.         release_screen();
  68.         rest(10);
  69.     }
  70.     allegro_exit();
  71.     return 0;
  72. }
  73. END_OF_MAIN()
  74.