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 / setup.c < prev    next >
Encoding:
C/C++ Source or Header  |  2006-09-15  |  4.0 KB  |  161 lines

  1. /////////////////////////////////////////////////////////////////////////
  2. // Game Programming All In One, Third Edition
  3. // Tank War Enhancement 7 - setup.c
  4. /////////////////////////////////////////////////////////////////////////
  5.  
  6. #include "tankwar.h"
  7.  
  8. void loadsprites()
  9. {
  10.     //load explosion image
  11.     if (explode_bmp == NULL)
  12.     {
  13.         explode_bmp = load_bitmap("explode.bmp", NULL);
  14.     }
  15.  
  16.     //initialize explosion sprites
  17.     explosions[0] = malloc(sizeof(SPRITE));
  18.     explosions[1] = malloc(sizeof(SPRITE));
  19.  
  20. }
  21.  
  22. void setuptanks()
  23. {
  24.     BITMAP *temp;
  25.     int anim;
  26.     int n;
  27.  
  28.     //configure player 1's tank
  29.     tanks[0] = &mytanks[0];
  30.     tanks[0]->x = 30;
  31.     tanks[0]->y = 40;
  32.     tanks[0]->xspeed = 0;
  33.     tanks[0]->dir = 3;
  34.     tanks[0]->curframe = 0;
  35.     tanks[0]->maxframe = 7;
  36.     tanks[0]->framecount = 0;
  37.     tanks[0]->framedelay = 10;
  38.     tanks[0]->animdir = 0;
  39.     scores[0] = 0;
  40.  
  41.     //load first tank
  42.     temp = load_bitmap("tank1.bmp", NULL);
  43.     for (anim=0; anim<8; anim++)
  44.     {
  45.         //grab animation frame
  46.         tank_bmp[0][anim][0] = grabframe(temp, 32, 32, 0, 0, 8, anim);
  47.  
  48.         //rotate image to generate all 8 directions
  49.         for (n=1; n<8; n++)
  50.         {
  51.             tank_bmp[0][anim][n] = create_bitmap(32, 32);
  52.             clear_to_color(tank_bmp[0][anim][n], makecol(255,0,255));
  53.             rotate_sprite(tank_bmp[0][anim][n], tank_bmp[0][anim][0], 
  54.                 0, 0, itofix(n*32));
  55.         }
  56.  
  57.     }
  58.     destroy_bitmap(temp);
  59.  
  60.     //configure player 2's tank
  61.     tanks[1] = &mytanks[1];
  62.     tanks[1]->x = SCREEN_W-30;
  63.     tanks[1]->y = SCREEN_H-30;
  64.     tanks[1]->xspeed = 0;
  65.     tanks[1]->dir = 7;
  66.     tanks[1]->curframe = 0;
  67.     tanks[1]->maxframe = 7;
  68.     tanks[1]->framecount = 0;
  69.     tanks[1]->framedelay = 10;
  70.     tanks[1]->animdir = 0;
  71.     scores[1] = 0;
  72.  
  73.     //load second tank
  74.     temp = load_bitmap("tank2.bmp", NULL);    
  75.     for (anim=0; anim<8; anim++)
  76.     {
  77.         //grab animation frame
  78.         tank_bmp[1][anim][0] = grabframe(temp, 32, 32, 0, 0, 8, anim);
  79.  
  80.         //rotate image to generate all 8 directions
  81.         for (n=1; n<8; n++)
  82.         {
  83.             tank_bmp[1][anim][n] = create_bitmap(32, 32);
  84.             clear_to_color(tank_bmp[1][anim][n], makecol(255,0,255));
  85.             rotate_sprite(tank_bmp[1][anim][n], tank_bmp[1][anim][0], 
  86.                 0, 0, itofix(n*32));
  87.         }
  88.     }
  89.     destroy_bitmap(temp);
  90.  
  91.     //load bullet image
  92.     if (bullet_bmp == NULL)
  93.         bullet_bmp = load_bitmap("bullet.bmp", NULL);
  94.  
  95.     //initialize bullets
  96.     for (n=0; n<2; n++)
  97.     {
  98.        bullets[n] = &mybullets[n];
  99.        bullets[n]->x = 0;
  100.        bullets[n]->y = 0;
  101.        bullets[n]->width = bullet_bmp->w;
  102.        bullets[n]->height = bullet_bmp->h;
  103.     }
  104.  
  105.     //center tanks inside scroll windows
  106.     tanks[0]->x = 5 + SCROLLW/2;
  107.     tanks[0]->y = 90 + SCROLLH/2;
  108.     tanks[1]->x = 325 + SCROLLW/2;
  109.     tanks[1]->y = 90 + SCROLLH/2;
  110.  
  111. }
  112.  
  113. void setupscreen()
  114. {
  115.     int ret;
  116.  
  117.     //set video mode    
  118.     set_color_depth(16);
  119.     ret = set_gfx_mode(MODE, WIDTH, HEIGHT, 0, 0);
  120.     if (ret != 0) {
  121.         allegro_message(allegro_error);
  122.         return;
  123.     }
  124.  
  125.     //load screen background
  126.     back = load_bitmap("background.bmp", NULL);
  127.     if (back == NULL)
  128.     {
  129.         set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
  130.         allegro_message("Error loading background.bmp file");
  131.         return;
  132.     }
  133.  
  134.     //create the double buffer
  135.     buffer = create_bitmap(WIDTH, HEIGHT);
  136.     if (buffer == NULL)
  137.     {
  138.         set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
  139.         allegro_message("Error creating double buffer");
  140.         return;
  141.     }
  142.  
  143.     //position the radar
  144.     radarx = 270;
  145.     radary = 1;
  146.  
  147.     //position each player
  148.     scrollx[0] = 100;
  149.     scrolly[0] = 100;
  150.     scrollx[1] = MAPW - 400;
  151.     scrolly[1] = MAPH - 500;
  152.  
  153.     //position the scroll windows
  154.     startx[0] = 5;
  155.     starty[0] = 93;
  156.     startx[1] = 325;
  157.     starty[1] = 93;
  158.  
  159. }
  160.  
  161.