]> git.mxchange.org Git - simgear.git/blobdiff - simgear/scene/model/modellib.cxx
- better error message when submodel loading failed
[simgear.git] / simgear / scene / model / modellib.cxx
index 3d3edd1bd1f483695ec9f839d9314f6bd32c1cd9..bd28d43d23eed91899a3bf90b8e388f587f46188 100644 (file)
@@ -1,9 +1,15 @@
 // modellib.cxx - implement an SSG model library.
 
+#ifdef HAVE_CONFIG_H
+#  include <simgear_config.h>
+#endif
+
 #include <simgear/compiler.h>
 #include <simgear/props/props.hxx>
 
 #include "model.hxx"
+#include "animation.hxx"
+#include "personality.hxx"
 
 #include "modellib.hxx"
 
@@ -19,11 +25,6 @@ SGModelLib::SGModelLib ()
 
 SGModelLib::~SGModelLib ()
 {
-    map<string, ssgBase *>::iterator it = _table.begin();
-    while (it != _table.end()) {
-        it->second->deRef();
-        _table.erase(it);
-    }
 }
 
 void
@@ -47,38 +48,61 @@ SGModelLib::flush1()
 
     return;
 
-    map<string, ssgBase *>::iterator it = _table.begin();
+    map<string, ssgSharedPtr<ssgEntity> >::iterator it = _table.begin();
     while (it != _table.end()) {
-        ssgBase *item = it->second;
                                 // If there is only one reference, it's
                                 // ours; no one else is using the item.
-        if (item->getRef() == 1) {
-            item->deRef();
+        if (!it->second.isShared()) {
+            string key = it->first;
             _table.erase(it);
-        }
-        it++;
+            it = _table.upper_bound(key);
+        } else
+            it++;
     }
 }
 
+static int
+personality_pretrav_callback(ssgEntity * entity, int mask)
+{
+    ((SGPersonalityBranch *)entity)->_old_current = SGAnimation::current_object;
+    SGAnimation::current_object = (SGPersonalityBranch *)entity;
+    return 1;
+}
+
+static int
+personality_posttrav_callback(ssgEntity * entity, int mask)
+{
+    SGAnimation::current_object = ((SGPersonalityBranch *)entity)->_old_current;
+    ((SGPersonalityBranch *)entity)->_old_current = 0;
+    return 1;
+}
 
 ssgEntity *
 SGModelLib::load_model( const string &fg_root,
                            const string &path,
                            SGPropertyNode *prop_root,
-                           double sim_time_sec )
+                           double sim_time_sec,
+                           bool cache_object,
+                           SGModelData *data )
 {
+    ssgBranch *personality_branch = new SGPersonalityBranch;
+    personality_branch->setTravCallback(SSG_CALLBACK_PRETRAV, personality_pretrav_callback);
+    personality_branch->setTravCallback(SSG_CALLBACK_POSTTRAV, personality_posttrav_callback);
+
                                 // FIXME: normalize path to
                                 // avoid duplicates.
-    map<string, ssgBase *>::iterator it = _table.find(path);
-    if (it == _table.end()) {
-        ssgEntity *model = sgLoad3DModel( fg_root, path, prop_root,
-                                          sim_time_sec );
-        model->ref();
-        _table[path] = model;      // add one reference to keep it around
-        return model;
+    map<string, ssgSharedPtr<ssgEntity> >::iterator it = _table.find(path);
+    if (!cache_object || it == _table.end()) {
+        ssgSharedPtr<ssgEntity> model = sgLoad3DModel(fg_root, path, prop_root,
+                                                      sim_time_sec, 0, data );
+        if (cache_object)
+            _table[path] = model;      // add one reference to keep it around
+
+        personality_branch->addKid( model );
     } else {
-        return (ssgEntity *)it->second;
+        personality_branch->addKid( it->second );
     }
+    return personality_branch;
 }