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

  1.  
  2. // JOYTEST.C - A demo of the joystick driver
  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 SHIP_FRAMES 16  // number of animaton frames of ship
  25.  
  26. // G L O B A L S  ////////////////////////////////////////////////////////////
  27.  
  28. pcx_picture image_pcx;  // general PCX image used to load background and imagery
  29.  
  30. sprite ship;            // the players ship
  31.  
  32. // these are the velocity lookup tables, they have pre-computed velocities
  33. // for each of the 16 directions the ship can be pointing in
  34.  
  35. int x_velocity[SHIP_FRAMES] = {0,2,4,4,6,4,4,2,0,-2,-4,-4,-6,-4,-4,-2};
  36. int y_velocity[SHIP_FRAMES] = {-6,-4,-4,-2,0,2,4,4,6,4,4,2,0,-2,-4,-4};
  37.  
  38. // M A I N //////////////////////////////////////////////////////////////////
  39.  
  40. void main(int argc, char **argv)
  41. {
  42.  
  43. int index,     // loop variable
  44.     dx,        // use to hold roughly 15% of the range of each
  45.     dy,        // joystick axis
  46.     joy_x,     // the final normalized joystick position values
  47.     joy_y;
  48.  
  49. // test if there is a joystick
  50.  
  51. if (!Joystick_Available(JOYSTICK_1))
  52.    {
  53.    printf("\nJoystick 1 not connected. Exiting.");
  54.    return;
  55.    } // end if
  56.  
  57. printf("\nJoystick 1 detected...");
  58.  
  59. // calibrate the stick
  60.  
  61. Joystick_Calibrate(JOYSTICK_1,USE_LOW_LEVEL);
  62.  
  63. // compute 15% of range
  64.  
  65. dx = (int)( (float).5 + (float).15 * (float)(joystick_1_max_x-joystick_1_min_x));
  66. dy = (int)( (float).5 + (float).15 * (float)(joystick_1_max_y-joystick_1_min_y));
  67.  
  68. // set the graphics mode to mode 13h
  69.  
  70. Set_Graphics_Mode(GRAPHICS_MODE13);
  71.  
  72. // create the double buffer
  73.  
  74. Create_Double_Buffer(200);
  75.  
  76. // load the imagery for ship
  77.  
  78. PCX_Init((pcx_picture_ptr)&image_pcx);
  79.  
  80. PCX_Load("falcon.pcx", (pcx_picture_ptr)&image_pcx,1);
  81.  
  82. // intialize the ship sprite
  83.  
  84. Sprite_Init((sprite_ptr)&ship,160,100,24,20,0,0,0,0,0,0);
  85.  
  86. // extract the bitmaps for the ship, there are 16 of them, one for each
  87. // pre-rotated angle
  88.  
  89. // get images off first row of template 0-11
  90.  
  91. for (index=0; index<12; index++)
  92.     PCX_Get_Sprite((pcx_picture_ptr)&image_pcx,(sprite_ptr)&ship,index,index,0);
  93.  
  94. // get images off second row of template 12-15
  95.  
  96. for (index=12; index<16; index++)
  97.     PCX_Get_Sprite((pcx_picture_ptr)&image_pcx,(sprite_ptr)&ship,index,index-12,1);
  98.  
  99. // done with this PCX file so delete memory associated with it
  100.  
  101. PCX_Delete((pcx_picture_ptr)&image_pcx);
  102.  
  103. // now load the background startfield
  104.  
  105. PCX_Init((pcx_picture_ptr)&image_pcx);
  106.  
  107. PCX_Load("frontier.pcx",(pcx_picture_ptr)&image_pcx,1);
  108.  
  109. // copy PCX image to double buffer
  110.  
  111. PCX_Copy_To_Buffer((pcx_picture_ptr)&image_pcx,double_buffer);
  112.  
  113. // delete the pcx image
  114.  
  115. PCX_Delete((pcx_picture_ptr)&image_pcx);
  116.  
  117. // scan under the ship, so the first time through the event loop
  118. // whas something to replace
  119.  
  120. Sprite_Under_Clip((sprite_ptr)&ship,double_buffer);
  121.  
  122. // main event loop, process until keyboard hit
  123.  
  124. while(!kbhit())
  125.      {
  126.  
  127.      // do animation cycle: 1. erase, 2.game logic, 3. scan, 4. draw
  128.  
  129.      // erase the ship
  130.  
  131.      Sprite_Erase_Clip((sprite_ptr)&ship,double_buffer);
  132.  
  133. // PLAYERS SHIP LOGIC
  134.  
  135.      // get joystick position and subtract away center to
  136.      // compute delta from center
  137.  
  138.      joy_x = Joystick(JOYSTICK_1_X) - joystick_1_neutral_x;
  139.      joy_y = Joystick(JOYSTICK_1_Y) - joystick_1_neutral_y;
  140.  
  141.      // test if player has moved stick past the 10% mark, if so transform
  142.      // ship
  143.  
  144.      if (joy_x > dx)
  145.         {
  146.         // rotate ship right
  147.  
  148.         if (++ship.curr_frame==SHIP_FRAMES)
  149.            ship.curr_frame = 0;
  150.         }
  151.      else
  152.      if (joy_x < -dx)
  153.         {
  154.         // rotate ship left
  155.  
  156.         if (--ship.curr_frame == -1)
  157.            ship.curr_frame = SHIP_FRAMES-1;
  158.  
  159.         } // end if rotating left
  160.  
  161.      // test if player is movinh ship foward or backward
  162.  
  163.      if (joy_y > dy)
  164.         {
  165.         // move ship backward
  166.  
  167.         // index into velocity table and translate ship with values
  168.  
  169.         ship.x -= x_velocity[ship.curr_frame];
  170.         ship.y -= y_velocity[ship.curr_frame];
  171.  
  172.         }
  173.      else
  174.      if (joy_y < -dy)
  175.         {
  176.         // move ship foward
  177.  
  178.         ship.x += x_velocity[ship.curr_frame];
  179.         ship.y += y_velocity[ship.curr_frame];
  180.  
  181.         } // end if foward
  182.  
  183.      // clip ship to screen universe
  184.  
  185.      if (ship.x > 319)
  186.          ship.x = -24;
  187.      else
  188.      if (ship.x < -24)
  189.          ship.x = 319;
  190.  
  191.      if (ship.y > 199)
  192.          ship.y = -20;
  193.      else
  194.      if (ship.y < -20)
  195.          ship.y = 199;
  196.  
  197.      // ready to draw ship, but first scan background under them
  198.  
  199.      Sprite_Under_Clip((sprite_ptr)&ship,double_buffer);
  200.  
  201.      Sprite_Draw_Clip((sprite_ptr)&ship,double_buffer,1);
  202.  
  203.      // display double buffer
  204.  
  205.      Display_Double_Buffer(double_buffer,0);
  206.  
  207.      // lock onto 18 frames per second max
  208.  
  209.      Time_Delay(1);
  210.  
  211.      } // end while
  212.  
  213. // exit in a very cool way
  214.  
  215. Screen_Transition(SCREEN_DARKNESS);
  216.  
  217. // free up all resources
  218.  
  219. Sprite_Delete((sprite_ptr)&ship);
  220. Delete_Double_Buffer();
  221.  
  222. Set_Graphics_Mode(TEXT_MODE);
  223.  
  224. } // end main
  225.  
  226.