Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

base/Universe.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002   Copyright (C)1996 David Jung <opensim@pobox.com>
00003 
00004   This program/file is free software; you can redistribute it and/or modify
00005   it under the terms of the GNU General Public License as published by
00006   the Free Software Foundation; either version 2 of the License, or
00007   (at your option) any later version.
00008   
00009   This program is distributed in the hope that it will be useful,
00010   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012   GNU General Public License for more details. (http://www.gnu.org)
00013   
00014   You should have received a copy of the GNU General Public License
00015   along with this program; if not, write to the Free Software
00016   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017   
00018   $Id: Universe.cpp 1029 2004-02-11 20:45:54Z jungd $
00019   $Revision: 1.4 $
00020   $Date: 2004-02-11 15:45:54 -0500 (Wed, 11 Feb 2004) $
00021   $Author: jungd $
00022  
00023 ****************************************************************************/
00024 
00025 #include <base/Universe>
00026 
00027 #include <base/World>
00028 #include <base/PathName>
00029 #include <functional>
00030 #include <string>
00031 
00032 
00033 using base::Universe;
00034 using base::Simulatable;
00035 using base::PathName;
00036 using base::World;
00037 
00038 
00039 
00040 
00041 Universe::Universe(ref<VFileSystem> fs, const String& resourceDirectoryName, const String& cacheDirectoryName)
00042   : vfilesystem(fs), renderWorldsDuringSim(true)
00043 {
00044   masterCache = ref<Cache>( NewNamedObj("masterCache") Cache(fs,PathName(resourceDirectoryName), PathName(cacheDirectoryName)) );
00045   preSimulate();
00046 }
00047 
00048 
00049 
00050 Universe::~Universe()
00051 {
00052 }
00053 
00054 
00055 
00056 void Universe::renderWorlds()
00057 {
00058   // renderFrame() all worlds
00059   reflist<World>::iterator w = worlds.begin();
00060   reflist<World>::iterator e = worlds.end();
00061   while (w != e) {
00062     (*w)->renderFrame();
00063     ++w;
00064   }
00065 
00066   frameCount++;
00067 }
00068 
00069 
00070 void Universe::preSimulate()
00071 {
00072   startRealTime = Time::now();
00073   frameCount=0;
00074 
00075   //  call all simulatables
00076   reflist<Simulatable>::iterator b = simulatables.begin();
00077   reflist<Simulatable>::iterator e = simulatables.end();
00078   while (b != e) {
00079     (*b)->preSimulate();
00080     ++b;
00081   }
00082 
00083 }
00084 
00085 
00086 void Universe::simulateForSimTime(const Time& dt)
00087 {
00088   // simulate all simulatables
00089   reflist<Simulatable>::iterator current = simulatables.begin();
00090   reflist<Simulatable>::iterator end = simulatables.end();
00091   while (current != end) {
00092     (*current)->simulateForSimTime(dt);
00093     ++current;
00094   }
00095   
00096   simTime += dt;
00097   
00098   // Render the worlds?
00099   if (renderWorldsDuringSim) {
00100     Time elapsed(Time::now() - startRealTime);
00101     if ( elapsed > frameCount*Real(1.0/maxDisplayFrameRate))  { // never render faster than maxDisplayFrameRate Hz
00102       renderWorlds();
00103     }
00104   }
00105 }
00106 
00107 
00108 void Universe::simulateForRealTime(const Time& dt, Real simTimeStepSize)
00109 {
00110   Time until(Time::now()+dt);
00111   do {
00112     simulateForSimTime(simTimeStepSize);
00113   } while (Time::now() < until);
00114 }
00115 
00116 
00117 void Universe::addWorld(ref<World> world)
00118 {
00119   if (!contains(worlds, world)) {
00120     worlds.push_back(world);
00121   }
00122   else
00123     throw std::invalid_argument(Exception("the world has already been added"));
00124 }
00125 
00126 
00127 base::ref<World> Universe::removeWorld(ref<World> world)
00128 {
00129   worlds.remove(world);
00130   return world;
00131 }
00132 
00133 
00134 void Universe::addSimulatable(ref<Simulatable> simulatable)
00135 {
00136   if (!contains(simulatables, simulatable)) {
00137     simulatables.push_back(simulatable);
00138   }
00139   else
00140     throw std::invalid_argument(Exception("the simulatable has already been added"));
00141 }
00142 
00143 
00144 ref<Simulatable> Universe::removeSimulatable(ref<Simulatable> simulatable)
00145 {
00146   simulatables.remove(simulatable);
00147   return simulatable;
00148 }
00149 
00150 
00151 

Generated on Thu Jul 29 15:56:16 2004 for OpenSim by doxygen 1.3.6