home *** CD-ROM | disk | FTP | other *** search
/ Black Art of 3D Game Programming / Black_Art_of_3D_Game_Programming.iso / source / borland / chap_5 / mousetst.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-02  |  7.6 KB  |  298 lines

  1.  
  2. // MOUSETST.C - A demo of the mouse driver with some added fun
  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. #include "black5.h"
  21.  
  22. // D E F I N E S /////////////////////////////////////////////////////////////
  23.  
  24. #define ANT_DEAD       0  // the ant is dead (smashed)
  25. #define ANT_EAST       1  // the ant is moving east
  26. #define ANT_WEST       2  // the ant is moving west
  27.  
  28. #define HAMMER_UP      0  // the hammer is in it's resting position
  29. #define HAMMER_MOVING  1  // the hammer is hammering!!!
  30.  
  31. // G L O B A L S  ////////////////////////////////////////////////////////////
  32.  
  33. pcx_picture image_pcx;  // general PCX image used to load background and imagery
  34.  
  35. sprite ant,             // the ant
  36.        hammer;          // the players hammer
  37.  
  38. // M A I N //////////////////////////////////////////////////////////////////
  39.  
  40. void main(int argc, char **argv)
  41. {
  42. int index,     // loop variable
  43.     mouse_x,   // mouse status
  44.     mouse_y,
  45.     buttons;
  46.  
  47. // set the graphics mode to mode 13h
  48.  
  49. Set_Graphics_Mode(GRAPHICS_MODE13);
  50.  
  51. // create the double buffer
  52.  
  53. Create_Double_Buffer(200);
  54.  
  55. // load the imagery for ant
  56.  
  57. PCX_Init((pcx_picture_ptr)&image_pcx);
  58.  
  59. PCX_Load("moreants.pcx", (pcx_picture_ptr)&image_pcx,1);
  60.  
  61. // intialize the ant sprite
  62.  
  63. Sprite_Init((sprite_ptr)&ant,0,0,12,6,0,0,0,0,0,0);
  64.  
  65. // extract the bitmaps for the ant, there are 3 animation cells for each
  66. // direction thus 6 cells
  67.  
  68. for (index=0; index<6; index++)
  69.     PCX_Get_Sprite((pcx_picture_ptr)&image_pcx,(sprite_ptr)&ant,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. // load the imagery for players hammer
  76.  
  77. PCX_Init((pcx_picture_ptr)&image_pcx);
  78.  
  79. PCX_Load("hammer.pcx", (pcx_picture_ptr)&image_pcx,1);
  80.  
  81. // intialize the hammer sprite that will take the place of the mouse
  82. // pointer
  83.  
  84. Sprite_Init((sprite_ptr)&hammer,0,0,22,20,0,0,0,0,0,0);
  85.  
  86. // extract the bitmaps for the hammer there are 5
  87.  
  88. for (index=0; index<5; index++)
  89.     PCX_Get_Sprite((pcx_picture_ptr)&image_pcx,(sprite_ptr)&hammer,index,index,0);
  90.  
  91. // done with this PCX file so delete memory associated with it
  92.  
  93. PCX_Delete((pcx_picture_ptr)&image_pcx);
  94.  
  95. // now load the picnik area background
  96.  
  97. PCX_Init((pcx_picture_ptr)&image_pcx);
  98.  
  99. PCX_Load("grass.pcx",(pcx_picture_ptr)&image_pcx,1);
  100.  
  101. // copy PCX image to double buffer
  102.  
  103. PCX_Copy_To_Buffer((pcx_picture_ptr)&image_pcx,double_buffer);
  104.  
  105. // delete the pcx image
  106.  
  107. PCX_Delete((pcx_picture_ptr)&image_pcx);
  108.  
  109. // scan under ant and hammer before entering the event loop, this must be
  110. // done or else on the first cycle the "erase" function will draw garbage
  111.  
  112. Sprite_Under_Clip((sprite_ptr)&ant,double_buffer);
  113. Sprite_Under_Clip((sprite_ptr)&hammer,double_buffer);
  114.  
  115. // reset the mouse and hide the pointer
  116.  
  117. Mouse_Control(MOUSE_RESET,NULL,NULL,&buttons);
  118. Mouse_Control(MOUSE_HIDE,NULL,NULL,NULL);
  119.  
  120. // main event loop, process until keyboard hit
  121.  
  122. while(!kbhit())
  123.      {
  124.  
  125.      // do animation cycle: 1. erase, 2.game logic, 3. scan, 4. draw
  126.  
  127.      // erase all objects by replacing what was under them
  128.  
  129.      if (ant.state!=ANT_DEAD)
  130.          Sprite_Erase_Clip((sprite_ptr)&ant,double_buffer);
  131.  
  132.      Sprite_Erase_Clip((sprite_ptr)&hammer,double_buffer);
  133.  
  134. // PLAYERS HAMMER LOGIC
  135.  
  136.      // obtain the new position of mouse and state of buttons
  137.  
  138.      Mouse_Control(MOUSE_POSITION_BUTTONS,&mouse_x,&mouse_y,&buttons);
  139.  
  140.      // map the mouse position to the screen and assign it to hammer
  141.  
  142.      hammer.x = (mouse_x >> 1)-16;
  143.      hammer.y = mouse_y;
  144.  
  145.      // test if player is trying to use hammer
  146.  
  147.      if (buttons==MOUSE_LEFT_BUTTON && hammer.state==HAMMER_UP)
  148.         {
  149.         // set state of hammer to moving
  150.  
  151.         hammer.state = HAMMER_MOVING;
  152.  
  153.         } // end if player trying to strike
  154.  
  155.      // test if hammer is animating
  156.  
  157.      if (hammer.state==HAMMER_MOVING)
  158.         {
  159.         // test if sequence complete
  160.  
  161.         if (++hammer.curr_frame==4)
  162.            {
  163.            hammer.state      = HAMMER_UP;
  164.            hammer.curr_frame = 0;
  165.            } // end if done
  166.  
  167.         // test if hammer is hitting ant
  168.  
  169.         if (hammer.curr_frame==3 && ant.state!=ANT_DEAD)
  170.            {
  171.            // do a collision test between the hammer and the ant
  172.  
  173.            if (ant.x > hammer.x && ant.x+12 < hammer.x+22 &&
  174.                ant.y > hammer.y && ant.y+6 < hammer.y+20)
  175.               {
  176.               // kill ant
  177.  
  178.               ant.state = ANT_DEAD;
  179.  
  180.               // draw a smashed ant, use frame 5 of hammer
  181.  
  182.               // set current frame to blood splat
  183.  
  184.               hammer.curr_frame = 4;
  185.  
  186.               // draw the splat
  187.  
  188.               Sprite_Draw_Clip((sprite_ptr)&hammer,double_buffer,1);
  189.  
  190.               // restore the hammer frame
  191.  
  192.               hammer.curr_frame = 3;
  193.  
  194.               } // end if hammer hit ant
  195.  
  196.            } // end if smash test
  197.  
  198.         } // end if hammer moving
  199.  
  200. // ANT LOGIC
  201.  
  202.     // test if it's time to start an ant
  203.  
  204.     if (ant.state == ANT_DEAD && ((rand()%10)==0))
  205.        {
  206.  
  207.        // which direction will ant move in
  208.  
  209.        if ((rand()%2)==0)
  210.           {
  211.           // move ant east
  212.  
  213.           ant.y         = rand()%200;    // starting y position
  214.           ant.x         = 0;             // starting x position
  215.           ant.counter_1 = 2 + rand()%10; // ant speed
  216.           ant.state     = ANT_EAST;      // ant direction
  217.           ant.curr_frame= 0;             // starting animation frame
  218.  
  219.           }
  220.        else
  221.           {
  222.           // move ant west
  223.  
  224.           ant.y         = rand()%200;     // starting y position
  225.           ant.x         = 320;            // starting x position
  226.           ant.counter_1 = -2 - rand()%10; // ant speed
  227.           ant.state     = ANT_WEST;       // ant direction
  228.           ant.curr_frame= 0;              // starting animation frame
  229.  
  230.           } // end else west
  231.  
  232.        } // end if an ant is started
  233.  
  234.     // test if ant is alive
  235.  
  236.     if (ant.state!=ANT_DEAD)
  237.        {
  238.        // process ant
  239.  
  240.        // move the ant
  241.  
  242.        ant.x+=ant.counter_1;
  243.  
  244.        // is ant off screen?
  245.  
  246.        if (ant.x <0 || ant.x > 320)
  247.           ant.state = ANT_DEAD;
  248.  
  249.        // animate the antm use proper animation cells based on direction
  250.        // cells 0-2 are for eastward motion, cells 3-5 are for westward motion
  251.  
  252.        if (ant.state==ANT_EAST)
  253.           if (++ant.curr_frame>2)
  254.              ant.curr_frame=0;
  255.  
  256.        if (ant.state==ANT_WEST)
  257.           if (++ant.curr_frame>5)
  258.              ant.curr_frame=3;
  259.  
  260.        } // end ant is alive
  261.  
  262.      // ready to draw objects, but first scan background under them
  263.  
  264.      if (ant.state!=ANT_DEAD)
  265.          Sprite_Under_Clip((sprite_ptr)&ant,double_buffer);
  266.  
  267.      Sprite_Under_Clip((sprite_ptr)&hammer,double_buffer);
  268.  
  269.      if (ant.state!=ANT_DEAD)
  270.          Sprite_Draw_Clip((sprite_ptr)&ant,double_buffer,1);
  271.  
  272.      Sprite_Draw_Clip((sprite_ptr)&hammer,double_buffer,1);
  273.  
  274.      // display double buffer
  275.  
  276.      Display_Double_Buffer(double_buffer,0);
  277.  
  278.      // lock onto 18 frames per second max
  279.  
  280.      Time_Delay(1);
  281.  
  282.      } // end while
  283.  
  284. // exit in a very cool way
  285.  
  286. Screen_Transition(SCREEN_SWIPE_X);
  287.  
  288. // free up all resources
  289.  
  290. Sprite_Delete((sprite_ptr)&ant);
  291. Sprite_Delete((sprite_ptr)&hammer);
  292. Delete_Double_Buffer();
  293.  
  294. Set_Graphics_Mode(TEXT_MODE);
  295.  
  296. } // end main
  297.  
  298.