home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Raytrace & Morphing / SOS-RAYTRACE.ISO / programm / source / crend5 / hpotptr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-10  |  3.7 KB  |  192 lines

  1. /*
  2.  POTENTIOMETER 2D head tracker:
  3.  pot 3 is pan
  4.  pot 4 is tilt
  5.  button 3 is hold
  6.  button 4 is recenter
  7.  config file use:
  8.     pot <pscale> <poff> <tscale> <toff> <nr> <nrinc>
  9.  by Dave Stampe, May 1993
  10.  (c) 1993 Dave Stampe
  11. */
  12.  
  13. #include <stdlib.h>
  14. #include <dos.h>
  15. #include <bios.h>
  16. #include <stdio.h>
  17. #include <conio.h>
  18. #include <signal.h>
  19. #include <mem.h>
  20.  
  21. #include "rend386.h"
  22. #include "userint.h"
  23. #include "pointer.h"
  24.  
  25.  
  26. /*****************/
  27.  
  28.  
  29.  
  30. /*
  31. static p_dump(int i)
  32. {
  33.  char c[100];
  34.  
  35.  sprintf(c,"%d: %d %.0f,%.0f,%.0f %.0f,%.0f,%.0f", i, status_6d,
  36.  ((float) x_6d) / 40.0, ((float) y_6d) / 40.0, ((float) z_6d) / 40.0,
  37.  ((float) rx_6d) / 40.0, ((float) ry_6d) / 40.0, ((float) rz_6d) / 40.0);
  38.  
  39.  popmsg(c);
  40. }
  41. */
  42.  
  43.  
  44. /************* POTENTIOMETER POINTER DRIVER *************/
  45.  
  46. static pconfig pot_pconfig = {
  47.     1640L, 1640L, 1640L,            /* position res: mm/tick in <16.16>  */
  48.     1048575L, 1048575L, 1048575L,   /* xyz ranges: just dummies */
  49.     -1048575L, -1048575L, -1048575L,
  50.     1640L, 1640L, 1640L,        /* rotation res: <ticks per */
  51.     7200L, 7200L, 7200L,
  52.     -7200L, -7200L, -7200L,         /* rotation range      */
  53.     320, 200, /* mouse emulation limits (writable) */
  54.     P_HASRX | P_HASRY,            /* databits  */
  55.     P_POINTER, 0, 0,                 /* modes, nullkey, flexnum           */
  56.     5, 5, 100,                     /* delay, idelay, reads/sec          */
  57.     P_IS6D | P_IS6H | P_IS3D,       /* uses  */
  58.     "Joystick Pot Head Tracker"
  59. };
  60.  
  61.  
  62. static int pan_off = 0;
  63. static int tilt_off = 0;
  64. static int boxnr = 0;
  65. static long boxnrs = 0;
  66.  
  67. static long rx, ry;
  68.  
  69. static pot_init()
  70. {
  71.  extern int use_pothead;
  72.  extern int joystick_check();
  73.  extern float hdo_x, hdo_y, hdo_z, hdo_rx, hdo_ry, hdo_rz;
  74.  
  75.  if((joystick_check() & 1)==0)
  76.   {
  77.    popmsg("No Pot Headtracker!");
  78.    getch();
  79.    exit(0);
  80.   }
  81.  
  82.  pot_pconfig.xrres = hdo_x*65536.0;    /* set scaling of rotation */
  83.  pot_pconfig.yrres = hdo_z*65536.0;
  84.  
  85.  pan_off = hdo_y;
  86.  tilt_off = hdo_rx;
  87.  
  88.  boxnr = hdo_ry;
  89.  boxnr = hdo_rz*256;
  90.  
  91.  rx = ry = 0;
  92.  
  93.  use_pothead++;
  94.  
  95.  hdo_x = hdo_y = hdo_z = hdo_rx = hdo_ry = hdo_rz = 0.0;  /* use only locally */
  96. }
  97.  
  98. static read_pots()
  99. {
  100.  extern int have_joystick;
  101.  extern int joystick_j1;
  102.  extern int joystick_j2;
  103.  extern int joystick_j3;
  104.  extern int joystick_j4;
  105.  extern int joystick_bt;
  106.  int x, y;
  107.  int boxx, boxy;
  108.  
  109.  if(!have_joystick)    /* make sure it's read */
  110.   {
  111.    joystick_data joy;
  112.  
  113.    joy.port = 0;
  114.    joy.scale = 0;
  115.    joystick_read(&joy);
  116.   }
  117.  
  118.  x = joystick_j4;
  119.  y = joystick_j3;
  120.  
  121.  if(joystick_bt & 8)  /* recenter */
  122.   {
  123.    pan_off = y;
  124.    tilt_off = x;
  125.    rx = 0;
  126.    ry = 0;
  127.    return 0;
  128.   }
  129.  if(joystick_bt & 4) /* hold */
  130.   {
  131.    tilt_off = x - rx;
  132.    pan_off = y - ry;
  133.    return 0;
  134.   }
  135.  
  136.  boxx = boxnr;
  137.  boxy = boxnr;
  138.  if(boxnrs)                 /* adj. threshold for noise level */
  139.   {
  140.    boxx += (x*boxnrs)>>8;
  141.    boxy += (y*boxnrs)>>8;
  142.   }
  143.  
  144.  x -= tilt_off;
  145.  y -= pan_off;
  146.  
  147.  if (x-rx > boxx) rx = x-boxx;         /* X nr */
  148.  else if (rx-x > boxx) rx = x+boxx;
  149.  if (y-ry > boxy) ry = y-boxy;         /* Y nr */
  150.  else if (ry-y > boxy) ry = y+boxy;
  151. }
  152.  
  153.  
  154. pconfig far *pot_driver(int op, POINTER *p, int mode)
  155. {
  156.     int type = FP_OFF(p);         /* way to get integer arg. */
  157.     int ft, fi, fm, fp;
  158.  
  159.     switch(op)
  160.     {
  161.     case DRIVER_CMD:
  162.     case DRIVER_RESET:
  163.         break;
  164.  
  165.     case DRIVER_INIT:
  166.         pot_init();
  167.         break;
  168.  
  169.     case DRIVER_READ:        /* head pointer (6DP) read */
  170.         if (mode == P_POINTER)
  171.          {
  172.           read_pots();
  173.           p->x = 0;
  174.           p->z = 0;
  175.           p->y = 0;
  176.           p->rx = rx*65536L;
  177.           p->ry = ry*65536L;
  178.           p->buttons = 0;
  179.           p->keys = 0;
  180.           p->gesture = -1 ;
  181.          }
  182.         break;
  183.  
  184.     case DRIVER_CHECK:
  185.         break;
  186.     case DRIVER_QUIT:
  187.         break;
  188.     }
  189.     return &pot_pconfig;
  190. }
  191.  
  192.