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

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