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

  1. // WORLD management (just a beginning)
  2. // 7/1/94 by Dave Stampe
  3.  
  4.  
  5. /*
  6.  This code is part of the VR-386 project, created by Dave Stampe.
  7.  VR-386 is a desendent of REND386, created by Dave Stampe and
  8.  Bernie Roehl.  Almost all the code has been rewritten by Dave
  9.  Stampre for VR-386.
  10.  
  11.  Copyright (c) 1994 by Dave Stampe:
  12.  May be freely used to write software for release into the public domain
  13.  or for educational use; all commercial endeavours MUST contact Dave Stampe
  14.  (dstampe@psych.toronto.edu) for permission to incorporate any part of
  15.  this software or source code into their products!  Usually there is no
  16.  charge for under 50-100 items for low-cost or shareware products, and terms
  17.  are reasonable.  Any royalties are used for development, so equipment is
  18.  often acceptable payment.
  19.  
  20.  ATTRIBUTION:  If you use any part of this source code or the libraries
  21.  in your projects, you must give attribution to VR-386 and Dave Stampe,
  22.  and any other authors in your documentation, source code, and at startup
  23.  of your program.  Let's keep the freeware ball rolling!
  24.  
  25.  DEVELOPMENT: VR-386 is a effort to develop the process started by
  26.  REND386, improving programmer access by rewriting the code and supplying
  27.  a standard API.  If you write improvements, add new functions rather
  28.  than rewriting current functions.  This will make it possible to
  29.  include you improved code in the next API release.  YOU can help advance
  30.  VR-386.  Comments on the API are welcome.
  31.  
  32.  CONTACT: dstampe@psych.toronto.edu
  33. */
  34.  
  35.  
  36.  
  37. #include <stdio.h>
  38. #include <stdlib.h>  /* for atol(), only for debugging! */
  39. #include <dos.h>
  40. #include <string.h>
  41. #include <alloc.h>
  42.  
  43. #include "vr_api.h"
  44. #include "intmath.h"
  45. #include "splits.h"
  46. #include "segment.h"
  47.  
  48.  
  49.  
  50. ////////////////////////////////////////////////////
  51. /// CURRENT WORLD STATE VARIABLES
  52. /// accessed by many functions
  53.  
  54. WORLD *default_world = NULL;
  55. WORLD *current_world = NULL;
  56.  
  57. SPLIT *global_world_root = NULL;    // the root of visibility tree
  58. SPLIT *current_split = NULL;        // used to add objects to splits
  59.  
  60. OBJLIST *default_objlist = NULL;       // used for loading objects
  61. OBJLIST *inactive_object_list = NULL;  // all objects not currently in world
  62.  
  63. CAMERA *default_camera = NULL;      // the "basis" camara for world
  64. CAMERA *current_camera = NULL;   // the current camera used for rendering
  65.  
  66. POSE initial_body_pose = {0,0,-8000,0,0,0}; // start position
  67. POSE *body_pose = &initial_body_pose;       // current "teleport" used
  68. POSE *home_pose = &initial_body_pose;        // current "teleport <home>
  69.  
  70. extern OBJECT *body_vehicle_object;
  71.  
  72.  
  73. ////////////////////////////////////////////////////
  74. /// WORLD MANAGEMENT:
  75. /// eventually will allow suspension, resumption, freeing of memory
  76. /// also loading (some)
  77.  
  78.  
  79. //typedef struct {                          // more later
  80. //        CAMERA  *current_camera;
  81. //        POSE    initial_body_pose;
  82. //        POSE    *body_pose;
  83. //        POSE    *home_pose;
  84. //        SPLIT   *world_root;
  85. //        OBJLIST *inactive_objects;
  86. //           } WORLD;
  87. //
  88.  
  89. void save_world_state(WORLD *w)
  90. {
  91.   w->home_pose = home_pose;
  92.   w->body_pose = body_pose;
  93.   w->initial_body_pose = *body_pose;
  94.   w->world_root = global_world_root;
  95.   w->inactive_objects = inactive_object_list;
  96. }
  97.  
  98. void restore_world_state(WORLD *w)
  99. {
  100. //  home_pose = w->home_pose;
  101. //  body_pose = w->body_pose;
  102. //  *body_pose = w->initial_body_pose;
  103.   global_world_root = w->world_root;
  104.   inactive_object_list = w->inactive_objects;
  105.   current_world = w;
  106. }
  107.  
  108.  
  109. WORLD *create_world(WORLD * template)  // arg not yet used
  110. {
  111.   WORLD *w = malloc(sizeof(WORLD));
  112.   if(!w) return NULL;
  113.   current_world = w;
  114.   save_world_state(w);
  115. }
  116.  
  117. void release_world(WORLD *w)   // NOT YET FUNCTIONAL
  118. {
  119. //  delete_objlist(w->inactive_objects);
  120. //  do_for_all_objlists(w->world_root, delete_objlist);
  121.   free(w);
  122.   current_world = default_world;
  123. }
  124.  
  125.  
  126. void create_default_world()
  127. {
  128.   if(!default_objlist) default_objlist = new_objlist();
  129.   inactive_object_list = new_objlist();
  130.   if(!default_camera)
  131.       default_camera = create_camera(stereo_type, NULL); // create BASIS camera
  132.   current_camera = default_camera;
  133.   global_world_root = create_initial_world_split();
  134.  
  135.   current_world = create_world(NULL);
  136. }
  137.  
  138.  
  139. void load_world(FILE *in, WORLD *w)
  140. {
  141. }  // use load_wld_file for now
  142.  
  143.  
  144. /////// WORLD OBJECT SUPPORT
  145.  
  146.  
  147. void add_object_to_world(OBJECT *obj)
  148. {
  149.   mark_object_visible(obj);    // allow to place in world
  150.   if (current_split)
  151.     add_obj_to_split_center(current_split, obj);
  152.   else
  153.     add_obj_to_split_area(global_world_root, obj);
  154. }
  155.  
  156.  
  157. void add_objlist_to_world(OBJLIST *olist)
  158. {
  159.   OBJECT *obj;
  160.  
  161.   obj = first_in_objlist(olist);
  162.   add_object_to_world(obj);          // auto. remove from objlist
  163.   while((obj=first_in_objlist(olist))!=NULL)  // cause it's removed!
  164.      add_object_to_world(obj);      // auto. remove from objlist
  165. }
  166.  
  167. void remove_object_from_world(OBJECT *obj)
  168. {
  169.   mark_object_invisible(obj);
  170.   add_to_objlist(inactive_object_list, obj);
  171. }
  172.  
  173.  
  174. BOOL add_split_to_world(COORD x, COORD y, COORD z,
  175.             COORD nx, COORD ny, COORD nz, WORD flags)
  176. {
  177.   current_split = add_split(&global_world_root,x,y,z,nx,ny,nz,flags);
  178. }
  179.  
  180.  
  181. BOOL end_of_world_splits()
  182. {
  183.   current_split = NULL;
  184. }
  185.  
  186.  
  187. ///////////// TELEPORTS
  188.  
  189. //typedef struct {
  190. //        POSE current_pose;
  191. //        OBJECT *vehicle;
  192. //        char * vehicle_name;    // in case reloading needed
  193. //        WORLD *world;
  194. //           } TELEPORT;
  195.  
  196.  
  197. TELEPORT *create_teleport();
  198. void delete_teleport(TELEPORT *t);
  199. void teleport_set_here(TELEPORT *t, POSE *p);
  200. void teleport_set_vehicle(TELEPORT *t, OBJECT *vehicle, char *vname);
  201. void teleport_to(TELEPORT *t);
  202.  
  203.  
  204.  
  205. TELEPORT *create_teleport()
  206. {
  207.   TELEPORT *t = calloc(sizeof(TELEPORT),1);
  208.   if(!t) return NULL;
  209.   t->current_pose = *body_pose;
  210.   t->world = current_world;
  211.   t->vehicle = body_vehicle_object;
  212.   t->vehicle_name = NULL;
  213.   return t;
  214. }
  215.  
  216. void delete_teleport(TELEPORT *t)
  217. {
  218.   if(t->vehicle_name) free(t->vehicle_name);
  219.   free(t);
  220. }
  221.  
  222. void teleport_set_here(TELEPORT *t, POSE *p)
  223. {
  224.   t->current_pose = *p;
  225. }
  226.  
  227. void teleport_set_vehicle(TELEPORT *t, OBJECT *vehicle, char *vname)
  228. {
  229.   t->vehicle = vehicle;
  230.   if(vname) t->vehicle_name = strdup(vname);
  231. }
  232.  
  233. void teleport_to(TELEPORT *t)
  234. {
  235. // if(world != current_world) ....    NOT YET IMPLEMENTED
  236.  
  237.   if(t->vehicle) connect_body(t->vehicle);
  238.   else disconnect_body();
  239.   *body_pose = t->current_pose;
  240. }
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.