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

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