1
0
Fork 0

Nasal Time objects can use simulated time.

Previously timer objects defaulted to using wall-clock (real) dt which
does not reflect pause/speed-up. Keep this as the default for
compatibility but make it possible to request simulated time.
This commit is contained in:
James Turner 2016-12-11 16:10:57 +00:00
parent 3dea0f73aa
commit 0af316d7fc

View file

@ -97,9 +97,7 @@ public:
_sys(sys),
_func(f),
_self(self),
_isRunning(false),
_interval(interval),
_singleShot(false)
_interval(interval)
{
char nm[128];
snprintf(nm, 128, "nasal-timer-%p", this);
@ -124,7 +122,19 @@ public:
_isRunning = false;
}
}
bool isSimTime() const { return _isSimTime; }
void setSimTime(bool value)
{
if (_isRunning) {
SG_LOG(SG_NASAL, SG_WARN, "can't change type of running timer!");
return;
}
_isSimTime = value;
}
void start()
{
if (_isRunning) {
@ -133,10 +143,11 @@ public:
_isRunning = true;
if (_singleShot) {
globals->get_event_mgr()->addEvent(_name, this, &TimerObj::invoke, _interval);
globals->get_event_mgr()->addEvent(_name, this, &TimerObj::invoke, _interval, _isSimTime);
} else {
globals->get_event_mgr()->addTask(_name, this, &TimerObj::invoke,
_interval, _interval /* delay */);
_interval, _interval /* delay */,
_isSimTime);
}
}
@ -172,14 +183,25 @@ private:
FGNasalSys* _sys;
naRef _func, _self;
int _gcRoot, _gcSelf;
bool _isRunning;
bool _isRunning = false;
double _interval;
bool _singleShot;
bool _singleShot = false;
bool _isSimTime = false;
};
typedef SGSharedPtr<TimerObj> TimerObjRef;
typedef nasal::Ghost<TimerObjRef> NasalTimerObj;
static void f_timerObj_setSimTime(TimerObj& timer, naContext c, naRef value)
{
if (timer.isRunning()) {
naRuntimeError(c, "Timer is running, cannot change type between real/sim time");
return;
}
timer.setSimTime(nasal::from_nasal<bool>(c, value));
}
///////////////////////////////////////////////////////////////////////////
// Read and return file contents in a single buffer. Note use of
@ -884,8 +906,10 @@ void FGNasalSys::init()
.method("stop", &TimerObj::stop)
.method("restart", &TimerObj::restart)
.member("singleShot", &TimerObj::isSingleShot, &TimerObj::setSingleShot)
.member("simulatedTime", &TimerObj::isSimTime, &f_timerObj_setSimTime)
.member("isRunning", &TimerObj::isRunning);
// Set allowed paths for Nasal I/O
fgInitAllowedPaths();