home *** CD-ROM | disk | FTP | other *** search
/ C++ Games Programming / CPPGAMES.ISO / thx / source / theatrix / jssrvr.cpp < prev    next >
C/C++ Source or Header  |  1995-05-03  |  2KB  |  108 lines

  1. #include <iostream.h>
  2. #include <fastgraf.h>
  3. #include <dos.h>
  4. #include <stdlib.h>
  5. #include "standard.h"
  6. #include "jsfold.h"
  7. #include "jssrvr.h"
  8.  
  9. JoystickServer::JoystickServer()
  10. {
  11.   dz = 0;
  12.   xadj = yadj = 0;
  13.   cx = cy = 0;
  14.   lx = ty = 0;
  15.   rx = by = 0;
  16.   joystick_supported = 0;
  17. }
  18.  
  19. int JoystickServer::setting(int *x, int *y)
  20. {
  21.   _AH = 0x84;
  22.   _DX = 1;
  23.   geninterrupt(0x15);
  24.   int ax = _AX;
  25.   int bx = _BX;
  26.   *x = ax;
  27.   *y = bx;
  28.  
  29.   _AH = 0x84;
  30.   _DX = 0;
  31.   geninterrupt(0x15);
  32.   int sw = _AL;
  33.   return (sw & 0x30) ^ 0x30;
  34. }
  35.  
  36. void JoystickServer::calibrate(char *pos, int *x, int *y)
  37. {
  38.   int sw;
  39.   // --- wait for no buttons down
  40.   do
  41.     sw = setting(x, y);
  42.   while (sw);
  43.   // --- wait for button down
  44.   cout << "\nMove joystick to "
  45.        << pos
  46.        << " and press a button\n";
  47.   do
  48.     sw = setting(x, y);
  49.   while (sw == 0);
  50.   cout << pos << ": " << *x << '/' << *y << '\n';
  51. }
  52.  
  53. void JoystickServer::startup()
  54.   {
  55.   if (!joystick_supported)
  56.     return;
  57.   int x, y;
  58.   setting(&x, &y);
  59.   if (x == 0 || y == 0)
  60.     {
  61.     cout << "\nJoystick not sensed";
  62.     joystick_supported = 0;
  63.     return;
  64.     }
  65.   // ---- get the dead zone factor
  66.   int i = 200;
  67.   int mnx = 10000, mxx = 0;
  68.   int mny = 10000, mxy = 0;
  69.   while (--i)    {
  70.     setting(&x, &y);
  71.     x = abs(x);
  72.     y = abs(y);
  73.     mxx = max(mxx, x);
  74.     mnx = min(mnx, x);
  75.     mxy = max(mxy, y);
  76.     mny = min(mny, y);
  77.   }
  78.  
  79.   xadj = (mxx - mnx + 1);
  80.   yadj = (mxy - mny + 1);
  81.  
  82.   cx = mnx + xadj;
  83.   cy = mny + yadj;
  84.  
  85.   calibrate("upper left", &lx, &ty);
  86.   calibrate("lower right", &rx, &by);
  87.   }
  88.  
  89. void JoystickServer::check(Folder& fld)
  90.   {
  91.   if (!joystick_supported)
  92.     return;
  93.   int x,y;
  94.   int sw = setting(&x, &y);
  95.   // --- normalize the coordinates
  96.   x = x - cx;
  97.   y = cy - y;
  98.   if (x >= -xadj && x <= xadj)
  99.     x = 0;
  100.   if (y >= -yadj && y <= yadj)
  101.     y = 0;
  102.  
  103.   if ((sw & 0x30) || x || y)
  104.     fld.dispatch(x, y, sw);
  105.   }
  106.  
  107.  
  108.