#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()
_firstUpdate = true;
_inited = true;
_dtRemainder = 0.0;
+ _adjustWarpOnUnfreeze = false;
_maxDtPerFrame = fgGetNode("/sim/max-simtime-per-frame", true);
_clockFreeze = fgGetNode("/sim/freeze/clock", true);
_timeOverride = fgGetNode("/sim/time/cur-time-override", true);
+ _warp = fgGetNode("/sim/time/warp", true);
+ _warp->addChangeListener(this);
+
+ _warpDelta = fgGetNode("/sim/time/warp-delta", true);
_longitudeDeg = fgGetNode("/position/longitude-deg", true);
_latitudeDeg = fgGetNode("/position/latitude-deg", true);
zone.append("Timezone");
double lon = _longitudeDeg->getDoubleValue() * SG_DEGREES_TO_RADIANS;
double lat = _latitudeDeg->getDoubleValue() * SG_DEGREES_TO_RADIANS;
+
_impl = new SGTime(lon, lat, zone.str(), _timeOverride->getLongValue());
- globals->set_warp_delta(0);
+ _warpDelta->setIntValue(0);
globals->get_event_mgr()->addTask("updateLocalTime", this,
&TimeManager::updateLocalTime, 30*60 );
updateLocalTime();
_impl->update(lon, lat, _timeOverride->getLongValue(),
- globals->get_warp());
+ _warp->getIntValue());
globals->set_time_params(_impl);
-// frame/update-rate counters
+ // frame-rate / worst-case latency / update-rate counters
_frameRate = fgGetNode("/sim/frame-rate", true);
- _lastFrameTime = _impl->get_cur_time();
+ _frameLatency = fgGetNode("/sim/frame-latency-max-ms", true);
+ _lastFrameTime = 0;
+ _frameLatencyMax = 0.0;
_frameCount = 0;
}
void TimeManager::reinit()
{
+ shutdown();
+ init();
+ postinit();
+}
+
+void TimeManager::shutdown()
+{
+ _warp->removeChangeListener(this);
+
globals->set_time_params(NULL);
delete _impl;
+ _impl = NULL;
_inited = false;
globals->get_event_mgr()->removeTask("updateLocalTime");
-
- init();
- postinit();
+}
+
+void TimeManager::valueChanged(SGPropertyNode* aProp)
+{
+ if (aProp == _warp) {
+ if (_clockFreeze->getBoolValue()) {
+ // if the warp is changed manually while frozen, don't modify it when
+ // un-freezing - the user wants to unfreeze with exactly the warp
+ // they specified.
+ _adjustWarpOnUnfreeze = false;
+ }
+
+ double lon = _longitudeDeg->getDoubleValue() * SG_DEGREES_TO_RADIANS;
+ double lat = _latitudeDeg->getDoubleValue() * SG_DEGREES_TO_RADIANS;
+ _impl->update(lon, lat,
+ _timeOverride->getLongValue(),
+ _warp->getIntValue());
+ }
}
void TimeManager::computeTimeDeltas(double& simDt, double& realDt)
if (!wait_for_scenery) {
throttleUpdateRate();
}
+ else
+ {
+ // suppress framerate while initial scenery isn't loaded yet (splash screen still active)
+ _lastFrameTime=0;
+ _frameCount = 0;
+ }
SGTimeStamp currentStamp;
currentStamp.stamp();
double dt = (currentStamp - _lastStamp).toSecs();
-
+ 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
// you can limit the maximum amount of time you will do simulations for
void TimeManager::update(double dt)
{
bool freeze = _clockFreeze->getBoolValue();
+ time_t now = time(NULL);
+
if (freeze) {
// clock freeze requested
if (_timeOverride->getLongValue() == 0) {
- fgSetLong( "/sim/time/cur-time-override", _impl->get_cur_time());
- globals->set_warp(0);
+ _timeOverride->setLongValue(now);
+ _adjustWarpOnUnfreeze = true;
}
} else {
// no clock freeze requested
if (_lastClockFreeze) {
- // clock just unfroze, let's set warp as the difference
- // between frozen time and current time so we don't get a
- // time jump (and corresponding sky object and lighting
- // jump.)
- globals->set_warp(_timeOverride->getLongValue() - time(NULL));
- fgSetLong( "/sim/time/cur-time-override", 0 );
+ if (_adjustWarpOnUnfreeze) {
+ // clock just unfroze, let's set warp as the difference
+ // between frozen time and current time so we don't get a
+ // time jump (and corresponding sky object and lighting
+ // jump.)
+ int adjust = _timeOverride->getLongValue() - now;
+ SG_LOG(SG_GENERAL, SG_INFO, "adjusting on un-freeze:" << adjust);
+ _warp->setIntValue(_warp->getIntValue() + adjust);
+ }
+ _timeOverride->setLongValue(0);
}
- if ( globals->get_warp_delta() != 0 ) {
- globals->inc_warp( globals->get_warp_delta() );
+ int warpDelta = _warpDelta->getIntValue();
+ if (warpDelta != 0) {
+ _warp->setIntValue(_warp->getIntValue() + warpDelta);
}
}
double lat = _latitudeDeg->getDoubleValue() * SG_DEGREES_TO_RADIANS;
_impl->update(lon, lat,
_timeOverride->getLongValue(),
- globals->get_warp());
+ _warp->getIntValue());
computeFrameRate();
}
void TimeManager::computeFrameRate()
{
// Calculate frame rate average
- if ((_impl->get_cur_time() != _lastFrameTime) && (_lastFrameTime > 0)) {
+ if ((_impl->get_cur_time() != _lastFrameTime)) {
_frameRate->setIntValue(_frameCount);
+ _frameLatency->setDoubleValue(_frameLatencyMax*1000);
_frameCount = 0;
+ _frameLatencyMax = 0.0;
}
_lastFrameTime = _impl->get_cur_time();
}
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 = globals->get_warp();
+ int orig_warp = _warp->getIntValue();
time_t cur_time = _impl->get_cur_time();
time_t currGMT = sgTimeGetGMT( gmtime(&cur_time) );
time_t systemLocalTime = sgTimeGetGMT( localtime(&cur_time) );
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;
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;
}
- globals->set_warp( orig_warp + warp );
- _impl->update(lon, lat, _timeOverride->getLongValue(),
- globals->get_warp() );
+ _warp->setIntValue( orig_warp + warp );
- SG_LOG( SG_GENERAL, SG_INFO, "After fgInitTimeOffset(): warp = "
- << globals->get_warp() );
+ SG_LOG( SG_GENERAL, SG_INFO, "After TimeManager::setTimeOffset(): warp = "
+ << _warp->getIntValue() );
}