home *** CD-ROM | disk | FTP | other *** search
/ Amiga ACS 1998 #6 / amigaacscoverdisc1998-061998.iso / games / descent / source / main / victor.c < prev    next >
C/C++ Source or Header  |  1998-06-08  |  9KB  |  310 lines

  1. /*
  2. THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
  3. SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
  4. END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
  5. ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
  6. IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
  7. SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
  8. FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
  9. CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
  10. AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.  
  11. COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
  12. */
  13. /*
  14.  * $Source: f:/miner/source/main/rcs/victor.c $
  15.  * $Revision: 2.5 $
  16.  * $Author: mike $
  17.  * $Date: 1995/03/30 16:35:59 $
  18.  * 
  19.  * Routines to access the VictorMaxx helmet.
  20.  * 
  21.  * $Log: victor.c $
  22.  * Revision 2.5  1995/03/30  16:35:59  mike
  23.  * text localization.
  24.  * 
  25.  * Revision 2.4  1995/03/09  15:34:33  john
  26.  * Fixed bug with timeout being too long.
  27.  * 
  28.  * Revision 2.3  1995/03/08  15:42:00  john
  29.  * Took out debug code.
  30.  * 
  31.  * Revision 2.2  1995/03/08  15:32:54  john
  32.  * Made VictorMaxx head tracking use Greenleaf code.
  33.  * 
  34.  * Revision 2.1  1995/03/06  15:24:03  john
  35.  * New screen techniques.
  36.  * 
  37.  * Revision 2.0  1995/02/27  11:31:23  john
  38.  * New version 2.0, which has no anonymous unions, builds with
  39.  * Watcom 10.0, and doesn't require parsing BITMAPS.TBL.
  40.  * 
  41.  * Revision 1.13  1994/12/06  16:30:40  yuan
  42.  * Localization
  43.  * 
  44.  * Revision 1.12  1994/11/27  23:15:06  matt
  45.  * Made changes for new mprintf calling convention
  46.  * 
  47.  * Revision 1.11  1994/11/19  15:14:56  mike
  48.  * remove unused code and data
  49.  * 
  50.  * Revision 1.10  1994/11/13  16:32:58  matt
  51.  * Made angle averaging work right when yaw is near 0/360 degrees
  52.  * Made angle read code empty buffer and use last values
  53.  * 
  54.  * Revision 1.9  1994/11/12  13:47:37  john
  55.  * Reenabled the victor screen mode setting code.
  56.  * 
  57.  * Revision 1.8  1994/11/11  18:33:26  john
  58.  * Made victor not set screen mode.
  59.  * 
  60.  * Revision 1.7  1994/08/25  18:43:44  john
  61.  * First revision of new control code.
  62.  * 
  63.  * Revision 1.6  1994/07/25  10:24:04  john
  64.  * Victor stuff.
  65.  * 
  66.  * Revision 1.5  1994/07/22  17:53:32  john
  67.  * Added better victormax support.
  68.  * 
  69.  * Revision 1.4  1994/07/21  21:31:31  john
  70.  * First cheapo version of VictorMaxx tracking.
  71.  * 
  72.  * Revision 1.3  1994/07/20  21:30:34  john
  73.  * Got screen mode change to work.
  74.  * 
  75.  * Revision 1.2  1994/07/20  21:04:31  john
  76.  * Add VictorMax VR helment support.
  77.  * 
  78.  * Revision 1.1  1994/07/20  20:49:01  john
  79.  * Initial revision
  80.  * 
  81.  * 
  82.  */
  83.  
  84.  
  85. #pragma off (unreferenced)
  86. static char rcsid[] = "$Id: victor.c 2.5 1995/03/30 16:35:59 mike Exp $";
  87. #pragma on (unreferenced)
  88.  
  89. #define DOS4G        
  90.  
  91. #include <stdio.h>
  92. #include <stdlib.h>
  93. #include <conio.h>
  94.  
  95. #include "error.h"
  96. #include "mono.h"
  97. #include "args.h"
  98. //#include "serial.h"
  99. #include "victor.h"
  100. #include "text.h"
  101. //#include "commlib.h" //Not included in public release -KRB
  102. //#include "fast.h" //Not included in public release -KRB
  103. #include "key.h"
  104. //This include is just to allow compiling. It doesn't mean it will work. Values in here are only dummy values
  105. #include "nocomlib.h"
  106.  
  107. int Victor_headset_installed=0;
  108. int Victor_mode=0;
  109.  
  110. PORT * VictorPort = NULL;
  111.  
  112. /********************************************************************
  113.  *                                                                  *
  114.  *  Functions for changing low level VGA registers                  *
  115.  *      (c) VictorMaxx Technologies, March 1994                     *
  116.  *                                                                  *
  117.  ********************************************************************/
  118.  
  119. // The structure containes all atributes for a VGA mode
  120. typedef struct {              // VGA atributes
  121.     unsigned char   MOR,      // Misccellaneous Output register
  122.     FCR,      // Feature Control Register
  123.     ISR0,     // Input Status Register 0
  124.     ISR1,     // Input Status Register 1
  125.     SR[5],
  126.     CRCT[25]; // Catode Ray Tybe Controller registers
  127. } VGAatr;
  128.  
  129. VGAatr VGA13 = { 0x27, 1, 0x10, 0x31,     // 320x200x256
  130.         3, 9, 0xf, 0, 0xe,
  131.         0x6b, 0x4f, 0x50, 0x8e,
  132.         0x5c, 0x84, 0x4, 0x11,
  133.         0, 0x40, 0, 0, 0, 0, 0,
  134.         0, 0xdf, 0xa2, 0xc7, 0x28,
  135.         0x40, 0xdc, 0xf0, 0xa3, 0xff};
  136.  
  137. VGAatr VGAi13 = {   0x63, 0x0, 0x10, 0x5,                   // 320x200x256
  138.         0x3, 0x1, 0xf, 0x0, 0xe,
  139.         0x5f, 0x4f, 0x50, 0x82, 0x54, 0x80, 0xbf, 0x1f, 0x0,
  140.         0x41, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x9c, 0x8e, 0x8f,
  141.         0x28, 0x40, 0x96, 0xb9, 0xa3, 0xff};
  142. //*******************************************************************
  143.  
  144. // This function sets the data defined above into proper VGA registers
  145. // in:  VGAatr structure
  146. // out: none
  147. void set_mode_atr(VGAatr *vga)
  148. {
  149.     unsigned char  i;
  150.     
  151.     outp( 0x3c2, vga->MOR );                        
  152.  
  153.     outp( 0x3c4, 0 );     
  154.     outp( 0x3c5, 0 ); /* choose index 0, reset */
  155.     for(i=1; i<=4; i++)     {
  156.         outp( 0x3c4, i );  /* choose index i */
  157.         outp( 0x3c5, vga->SR[i] );  /* choose index i */
  158.     }
  159.  
  160.     outp( 0x3c4, 0 );
  161.     outp( 0x3c5, 3 ); /* choose index 0 set 11b*/
  162.  
  163.     /* CRTC Registers */
  164.     outp( 0x3d4, 0x11 ); /* WRITE index 11H */
  165.     i = inp( 0x3d5 );
  166.     outp( 0x3d5, i & 0x7f  ); /* ENABLE ACCESS TO THE REGISTERS */
  167.     
  168.     for(i=0; i<=7; i++)    {
  169.         if(i!=1 && i!=2)        {
  170.             outp( 0x3d4, i );
  171.             outp( 0x3d5, vga->CRCT[i] );
  172.         }
  173.     }
  174.  
  175.     outp( 0x3d4, 0x11 );
  176.     outp( 0x3d5, 128 ); /* DISABLE ACCESS TO 0-7 */
  177.        
  178.     for(i=9; i<23; i++)     {
  179.         outp( 0x3d4, i );
  180.         outp( 0x3d5, vga->CRCT[i] );
  181.     }
  182.  
  183. }
  184.  
  185. void victor_init_graphics()    
  186. {
  187.     if (Victor_mode==0) {
  188.         Victor_mode = 1;
  189.         set_mode_atr(&VGA13);
  190.     } else {
  191.         Victor_mode = 0;
  192.         set_mode_atr(&VGAi13);
  193.     }
  194. }
  195.  
  196. void victor_init_tracking(int serial_port)    
  197. {
  198.     if (Victor_headset_installed) return;
  199.  
  200.     printf( "%s %d\n", TXT_INIT_VICTOR, serial_port );
  201.  
  202.     if ( (serial_port < 1) || (serial_port > 4) )    {
  203.         Error( "Invalid serial port parameter for -maxxtrak!\n" );
  204.     }
  205.     VictorPort = PortOpenGreenleafFast(serial_port-1, 19200, 'N', 8, 1 );
  206.     if ( !VictorPort )    {
  207.         printf( "%s %d\n", TXT_SERIAL_FAILURE, VictorPort->status );
  208.         return;
  209.     }
  210. //    printf( "Port opened\n" );
  211.  
  212.     SetDtr( VictorPort, 1 );
  213. //    printf( "Step #1\n" );
  214.     SetRts( VictorPort, 1 );
  215. //    printf( "Step #2\n" );
  216.     UseRtsCts( VictorPort, 0 );
  217. //    printf( "Step #3\n" );
  218.  
  219.     Victor_headset_installed = 1;
  220.     atexit( victor_close_tracking );
  221.  
  222.      WriteChar( VictorPort, 'A' );        // Absolute coordinate mode
  223.      WriteChar( VictorPort, 'C' );        // High accuracy mode
  224.      WriteChar( VictorPort, 'F' );        // Binary Mode
  225.      WriteChar( VictorPort, 'G' );        // Sampling upon request
  226.  
  227.     ClearRXBuffer( VictorPort );
  228.  
  229.      WriteChar( VictorPort, 'S' );        // Request a packet
  230.  
  231. //    { 
  232. //        int k=0;
  233. //        while ( k!=KEY_ESC )    { 
  234. //            fix y, p, r;
  235. //            victor_read_headset_filtered( &y, &p, &r );
  236. //            printf( "%d\t%d\t%d\n", y, p, r );
  237. //            k = key_inkey();
  238. //        }
  239. //    }
  240.  
  241. }
  242.  
  243. void victor_close_tracking()    {
  244.     if ( Victor_headset_installed )    {
  245.         Victor_headset_installed = 0;
  246.         VictorPort     =  NULL;
  247.     }
  248. }
  249.  
  250. fix v_yaw = 0, v_pitch = 0, v_roll = 0;
  251.  
  252. void victor_read_headset_raw( fix *yaw, fix *pitch, fix *roll )
  253. {
  254.     int y, p, r, count;
  255.     static unsigned char buff[6];
  256.     ReadBufferTimed(VictorPort, buff, 6, 10);
  257.  
  258.     count = VictorPort->count;
  259.     if ( count < 6 )    {
  260.         ClearRXBuffer(VictorPort);
  261.         WriteChar( VictorPort, 'S' );
  262.         *yaw = v_yaw;
  263.         *pitch = v_pitch;
  264.         *roll = v_roll;
  265.         return;
  266.     }
  267.     WriteChar( VictorPort, 'S' );
  268.  
  269.     y  =  (short)(buff[0] << 8) | buff[1];
  270.     p  =  (short)(buff[2] << 8) | buff[3];
  271.     r  =  (short)(buff[4] << 8) | buff[5];
  272.     
  273.     v_yaw = *yaw = y;
  274.     v_pitch = *pitch = -1*(p - 16384);
  275.     v_roll = *roll = -1*(r - 16384);
  276. }
  277.  
  278.  
  279. void victor_read_headset_filtered( fix *yaw, fix *pitch, fix *roll )
  280. {
  281.     int i;
  282.     fix _yaw, _pitch, _roll;
  283.     fix t_yaw, t_pitch, t_roll;
  284.     fix last_yaw;
  285.  
  286.     t_yaw=t_pitch=t_roll=0;
  287.  
  288.     for (i=0; i<2; i++ )    {
  289.         victor_read_headset_raw( &_yaw, &_pitch, &_roll );
  290.  
  291.         //if (i>0)
  292.         //    if (last_yaw<i2f(90) && _yaw>i2f(270))
  293.         //        _yaw -= i2f(360);
  294.         //    else if (_yaw<i2f(90) && last_yaw>i2f(270))
  295.         //        _yaw += i2f(360);
  296.  
  297.          t_yaw += _yaw; 
  298.         t_pitch += _pitch;
  299.         t_roll += _roll;
  300.  
  301.         last_yaw = _yaw;
  302.     }
  303.     
  304.     *yaw = t_yaw / 2;
  305.     *pitch = t_pitch / 2;
  306.     *roll = t_roll / 2;
  307.  
  308. }
  309.  
  310.