]> git.mxchange.org Git - flightgear.git/commitdiff
NasalSys.[ch]xx:
authormfranz <mfranz>
Thu, 9 Mar 2006 09:04:03 +0000 (09:04 +0000)
committermfranz <mfranz>
Thu, 9 Mar 2006 09:04:03 +0000 (09:04 +0000)
  implement FGNasalModelData class for execution of XML <load> and <unload>
  scripts. modelLoaded() is called by the model loader, and the destructor
  on branch removal.

modelmgr.cxx:
tilemgr.cxx:
tileentry.[ch]xx:
  make scenery and custom objects run their Nasal scripts on loading
  and unloading. Let OBJECT_STATIC object not be cached.

src/Model/modelmgr.cxx
src/Scenery/tileentry.cxx
src/Scenery/tileentry.hxx
src/Scenery/tilemgr.cxx
src/Scripting/NasalSys.cxx
src/Scripting/NasalSys.hxx

index 50f5e60cc64e48ffa41b39a118686555a3e21a84..82c00f948e6abfd6f1da3c73a4db6c04d92d72eb 100644 (file)
@@ -18,6 +18,7 @@
 
 #include <Main/fg_props.hxx>
 #include <Scenery/scenery.hxx>
+#include <Scripting/NasalSys.hxx>
 
 
 #include "modelmgr.hxx"
@@ -56,7 +57,7 @@ FGModelMgr::init ()
                          node->getStringValue("path",
                                               "Models/Geometry/glider.ac"),
                          globals->get_props(),
-                         globals->get_sim_time_sec() );
+                         globals->get_sim_time_sec(), 0, new FGNasalModelData );
     model->init( object );
 
                                // Set position and orientation either
index 173b1d2b5c76a3c172c9b08adffda9b4a2600671..b9487b0e092fcf365eecf7cbd00d9aa09611fe95 100644 (file)
@@ -876,7 +876,8 @@ FGTileEntry::load( const string_list &path_list, bool is_base )
                 = new FGDeferredModel( custom_path.str(),
                                        obj->path.str(),
                                        tile_bucket,
-                                       this, obj_trans );
+                                       this, obj_trans,
+                                       obj->type == OBJECT_SHARED );
             FGTileMgr::model_ready( dm );
 
 
index 75883f39781e114e39c1183c7705b176a3c8ad6a..fee04866c4758a978c4a21f18edc458c337661f3 100644 (file)
@@ -25,9 +25,9 @@
 #define _TILEENTRY_HXX
 
 
-#ifndef __cplusplus                                                          
+#ifndef __cplusplus
 # error This library requires C++
-#endif                                   
+#endif
 
 #ifdef HAVE_CONFIG_H
 #  include <config.h>
@@ -78,24 +78,27 @@ private:
     FGTileEntry *tile;
     ssgSharedPtr<ssgTransform> obj_trans;
     SGBucket bucket;
+    bool cache_obj;
 
 
 public:
 
     inline FGDeferredModel() { }
     inline FGDeferredModel( const string& mp, const string& tp, SGBucket b,
-                    FGTileEntry *t, ssgTransform *ot )
+                    FGTileEntry *t, ssgTransform *ot, bool co )
     {
        model_path = mp;
        texture_path = tp;
         bucket = b;
        tile = t;
        obj_trans = ot;
+       cache_obj = co;
     }
     inline ~FGDeferredModel() { }
     inline const string& get_model_path() const { return model_path; }
     inline const string& get_texture_path() const { return texture_path; }
     inline const SGBucket& get_bucket() const { return bucket; }
+    inline const bool get_cache_state() const { return cache_obj; }
     inline FGTileEntry *get_tile() const { return tile; }
     inline ssgTransform *get_obj_trans() const { return obj_trans; }
 };
index 83230557cdaf1029d653c1359926173abdfdb36f..ea36df53080e9a94b17789d89636d272c710b06c 100644 (file)
@@ -40,6 +40,7 @@
 #include <Main/globals.hxx>
 #include <Main/fg_props.hxx>
 #include <Main/viewer.hxx>
+#include <Scripting/NasalSys.hxx>
 
 #include "newcache.hxx"
 #include "scenery.hxx"
@@ -316,7 +317,9 @@ void FGTileMgr::update_queues()
                         globals->get_model_lib()->load_model( ".",
                                                   dm->get_model_path(),
                                                   globals->get_props(),
-                                                  globals->get_sim_time_sec() );
+                                                  globals->get_sim_time_sec(),
+                                                  dm->get_cache_state(),
+                                                  new FGNasalModelData );
                     if ( obj_model != NULL ) {
                         dm->get_obj_trans()->addKid( obj_model );
                         shadows->addOccluder( (ssgBranch *) obj_model->getParent(0),
index 2201d9b1d7d7e1ab3c1f332274bd0ca02640ed5a..36a344ef58e1fe868e390bd191f4e1ab574ba372 100644 (file)
@@ -666,3 +666,42 @@ naRef FGNasalSys::removeListener(int argc, naRef* args)
     return naNum(_listener.size());
 }
 
+
+
+// FGNasalModelData class.  If sgLoad3DModel() is called with a pointer to
+// such a class, then the modelLoaded() function is executed by the loader,
+// and the destructor when the model branch is removed from the scene graph.
+// They are used for calling a model's <load> and <unload> scripts.
+
+void FGNasalModelData::modelLoaded(const string& path, SGPropertyNode *prop,
+                                   ssgBranch *)
+{
+    SGPropertyNode *n = prop->getNode("nasal"), *load;
+    if (!n)
+        return;
+
+    load = n->getNode("load");
+    _unload = n->getNode("unload");
+    if (!load && !_unload)
+        return;
+
+    _module = path;
+    const char *s = load ? load->getStringValue() : "";
+    FGNasalSys *nas = (FGNasalSys *)globals->get_subsystem("nasal");
+    nas->createModule(_module.c_str(), _module.c_str(), s, strlen(s));
+}
+
+FGNasalModelData::~FGNasalModelData()
+{
+    if (_module.empty())
+        return;
+
+    FGNasalSys *nas = (FGNasalSys *)globals->get_subsystem("nasal");
+    if (_unload) {
+        const char *s = _unload->getStringValue();
+        nas->createModule(_module.c_str(), _module.c_str(), s, strlen(s));
+    }
+    nas->deleteModule(_module.c_str());
+}
+
+
index 669b26eea68f82f5de0fbd5ae7a31836d46d02bc..c4acd37b73d881aa3b171ab7af7275147369ed9f 100644 (file)
@@ -4,6 +4,9 @@
 #include <simgear/misc/sg_path.hxx>
 #include <simgear/structure/subsystem_mgr.hxx>
 #include <simgear/nasal/nasal.h>
+#include <simgear/scene/model/model.hxx>
+
+#include <Main/globals.hxx>
 
 #include <map>
 SG_USING_STD(map);
@@ -151,4 +154,16 @@ private:
     FGNasalSys* _nas;
 };
 
+
+class FGNasalModelData : public SGModelData {
+public:
+    FGNasalModelData() : _unload(0) {}
+    ~FGNasalModelData();
+    void modelLoaded(const string& path, SGPropertyNode *prop, ssgBranch *);
+
+private:
+    string _module;
+    SGPropertyNode_ptr _unload;
+};
+
 #endif // __NASALSYS_HXX