]> git.mxchange.org Git - flightgear.git/blob - src/Scenery/tileentry.cxx
Remove ssg kids in a an attepmpt to fix one memory leak. We think the
[flightgear.git] / src / Scenery / tileentry.cxx
1 // tile.cxx -- routines to handle a scenery tile
2 //
3 // Written by Curtis Olson, started May 1998.
4 //
5 // Copyright (C) 1998, 1999  Curtis L. Olson  - curt@flightgear.org
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22
23
24 #include <Include/compiler.h>
25
26 #ifdef FG_MATH_EXCEPTION_CLASH
27 #  include <math.h>
28 #endif
29
30 #include STL_FUNCTIONAL
31 #include STL_ALGORITHM
32
33 #include <Debug/logstream.hxx>
34 #include <Bucket/newbucket.hxx>
35
36 #include "tileentry.hxx"
37
38 FG_USING_STD(for_each);
39 FG_USING_STD(mem_fun_ref);
40
41
42 // Constructor
43 FGTileEntry::FGTileEntry ( void )
44     : ncount(0),
45       state(Unused),
46       vtlist(NULL),
47       vnlist(NULL),
48       tclist(NULL)
49 {
50     nodes.clear();
51 }
52
53
54 // Destructor
55 FGTileEntry::~FGTileEntry ( void ) {
56     // cout << "nodes = " << nodes.size() << endl;;
57     // delete[] nodes;
58 }
59
60
61 // Step through the fragment list, deleting the display list, then the
62 // fragment, until the list is empty.  Also delete the arrays used by
63 // ssg as well as the whole ssg branch
64 void
65 FGTileEntry::free_tile()
66 {
67     FG_LOG( FG_TERRAIN, FG_DEBUG,
68             "FREEING TILE = (" << tile_bucket << ")" );
69
70     // mark tile unused
71     mark_unused();
72
73     // delete fragment list and node list
74     FG_LOG( FG_TERRAIN, FG_DEBUG,
75             "  deleting " << fragment_list.size() << " fragments" );
76     fragment_list.clear();
77     FG_LOG( FG_TERRAIN, FG_DEBUG,
78             "  deleting " << nodes.size() << " nodes" );
79     nodes.clear();
80
81     // delete the ssg structures
82     FG_LOG( FG_TERRAIN, FG_DEBUG,
83             "  deleting vertex, normal, and texture coordinate arrays" );
84     FG_LOG( FG_TERRAIN, FG_DEBUG,
85             "    deleting vertex array" );
86     if ( vtlist != NULL ) {
87         delete vtlist;
88     }
89     FG_LOG( FG_TERRAIN, FG_DEBUG,
90             "    deleting normal array" );
91     if ( vnlist != NULL ) {
92         delete vnlist;
93     }
94     FG_LOG( FG_TERRAIN, FG_DEBUG,
95             "    deleting texture coordinate array" );
96     if ( tclist != NULL ) {
97         delete tclist;
98     }
99
100     // delete the ssg branch
101
102     // make sure we have a sane number of parents
103     int pcount = select_ptr->getNumParents();
104     if ( pcount > 0 ) {
105         // find the first parent (should only be one)
106         ssgBranch *parent = select_ptr->getParent( 0 ) ;
107         // find the number of kids this parent has
108         int kcount = parent->getNumKids();
109         // find the kid that matches our original select_ptr
110         bool found_kid = false;
111         for ( int i = 0; i < kcount; ++i ) {
112             ssgEntity *kid = parent->getKid( i );
113             if ( kid == select_ptr ) {
114                 FG_LOG( FG_TERRAIN, FG_DEBUG,
115                         "Found a kid to delete " << kid);
116                 found_kid = true;
117                 parent->removeKid( i );
118             }
119         }
120         if ( ! found_kid ) {
121             FG_LOG( FG_TERRAIN, FG_ALERT,
122                     "Couldn't find the kid to delete!  Dying" );
123             exit(-1);
124         }
125     } else {
126         FG_LOG( FG_TERRAIN, FG_ALERT,
127                 "Parent count is zero for an ssg tile!  Dying" );
128         exit(-1);
129     }
130 }
131
132
133 // when a tile is still in the cache, but not in the immediate draw
134 // list, it can still remain in the scene graph, but we use a range
135 // selector to disable it from ever being drawn.
136 void 
137 FGTileEntry::ssg_disable() {
138     // cout << "TILE STATE = " << state << endl;
139     if ( state == Scheduled_for_use ) {
140         state = Scheduled_for_cache;
141     } else if ( state == Scheduled_for_cache ) {
142         // do nothing
143     } else if ( (state == Loaded) || (state == Cached) ) {
144         state = Cached;
145         // cout << "DISABLING SSG NODE" << endl;
146         select_ptr->select(0);
147     } else {
148         FG_LOG( FG_TERRAIN, FG_ALERT,
149                 "Trying to disable an unused tile!  Dying" );
150         exit(-1);
151     }   
152     // cout << "TILE STATE = " << state << endl;
153 }