]> git.mxchange.org Git - flightgear.git/blobdiff - src/Scenery/tileentry.cxx
Continuing work on cleanups.
[flightgear.git] / src / Scenery / tileentry.cxx
index 5633e2f7b65f9da9cbd9e5fbce917f18b51ef8c3..573a5234658aeb6227b6b62fb26c7da1e3c148cc 100644 (file)
@@ -21,7 +21,7 @@
 // $Id$
 
 
-#include <Include/compiler.h>
+#include <simgear/compiler.h>
 
 #ifdef FG_MATH_EXCEPTION_CLASH
 #  include <math.h>
@@ -30,8 +30,8 @@
 #include STL_FUNCTIONAL
 #include STL_ALGORITHM
 
-#include <Debug/logstream.hxx>
-#include <Bucket/newbucket.hxx>
+#include <simgear/bucket/newbucket.hxx>
+#include <simgear/debug/logstream.hxx>
 
 #include "tileentry.hxx"
 
@@ -42,12 +42,10 @@ FG_USING_STD(mem_fun_ref);
 // Constructor
 FGTileEntry::FGTileEntry ( void )
     : ncount(0),
-      state(Unused),
-      vtlist(NULL),
-      vnlist(NULL),
-      tclist(NULL)
+      state(Unused)
 {
     nodes.clear();
+    select_ptr = NULL;
 }
 
 
@@ -58,66 +56,79 @@ FGTileEntry::~FGTileEntry ( void ) {
 }
 
 
-// Step through the fragment list, deleting the display list, then the
-// fragment, until the list is empty.  Also delete the arrays used by
+// recurse an ssg tree and call removeKid() on every node from the
+// bottom up.  Leaves the original branch in existance, but empty so
+// it can be removed by the calling routine.
+static void my_remove_branch( ssgBranch * branch ) {
+    for ( ssgEntity *k = branch->getKid( 0 );
+         k != NULL; 
+         k = branch->getNextKid() )
+    {
+       if ( k -> isAKindOf ( ssgTypeBranch() ) ) {
+           my_remove_branch( (ssgBranch *)k );
+           branch -> removeKid ( k );
+       } else if ( k -> isAKindOf ( ssgTypeLeaf() ) ) {
+           branch -> removeKid ( k ) ;
+       }
+    }
+}
+
+
+// Clean up the memory used by this tile and delete the arrays used by
 // ssg as well as the whole ssg branch
-void
-FGTileEntry::free_tile()
-{
-    FG_LOG( FG_TERRAIN, FG_INFO,
+void FGTileEntry::free_tile() {
+    int i;
+    FG_LOG( FG_TERRAIN, FG_DEBUG,
            "FREEING TILE = (" << tile_bucket << ")" );
 
     // mark tile unused
     mark_unused();
 
-    // delete fragment list
-    FG_LOG( FG_TERRAIN, FG_INFO,
-           "  deleting " << fragment_list.size() << " fragments" );
-    // for_each( begin(), end(),
-    //           mem_fun_ref( &fgFRAGMENT::deleteDisplayList ));
-    fragment_list.erase( begin(), end() );
-
-    // delete the ssg used structures
-    FG_LOG( FG_TERRAIN, FG_INFO,
-           "  deleting vertex, normal, and texture coordinate arrays" );
-    FG_LOG( FG_TERRAIN, FG_INFO,
-           "    deleting vertex array" );
-    if ( vtlist != NULL ) {
-       delete vtlist;
+    FG_LOG( FG_TERRAIN, FG_DEBUG,
+           "  deleting " << nodes.size() << " nodes" );
+    nodes.clear();
+
+    // delete the ssg structures
+    FG_LOG( FG_TERRAIN, FG_DEBUG,
+           "  deleting (leaf data) vertex, normal, and "
+           << " texture coordinate arrays" );
+
+    for ( i = 0; i < (int)vec3_ptrs.size(); ++i ) {
+#ifdef macintosh
+       delete [] vec3_ptrs[i];
+#else
+       delete vec3_ptrs[i];
+#endif
     }
-    FG_LOG( FG_TERRAIN, FG_INFO,
-           "    deleting normal array" );
-    if ( vnlist != NULL ) {
-       delete vnlist;
+    vec3_ptrs.clear();
+
+    for ( i = 0; i < (int)vec2_ptrs.size(); ++i ) {
+#ifdef macintosh
+       delete [] vec2_ptrs[i];
+#else
+       delete vec2_ptrs[i];
+#endif
     }
-    FG_LOG( FG_TERRAIN, FG_INFO,
-           "    deleting texture coordinate array" );
-    if ( tclist != NULL ) {
-       delete tclist;
+    vec2_ptrs.clear();
+
+    for ( i = 0; i < (int)index_ptrs.size(); ++i ) {
+       delete index_ptrs[i];
     }
+    index_ptrs.clear();
 
     // delete the ssg branch
 
-    // make sure we have a sane number of parents
     int pcount = select_ptr->getNumParents();
     if ( pcount > 0 ) {
        // find the first parent (should only be one)
        ssgBranch *parent = select_ptr->getParent( 0 ) ;
-       // find the number of kids this parent has
-       int kcount = parent->getNumKids();
-       // find the kid that matches our original select_ptr
-       bool found_kid = false;
-       for ( int i = 0; i < kcount; ++i ) {
-           ssgEntity *kid = parent->getKid( i );
-           if ( kid == select_ptr ) {
-               FG_LOG( FG_TERRAIN, FG_INFO,
-                       "Found a kid to delete " << kid);
-               found_kid = true;
-           }
-       }
-       if ( ! found_kid ) {
+       if( parent ) {
+           // my_remove_branch( select_ptr );
+           parent->removeKid( select_ptr );
+           select_ptr = NULL;
+       } else {
            FG_LOG( FG_TERRAIN, FG_ALERT,
-                   "Couldn't find the kid to delete!  Dying" );
+                   "parent pointer is NULL!  Dying" );
            exit(-1);
        }
     } else {
@@ -133,19 +144,19 @@ FGTileEntry::free_tile()
 // selector to disable it from ever being drawn.
 void 
 FGTileEntry::ssg_disable() {
-    cout << "TILE STATE = " << state << endl;
+    // cout << "TILE STATE = " << state << endl;
     if ( state == Scheduled_for_use ) {
        state = Scheduled_for_cache;
     } else if ( state == Scheduled_for_cache ) {
        // do nothing
     } else if ( (state == Loaded) || (state == Cached) ) {
        state = Cached;
-       cout << "DISABLING SSG NODE" << endl;
+       // cout << "DISABLING SSG NODE" << endl;
        select_ptr->select(0);
     } else {
        FG_LOG( FG_TERRAIN, FG_ALERT,
                "Trying to disable an unused tile!  Dying" );
        exit(-1);
     }  
-    cout << "TILE STATE = " << state << endl;
+    // cout << "TILE STATE = " << state << endl;
 }