home *** CD-ROM | disk | FTP | other *** search
/ Teach Yourself Game Programming in 21 Days / TYGAMES_R.ISO / source / day_07 / graph7j.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-16  |  6.9 KB  |  279 lines

  1.  
  2. // I N C L U D E S ///////////////////////////////////////////////////////////
  3.  
  4. #include <dos.h>
  5. #include <bios.h>
  6. #include <stdio.h>
  7. #include <math.h>
  8. #include <conio.h>
  9.  
  10. #include "graph7j.h"
  11.  
  12. // G L O B A L S  ////////////////////////////////////////////////////////////
  13.  
  14. unsigned int joy_1_max_x,   // global joystick calibration variables
  15.              joy_1_max_y,
  16.              joy_1_min_x,
  17.              joy_1_min_y,
  18.              joy_1_cx,
  19.              joy_1_cy,
  20.              joy_2_max_x,
  21.              joy_2_max_y,
  22.              joy_2_min_x,
  23.              joy_2_min_y,
  24.              joy_2_cx,
  25.              joy_2_cy;
  26.  
  27.  
  28.  
  29. // F U N C T I O N S /////////////////////////////////////////////////////////
  30.  
  31. unsigned char Buttons(unsigned char button)
  32. {
  33. // read the joystick buttons by peeking the port that the switches are attached
  34. // to.
  35.  
  36. outp(JOYPORT,0); // clear the latch and request a sample
  37.  
  38. // invert buttons then mask with request
  39.  
  40. return( ~inp(JOYPORT) & button);
  41.  
  42. } // end Buttons
  43.  
  44. //////////////////////////////////////////////////////////////////////////////
  45.  
  46. unsigned int Joystick(unsigned char stick)
  47. {
  48. // reads the joystick values manually by conting how long the capacitors take
  49. // to charge/discharge
  50. // let's use the inline assembler.  It's Cool!
  51.  
  52. __asm
  53.     {
  54.     cli                    ; disable interupts
  55.  
  56.     mov ah, byte ptr stick ; get mask into ah to selct joystick to read
  57.     xor al,al              ; zero out al, xor is a trick
  58.     xor cx,cx              ; same with cx which we will use as a counter
  59.     mov dx,JOYPORT         ; dx is used by inp and outp
  60.     out dx,al              ; write 0's to the port
  61. discharge:
  62.     in al,dx               ; read the data back from port
  63.     test al,ah             ; has the bit in question changed?
  64.     loopne discharge       ; if the stick isn't ready then --cx and loop
  65.  
  66.     sti                    ; re-enable interrupts
  67.     xor ax,ax              ; zero out ax
  68.     sub ax,cx              ; ax now holds the position of the axis switch
  69.  
  70.     } // end asm
  71.  
  72. // since ax has the result the function will return it properly
  73.  
  74. } // end Joystick
  75.  
  76. //////////////////////////////////////////////////////////////////////////////
  77.  
  78. unsigned int Joystick_Bios(unsigned char stick)
  79. {
  80. // bios version of joystick read
  81.  
  82. union _REGS inregs, outregs;
  83.  
  84. inregs.h.ah = 0x84; // joystick function 84h
  85. inregs.x.dx = 0x01; // read joysticks subfunction 1h
  86.  
  87. // call dos
  88.  
  89. _int86(0x15,&inregs, &outregs);
  90.  
  91. // return proper value depending on sent command
  92.  
  93. switch(stick)
  94.       {
  95.       case JOYSTICK_1_X:
  96.            {
  97.            return(outregs.x.ax);
  98.            } break;
  99.  
  100.       case JOYSTICK_1_Y:
  101.            {
  102.            return(outregs.x.bx);
  103.            } break;
  104.  
  105.       case JOYSTICK_2_X:
  106.            {
  107.            return(outregs.x.cx);
  108.            } break;
  109.  
  110.       case JOYSTICK_2_Y:
  111.            {
  112.            return(outregs.x.dx);
  113.            } break;
  114.  
  115.       default:break;
  116.  
  117.       } // end switch stick
  118.  
  119. } // end Joystick_Bios
  120.  
  121.  
  122. //////////////////////////////////////////////////////////////////////////////
  123.  
  124. unsigned char Buttons_Bios(unsigned char button)
  125. {
  126. // bios version of buttons read
  127.  
  128. union _REGS inregs, outregs;
  129.  
  130. inregs.h.ah = 0x84; // joystick function 84h
  131. inregs.x.dx = 0x00; // read buttons subfunction 0h
  132.  
  133. // call dos
  134.  
  135. _int86(0x15,&inregs, &outregs);
  136.  
  137. // invert buttons then mask with request
  138.  
  139. return( (~outregs.h.al) & button);
  140.  
  141. } // end Buttons_Bios
  142.  
  143. //////////////////////////////////////////////////////////////////////////////
  144.  
  145. void Joystick_Calibrate(int stick)
  146. {
  147. // calibrates the joystick by finding the min and max deflections in both the
  148. // X and Y axis.  Then stores it in a global data structure for future use.
  149.  
  150. unsigned int x_new,y_new; // temp joystick positions
  151.  
  152. // set vars so that we can find there actual values
  153.  
  154. if (stick==JOY_1_CAL)
  155.    {
  156.  
  157.    printf("\nCalibrating Joystick #1: Swirl stick then release and press fire");
  158.  
  159.    // set calibrations to impossible values
  160.  
  161.    joy_1_max_x=0;
  162.    joy_1_max_y=0;
  163.    joy_1_min_x=10000;
  164.    joy_1_min_y=10000;
  165.  
  166.    // now the user should shwirl joystick let the stick fall neutral then press
  167.    // any button
  168.  
  169.    while(!Buttons(BUTTON_1_1 | BUTTON_1_2))
  170.         {
  171.         // get the new values and try to update calibration
  172.         x_new = Joystick_Bios(JOYSTICK_1_X);
  173.         y_new = Joystick_Bios(JOYSTICK_1_Y);
  174.  
  175.         // process X - axis
  176.  
  177.         if (x_new >= joy_1_max_x)
  178.             joy_1_max_x = x_new;
  179.  
  180.         if (x_new <= joy_1_min_x)
  181.             joy_1_min_x = x_new;
  182.  
  183.         // process Y - axis
  184.  
  185.         if (y_new >= joy_1_max_y)
  186.             joy_1_max_y = y_new;
  187.  
  188.         if (y_new <= joy_1_min_y)
  189.             joy_1_min_y = y_new;
  190.  
  191.         } // end while
  192.  
  193.         // user has let stick go to center so that must be the center
  194.  
  195.         joy_1_cx = x_new;
  196.         joy_1_cy = y_new;
  197.  
  198.    } // end calibrate joystick #1
  199. else
  200. if (stick==JOY_2_CAL)
  201.    {
  202.    printf("\nCalibrating Joystick #2: Swirl stick then release and press fire");
  203.  
  204.    // set calibrations to impossible values
  205.  
  206.    joy_2_max_x=0;
  207.    joy_2_max_y=0;
  208.    joy_2_min_x=10000;
  209.    joy_2_min_y=10000;
  210.  
  211.    // now the user should shwirl joystick let the stick fall neutral then press
  212.    // any button
  213.  
  214.    while(!Buttons(BUTTON_2_1 | BUTTON_2_2))
  215.         {
  216.         // get the new values and try to update calibration
  217.         x_new = Joystick(JOYSTICK_2_X);
  218.         y_new = Joystick(JOYSTICK_2_Y);
  219.  
  220.         // process X - axis
  221.  
  222.         if (x_new >= joy_2_max_x)
  223.             joy_2_max_x = x_new;
  224.         else
  225.         if (x_new <= joy_2_min_x)
  226.             joy_2_min_x = x_new;
  227.  
  228.         // process Y - axis
  229.  
  230.         if (y_new >= joy_2_max_y)
  231.             joy_2_max_y = y_new;
  232.         else
  233.         if (y_new <= joy_2_min_y)
  234.             joy_2_min_y = y_new;
  235.  
  236.         } // end while
  237.  
  238.         // user has let stick go to center so that must be the center
  239.  
  240.         joy_2_cx = x_new;
  241.         joy_2_cy = y_new;
  242.  
  243.  
  244.    } // end calibrate joystick #2
  245.  
  246. printf("\nCalibration Complete...hit any key to continue.");
  247.  
  248. getch();
  249.  
  250. } // end Joystick_Calibrate
  251.  
  252. //////////////////////////////////////////////////////////////////////////////
  253.  
  254. int Joystick_Available(int stick_num)
  255. {
  256. // test if the joystick is plugged in that the user is requesting tested
  257.  
  258. if (stick_num == JOYSTICK_1)
  259.    {
  260.    // test if joystick 1 is plugged in by testing the port values
  261.    // they will be 0,0 if there is no stick
  262.  
  263.    return(Joystick_Bios(JOYSTICK_1_X)+Joystick_Bios(JOYSTICK_1_Y));
  264.  
  265.    } // end if joystick 1
  266. else
  267.    {
  268.    // test if joystick 2 is plugged in by testing the port values
  269.    // they will be 0,0 if there is no stick
  270.  
  271.    return(Joystick_Bios(JOYSTICK_2_X)+Joystick_Bios(JOYSTICK_2_Y));
  272.  
  273.    } // end else joystick 2
  274.  
  275. } // end Joystick_Available
  276.  
  277.  
  278. ///////////////////////////////////////////////////////////////////////////////
  279.