]> git.mxchange.org Git - flightgear.git/blobdiff - src/Objects/matlib.cxx
Sync with latest JSBSim CVS
[flightgear.git] / src / Objects / matlib.cxx
index 6bac2370b0da27da6ffef3bfe741b313ed7ee940..9d110bd692d762b0fc86cfec8882faba095b882d 100644 (file)
@@ -61,6 +61,7 @@ FGMaterialLib material_lib;
 
 // Constructor
 FGMaterialLib::FGMaterialLib ( void ) {
+  set_step(0);
 }
 
 
@@ -71,18 +72,6 @@ static bool local_file_exists( const string& path ) {
     } else {
        return true;
     }
-
-#if 0
-    cout << path << " ";
-    FILE *fp = fopen( path.c_str(), "r" );
-    if ( fp == NULL ) {
-       cout << " doesn't exist\n";
-       return false;
-    } else {
-       cout << " exists\n";
-       return true;
-    }
-#endif
 }
 
 
@@ -122,6 +111,7 @@ bool FGMaterialLib::load( const string& mpath ) {
            FGNewMat *m = matlib[src_mat];
             if ( m != NULL ) {
                 matlib[dst_mat] = m;
+               m->ref();
             } else {
                 SG_LOG( SG_GENERAL, SG_ALERT,
                         "Bad material alias pointing to nonexistant material" );
@@ -130,8 +120,15 @@ bool FGMaterialLib::load( const string& mpath ) {
            in >> token;
 
            if ( token == '{' ) {
+                // Read the data into a temporary but stack allocated
+                // copy of the structure
+                FGNewMat tmp;
+               in >> tmp;
+
+                // create a pointer to a heap allocated copy of the structure
                FGNewMat *m = new FGNewMat;
-               in >> *m;
+                *m = tmp;
+               m->ref();
 
                // build the ssgSimpleState
                SGPath tex_path( globals->get_fg_root() );
@@ -267,6 +264,14 @@ FGNewMat *FGMaterialLib::find( const string& material ) {
 
 // Destructor
 FGMaterialLib::~FGMaterialLib ( void ) {
+    // Free up all the material entries first
+    for ( material_map_iterator it = begin(); it != end(); it++ ) {
+       FGNewMat *slot = it->second;
+       slot->deRef();
+       if ( slot->getRef() <= 0 ) {
+            delete slot;
+        }
+    }
 }
 
 
@@ -284,6 +289,14 @@ void FGMaterialLib::set_step ( int step )
 }
 
 
+// Get the step for the state selectors
+int FGMaterialLib::get_step ()
+{
+  material_map_iterator it = begin();
+  return it->second->get_state()->getSelectStep();
+}
+
+
 // Load one pending "deferred" texture.  Return true if a texture
 // loaded successfully, false if no pending, or error.
 void FGMaterialLib::load_next_deferred() {