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

  1. // Initialization routines for VR-386 demo program.
  2. // You may want to simplify these for your own code,
  3. // especially if you're not supporting all devices
  4.  
  5. // For VR-386 by Dave Stampe, 9/1/94
  6.  
  7. // THIS IS FOR THE MINI-PROGRAM DEMO
  8.  
  9.  
  10. /*
  11.  This code is part of the VR-386 project, created by Dave Stampe.
  12.  VR-386 is a desendent of REND386, created by Dave Stampe and
  13.  Bernie Roehl.  Almost all the code has been rewritten by Dave
  14.  Stampre for VR-386.
  15.  
  16.  Copyright (c) 1994 by Dave Stampe:
  17.  May be freely used to write software for release into the public domain
  18.  or for educational use; all commercial endeavours MUST contact Dave Stampe
  19.  (dstampe@psych.toronto.edu) for permission to incorporate any part of
  20.  this software or source code into their products!  Usually there is no
  21.  charge for under 50-100 items for low-cost or shareware products, and terms
  22.  are reasonable.  Any royalties are used for development, so equipment is
  23.  often acceptable payment.
  24.  
  25.  ATTRIBUTION:  If you use any part of this source code or the libraries
  26.  in your projects, you must give attribution to VR-386 and Dave Stampe,
  27.  and any other authors in your documentation, source code, and at startup
  28.  of your program.  Let's keep the freeware ball rolling!
  29.  
  30.  DEVELOPMENT: VR-386 is a effort to develop the process started by
  31.  REND386, improving programmer access by rewriting the code and supplying
  32.  a standard API.  If you write improvements, add new functions rather
  33.  than rewriting current functions.  This will make it possible to
  34.  include you improved code in the next API release.  YOU can help advance
  35.  VR-386.  Comments on the API are welcome.
  36.  
  37.  CONTACT: dstampe@psych.toronto.edu
  38. */
  39.  
  40.  
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <math.h>
  44. #include <ctype.h>   /* toupper() */
  45. #include <string.h>
  46. #include <mem.h>     /* memmove() */
  47. #include <dos.h>
  48. #include <alloc.h>   /* coreleft */
  49.  
  50. #include "config.h"
  51. #include "pointer.h"
  52. #include "vr_api.h"
  53. #include "intmath.h"
  54. #include "pcdevice.h"
  55. #include "splits.h"
  56. #include "renderer.h"   // setup_render()
  57. #include "xmem.h"
  58.  
  59. #include "f3dkitd.h"  /* for load_dac_colors() */
  60.  
  61.  
  62. extern unsigned _stklen = 10000; /* need lots of stack for recursion */
  63.  
  64.  
  65. /************ SYSTEM DATA **************/
  66.  
  67. WORD position_changed = 0;   // reasons to refresh screen
  68. WORD display_changed = 1;
  69. WORD world_changed = 1;
  70.  
  71.  
  72. /************* DRIVER INFORMATION **************/
  73.  
  74. char swdname[40] = "sega";
  75. char mdname[40] = "mouse";
  76.  
  77. PDRIVER *cursor_device = NULL; /* primary mouse device   */
  78. PDRIVER *menu_device = NULL; /* secondary mouse device */
  79.  
  80. int manip_2D_avail = 0; /* can do mouse manipulation */
  81.  
  82. // KEEP AROUND FOR CONFIG LOAD, EVVEN THOUGH NOT USED
  83.  
  84. char gpdname[40] = "pglove";
  85. char gpcursor[40] = "hand.fig";
  86. POSE ptr_scale = { 65536L,65536L,3*65536L,65536L,65536L,65536L};
  87. int have_glove = 0;
  88. int have_ptr = 0;
  89.  
  90. char hdname[40] = "none";
  91. POSE hd_offset = ZERO_POSE;
  92. int use_ht = 0;
  93.  
  94. int use_keyjoy = 0;    // use keys as joystick
  95.  
  96. /************* STEREOSCOPIC DATA ***************/
  97.  
  98. extern STEREO default_stereo;
  99. extern WORD stereo_type;
  100.  
  101. /************ OPTIONS FLAGS ***************/
  102.  
  103. int use_BW = 0;
  104. int swap_eyes = 0;
  105. int use_glove = 0;
  106.  
  107. /************ MISC. COMMUNICTION *************/
  108.  
  109. long emmrequest = 0;    // nonzero if EMM request: number of K
  110.  
  111. long originalmem,aftervideomem,afterloadmem;
  112.  
  113. void *temp_mem = NULL; /* points to area to be used for temp storage */
  114. long temp_size = 0;
  115.  
  116.  
  117. FILE *log_file = NULL;
  118.  
  119.  
  120. /************** VIDEO DRIVER *************/
  121.  
  122.  
  123. int npalette = 0; /* non-zero if we have a palette loaded */
  124. unsigned char palette[256*3];
  125.  
  126. extern struct Screeninfo *screeninfo;
  127.  
  128. char vdname[40] = "vd256.rvd";
  129. int vdmode = 0x14;
  130. int vd_loaded = 0;
  131.  
  132. void *v_driver_pointer = NULL;
  133.  
  134. void load_video_driver(char *dfile)
  135. {
  136.   v_driver_pointer = load_driver(dfile);
  137.   if (v_driver_pointer == NULL)
  138.    {
  139.       errprintf("Cannot read video driver %s\n", dfile);
  140.       exit(0);
  141.     }
  142.   screeninfo = screen_data();
  143. }
  144.  
  145.  
  146. /************ OPTIONS SCAN **********/
  147.  
  148. static int file_arg_index[10] = {0,0,0,0,0,0,0,0,0,0};
  149.  
  150.     // get cmd line options, loads cfg file
  151.     // also marks files on cmd line for loading
  152. void read_configuration(int argc, char *argv[])
  153. {
  154.   int i;
  155.   int fp = 0;
  156.   FILE *in;
  157.   char *fname;
  158.   char *config_file_name = "vr386.cfg";
  159.  
  160.   if(argc>0)    // any args?
  161.     {
  162.       for (i=1; i < argc; i++)
  163.     {
  164.       if (argv[i][0] == '/' || argv[i][0] == '-')
  165.         {
  166.           switch(toupper(argv[i][1]))
  167.         {
  168.           case 'L':    /* log file */
  169.             {
  170.               char *lf;
  171.               if (argv[i][2]) lf = &argv[i][2];
  172.               else lf = argv[++i];
  173.               log_file = fopen(lf, "w");
  174.               if (log_file == NULL)
  175.             {
  176.               errprintf("Could not open log file '%s'\n", lf);
  177.               exit(2);
  178.             }
  179.             } break;
  180.  
  181.           case 'M':    /* disable MS mouse */
  182.             mdname[0] = 0;
  183.             break;
  184.  
  185.           case 'R':/* swap eyes on Sega driver */
  186.             swap_eyes = 1;
  187.             break;
  188.  
  189.           case 'E':    /* set eye spacing OR EMM on */
  190.             if(toupper(argv[i][2])=='M')
  191.               {
  192.             if(isdigit(argv[i+1][0]))
  193.             emmrequest = atol(argv[++i]);
  194.             else emmrequest = 4000;
  195.             atexit(resetEMMalloc);
  196.             if(!initEMMalloc(emmrequest>>4))
  197.                emmrequest = 0;    // clear flag if failure
  198.             break;
  199.               }
  200.             break;
  201.  
  202.           case 'X':/* turn on stereo */
  203.             stereo_type = SWITCHED;
  204.             break;
  205.  
  206.           case '1':/* com 1 for SEGA */
  207.             select_sega_port(0x3FC);
  208.             break;
  209.  
  210.           case '2':/* com 2 for SEGA */
  211.             select_sega_port(0x2FC);
  212.             break;
  213.  
  214.           case 'G':/* enable glove */
  215.             use_glove = 1;
  216.             if (!have_ptr) have_glove = 1;
  217.             break;
  218.  
  219.           case 'H':/* enable head tracker */
  220.             use_ht = 1;
  221.             break;
  222.  
  223.           case 'B':/* force all colors to monochrome */
  224.             use_BW = 1;
  225.             break;
  226.  
  227.           case 'K':/* turn on key monitor for joy-keys */
  228.             use_keyjoy++;
  229.             init_key_monitor();
  230.             break;
  231.  
  232.           case 'C':    // config file name
  233.             config_file_name = &(argv[++i][0]);
  234.             break;
  235.         }
  236.         }
  237.       else file_arg_index[fp++] = i;    // record for later loading
  238.     }
  239.     }
  240.  
  241.   if ((in = fopen(config_file_name, "r")) == NULL)
  242.     errprintf("Note -- config file '%s' not found; using defaults.\n", config_file_name);
  243.   else
  244.     {
  245.       read_config_file(in);
  246.       fclose(in);
  247.     }
  248. }
  249.  
  250.  
  251. void read_input_files(int argc, char *argv[])
  252. {
  253.   int i;
  254.   FILE *in;
  255.   char *fname;
  256.   OBJECT *obj;
  257.  
  258.   i = 0;
  259.   while(file_arg_index[i])
  260.     {
  261.       fname = fix_fname(argv[file_arg_index[i]]);
  262.       if ((in = fopen(fname, "r")) == NULL)
  263.      errprintf("Could not open '%s'\n", fname);
  264.       else if (strstr(fname,".plg")) /* check if plg or fig file */
  265.     {
  266.       POSE p = ZERO_POSE;
  267.  
  268.       while ((obj = load_plg_object(in, &p, 1, 1, 1, 0)) != NULL)
  269.         {
  270.           if (make_fixed_object_moveable(obj,NULL) == NULL)
  271.          errprintf("Warning: out of memory while loading an object\n");
  272.           else
  273.         {
  274.           update_object(obj);    // adds to world if not in it
  275.           add_object_to_world(obj);
  276.         }
  277.         }
  278.       if (plg_error(NULL))
  279.         {
  280.           errprintf("%s in file %s\n", plg_error(NULL), fname);
  281.           getkey();
  282.         }
  283.     }
  284.       else if (strstr(fname,".fig")) /* check if plg or fig file */
  285.     {
  286.       obj = load_figure_as_object(in, default_objlist, NULL, 0, 1, 1, 1);
  287.       if (obj)
  288.         {
  289.           add_objlist_to_world(default_objlist);
  290.           update_object(obj);
  291.          }
  292.       if (seg_error(NULL))
  293.        {
  294.          errprintf("%s in file %s\n", seg_error(NULL), fname);
  295.        }
  296.     }       // NO READ WORLD FOR MINI DEMO
  297.      fclose(in);
  298.      i++;
  299.    }
  300. }
  301.  
  302.  
  303. /********** CLOSE PROGRAM ***********/
  304.  
  305. void exit_handler(void) /* end program */
  306. {
  307.   int i;
  308.  
  309.   if (in_graphics) exit_graphics();
  310.   in_graphics = 0;
  311.   reset_render();
  312.   if (log_file) fclose(log_file);
  313. }
  314.  
  315.  
  316. /************** MAIN PROGRAM ***************/
  317.  
  318. extern void move_rep(OBJECT *o);   // the default renderer object update handler
  319.  
  320. void preload_initialize(int argc, char *argv[])
  321. {
  322.   originalmem = farcoreleft();        // initialize RENDERER
  323.   temp_mem = setup_render(63,1500);      /* # of K, # of polys */
  324.   temp_size = 63000L;
  325.  
  326.   set_renderer_update_handler(move_rep);  // MOTION HANDLERS
  327.   set_move_handler(split_move_handler);
  328.  
  329.   read_configuration(argc, argv);       // configure system
  330.  
  331.         /* preload video driver to set defaults */
  332. #ifdef LOAD_VIDEO_DRIVER
  333.   load_video_driver(vdname);
  334. #endif
  335.   aftervideomem = farcoreleft();
  336.  
  337.   screeninfo = screen_data();        // pre-world-load setup data
  338.   preset_default_colors();              // set up colors for
  339.  
  340.   create_default_world();
  341.  
  342.   init_body_links();            // create cameras, body
  343. }
  344.  
  345.  
  346.  
  347. void load_memory_report()
  348. {
  349.   afterloadmem = farcoreleft();     // memory report
  350.   if(emmrequest)
  351.     {
  352.       extern long alloccount;
  353.       fprintf(stderr,"EMM used: %ld in %ld allocs,  EMM available: %ld  \n",
  354.                 EMMheapused(), alloccount, EMMheapsize() );
  355.     }
  356.  
  357.   fprintf(stderr,"DOS memory:\n\t Renderer = %ld,\n\t World = %ld,\n\t Total = %ld;  %ld free\n",
  358.             originalmem - aftervideomem,
  359.             aftervideomem - afterloadmem,
  360.             originalmem - afterloadmem , afterloadmem   );
  361. }
  362.  
  363.  
  364. void video_initialize()
  365. {
  366.   enter_graphics(vdmode, use_BW);    // GRAPHICS INIT
  367.   in_graphics++;
  368.   compute_camera_factors(default_camera);
  369.   if (npalette)
  370.     load_DAC_colors(palette, screeninfo->colors, use_BW,0);
  371.   reset_screens();
  372. }
  373.  
  374.  
  375. void device_initialize()
  376. {
  377.   PDRIVER *dm;
  378.  
  379.             // system timer startup
  380.   init_timer(0,NULL);    // uses default or driver speeds
  381.  
  382.  
  383.             // alternate-frame stero driver start
  384.             // may take over timer
  385.   if(stereo_type==SWITCHED) init_switch_driver(swdname);
  386.  
  387.   add_joy_device("joystick");       // navigation devices
  388.   add_joy_device("keys");
  389.   add_joy_device("mjoy");
  390.   if(use_keyjoy)
  391.      add_joy_device("keymon");
  392.                      // head tracker startup
  393.   if (stricmp(hdname,"none") && use_ht)
  394.       if(!init_head_device(hdname, &hd_offset)) use_ht = 0;
  395.  
  396.   if ((dm=mouseptr_init(mdname))!=NULL)  // mouse/cursor initialize
  397.     {
  398.       cursor_enable(TRUE);    // cursor available
  399.       cursor_show();
  400.       manip_2D_avail++;         // and can be used for manipulation
  401.     }
  402.   cursor_device = dm;        // mouse for object selection
  403.   menu_device = dm;             // mouse for menu interface
  404.  
  405. // NO GLOVE FOR MINI DEMO
  406. }
  407.  
  408.