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