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

  1. //////////////////////////////////////////////////////
  2. // Game Programming All In One, Third Edition
  3. // Chapter 10 - CompiledSprites program
  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. COMPILED_SPRITE *dragonimg[6];
  27. SPRITE thedragon;
  28. SPRITE *dragon = &thedragon;
  29.  
  30.  
  31. void erasesprite(BITMAP *dest, SPRITE *spr)
  32. {
  33.     //erase the sprite using BLACK color fill
  34.     rectfill(dest, spr->x, spr->y, spr->x + spr->width, 
  35.         spr->y + spr->height, BLACK);
  36. }
  37.  
  38. void updatesprite(SPRITE *spr)
  39. {
  40.     //update x position
  41.     if (++spr->xcount > spr->xdelay)
  42.     {
  43.         spr->xcount = 0;
  44.         spr->x += spr->xspeed;
  45.     }
  46.  
  47.     //update y position
  48.     if (++spr->ycount > spr->ydelay)
  49.     {
  50.         spr->ycount = 0;
  51.         spr->y += spr->yspeed;
  52.     }
  53.  
  54.     //update frame based on animdir
  55.     if (++spr->framecount > spr->framedelay)
  56.     {
  57.         spr->framecount = 0;
  58.         if (spr->animdir == -1)
  59.         {
  60.             if (--spr->curframe < 0)
  61.                 spr->curframe = spr->maxframe;
  62.         }
  63.         else if (spr->animdir == 1)
  64.         {
  65.             if (++spr->curframe > spr->maxframe)
  66.                 spr->curframe = 0;
  67.         }
  68.     }
  69. }
  70.  
  71. void warpsprite(SPRITE *spr)
  72. {
  73.     //simple screen warping behavior
  74.     if (spr->x < 0)
  75.     {
  76.         spr->x = SCREEN_W - spr->width;
  77.     }
  78.  
  79.     else if (spr->x > SCREEN_W - spr->width)
  80.     {
  81.         spr->x = 0;
  82.     }
  83.  
  84.     if (spr->y < 40)
  85.     {
  86.         spr->y = SCREEN_H - spr->height;
  87.     }
  88.  
  89.     else if (spr->y > SCREEN_H - spr->height)
  90.     {
  91.         spr->y = 40;
  92.     }
  93.  
  94. }
  95.  
  96. COMPILED_SPRITE *compiled_grabframe(BITMAP *source, 
  97.                   int width, int height, 
  98.                   int startx, int starty, 
  99.                   int columns, int frame)
  100. {
  101.     COMPILED_SPRITE *sprite;
  102.     BITMAP *temp = create_bitmap(width,height);
  103.  
  104.     int x = startx + (frame % columns) * width;
  105.     int y = starty + (frame / columns) * height;
  106.     
  107.     blit(source,temp,x,y,0,0,width,height);
  108.  
  109.     //remember FALSE is always used in second parameter
  110.     sprite = get_compiled_sprite(temp, FALSE);
  111.     
  112.     destroy_bitmap(temp);
  113.  
  114.     return sprite;
  115. }
  116.  
  117. void main(void)
  118. {
  119.     BITMAP *temp;
  120.     int n, x, y;
  121.  
  122.     //initialize
  123.     allegro_init();
  124.     set_color_depth(16);
  125.     set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
  126.     install_keyboard();
  127.     install_timer();
  128.     srand(time(NULL));
  129.     textout_ex(screen, font, "CompiledSprites Program (ESC to quit)", 
  130.         0, 0, WHITE, 0);
  131.     
  132.     //load and draw the blocks
  133.     temp = load_bitmap("block1.bmp", NULL);
  134.     for (y=0; y < SCREEN_H/2/temp->h+temp->h; y++)
  135.         for (x=0; x < SCREEN_W/temp->w; x++)
  136.             draw_sprite(screen, temp, x*temp->w, SCREEN_H/2+y*temp->h);
  137.     destroy_bitmap(temp);
  138.  
  139.     temp = load_bitmap("block2.bmp", NULL);
  140.     for (x=0; x < SCREEN_W/temp->w; x++)
  141.         draw_sprite(screen, temp, x*temp->w, SCREEN_H/2);
  142.     destroy_bitmap(temp);
  143.  
  144.     //load compiled sprite of dragon
  145.     temp = load_bitmap("dragon.bmp", NULL);
  146.     for (n=0; n<6; n++)
  147.         dragonimg[n] = compiled_grabframe(temp,128,64,0,0,3,n);
  148.     destroy_bitmap(temp);
  149.  
  150.     //initialize the sprite with lots of randomness
  151.     dragon->x = 500;
  152.     dragon->y = 150;
  153.     dragon->width = dragonimg[0]->w;
  154.     dragon->height = dragonimg[0]->h;
  155.     dragon->xdelay = 1;
  156.     dragon->ydelay = 0;
  157.     dragon->xcount = 0;
  158.     dragon->ycount = 0;
  159.     dragon->xspeed = -4;
  160.     dragon->yspeed = 0;
  161.     dragon->curframe = 0;
  162.     dragon->maxframe = 5;
  163.     dragon->framecount = 0;
  164.     dragon->framedelay = 10;
  165.     dragon->animdir = 1;
  166.  
  167.     //game loop
  168.     while (!key[KEY_ESC])
  169.     {
  170.         //erase the dragon
  171.         erasesprite(screen, dragon);
  172.  
  173.         //move/animate the dragon
  174.         updatesprite(dragon);
  175.         warpsprite(dragon);
  176.  
  177.         //draw the dragon
  178.         acquire_screen();
  179.         draw_compiled_sprite(screen, dragonimg[dragon->curframe], dragon->x, dragon->y);
  180.         release_screen();
  181.  
  182.         rest(10);
  183.     }
  184.  
  185.     for (n=0; n<6; n++)
  186.         destroy_compiled_sprite(dragonimg[n]);
  187.  
  188.     return;
  189. }
  190.  
  191. END_OF_MAIN();
  192.  
  193.