home *** CD-ROM | disk | FTP | other *** search
/ Black Art of 3D Game Programming / Black_Art_of_3D_Game_Programming.iso / source / msc / chap_4 / worms.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-19  |  4.0 KB  |  164 lines

  1.  
  2. // WORMS.C - A demo of sprites, clipping and double buffering
  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 worm,ant;        // the worm and ant
  26.  
  27. // M A I N //////////////////////////////////////////////////////////////////
  28.  
  29. void main(int argc, char **argv)
  30. {
  31.  
  32. int index;  // loop variable
  33.  
  34. // set the graphics mode to mode 13h
  35.  
  36. Set_Graphics_Mode(GRAPHICS_MODE13);
  37.  
  38. // create the double buffer
  39.  
  40. Create_Double_Buffer(200);
  41.  
  42. // load the imagery for worm
  43.  
  44. PCX_Init((pcx_picture_ptr)&image_pcx);
  45.  
  46. PCX_Load("wormimg.pcx", (pcx_picture_ptr)&image_pcx,1);
  47.  
  48. // intialize the worm sprite
  49.  
  50. Sprite_Init((sprite_ptr)&worm,160,100,38,20,0,0,0,0,0,0);
  51.  
  52. // extract the bitmaps for the worm, there are 4 animation cells
  53.  
  54. for (index=0; index<4; index++)
  55.     PCX_Get_Sprite((pcx_picture_ptr)&image_pcx,(sprite_ptr)&worm,index,index,0);
  56.  
  57. // done with this PCX file so delete memory associated with it
  58.  
  59. PCX_Delete((pcx_picture_ptr)&image_pcx);
  60.  
  61. // load the imagery for ant
  62.  
  63. PCX_Init((pcx_picture_ptr)&image_pcx);
  64.  
  65. PCX_Load("antimg.pcx", (pcx_picture_ptr)&image_pcx,1);
  66.  
  67. // intialize the ant sprite
  68.  
  69. Sprite_Init((sprite_ptr)&ant,160,180,12,6,0,0,0,0,0,0);
  70.  
  71. // extract the bitmaps for the ant, there are 3 animation cells
  72.  
  73. for (index=0; index<3; index++)
  74.     PCX_Get_Sprite((pcx_picture_ptr)&image_pcx,(sprite_ptr)&ant,index,index,0);
  75.  
  76. // done with this PCX file so delete memory associated with it
  77.  
  78. PCX_Delete((pcx_picture_ptr)&image_pcx);
  79.  
  80. // now load the background that the worm and ant will run around in
  81.  
  82. PCX_Init((pcx_picture_ptr)&image_pcx);
  83.  
  84. PCX_Load("mushroom.pcx",(pcx_picture_ptr)&image_pcx,1);
  85.  
  86. // copy PCX image to double buffer
  87.  
  88. PCX_Copy_To_Buffer((pcx_picture_ptr)&image_pcx,double_buffer);
  89.  
  90. PCX_Delete((pcx_picture_ptr)&image_pcx);
  91.  
  92. // scan under ant and worm before entering the event loop, this must be
  93. // done or else on the first cycle the "erase" function will draw garbage
  94.  
  95. Sprite_Under((sprite_ptr)&ant,double_buffer);
  96. Sprite_Under_Clip((sprite_ptr)&worm,double_buffer);
  97.  
  98. // main event loop, process until keyboard hit
  99.  
  100. while(!kbhit())
  101.      {
  102.  
  103.      // do animation cycle, erase, move draw...
  104.  
  105.  
  106.      // erase all objects by replacing what was under them
  107.  
  108.      Sprite_Erase((sprite_ptr)&ant,double_buffer);
  109.  
  110.      Sprite_Erase_Clip((sprite_ptr)&worm,double_buffer);
  111.  
  112.      // move objects, test if they have ran off right edge of screen
  113.  
  114.      if ((ant.x+=1) > 320-12)
  115.         ant.x = 0;
  116.  
  117.      if ((worm.x+=6) > 320)
  118.         worm.x = -40;   // start worm back one length beyond the
  119.                         // left edge of screen
  120.  
  121.      // do animation for objects
  122.  
  123.      if (++ant.curr_frame==3)   // if all frames have been displayed then
  124.         ant.curr_frame = 0;     // reset back to frame 0
  125.  
  126.      if (++worm.curr_frame==4)  // if all frames have been displayed then
  127.         worm.curr_frame = 0;    // reset back to frame 0
  128.  
  129.  
  130.      // ready to draw objects, but first scan background under them
  131.  
  132.      Sprite_Under((sprite_ptr)&ant,double_buffer);
  133.      Sprite_Under_Clip((sprite_ptr)&worm,double_buffer);
  134.  
  135.      Sprite_Draw((sprite_ptr)&ant,double_buffer,1);
  136.      Sprite_Draw_Clip((sprite_ptr)&worm,double_buffer,1);
  137.  
  138.      // display double buffer
  139.  
  140.      Display_Double_Buffer(double_buffer,0);
  141.  
  142.      // lock onto 18 frames per second max
  143.  
  144.      Time_Delay(1);
  145.  
  146.      } // end while
  147.  
  148. // exit in a very cool way
  149.  
  150. Screen_Transition(SCREEN_SWIPE_X);
  151.  
  152. // free up all resources
  153.  
  154. Sprite_Delete((sprite_ptr)&worm);
  155.  
  156. Sprite_Delete((sprite_ptr)&ant);
  157.  
  158. Delete_Double_Buffer();
  159.  
  160. Set_Graphics_Mode(TEXT_MODE);
  161.  
  162. } // end main
  163.  
  164.