]> git.mxchange.org Git - flightgear.git/blobdiff - src/Scripting/NasalSys.cxx
Performance optimization: empty() instead of size()>0
[flightgear.git] / src / Scripting / NasalSys.cxx
index f7a1cbd38a927923867751a0fee1faa4306d517f..ca7dde87d5ed81a9e408f752bc117ec235dba5a9 100644 (file)
@@ -23,7 +23,6 @@
 #include <simgear/math/sg_random.h>
 #include <simgear/misc/sg_path.hxx>
 #include <simgear/misc/sg_dir.hxx>
-#include <simgear/misc/interpolator.hxx>
 #include <simgear/structure/commands.hxx>
 #include <simgear/math/sg_geodesy.hxx>
 #include <simgear/structure/event_mgr.hxx>
@@ -47,7 +46,6 @@
 #include <Main/util.hxx>
 #include <Main/fg_props.hxx>
 
-
 using std::map;
 
 void postinitNasalGUI(naRef globals, naContext c);
@@ -107,6 +105,8 @@ public:
     _sys->gcRelease(_gcSelf);
   }
   
+  bool isRunning() const { return _isRunning; }
+    
   void stop()
   {
     if (_isRunning) {
@@ -141,6 +141,9 @@ public:
   {
     naRef *args = NULL;
     _sys->callMethod(_func, _self, 0, args, naNil() /* locals */);
+    if (_singleShot) {
+      _isRunning = false;
+    }
   }
   
   void setSingleShot(bool aSingleShot)
@@ -514,29 +517,31 @@ static naRef f_cmdarg(naContext c, naRef me, int argc, naRef* args)
 // value/delta numbers.
 static naRef f_interpolate(naContext c, naRef me, int argc, naRef* args)
 {
-    SGPropertyNode* node;
-    naRef prop = argc > 0 ? args[0] : naNil();
-    if(naIsString(prop)) node = fgGetNode(naStr_data(prop), true);
-    else if(naIsGhost(prop)) node = *(SGPropertyNode_ptr*)naGhost_ptr(prop);
-    else return naNil();
-
-    naRef curve = argc > 1 ? args[1] : naNil();
-    if(!naIsVector(curve)) return naNil();
-    int nPoints = naVec_size(curve) / 2;
-    double* values = new double[nPoints];
-    double* deltas = new double[nPoints];
-    for(int i=0; i<nPoints; i++) {
-        values[i] = naNumValue(naVec_get(curve, 2*i)).num;
-        deltas[i] = naNumValue(naVec_get(curve, 2*i+1)).num;
-    }
+  SGPropertyNode* node;
+  naRef prop = argc > 0 ? args[0] : naNil();
+  if(naIsString(prop)) node = fgGetNode(naStr_data(prop), true);
+  else if(naIsGhost(prop)) node = *(SGPropertyNode_ptr*)naGhost_ptr(prop);
+  else return naNil();
 
-    ((SGInterpolator*)globals->get_subsystem_mgr()
-        ->get_group(SGSubsystemMgr::INIT)->get_subsystem("interpolator"))
-        ->interpolate(node, nPoints, values, deltas);
+  naRef curve = argc > 1 ? args[1] : naNil();
+  if(!naIsVector(curve)) return naNil();
+  int nPoints = naVec_size(curve) / 2;
 
-    delete[] values;
-    delete[] deltas;
-    return naNil();
+  simgear::PropertyList value_nodes;
+  value_nodes.reserve(nPoints);
+  double_list deltas;
+  deltas.reserve(nPoints);
+
+  for( int i = 0; i < nPoints; ++i )
+  {
+    SGPropertyNode* val = new SGPropertyNode;
+    val->setDoubleValue(naNumValue(naVec_get(curve, 2*i)).num);
+    value_nodes.push_back(val);
+    deltas.push_back(naNumValue(naVec_get(curve, 2*i+1)).num);
+  }
+
+  node->interpolate("numeric", value_nodes, deltas, "linear");
+  return naNil();
 }
 
 // This is a better RNG than the one in the default Nasal distribution
@@ -783,7 +788,8 @@ void FGNasalSys::init()
       .method("start", &TimerObj::start)
       .method("stop", &TimerObj::stop)
       .method("restart", &TimerObj::restart)
-      .member("singleShot", &TimerObj::isSingleShot, &TimerObj::setSingleShot);
+      .member("singleShot", &TimerObj::isSingleShot, &TimerObj::setSingleShot)
+      .member("isRunning", &TimerObj::isRunning);
   
     // Now load the various source files in the Nasal directory
     simgear::Dir nasalDir(SGPath(globals->get_fg_root(), "Nasal"));
@@ -816,8 +822,7 @@ naRef FGNasalSys::wrappedPropsNode(SGPropertyNode* aProps)
 {
     static naRef wrapNodeFunc = naNil();
     if (naIsNil(wrapNodeFunc)) {
-        nasal::Hash g(_globals, _context);
-        nasal::Hash props = g.get<nasal::Hash>("props");
+        nasal::Hash props = getGlobals().get<nasal::Hash>("props");
         wrapNodeFunc = props.get("wrapNode");
     }
     
@@ -839,8 +844,11 @@ void FGNasalSys::update(double)
 
     if (!_loadList.empty())
     {
-        // process Nasal load hook (only one per update loop to avoid excessive lags)
-        _loadList.pop()->load();
+        if( _delay_load )
+          _delay_load = false;
+        else
+          // process Nasal load hook (only one per update loop to avoid excessive lags)
+          _loadList.pop()->load();
     }
     else
     if (!_unloadList.empty())
@@ -885,7 +893,7 @@ void FGNasalSys::loadScriptDirectory(simgear::Dir nasalDir)
 // Create module with list of scripts
 void FGNasalSys::addModule(string moduleName, simgear::PathList scripts)
 {
-    if (scripts.size()>0)
+    if (! scripts.empty())
     {
         SGPropertyNode* nasal = globals->get_props()->getNode("nasal");
         SGPropertyNode* module_node = nasal->getChild(moduleName,0,true);
@@ -1235,7 +1243,9 @@ naRef FGNasalSys::removeListener(naContext c, int argc, naRef* args)
 
 void FGNasalSys::registerToLoad(FGNasalModelData *data)
 {
-    _loadList.push(data);
+  if( _loadList.empty() )
+    _delay_load = true;
+  _loadList.push(data);
 }
 
 void FGNasalSys::registerToUnload(FGNasalModelData *data)