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

  1. ////////////////////////////////////////////////////////////////
  2. // Game Programming All In One, Third Edition
  3. // Chapter 9 - SpriteHandler
  4. ////////////////////////////////////////////////////////////////
  5.  
  6. #include <stdio.h>
  7. #include <allegro.h>
  8.  
  9. #define BLACK makecol(0,0,0)
  10. #define WHITE makecol(255,255,255)
  11.  
  12. //define the sprite structure
  13. typedef struct SPRITE
  14. {
  15.     int x,y;
  16.     int width,height;
  17.     int xspeed,yspeed;
  18.     int xdelay,ydelay;
  19.     int xcount,ycount;
  20.     int curframe,maxframe,animdir;
  21.     int framecount,framedelay;
  22.     
  23. }SPRITE;
  24.  
  25. //sprite variables
  26. BITMAP *ballimg[16];
  27. SPRITE theball;
  28. SPRITE *ball = &theball;
  29.  
  30. //support variables
  31. char s[20];
  32. int n;
  33.  
  34. void erasesprite(BITMAP *dest, SPRITE *spr)
  35. {
  36.     //erase the sprite using BLACK color fill
  37.     rectfill(dest, spr->x, spr->y, spr->x + spr->width, 
  38.         spr->y + spr->height, BLACK);
  39. }
  40.  
  41. void updatesprite(SPRITE *spr)
  42. {
  43.     //update x position
  44.     if (++spr->xcount > spr->xdelay)
  45.     {
  46.         spr->xcount = 0;
  47.         spr->x += spr->xspeed;
  48.     }
  49.  
  50.     //update y position
  51.     if (++spr->ycount > spr->ydelay)
  52.     {
  53.         spr->ycount = 0;
  54.         spr->y += spr->yspeed;
  55.     }
  56.  
  57.     //update frame based on animdir
  58.     if (++spr->framecount > spr->framedelay)
  59.     {
  60.         spr->framecount = 0;
  61.         if (spr->animdir == -1)
  62.         {
  63.             if (--spr->curframe < 0)
  64.                 spr->curframe = spr->maxframe;
  65.         }
  66.         else if (spr->animdir == 1)
  67.         {
  68.             if (++spr->curframe > spr->maxframe)
  69.                 spr->curframe = 0;
  70.         }
  71.     }
  72. }
  73.  
  74. void bouncesprite(SPRITE *spr)
  75. {
  76.     //simple screen bouncing behavior
  77.     if (spr->x < 0)
  78.     {
  79.         spr->x = 0;
  80.         spr->xspeed = rand() % 2 + 4;
  81.         spr->animdir *= -1;
  82.     }
  83.  
  84.     else if (spr->x > SCREEN_W - spr->width)
  85.     {
  86.         spr->x = SCREEN_W - spr->width;
  87.         spr->xspeed = rand() % 2 - 6;
  88.         spr->animdir *= -1;
  89.     }
  90.  
  91.     if (spr->y < 40)
  92.     {
  93.         spr->y = 40;
  94.         spr->yspeed = rand() % 2 + 4;
  95.         spr->animdir *= -1;
  96.     }
  97.  
  98.     else if (spr->y > SCREEN_H - spr->height)
  99.     {
  100.         spr->y = SCREEN_H - spr->height;
  101.         spr->yspeed = rand() % 2 - 6;
  102.         spr->animdir *= -1;
  103.     }
  104.  
  105. }
  106.  
  107. int main(void)
  108. {
  109.     //initialize
  110.     allegro_init();
  111.     set_color_depth(16);
  112.     set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
  113.     install_keyboard();
  114.     install_timer();
  115.     srand(time(NULL));
  116.     textout_ex(screen, font, "SpriteHandler Program (ESC to quit)", 
  117.         0, 0, WHITE, 0);
  118.     
  119.     //load sprite images
  120.     for (n=0; n<16; n++)
  121.     {
  122.         sprintf(s,"ball%d.bmp",n+1);
  123.         ballimg[n] = load_bitmap(s, NULL);
  124.     }
  125.  
  126.     //initialize the sprite with lots of randomness
  127.     ball->x = rand() % (SCREEN_W - ballimg[0]->w);
  128.     ball->y = rand() % (SCREEN_H - ballimg[0]->h);
  129.     ball->width = ballimg[0]->w;
  130.     ball->height = ballimg[0]->h;
  131.     ball->xdelay = rand() % 2 + 1;
  132.     ball->ydelay = rand() % 2 + 1;
  133.     ball->xcount = 0;
  134.     ball->ycount = 0;
  135.     ball->xspeed = rand() % 2 + 4; 
  136.     ball->yspeed = rand() % 2 + 4;
  137.     ball->curframe = 0;
  138.     ball->maxframe = 15;
  139.     ball->animdir = 1;
  140.     ball->framecount = 0;
  141.     ball->framedelay = rand() % 3 + 1;
  142.     ball->animdir = 1;
  143.  
  144.     //game loop
  145.     while (!key[KEY_ESC])
  146.     {
  147.         erasesprite(screen, ball);
  148.  
  149.         //perform standard position/frame update
  150.         updatesprite(ball);
  151.  
  152.         //now do something with the sprite--a basic screen bouncer
  153.         bouncesprite(ball);
  154.  
  155.         //lock the screen
  156.         acquire_screen();
  157.  
  158.         //draw the ball sprite
  159.         draw_sprite(screen, ballimg[ball->curframe], ball->x, ball->y);
  160.  
  161.         //display some logistics
  162.         textprintf_ex(screen, font, 0, 20, WHITE, 0,
  163.             "x,y,xspeed,yspeed: %2d,%2d,%2d,%2d",
  164.             ball->x, ball->y, ball->xspeed, ball->yspeed);
  165.         textprintf_ex(screen, font, 0, 30, WHITE, 0,
  166.             "xcount,ycount,framecount,animdir: %2d,%2d,%2d,%2d",
  167.             ball->xcount, ball->ycount, ball->framecount, ball->animdir);
  168.  
  169.         //unlock the screen
  170.         release_screen();
  171.  
  172.         rest(10);
  173.     }
  174.     for (n=0; n<15; n++)
  175.         destroy_bitmap(ballimg[n]);
  176.  
  177.     allegro_exit();
  178.     return 0;
  179. }
  180. END_OF_MAIN()
  181.