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

  1. /////////////////////////////////////////////////////////////////////////
  2. // Game Programming All In One, Third Edition
  3. // Tank War Enhancement 5 - 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.  
  62.     int x = startx + (frame % columns) * width;
  63.     int y = starty + (frame / columns) * height;
  64.  
  65.     blit(source,temp,x,y,0,0,width,height);
  66.  
  67.     return temp;
  68. }
  69.  
  70. //main function
  71. void main(void)
  72. {
  73.     //initialize the game
  74.     allegro_init(); 
  75.     install_keyboard(); 
  76.     install_timer();
  77.     srand(time(NULL));
  78.     setupscreen();
  79.     setuptanks();
  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.             movebullet(n);
  89.         }
  90.  
  91.         //draw background bitmap
  92.         blit(back, buffer, 0, 0, 0, 0, back->w, back->h);
  93.  
  94.         //draw scrolling windows
  95.         for (n=0; n<2; n++)
  96.             blit(scroll, buffer, scrollx[n], scrolly[n], 
  97.                 startx[n], starty[n], SCROLLW, SCROLLH);
  98.  
  99.         //update the radar
  100.         rectfill(buffer,radarx+1,radary+1,radarx+99,radary+88,BLACK);
  101.         rect(buffer,radarx,radary,radarx+100,radary+89,WHITE);
  102.  
  103.         //draw mini tanks on radar
  104.         for (n=0; n<2; n++)
  105.             stretch_sprite(buffer, tank_bmp[n][tanks[n]->dir], 
  106.                 radarx + scrollx[n]/10 + (SCROLLW/10)/2-4,
  107.                 radary + scrolly[n]/12 + (SCROLLH/12)/2-4,
  108.                 8, 8);
  109.  
  110.         //draw player viewport on radar
  111.         for (n=0; n<2; n++)
  112.             rect(buffer,radarx+scrollx[n]/10, radary+scrolly[n]/12,
  113.                 radarx+scrollx[n]/10+SCROLLW/10, 
  114.                 radary+scrolly[n]/12+SCROLLH/12, GRAY);
  115.  
  116.         //display score
  117.         for (n=0; n<2; n++)
  118.             textprintf_ex(buffer, font, startx[n], HEIGHT-10, -1,
  119.                 BURST, "Score: %d", scores[n]);
  120.  
  121.         //draw the tanks and bullets
  122.         for (n=0; n<2; n++)
  123.         {
  124.             drawtank(n);
  125.             drawbullet(n);
  126.         }
  127.  
  128.         //refresh the screen
  129.         acquire_screen();
  130.         blit(buffer, screen, 0, 0, 0, 0, WIDTH, HEIGHT);
  131.         release_screen();
  132.  
  133.         //check for keypresses
  134.         if (keypressed())
  135.             getinput();
  136.  
  137.         //slow the game down
  138.         rest(20);
  139.     }
  140.  
  141.     //destroy bitmaps
  142.     destroy_bitmap(explode_bmp);
  143.     destroy_bitmap(back);
  144.     destroy_bitmap(scroll);
  145.     destroy_bitmap(buffer);
  146.     for (n=0; n<8; n++)
  147.     {
  148.         destroy_bitmap(tank_bmp[0][n]);
  149.         destroy_bitmap(tank_bmp[1][n]);
  150.     }
  151.     return;
  152. }
  153. END_OF_MAIN();
  154.  
  155.  
  156.