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

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