home *** CD-ROM | disk | FTP | other *** search
/ Virtual Reality Homebrewer's Handbook / vr.iso / vr386 / readcfg.c < prev    next >
C/C++ Source or Header  |  1996-03-19  |  9KB  |  312 lines

  1. //* Reads a Configuration file
  2. // Written by Dave Stampe, 9/1/94
  3. // Relies heavily on the code in wparse.c
  4.  
  5.  
  6. /*
  7.  This code is part of the VR-386 project, created by Dave Stampe.
  8.  VR-386 is a desendent of REND386, created by Dave Stampe and
  9.  Bernie Roehl.  Almost all the code has been rewritten by Dave
  10.  Stampre for VR-386.
  11.  
  12.  Copyright (c) 1994 by Dave Stampe:
  13.  May be freely used to write software for release into the public domain
  14.  or for educational use; all commercial endeavours MUST contact Dave Stampe
  15.  (dstampe@psych.toronto.edu) for permission to incorporate any part of
  16.  this software or source code into their products!  Usually there is no
  17.  charge for under 50-100 items for low-cost or shareware products, and terms
  18.  are reasonable.  Any royalties are used for development, so equipment is
  19.  often acceptable payment.
  20.  
  21.  ATTRIBUTION:  If you use any part of this source code or the libraries
  22.  in your projects, you must give attribution to VR-386 and Dave Stampe,
  23.  and any other authors in your documentation, source code, and at startup
  24.  of your program.  Let's keep the freeware ball rolling!
  25.  
  26.  DEVELOPMENT: VR-386 is a effort to develop the process started by
  27.  REND386, improving programmer access by rewriting the code and supplying
  28.  a standard API.  If you write improvements, add new functions rather
  29.  than rewriting current functions.  This will make it possible to
  30.  include you improved code in the next API release.  YOU can help advance
  31.  VR-386.  Comments on the API are welcome.
  32.  
  33.  CONTACT: dstampe@psych.toronto.edu
  34. */
  35.  
  36.  
  37. #include <stdio.h>
  38. #include <string.h>
  39. #include <stdlib.h>
  40. #include <alloc.h>
  41. #include <dos.h>       /* delay() */
  42.  
  43. #include "vr_api.h"
  44. #include "intmath.h"
  45. #include "pcdevice.h"
  46. #include "wparse.h"
  47.  
  48. extern int do_screen_clear;
  49. extern STEREO default_stereo;
  50. extern unsigned char palette[];
  51. extern int npalette;
  52.  
  53. extern char *fix_fname(char *fname);
  54.  
  55. extern char loadpath[];
  56.  
  57. #define match(a,b)  !strnicmp(a, b, strlen(b))
  58.  
  59. static char ps[] = " \t\n,#";
  60. static char st[] = "%s";
  61.  
  62. /************* READ A CFG FILE *********/
  63.  
  64.  
  65. // LIST OF COMMANDS
  66.  
  67. static char *cfgcommands[] = {
  68.     "loadpath", "palette", "skycolor", "groundcolor", "screencolor",
  69.     "worldscale", "key", "control", "viewframe", "hither", "yon",
  70.     "eyespacing", "screendist", "include", "screenwidth",
  71.     "convergence", "stepsize", "options", "videodev",
  72.     "mousedev", "headdev", "glovedev", "ptrdev",
  73.     "switchdev", "glovecursor", "ptrcursor", "segaport", "switchport",
  74.     "pgloveport", "pglovetime", "stereoset", "stereotype",
  75.     "stereoleft", "stereoright",
  76.     NULL
  77. };
  78.  
  79. enum com_codes {
  80.     c_loadpath = 0, c_palette, c_skycolor, c_groundcolor, c_screencolor,
  81.     c_worldscale, c_key, c_control, c_viewframe, c_hither, c_yon,
  82.     c_eyespacing, c_screendist, c_include, c_screenwidth,
  83.     c_convergence, c_stepsize, c_options, c_videodev,
  84.     c_mousedev, c_headdev, c_glovedev, c_ptrdev,
  85.     c_switchdev, c_glovecursor, c_ptrcursor, c_segaport, c_switchport,
  86.     c_pgloveport, c_pglovetime, c_stereoset, c_stereotype,
  87.     c_stereoleft, c_stereoright
  88. };
  89.  
  90.  
  91. void read_config_file(FILE *in)
  92. {
  93.   char obuff[256];
  94.   char inbuff[256], *buff, fname[100];
  95.   char *args;
  96.   char *pname;
  97.   int i,cmd;
  98.  
  99.   while (fgets(inbuff, sizeof(inbuff), in))
  100.     {
  101.       strcpy(obuff,inbuff);
  102.       buff = strtok(inbuff,"#");
  103.       for (buff = inbuff; isspace(*buff); ++buff);
  104.       if(buff[0]==0) continue;
  105.       for (args = buff+1; isalpha(*args); ++args);
  106.       args++;
  107.       buff = strtok(buff," \t\n");
  108.  
  109.       for (cmd = 0; cfgcommands[cmd]; cmd++)
  110.     if (!stricmp(cfgcommands[cmd], buff)) break;
  111.       if (cfgcommands[cmd] == NULL) continue;
  112.  
  113.       switch(cmd)
  114.     {
  115.        case c_loadpath:
  116.            sscanf(args, st, loadpath);
  117.            break;
  118.  
  119.        case c_videodev:
  120.          {
  121.            extern char vdname[];
  122.            extern int vdmode;
  123.  
  124.            sscanf(args, "%s %x", vdname, &vdmode);
  125.          }     break;
  126.  
  127.        case c_mousedev:
  128.          {
  129.            extern char mdname[];
  130.  
  131.            sscanf(args, st, mdname);
  132.          } break;
  133.  
  134.        case c_headdev:
  135.          {
  136.            extern char hdname[];
  137.            extern POSE hd_offset;
  138.            float x,y,z,rx,ry,rz;
  139.  
  140.            sscanf(args, "%s %f %f %f %f %f %f", hdname, &x, &y, &z, &rx, &ry, &rz);
  141.            hd_offset.x = x;
  142.            hd_offset.y = y;
  143.            hd_offset.z = z;
  144.            hd_offset.rx = float2angle(rx);
  145.            hd_offset.ry = float2angle(ry);
  146.            hd_offset.rz = float2angle(rz);
  147.          }
  148.         break;
  149.  
  150.        case c_glovedev:
  151.          {
  152.            extern char gpdname[];
  153.            extern int have_glove;
  154.            extern POSE ptr_scale;
  155.            float x,y,z,rx,ry,rz;
  156.  
  157.            have_glove = 1;
  158.            sscanf(args, "%s %f %f %f %f %f %f", gpdname, &x, &y, &z, &rx, &ry, &rz);
  159.            ptr_scale.x = float2scale(x);
  160.            ptr_scale.y = float2scale(y);
  161.            ptr_scale.z = float2scale(z);
  162.            ptr_scale.rx = float2scale(rx);
  163.            ptr_scale.ry = float2scale(ry);
  164.            ptr_scale.rz = float2scale(rz);
  165.          }    break;
  166.  
  167.        case c_ptrdev:
  168.          {
  169.            extern char gpdname[];
  170.            extern int have_ptr;
  171.            extern POSE ptr_scale;
  172.            float x,y,z,rx,ry,rz;
  173.  
  174.            have_ptr = 1;
  175.            sscanf(args, "%s %f %f %f %f %f %f", gpdname, &x, &y, &z, &rx, &ry, &rz);
  176.            ptr_scale.x = float2scale(x);
  177.            ptr_scale.y = float2scale(y);
  178.            ptr_scale.z = float2scale(z);
  179.            ptr_scale.rx = float2scale(rx);
  180.            ptr_scale.ry = float2scale(ry);
  181.            ptr_scale.rz = float2scale(rz);
  182.          }     break;
  183.  
  184.        case c_switchdev:
  185.          {
  186.            extern char swdname[];
  187.  
  188.            sscanf(args, st, swdname);
  189.          }    break;
  190.  
  191.        case c_glovecursor:
  192.          {
  193.            extern char gpcursor[];
  194.            sscanf(args, st, gpcursor);
  195.          }break;
  196.  
  197.        case c_ptrcursor:
  198.          {
  199.            extern char gpcursor[];
  200.            sscanf(args, st, gpcursor);
  201.          } break;
  202.  
  203.        /* CONFIG: DONE IMMEDIATELY */
  204.  
  205.        case c_switchport:  /* synonym with no trademark problems */
  206.        case c_segaport:
  207.          {
  208.            sscanf(args, "%x %x %x %x %x", &sega_address,
  209.             &sega_mask, &sega_left, &sega_right, &sega_doff, &sega_port_image);
  210.          }    break;
  211.  
  212.        case c_pgloveport:
  213.          {
  214.            sscanf(args, "%x %x %x %x %x %x %x %x", &glove_in_port, &glove_out_port,
  215.             &glove_write_mask, &glove_none_mask, &glove_latch_mask, &glove_clock_mask,
  216.             &glove_clock_latch, &glove_data_mask, &port_image );
  217.          }    break;
  218.  
  219.        case c_pglovetime:
  220.          {
  221.            sscanf(args, "%d %d", &glove_bit_delay, &glove_byte_delay );
  222.          }    break;
  223.  
  224.        case c_stereoset:
  225.          {
  226.            float ws = 1.0;
  227.            if(!stereo_type) break;
  228.            sscanf(args, "%ld %ld %ld %ld %ld %f", &(default_stereo.phys_screen_dist),
  229.             &(default_stereo.phys_screen_width), &(default_stereo.pixel_width),
  230.             &(default_stereo.phys_eye_spacing), &(default_stereo.phys_convergence), &ws);
  231.            default_stereo.world_scaling = 65536.0 * ws;
  232.          }     break;
  233.  
  234.        case c_stereotype:
  235.          {
  236.            char sty[80];
  237.  
  238.            sscanf(args, st, sty);
  239.            if (match(sty, "OFF")) stereo_type = MONOSCOPIC;
  240.            else if (match(sty, "SWITCH")) stereo_type = SWITCHED;
  241.            else if (match(sty, "SEPARATE")) stereo_type = SEPARATE;
  242.            else if (match(sty, "SPLIT")) stereo_type = SPLITLR;
  243.          }    break;
  244.  
  245.        case c_stereoleft:
  246.          {
  247.            float ry;
  248.            if(!stereo_type) break;
  249. #define STWL &default_stereo.window[LEFT_EYE]
  250.            sscanf(args, "%d %d %f %ld %ld %ld %ld %ld", STWL.xoff, STWL.orientation,
  251.             &ry, STWL.l, STWL.t, STWL.r, STWL.b );
  252.            *STWL.ry = float2angle(ry);
  253.          } break;
  254.  
  255.        case c_stereoright:
  256.          {
  257.            float ry;
  258.            if(!stereo_type) break;
  259. #define STWR &default_stereo.window[RIGHT_EYE]
  260.            sscanf(args, "%d %d %f %ld %ld %ld %ld %ld", STWR.xoff, STWR.orientation,
  261.             &ry, STWR.l, STWR.t, STWR.r, STWR.b );
  262.            *STWR.ry = float2angle(ry);
  263.          }break;
  264.  
  265.        case c_include:
  266.          {
  267.            char fname[100];
  268.            FILE *in;
  269.            char *f;
  270.            strcpy(fname,strtok(NULL,ps));
  271.            if ((in = fopen(f = fix_fname(fname), "r")) != NULL)
  272.          {
  273.            read_config_file(in);
  274.            fclose(in);
  275.          }
  276.            else
  277.          errprintf("Could not open '%s'as include file", f);
  278.          }break;
  279.  
  280.        case c_hither:
  281.          {
  282.           COORD h = atol(strtok(NULL,ps));
  283.           if(h<1) h=1;
  284.           set_camera_hither(default_camera,h);
  285.           break;
  286.          }
  287.        case c_yon:
  288.          {
  289.           COORD y = atol(strtok(NULL,ps));
  290.           if(y>530000000L) y=530000000L;
  291.           set_camera_yon(default_camera,y);
  292.           break;
  293.          }
  294.        case c_eyespacing:
  295.            default_stereo.phys_eye_spacing = atol(strtok(NULL,ps));
  296.            break;
  297.        case c_screendist:
  298.            default_stereo.phys_screen_dist = atol(strtok(NULL,ps));
  299.            break;
  300.        case c_screenwidth:
  301.            default_stereo.phys_screen_width = atol(strtok(NULL,ps));
  302.            default_stereo.pixel_width = atoi(strtok(NULL,ps));
  303.            break;
  304.        case c_convergence:
  305.            default_stereo.phys_convergence = atol(strtok(NULL,ps));
  306.            break;
  307.      }
  308.      }
  309.   return;
  310. }
  311.  
  312.