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

  1.  
  2. // FLOATER.C - A demo of 2-D terrain following
  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.  
  22. // G L O B A L S  ////////////////////////////////////////////////////////////
  23.  
  24. pcx_picture image_pcx;  // general PCX image used to load background and imagery
  25.  
  26. sprite speeder;         // the floating speeder
  27.  
  28.  
  29. // M A I N //////////////////////////////////////////////////////////////////
  30.  
  31. void main(int argc, char **argv)
  32. {
  33.  
  34. int index,         // loop variable
  35.     terr_y=160,    // these are used to draw random terrain
  36.     terr_draw=1,
  37.     rough,        // roughness of terrain ,input by user
  38.     x,y,
  39.     hover_height = 2; // minimum terrain following height
  40.  
  41. // query user about terrain roughness and terrain following height
  42.  
  43. printf("\nEnter the roughness of terrain from 1-10?");
  44. scanf("%d",&rough);
  45.  
  46. printf("\nEnter the the terrain following height from 1-50?");
  47. scanf("%d",&hover_height);
  48.  
  49. // set the graphics mode to mode 13h
  50.  
  51. Set_Graphics_Mode(GRAPHICS_MODE13);
  52.  
  53. // create the double buffer
  54.  
  55. Create_Double_Buffer(200);
  56.  
  57. // load the imagery for the speeder
  58.  
  59. PCX_Init((pcx_picture_ptr)&image_pcx);
  60. PCX_Load("floatspd.pcx", (pcx_picture_ptr)&image_pcx,1);
  61.  
  62. // intialize the speeder
  63.  
  64. Sprite_Init((sprite_ptr)&speeder,320,100,40,10,0,0,0,0,0,0);
  65.  
  66. // extract the bitmaps for the speeder, there are 4 animation cells
  67.  
  68. for (index=0; index<4; index++)
  69.     PCX_Get_Sprite((pcx_picture_ptr)&image_pcx,(sprite_ptr)&speeder,index,index,0);
  70.  
  71. // done with this PCX file so delete memory associated with it
  72.  
  73. PCX_Delete((pcx_picture_ptr)&image_pcx);
  74.  
  75. // draw the terrain one vetical strip at a time
  76.  
  77. // seed the random number generator with current time
  78.  
  79. srand(*(int far *)0x0000046CL);
  80.  
  81. for (x=0; x<320; x++)
  82.     {
  83.     // test if its time to change directions
  84.  
  85.     if (--terr_draw<=0)
  86.        {
  87.  
  88.        terr_draw=rand()%(20/rough);
  89.        terr_y = terr_y - 1 + rand()%3;
  90.  
  91.        } // end if time to select new direction
  92.  
  93.     // draw a vertical strip at current x location
  94.  
  95.     Write_Pixel_DB(x,terr_y,15);
  96.  
  97.     for (y=terr_y+1; y<200 ;y++)
  98.         Write_Pixel_DB(x,y,200+rand()%16);
  99.  
  100.     } // end for index
  101.  
  102. // scan background before entering event loop
  103.  
  104. Sprite_Under_Clip((sprite_ptr)&speeder,double_buffer);
  105.  
  106. // put up exit instructions
  107.  
  108. Print_String_DB(80,2,9,"Hit any key to exit",1);
  109.  
  110. // main event loop, process until keyboard hit
  111.  
  112. while(!kbhit())
  113.      {
  114.  
  115.      // do animation cycle, erase, move draw...
  116.  
  117.      // erase all objects by replacing what was under them
  118.  
  119.      Sprite_Erase_Clip((sprite_ptr)&speeder,double_buffer);
  120.  
  121.      // move speeder
  122.  
  123.      // if there is no terrain under speeder then apply downward thrust
  124.      // at constant velocity
  125.  
  126.      if (!Read_Pixel_DB(speeder.x+4,speeder.y+12+hover_height))
  127.          speeder.y+=2;
  128.  
  129.      // now horizontal thrust
  130.  
  131.      speeder.x-=6;
  132.  
  133.      // now probe under speeder for terrain and apply upward thrust
  134.      // if crust is found
  135.  
  136.      if (Read_Pixel_DB(speeder.x+4,speeder.y+12+hover_height))
  137.         speeder.y-=2;
  138.  
  139.      // test if speeder has moved off screen
  140.  
  141.      if (speeder.x <-40)
  142.         speeder.x = 320;
  143.  
  144.      // this should never happen, but just in case
  145.  
  146.      if (speeder.y > 200)
  147.          speeder.y=200;
  148.  
  149.      // animate speeder
  150.  
  151.      if (++speeder.curr_frame == 4)
  152.         speeder.curr_frame = 0;
  153.  
  154.      // ready to draw speeder, but first scan background under it
  155.  
  156.      Sprite_Under_Clip((sprite_ptr)&speeder,double_buffer);
  157.  
  158.      Sprite_Draw_Clip((sprite_ptr)&speeder,double_buffer,1);
  159.  
  160.      // display double buffer
  161.  
  162.      Display_Double_Buffer(double_buffer,0);
  163.  
  164.      // lock onto 18 frames per second max
  165.  
  166.      Time_Delay(1);
  167.  
  168.      } // end while
  169.  
  170. // exit in a very cool way
  171.  
  172. Screen_Transition(SCREEN_DARKNESS);
  173.  
  174. // free up all resources
  175.  
  176. Sprite_Delete((sprite_ptr)&speeder);
  177. Delete_Double_Buffer();
  178.  
  179. Set_Graphics_Mode(TEXT_MODE);
  180.  
  181. } // end main
  182.  
  183.