home *** CD-ROM | disk | FTP | other *** search
/ Virtual Reality Zone / VRZONE.ISO / mac / PC / REND386 / JIREND / GLOVEPTR.C < prev    next >
C/C++ Source or Header  |  1993-04-11  |  5KB  |  175 lines

  1. /* Glove pointer device */
  2.   
  3. /* Written by Dave Stampe, August 1992 */
  4.   
  5. /* Copyright 1992 by Dave Stampe and Bernie Roehl.
  6.    May be freely used to write software for release into the public domain;
  7.    all commercial endeavours MUST contact Bernie Roehl and Dave Stampe
  8.    for permission to incorporate any part of this software into their
  9.    products!
  10.  */
  11.   
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #include <dos.h>
  15. #include <conio.h>
  16.  
  17. #include "rend386.h"
  18. #include "userint.h"
  19. #include "segasupp.h"
  20. #include "pointer.h"
  21. #include "intmath.h"
  22.   
  23. /************** INTERNAL GLOVE SETUP ***********/
  24.   
  25. extern PDRIVER *menu_device;
  26. extern PDRIVER *manip_device;
  27. PDRIVER *glove_device;
  28.   
  29. extern int stereo_type; /* required for proper int. initialization */
  30.   
  31. void gloveptr_quit(void)
  32. {
  33.    pointer_quit(glove_device);
  34. }
  35.   
  36.   
  37. PDRIVER *gloveptr_init(char *gname, long sx, long sy, long sz, long srx, long sry, long srz)
  38. {
  39.    PDRIVER *p;
  40.    POINTER x;
  41.   
  42.    p = pointer_init(P_IS3DG | P_IS6DG, gname); /* setup glove device */
  43.    if (p==NULL) return NULL;
  44.   
  45.    init_pointer(&x); /* so that defaults are OK */
  46.    /* use abs. glove motion */
  47.    pointer_abscale(p, sx, sy, sz, srx, sry, srz);
  48.    set_mouse_limits(p, screeninfo->xmax, screeninfo->ymax);
  49.    pointer_read(p, &x);
  50.    pointer_read(p, &x); /* save current position */
  51.    mouse_read(p, NULL, NULL, NULL);
  52.   
  53.    glove_device = p;
  54.    atexit(gloveptr_quit);
  55.   
  56.    return p;
  57. }
  58.   
  59.   
  60.   
  61. /***************** GLOVE POINTER DRIVER *************/
  62.   
  63. pconfig glove_pconfig = {
  64.    5*65536L, 5*65536L, -15*65536L, /* position res: mm/tick in <16.16>  */
  65.    120, 120, 120, -120, -120, -120, /* xyz ranges:                */
  66.    0, -30*65536L, 0,
  67.    0, 0, 0, 120, 11, 120, /* some rotation (swing emulation)       */
  68.    320, 200, /* mouse emulation limits (writable) */
  69.    P_HASX | P_HASY | P_HASZ | P_HASRY | P_HASSCR |
  70.    P_HASGEST | P_HASFLEX | P_HASKEYS, /* databits  */
  71.    P_CENTER | P_SCREEN, 0xFF, 11, /* modes, nullkey, flexnum           */
  72.    2, 50, 20, /* delay, idelay, reads/sec          */
  73.    P_IS3DG | P_IS3D | P_IS2D, /* uses  */
  74.    "Default Powerglove Driver"
  75. };
  76.   
  77. static glove_data glove_new; /* glove data structure */
  78.   
  79. #define DEBOUNCE_TIME 8
  80.   
  81. int setup_glove(void) /* glove initialize (incl. int. system) */
  82. {
  83.    glove_init(1);
  84.    if (stereo_type != SWITCHED)
  85.       init_SG_interrupt(NULL, glove_int_handler,6500);
  86.    else
  87.    {
  88.       init_SG_interrupt(switch_sega,glove_int_handler,6500); /* start Sega interrupt */
  89.       atexit(sega_off);
  90.    }
  91.    popmsg("Waiting for glove...");
  92.    delay(1000);
  93.   
  94.    while (!glove_ready())
  95.       if (kbhit())
  96.       {
  97.          getkey();
  98.          return -3;
  99.       }
  100.    refresh_display();
  101.    glove_read(&glove_new);
  102.    return 0;
  103. }
  104.   
  105.   
  106. static fbend[4] = { 0, 40, 90, 127 }; /* finger flexions      */
  107. static tbbend[4] = { 0, 25, 72, 127 };
  108. static ttbend[4] = { 0, 35, 80, 127 };
  109.   
  110. pconfig *glove_driver(int op, POINTER *p, int mode)
  111. {
  112.    int type = FP_OFF(p); /* way to get integer arg. */
  113.    int ft, fi, fm, fp;
  114.   
  115.    switch(op)
  116.    {
  117.       case DRIVER_CMD:
  118.       case DRIVER_RESET:
  119.          break;
  120.   
  121.       case DRIVER_INIT:
  122.          setup_glove();
  123.          break;
  124.   
  125.       case DRIVER_READ:/* pointer (2DP) read */
  126.          if (mode == P_POINTER)
  127.          {
  128.             if (glove_ready() == 0) return NULL;
  129.             glove_read(&glove_new);
  130.             p->x = glove_new.x;
  131.             p->y = glove_new.y;
  132.             p->z = glove_new.z;
  133.             p->rx = 0;
  134.             p->ry = ((long) glove_new.rot) << 16;
  135.             p->rz = 0;
  136.             p->buttons = 0;
  137.             p->keys = glove_new.keys;
  138.   
  139.             ft = (glove_new.fingers >> 6) & 3; /* finger joint angles */
  140.             fi = (glove_new.fingers >> 4) & 3;
  141.             fm = (glove_new.fingers >> 2) & 3;
  142.             fp = (glove_new.fingers >> 0) & 3;
  143.   
  144.             p->flex[0] = tbbend[ft];
  145.             p->flex[1] = ttbend[ft];
  146.             p->flex[2] = p->flex[3] = fbend[fi];
  147.             p->flex[4] = p->flex[5] = fbend[fm];
  148.             p->flex[6] = p->flex[7] = fbend[fp];
  149.             p->flex[8] = p->flex[9] = fbend[fp];
  150.   
  151.             p->gesture = (gesture_time>DEBOUNCE_TIME)?gesture_type:G_UNKNOWN ;
  152.          }
  153.          else if (mode == P_SCREEN) /* mouse read (640x480) */
  154.          {
  155.             glove_read(&glove_new);
  156.             p->x = scale_16(((long)glove_pconfig.maxsx) << 8, 127, (long)glove_new.x);
  157.             p->y = scale_16(((long)glove_pconfig.maxsy) << 8, 127, -(long)glove_new.y);
  158.             if (p->x < 0) p->x = 0;
  159.             if (p->y < 0) p->y = 0;
  160.             if (p->x > glove_pconfig.maxsx) p->x = glove_pconfig.maxsx;
  161.             if (p->y > glove_pconfig.maxsy) p->y = glove_pconfig.maxsy;
  162.             p->gesture = (gesture_time > DEBOUNCE_TIME) ? gesture_type : G_UNKNOWN;
  163.             p->buttons = (p->gesture==G_FIST) ? 1 : 0;
  164.          }
  165.          break;
  166.   
  167.       case DRIVER_CHECK:
  168.          break;
  169.       case DRIVER_QUIT:
  170.          break;
  171.    }
  172.    return &glove_pconfig;
  173. }
  174.   
  175.