home *** CD-ROM | disk | FTP | other *** search
/ Black Art of 3D Game Programming / Black_Art_of_3D_Game_Programming.iso / source / borland / chap_7 / lostnspc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-16  |  4.0 KB  |  181 lines

  1.  
  2. // LOSTNSPC.C - A demo of random motion
  3.  
  4. // I N C L U D E S ///////////////////////////////////////////////////////////
  5.  
  6. #include <io.h>
  7. #include <conio.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <dos.h>
  11. #include <bios.h>
  12. #include <fcntl.h>
  13. #include <memory.h>
  14. #include <malloc.h>
  15. #include <math.h>
  16. #include <string.h>
  17.  
  18. #include "black3.h"
  19. #include "black4.h"
  20.  
  21. // G L O B A L S  ////////////////////////////////////////////////////////////
  22.  
  23. pcx_picture image_pcx;  // general PCX image used to load background and imagery
  24.  
  25. sprite ship;            // the alien ship
  26.  
  27. // M A I N //////////////////////////////////////////////////////////////////
  28.  
  29. void main(int argc, char **argv)
  30. {
  31.  
  32. int index,         // loop variable
  33.     velocity_x=0,  // used to control velocity of ship
  34.     velocity_y=0;
  35.  
  36. // set the graphics mode to mode 13h
  37.  
  38. Set_Graphics_Mode(GRAPHICS_MODE13);
  39.  
  40. // create the double buffer
  41.  
  42. Create_Double_Buffer(200);
  43.  
  44. // load the imagery for the ship
  45.  
  46. PCX_Init((pcx_picture_ptr)&image_pcx);
  47. PCX_Load("lostship.pcx", (pcx_picture_ptr)&image_pcx,1);
  48.  
  49. // intialize the ship
  50.  
  51. Sprite_Init((sprite_ptr)&ship,160,100,18,16,0,0,0,0,0,0);
  52.  
  53. // make ship sit for the first 1.5 seconds
  54.  
  55. ship.counter_1   = 0;
  56. ship.threshold_1 = 25;
  57.  
  58. // extract the bitmaps for the ship, there are 2 animation cells
  59.  
  60. for (index=0; index<2; index++)
  61.     PCX_Get_Sprite((pcx_picture_ptr)&image_pcx,(sprite_ptr)&ship,index,index,0);
  62.  
  63. // done with this PCX file so delete memory associated with it
  64.  
  65. PCX_Delete((pcx_picture_ptr)&image_pcx);
  66.  
  67. // now load the background image
  68.  
  69. PCX_Init((pcx_picture_ptr)&image_pcx);
  70. PCX_Load("lostback.pcx",(pcx_picture_ptr)&image_pcx,1);
  71.  
  72. // copy PCX image to double buffer
  73.  
  74. PCX_Copy_To_Buffer((pcx_picture_ptr)&image_pcx,double_buffer);
  75. PCX_Delete((pcx_picture_ptr)&image_pcx);
  76.  
  77. // scan background beforee entering event loop
  78.  
  79. Sprite_Under_Clip((sprite_ptr)&ship,double_buffer);
  80.  
  81. // put up exit instructions
  82.  
  83. Print_String_DB(80,2,9,"Hit any key to exit",1);
  84.  
  85. // main event loop, process until keyboard hit
  86.  
  87. while(!kbhit())
  88.      {
  89.  
  90.      // do animation cycle, erase, move draw...
  91.  
  92.      // erase all objects by replacing what was under them
  93.  
  94.      Sprite_Erase_Clip((sprite_ptr)&ship,double_buffer);
  95.  
  96.  
  97. // BEGIN RANDOM MOTION LOGIC //////////////////////////////////////////////////
  98.  
  99.      // test if ship is complete with current trajectory and
  100.      // needs a new one
  101.  
  102.      if (++ship.counter_1 > ship.threshold_1)
  103.         {
  104.  
  105.         // select new direction vector
  106.  
  107.         velocity_x = -5 + rand()%10;
  108.         velocity_y = -5 + rand()%10;
  109.  
  110.         // select a random number of frames to stay on new heading
  111.  
  112.         ship.threshold_1 = 5 + rand()%50;
  113.  
  114.         // reset counter
  115.  
  116.         ship.counter_1   = 0;
  117.  
  118.         } // end if
  119.  
  120.      // move ship
  121.  
  122.      ship.x+=velocity_x;
  123.      ship.y+=velocity_y;
  124.  
  125.      // test if ship went beyond screen edges
  126.  
  127.      if (ship.x > 320)
  128.          ship.x = -18;
  129.      else
  130.      if (ship.x < -18)
  131.          ship.x = 320;
  132.  
  133.      if (ship.y > 200)
  134.          ship.y = -16;
  135.      else
  136.      if (ship.y < -16)
  137.          ship.y = 200;
  138.  
  139.  
  140. // END RANDOM MOTION LOGIC /////////////////////////////////////////////////////
  141.  
  142.      // animate ship
  143.  
  144.      if (++ship.curr_frame == 2)
  145.         ship.curr_frame = 0;
  146.  
  147.      // add some special effects via a vapor trail
  148.  
  149.      if (rand()%10==1)
  150.         Write_Pixel_DB(ship.x+rand()%20,ship.y+12+rand()%4,24+rand()%4);
  151.  
  152.      // ready to draw ship, but first scan background under it
  153.  
  154.      Sprite_Under_Clip((sprite_ptr)&ship,double_buffer);
  155.  
  156.      Sprite_Draw_Clip((sprite_ptr)&ship,double_buffer,1);
  157.  
  158.      // display double buffer
  159.  
  160.      Display_Double_Buffer(double_buffer,0);
  161.  
  162.      // lock onto 18 frames per second max
  163.  
  164.      Time_Delay(1);
  165.  
  166.      } // end while
  167.  
  168. // exit in a very cool way
  169.  
  170. Screen_Transition(SCREEN_DARKNESS);
  171.  
  172. // free up all resources
  173.  
  174. Sprite_Delete((sprite_ptr)&ship);
  175. Delete_Double_Buffer();
  176.  
  177. Set_Graphics_Mode(TEXT_MODE);
  178.  
  179. } // end main
  180.  
  181.