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

  1. ////////////////////////////////////////////////////
  2. // Game Programming All In One, Third Edition
  3. // Chapter 5, Strategic Defense Program
  4. ////////////////////////////////////////////////////
  5.  
  6. #include <allegro.h>
  7.  
  8. //create some colors
  9. #define WHITE makecol(255,255,255)
  10. #define BLACK makecol(0,0,0)
  11. #define RED makecol(255,0,0)
  12. #define GREEN makecol(0,255,0)
  13. #define BLUE makecol(0,0,255)
  14. #define SMOKE makecol(140,130,120)
  15.  
  16. //point structure used to draw lines
  17. typedef struct POINT
  18. {
  19.     int x,y;
  20. }POINT;
  21.  
  22. //points array holds do_line points for drawing a line
  23. POINT points[2000];
  24. int curpoint,totalpoints;
  25.  
  26. //bitmap images
  27. BITMAP *buffer;
  28. BITMAP *crosshair;
  29. BITMAP *city;
  30.  
  31. //misc variables
  32. int x1,y1,x2,y2;
  33. int done=0;
  34. int destroyed=1;
  35. int n;
  36. int mx,my,mb;
  37. int score = -1;
  38.  
  39. void updatescore()
  40. {
  41.     //update and display the score
  42.     score++;
  43.     textprintf_right_ex(buffer,font,SCREEN_W-5,1,WHITE, 0,
  44.         "SCORE: %d  ", score);
  45. }
  46.     
  47. void explosion(BITMAP *bmp, int x,int y,int finalcolor)
  48. {
  49.     int color,size;
  50.  
  51.     for (n=0; n<20; n++)
  52.     {
  53.         //generate a random color
  54.         color = makecol(rand()%255,rand()%255,rand()%255);
  55.         //random explosion size
  56.         size = 20+rand()%20;
  57.         //draw the random filled circle
  58.         circlefill(bmp, x, y, size, color);
  59.         //short pause
  60.         rest(2);
  61.     }
  62.     //missile tracker looks for this explosion color
  63.     circlefill(bmp, x, y, 40, finalcolor);
  64. }
  65.  
  66.  
  67. void doline(BITMAP *bmp, int x, int y, int d)
  68. {
  69.     //line callback function...fills the points array
  70.     points[totalpoints].x = x;
  71.     points[totalpoints].y = y;
  72.     totalpoints++;
  73. }
  74.  
  75. void firenewmissile()
  76. {
  77.     //activate the new missile
  78.     destroyed=0;
  79.     totalpoints = 0;
  80.     curpoint = 0;
  81.  
  82.     //random starting location
  83.     x1 = rand() % (SCREEN_W-1);
  84.     y1 = 20;
  85.     
  86.     //random ending location
  87.     x2 = rand() % (SCREEN_W-1);
  88.     y2 = SCREEN_H-50;
  89.         
  90.     //construct the line point-by-point
  91.     do_line(buffer,x1,y1,x2,y2,0,&doline);
  92. }
  93.  
  94. void movemissile()
  95. {
  96.     //grab a local copy of the current point
  97.     int x = points[curpoint].x;
  98.     int y = points[curpoint].y;
  99.     
  100.     //hide mouse pointer
  101.     scare_mouse();
  102.  
  103.     //erase missile
  104.     rectfill(buffer,x-6,y-3,x+6,y+1,BLACK);
  105.  
  106.     //see if missile was hit by defense weapon
  107.     if (getpixel(screen,x,y) == GREEN)
  108.     {
  109.         //missile destroyed! score a point
  110.         destroyed++;
  111.         updatescore();
  112.     }
  113.     else
  114.     //no hit, just draw the missile and smoke trail
  115.     {
  116.         //draw the smoke trail
  117.         putpixel(buffer,x,y-3,SMOKE);
  118.         //draw the missile
  119.         circlefill(buffer,x,y,2,BLUE);
  120.     }
  121.     
  122.     //show mouse pointer
  123.     unscare_mouse();
  124.  
  125.     //did the missile hit a city?
  126.     curpoint++;
  127.     if (curpoint >= totalpoints)
  128.     {
  129.         //destroy the missile
  130.         destroyed++;
  131.         //animate explosion directly on screen
  132.         explosion(screen, x, y, BLACK);
  133.         //show the damage on the backbuffer
  134.         circlefill(buffer, x, y, 40, BLACK);
  135.     }
  136. }
  137.  
  138. int main(void)
  139. {
  140.     //initialize program    
  141.     allegro_init();
  142.     set_color_depth(16);
  143.     set_gfx_mode(GFX_AUTODETECT_FULLSCREEN, 640, 480, 0, 0);
  144.     install_keyboard();
  145.     install_mouse();
  146.     install_timer();
  147.     srand(time(NULL));
  148.  
  149.     //create a secondary screen buffer
  150.     buffer = create_bitmap(640,480);
  151.  
  152.     //display title
  153.     textout_ex(buffer,font,"Strategic Defense (ESC to quit)",0,1,WHITE,0);
  154.     
  155.     //display score
  156.     updatescore();
  157.     
  158.     //draw border around screen
  159.     rect(buffer, 0, 12, SCREEN_W-2, SCREEN_H-2, RED);
  160.     
  161.     //load and draw the city images
  162.     city = load_bitmap("city.bmp", NULL);
  163.     for (n = 0; n < 5; n++)
  164.         masked_blit(city, buffer, 0, 0, 50+n*120, 
  165.             SCREEN_H-city->h-2, city->w, city->h);
  166.  
  167.     //load the mouse cursor
  168.     crosshair = load_bitmap("crosshair.bmp", NULL); 
  169.     set_mouse_sprite(crosshair);
  170.     set_mouse_sprite_focus(15,15);
  171.     show_mouse(buffer); 
  172.     
  173.     //main loop
  174.     while (!key[KEY_ESC])
  175.     {
  176.         //grab the current mouse values
  177.         mx = mouse_x;
  178.         my = mouse_y;
  179.         mb = (mouse_b & 1);
  180.  
  181.         //fire another missile if needed
  182.         if (destroyed)
  183.             firenewmissile();
  184.  
  185.         //left mouse button, fire the defense weapon
  186.         if (mb)
  187.             explosion(screen,mx,my,GREEN);
  188.  
  189.         //update enemy missile position
  190.         movemissile();
  191.  
  192.         //update screen       
  193.         blit(buffer,screen,0,0,0,0,640,480);
  194.         
  195.         //pause
  196.         rest(10);
  197.     }
  198.     
  199.     set_mouse_sprite(NULL);
  200.     destroy_bitmap(city);
  201.     destroy_bitmap(crosshair);
  202.     allegro_exit();
  203.  
  204. }
  205. END_OF_MAIN();
  206.  
  207.  
  208.