]> git.mxchange.org Git - flightgear.git/blobdiff - src/Time/TimeManager.cxx
Potential fix for bug #471 (crash in checkspeedadjustment).
[flightgear.git] / src / Time / TimeManager.cxx
index 7d4ed5607ff88661a0100f6f5521cb8096e2f25d..52b385e378fd3662f2da9cd3a3f069520e4bdd23 100644 (file)
 #include <simgear/structure/event_mgr.hxx>
 #include <simgear/misc/sg_path.hxx>
 #include <simgear/timing/lowleveltime.h>
+#include <simgear/structure/commands.hxx>
 
 #include <Main/fg_props.hxx>
 #include <Main/globals.hxx>
 #include <Time/sunsolver.hxx>
 
+using std::string;
+
+static bool do_timeofday (const SGPropertyNode * arg)
+{
+    const string &offset_type = arg->getStringValue("timeofday", "noon");
+    int offset = arg->getIntValue("offset", 0);
+    TimeManager* self = (TimeManager*) globals->get_subsystem("time");
+    if (offset_type == "real") {
+    // without this, setting 'real' time is a no-op, since the current
+    // wrap value (orig_warp) is retained in setTimeOffset. Ick.
+        fgSetInt("/sim/time/warp", 0);
+    }
+    
+    self->setTimeOffset(offset_type, offset);
+    return true;
+}
+
 TimeManager::TimeManager() :
   _inited(false),
   _impl(NULL)
 {
-  
+  SGCommandMgr::instance()->addCommand("timeofday", do_timeofday);
 }
 
 void TimeManager::init()
@@ -88,11 +106,11 @@ void TimeManager::init()
                _warp->getIntValue());
   globals->set_time_params(_impl);
     
-  // frame-rate / worst-case delay / update-rate counters
+  // frame-rate / worst-case latency / update-rate counters
   _frameRate = fgGetNode("/sim/frame-rate", true);
-  _frameDelayMax = fgGetNode("/sim/frame-delay-max-ms", true);
+  _frameLatency = fgGetNode("/sim/frame-latency-max-ms", true);
   _lastFrameTime = 0;
-  _wcFrameDelayMax = 0.0;
+  _frameLatencyMax = 0.0;
   _frameCount = 0;
 }
 
@@ -162,8 +180,8 @@ void TimeManager::computeTimeDeltas(double& simDt, double& realDt)
   SGTimeStamp currentStamp;
   currentStamp.stamp();
   double dt = (currentStamp - _lastStamp).toSecs();
-  if (dt > _wcFrameDelayMax)
-      _wcFrameDelayMax = dt;
+  if (dt > _frameLatencyMax)
+      _frameLatencyMax = dt;
 
 // Limit the time we need to spend in simulation loops
 // That means, if the /sim/max-simtime-per-frame value is strictly positive
@@ -255,9 +273,9 @@ void TimeManager::computeFrameRate()
   // Calculate frame rate average
   if ((_impl->get_cur_time() != _lastFrameTime)) {
     _frameRate->setIntValue(_frameCount);
-    _frameDelayMax->setDoubleValue(_wcFrameDelayMax*1000);
+    _frameLatency->setDoubleValue(_frameLatencyMax*1000);
     _frameCount = 0;
-    _wcFrameDelayMax = 0.0;
+    _frameLatencyMax = 0.0;
   }
   
   _lastFrameTime = _impl->get_cur_time();
@@ -345,6 +363,14 @@ void TimeManager::updateLocalTime()
 }
 
 void TimeManager::initTimeOffset()
+{
+
+  int offset = fgGetInt("/sim/startup/time-offset");
+  string offset_type = fgGetString("/sim/startup/time-offset-type");
+  setTimeOffset(offset_type, offset);
+}
+
+void TimeManager::setTimeOffset(const std::string& offset_type, int offset)
 {
   // Handle potential user specified time offsets
   int orig_warp = _warp->getIntValue();
@@ -355,8 +381,6 @@ void TimeManager::initTimeOffset()
       sgTimeGetGMT( fgLocaltime(&cur_time, _impl->get_zonename() ) );
     
   // Okay, we now have several possible scenarios
-  int offset = fgGetInt("/sim/startup/time-offset");
-  string offset_type = fgGetString("/sim/startup/time-offset-type");
   double lon = _longitudeDeg->getDoubleValue() * SG_DEGREES_TO_RADIANS;
   double lat = _latitudeDeg->getDoubleValue() * SG_DEGREES_TO_RADIANS;
   int warp = 0;
@@ -394,12 +418,12 @@ void TimeManager::initTimeOffset()
       warp = offset - (aircraftLocalTime - currGMT)- cur_time; 
   } else {
     SG_LOG( SG_GENERAL, SG_ALERT,
-          "TimeManager::initTimeOffset: unsupported offset: " << offset_type );
+          "TimeManager::setTimeOffset: unsupported offset: " << offset_type );
      warp = 0;
   }
   
   _warp->setIntValue( orig_warp + warp );
 
-  SG_LOG( SG_GENERAL, SG_INFO, "After fgInitTimeOffset(): warp = " 
+  SG_LOG( SG_GENERAL, SG_INFO, "After TimeManager::setTimeOffset(): warp = " 
             << _warp->getIntValue() );
 }