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

  1. /////////////////////////////////////////////////////////////////////////
  2. // Game Programming All In One, Third Edition
  3. // Tank War Enhancement 6 - 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] = (SPRITE*)malloc(sizeof(SPRITE));
  18.     explosions[1] = (SPRITE*)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.     //create the virtual background
  126.     scroll = create_bitmap(MAPW, MAPH);
  127.     if (scroll == NULL)
  128.     {
  129.         set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
  130.         allegro_message("Error creating virtual background");
  131.         return;
  132.     }
  133.  
  134.     //load the tile bitmap
  135.     tiles = load_bitmap("tiles.bmp", NULL);
  136.     if (tiles == NULL)
  137.     {
  138.         set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
  139.         allegro_message("Error loading tiles.bmp file");
  140.         return;
  141.     }
  142.  
  143.     //now draw tiles on virtual background
  144.     for (tiley=0; tiley < scroll->h; tiley+=TILEH)
  145.     {
  146.         for (tilex=0; tilex < scroll->w; tilex+=TILEW)
  147.         {
  148.             //use the result of grabframe directly in blitter
  149.             blit(grabframe(tiles, TILEW+1, TILEH+1, 0, 0, COLS, map[n++]), 
  150.                 scroll, 0, 0, tilex, tiley, TILEW, TILEH);
  151.         }
  152.     }
  153.  
  154.     //done with tiles
  155.     destroy_bitmap(tiles);
  156.  
  157.     //load screen background
  158.     back = load_bitmap("background.bmp", NULL);
  159.     if (back == NULL)
  160.     {
  161.         set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
  162.         allegro_message("Error loading background.bmp file");
  163.         return;
  164.     }
  165.  
  166.     //create the double buffer
  167.     buffer = create_bitmap(WIDTH, HEIGHT);
  168.     if (buffer == NULL)
  169.     {
  170.         set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
  171.         allegro_message("Error creating double buffer");
  172.         return;
  173.     }
  174.  
  175.     //position the radar
  176.     radarx = 270;
  177.     radary = 1;
  178.  
  179.     //position each player
  180.     scrollx[0] = 100;
  181.     scrolly[0] = 100;
  182.     scrollx[1] = MAPW - 400;
  183.     scrolly[1] = MAPH - 500;
  184.  
  185.     //position the scroll windows
  186.     startx[0] = 5;
  187.     starty[0] = 93;
  188.     startx[1] = 325;
  189.     starty[1] = 93;
  190.  
  191. }
  192.  
  193.