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

  1.  
  2. // JUMPER.C - A demo of spider jumping around using patterns
  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. // D E F I N E S /////////////////////////////////////////////////////////////
  22.  
  23. #define NUM_PATTERNS 6
  24.  
  25. // G L O B A L S  ////////////////////////////////////////////////////////////
  26.  
  27. pcx_picture image_pcx;  // general PCX image used to load background and imagery
  28.  
  29. sprite spider;          // the jumping spider
  30.  
  31. // these are the patterns encoded as strings, with a max of 64 commands
  32. // kinda like DNA patterns
  33.  
  34. char *patterns[NUM_PATTERNS] = {
  35.  
  36. "rrrrrrrrrrrrruuuuuuuuuuuulllllllllllllllldddddddddddddlllllllll.",
  37. "urururururururururrrrrrrrdrdrdrdrdrdrdrdrlllllllllllllddddllddd.",
  38. "rrrrurrrrurrrruururururururuulululululululdldldldldrdrdrdrdrdrd.",
  39. "xxxxxxxxxxxuuuuuuuuuudddduuuudddduuuddduuuddduuuddduuuddduuulll.",
  40. "rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrruuuuuuuuuuuuuuuuuuuuuuuuuuuuuu.",
  41. "lllllllllllllllrrrrrrrrrrrrrrrrrddddddddddddddddxxxxxxxrrrrrruu.", };
  42.  
  43.  
  44. // M A I N //////////////////////////////////////////////////////////////////
  45.  
  46. void main(int argc, char **argv)
  47. {
  48.  
  49. int index;         // loop variable
  50.  
  51. char buffer[64];   // used to print strings
  52.  
  53. // set the graphics mode to mode 13h
  54.  
  55. Set_Graphics_Mode(GRAPHICS_MODE13);
  56.  
  57. // create the double buffer
  58.  
  59. Create_Double_Buffer(200);
  60.  
  61. // load the imagery for the spider
  62.  
  63. PCX_Init((pcx_picture_ptr)&image_pcx);
  64. PCX_Load("jumpspd.pcx", (pcx_picture_ptr)&image_pcx,1);
  65.  
  66. // intialize the spider
  67.  
  68. Sprite_Init((sprite_ptr)&spider,160,100,30,24,0,0,0,0,0,0);
  69.  
  70. spider.state     = 0;   // select pattern one
  71. spider.counter_1 = 0;
  72.  
  73. // extract the bitmaps for the spider, there are 4 animation cells
  74.  
  75. for (index=0; index<4; index++)
  76.     PCX_Get_Sprite((pcx_picture_ptr)&image_pcx,(sprite_ptr)&spider,index,index,0);
  77.  
  78. // done with this PCX file so delete memory associated with it
  79.  
  80. PCX_Delete((pcx_picture_ptr)&image_pcx);
  81.  
  82. // now load the background image
  83.  
  84. PCX_Init((pcx_picture_ptr)&image_pcx);
  85. PCX_Load("jumpbak.pcx",(pcx_picture_ptr)&image_pcx,1);
  86.  
  87. // copy PCX image to double buffer
  88.  
  89. PCX_Copy_To_Buffer((pcx_picture_ptr)&image_pcx,double_buffer);
  90. PCX_Delete((pcx_picture_ptr)&image_pcx);
  91.  
  92. // scan background before entering event loop
  93.  
  94. Sprite_Under_Clip((sprite_ptr)&spider,double_buffer);
  95.  
  96. // put up exit instructions
  97.  
  98. Print_String_DB(80,2,9,"Hit any key to exit",1);
  99.  
  100. // main event loop, process until keyboard hit
  101.  
  102. while(!kbhit())
  103.      {
  104.  
  105.      // do animation cycle, erase, move draw...
  106.  
  107.      // erase all objects by replacing what was under them
  108.  
  109.      Sprite_Erase_Clip((sprite_ptr)&spider,double_buffer);
  110.  
  111.  
  112. // BEGIN PATTERN MOTION LOGIC ///////////////////////////////////////////////
  113.  
  114.      // move spider, test if there is still data commands in current pattern
  115.  
  116.      if (patterns[spider.state][spider.counter_1]!='.')
  117.         {
  118.         // what is the command
  119.  
  120.         switch(patterns[spider.state][spider.counter_1])
  121.               {
  122.  
  123.               case 'r': // right
  124.                    {
  125.                    // move spider
  126.  
  127.                    spider.x+=4;
  128.  
  129.                    // test if off edge
  130.  
  131.                    if (spider.x > 320)
  132.                       spider.x=-30;
  133.  
  134.                    } break;
  135.  
  136.               case 'l': // left
  137.                    {
  138.  
  139.                    // move spider
  140.  
  141.                    spider.x-=4;
  142.  
  143.                    // test if off edge
  144.  
  145.                    if (spider.x < -30)
  146.                       spider.x=320;
  147.  
  148.                    } break;
  149.  
  150.               case 'u': // up
  151.                    {
  152.  
  153.                    // move spider
  154.  
  155.                    spider.y-=4;
  156.  
  157.                    // test if off edge
  158.  
  159.                    if (spider.y <-24)
  160.                       spider.y=200;
  161.  
  162.                    } break;
  163.  
  164.               case 'd': // down
  165.                    {
  166.  
  167.                    // move spider
  168.  
  169.                    spider.y+=4;
  170.  
  171.                    // test if off edge
  172.  
  173.                    if (spider.y > 200)
  174.                       spider.y=-24;
  175.  
  176.                    } break;
  177.  
  178.               case 'x': // do nothing
  179.                    {
  180.  
  181.                    } break;
  182.  
  183.               default:break;
  184.  
  185.               } // end switch
  186.         // increment pattern index
  187.  
  188.         spider.counter_1++;
  189.  
  190.         } // end if pattern data hasn't been consumed
  191.      else
  192.         {
  193.         // select a new pattern and reset pattern index
  194.  
  195.         spider.state     = rand()%NUM_PATTERNS;
  196.         spider.counter_1 = 0;
  197.  
  198.         } // end else pattern done
  199.  
  200. // END PATTERN MOTION LOGIC //////////////////////////////////////////////////
  201.  
  202.      // animate spider
  203.  
  204.      if (++spider.curr_frame == 4)
  205.         spider.curr_frame = 0;
  206.  
  207.      // display current pattern and data
  208.  
  209.      sprintf(buffer,"Pattern #%d, data=%c",spider.state,
  210.                                 patterns[spider.state][spider.counter_1]);
  211.  
  212.      Print_String_DB(88,190,15,buffer,0);
  213.  
  214.      // ready to draw spider, but first scan background under it
  215.  
  216.      Sprite_Under_Clip((sprite_ptr)&spider,double_buffer);
  217.  
  218.      Sprite_Draw_Clip((sprite_ptr)&spider,double_buffer,1);
  219.  
  220.      // display double buffer
  221.  
  222.      Display_Double_Buffer(double_buffer,0);
  223.  
  224.      // lock onto 18 frames per second max
  225.  
  226.      Time_Delay(1);
  227.  
  228.      } // end while
  229.  
  230. // exit in a very cool way
  231.  
  232. Screen_Transition(SCREEN_DARKNESS);
  233.  
  234. // free up all resources
  235.  
  236. Sprite_Delete((sprite_ptr)&spider);
  237. Delete_Double_Buffer();
  238.  
  239. Set_Graphics_Mode(TEXT_MODE);
  240.  
  241. } // end main
  242.  
  243.