home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / demos / audio / drive / Simulation.c++ < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  3.0 KB  |  120 lines

  1. /*
  2.  * Copyright 1992-1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. //////////////////////////////////////////////////////////////////////
  18. // Simulation.c++ - implementation of the simulation class
  19. //
  20. // This is the main loop.
  21. //////////////////////////////////////////////////////////////////////
  22.  
  23. #include <math.h>
  24. #include "Car.h"
  25. #include "Road.h"
  26. #include "Driver.h"
  27. #include "Sound.h"
  28. #include "Simulation.h"
  29.  
  30. Simulation::Simulation(
  31.     SbString roadfile, SbString carfile, SbString path,
  32.     int num_robots, Boolean quiet, Boolean race)
  33. {
  34.     // initial guest at frames per second, will get updated soon
  35.     _fps = 60.0;
  36.     
  37.     // set up audio
  38.     if (quiet)
  39.         _sound_capable = FALSE;
  40.     else
  41.         _sound_capable = audio_lib_capable();
  42.         
  43.     _play_sound = _sound_capable;
  44.  
  45.     _path = path.getString();
  46.     SbString slash("/");
  47.     _path += slash;
  48.     
  49.     SbString pathroad(_path.getString());
  50.     pathroad += roadfile;
  51.     _road = new Road(pathroad);
  52.  
  53.     // create list of cars in this simulation
  54.     _car_list = new SbPList();
  55.     
  56.     // create the local car, always at position 0 in car list
  57.     SbString pathcar(_path.getString());
  58.     pathcar += carfile;
  59.     _car_list->append(new Car(this, pathcar, _road, LOCAL_CAR));
  60.  
  61.     // add some robot cars
  62.     for (int i = 0; i < num_robots; i++)
  63.         _car_list->append(new Car(this, pathcar, _road, ROBOT_CAR));
  64.  
  65.     // if racing, add other car
  66.     if (race)
  67.         printf("Yup. We're racing!\n");
  68. /*    
  69.     {
  70.         _network = new Network();
  71.         SbPlist other_cars = _network->getOthers();
  72.  
  73.         for (int i = 0; i < other_cars.length(); i++)
  74.             _car_list->append(other_cars[i]);
  75.     }
  76.     else
  77.         _network = NULL;
  78. */
  79.  
  80. }
  81.  
  82.  
  83. Simulation::~Simulation()
  84. {
  85.     delete _road;
  86.     
  87.     // work-around for c++ delete bug
  88.     for (int i = 0; i < _car_list->length(); i++)
  89.         delete (Car *)((* _car_list)[i]);
  90.  
  91.     delete _car_list;
  92. }
  93.  
  94.  
  95. void Simulation::go()
  96. {
  97.     Boolean done = FALSE;
  98.     
  99.     while (! done)
  100.     {
  101.         int num_cars = _car_list->length();
  102.  
  103.         // it is ok for the process to go sleep if we are the only
  104.         // car, or the driver is looking at the help screen.
  105.         Boolean ok_to_sleep = (num_cars == 1) ||
  106.             ((Car *)(* _car_list)[0])->get_driver()->helping();        
  107.         
  108.         for (int i = 0; i < num_cars ; i++)
  109.             // OK for update process to sleep if there is only one car
  110.             ((Car *)(* _car_list)[i])->update(ok_to_sleep);
  111.  
  112.         // frames per second seen by the local driver
  113.         _fps = ((Car *)(* _car_list)[0])->get_driver()->get_fps();
  114.         
  115.         // done if the local car is done
  116.         done = ((Car *)(* _car_list)[0])->done();
  117.     }
  118. }
  119.  
  120.