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

  1. /////////////////////////////////////////////////////////////////////////
  2. // Game Programming All In One, Third Edition
  3. // Tank War Enhancement 6 - main.c
  4. /////////////////////////////////////////////////////////////////////////
  5.  
  6. #include "tankwar.h"
  7.  
  8. #define B 39
  9. int map[MAPW*MAPH] = {
  10.     B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,
  11.     B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,
  12.     B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,
  13.     B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,
  14.     B,B,B,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,B,B,B,
  15.     B,B,B,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,B,B,B,
  16.     B,B,B,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,B,B,B,
  17.     B,B,B,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,B,B,B,
  18.     B,B,B,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,B,B,B,
  19.     B,B,B,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,B,B,B,
  20.     B,B,B,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,B,B,B,
  21.     B,B,B,0,2,2,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,2,2,2,2,2,2,2,2,2,2,2,0,B,B,B,
  37.     B,B,B,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,B,B,B,
  38.     B,B,B,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,B,B,B,
  39.     B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,
  40.     B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,
  41.     B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,
  42.     B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B
  43. };
  44.  
  45. //perform basic collision detection
  46. int inside(int x,int y,int left,int top,int right,int bottom)
  47. {
  48.     if (x > left && x < right && y > top && y < bottom)
  49.         return 1;
  50.     else
  51.         return 0;
  52. }
  53.  
  54. //reuse our friendly tile grabber from chapter 9
  55. BITMAP *grabframe(BITMAP *source, 
  56.                   int width, int height, 
  57.                   int startx, int starty, 
  58.                   int columns, int frame)
  59. {
  60.     BITMAP *temp = create_bitmap(width,height);
  61.     int x = startx + (frame % columns) * width;
  62.     int y = starty + (frame / columns) * height;
  63.     blit(source,temp,x,y,0,0,width,height);
  64.     return temp;
  65. }
  66.  
  67. //main function
  68. int main(void)
  69. {
  70.     int anim;
  71.  
  72.     //initialize the game
  73.     allegro_init(); 
  74.     install_keyboard(); 
  75.     install_timer();
  76.     srand(time(NULL));
  77.     setupscreen();
  78.     setuptanks();
  79.     loadsprites();
  80.  
  81.     //game loop
  82.     while(!gameover)
  83.     {
  84.         //move the tanks and bullets
  85.         for (n=0; n<2; n++)
  86.         {
  87.             movetank(n);
  88.             animatetank(n);
  89.             movebullet(n);
  90.         }
  91.  
  92.         //draw background bitmap
  93.         blit(back, buffer, 0, 0, 0, 0, back->w, back->h);
  94.  
  95.         //draw scrolling windows
  96.         for (n=0; n<2; n++)
  97.             blit(scroll, buffer, scrollx[n], scrolly[n], 
  98.                 startx[n], starty[n], SCROLLW, SCROLLH);
  99.  
  100.         //update the radar
  101.         rectfill(buffer,radarx+1,radary+1,radarx+99,radary+88,BLACK);
  102.         rect(buffer,radarx,radary,radarx+100,radary+89,WHITE);
  103.  
  104.         //draw mini tanks on radar
  105.         for (n=0; n<2; n++)
  106.             stretch_sprite(buffer, tank_bmp[n][tanks[n]->curframe][tanks[n]->dir], 
  107.                 radarx + scrollx[n]/10 + (SCROLLW/10)/2-4,
  108.                 radary + scrolly[n]/12 + (SCROLLH/12)/2-4,
  109.                 8, 8);
  110.  
  111.         //draw player viewport on radar
  112.         for (n=0; n<2; n++)
  113.             rect(buffer,radarx+scrollx[n]/10, radary+scrolly[n]/12,
  114.                 radarx+scrollx[n]/10+SCROLLW/10, 
  115.                 radary+scrolly[n]/12+SCROLLH/12, GRAY);
  116.  
  117.         //display score
  118.         for (n=0; n<2; n++)
  119.             textprintf_ex(buffer, font, startx[n], HEIGHT-10,
  120.                 BURST, -1, "Score: %d", scores[n]);
  121.  
  122.         //draw the tanks and bullets
  123.         for (n=0; n<2; n++)
  124.         {
  125.             drawtank(n);
  126.             drawbullet(n);
  127.         }
  128.  
  129.         //explosions come last (so they draw over tanks)
  130.         for (n=0; n<2; n++)
  131.             updateexplosion(n);
  132.  
  133.         //refresh the screen
  134.         acquire_screen();
  135.         blit(buffer, screen, 0, 0, 0, 0, WIDTH-1, HEIGHT-1);
  136.         release_screen();
  137.  
  138.         //check for keypresses
  139.         if (keypressed())
  140.             getinput();
  141.  
  142.         //slow the game down
  143.         rest(20);
  144.     }
  145.  
  146.     //destroy bitmaps
  147.     destroy_bitmap(explode_bmp);
  148.     destroy_bitmap(back);
  149.     destroy_bitmap(scroll);
  150.     destroy_bitmap(buffer);
  151.     
  152.     //free tank bitmaps
  153.     for (anim=0; anim<8; anim++)
  154.         for (n=0; n<8; n++)
  155.         {
  156.             destroy_bitmap(tank_bmp[0][anim][n]);
  157.             destroy_bitmap(tank_bmp[1][anim][n]);
  158.         }
  159.  
  160.     //free explosion sprites
  161.     for (n=0; n<2; n++)
  162.         free(explosions[n]);
  163.  
  164.     allegro_exit();
  165.     return 0;
  166. }
  167. END_OF_MAIN()
  168.