]> git.mxchange.org Git - flightgear.git/blob - src/Scenery/tileentry.cxx
Continuing work on cleanups.
[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 <simgear/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 <simgear/bucket/newbucket.hxx>
34 #include <simgear/debug/logstream.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 {
47     nodes.clear();
48     select_ptr = NULL;
49 }
50
51
52 // Destructor
53 FGTileEntry::~FGTileEntry ( void ) {
54     // cout << "nodes = " << nodes.size() << endl;;
55     // delete[] nodes;
56 }
57
58
59 // recurse an ssg tree and call removeKid() on every node from the
60 // bottom up.  Leaves the original branch in existance, but empty so
61 // it can be removed by the calling routine.
62 static void my_remove_branch( ssgBranch * branch ) {
63     for ( ssgEntity *k = branch->getKid( 0 );
64           k != NULL; 
65           k = branch->getNextKid() )
66     {
67         if ( k -> isAKindOf ( ssgTypeBranch() ) ) {
68             my_remove_branch( (ssgBranch *)k );
69             branch -> removeKid ( k );
70         } else if ( k -> isAKindOf ( ssgTypeLeaf() ) ) {
71             branch -> removeKid ( k ) ;
72         }
73     }
74 }
75
76
77 // Clean up the memory used by this tile and delete the arrays used by
78 // ssg as well as the whole ssg branch
79 void FGTileEntry::free_tile() {
80     int i;
81     FG_LOG( FG_TERRAIN, FG_DEBUG,
82             "FREEING TILE = (" << tile_bucket << ")" );
83
84     // mark tile unused
85     mark_unused();
86
87     FG_LOG( FG_TERRAIN, FG_DEBUG,
88             "  deleting " << nodes.size() << " nodes" );
89     nodes.clear();
90
91     // delete the ssg structures
92     FG_LOG( FG_TERRAIN, FG_DEBUG,
93             "  deleting (leaf data) vertex, normal, and "
94             << " texture coordinate arrays" );
95
96     for ( i = 0; i < (int)vec3_ptrs.size(); ++i ) {
97 #ifdef macintosh
98         delete [] vec3_ptrs[i];
99 #else
100         delete vec3_ptrs[i];
101 #endif
102     }
103     vec3_ptrs.clear();
104
105     for ( i = 0; i < (int)vec2_ptrs.size(); ++i ) {
106 #ifdef macintosh
107         delete [] vec2_ptrs[i];
108 #else
109         delete vec2_ptrs[i];
110 #endif
111     }
112     vec2_ptrs.clear();
113
114     for ( i = 0; i < (int)index_ptrs.size(); ++i ) {
115         delete index_ptrs[i];
116     }
117     index_ptrs.clear();
118
119     // delete the ssg branch
120
121     int pcount = select_ptr->getNumParents();
122     if ( pcount > 0 ) {
123         // find the first parent (should only be one)
124         ssgBranch *parent = select_ptr->getParent( 0 ) ;
125         if( parent ) {
126             // my_remove_branch( select_ptr );
127             parent->removeKid( select_ptr );
128             select_ptr = NULL;
129         } else {
130             FG_LOG( FG_TERRAIN, FG_ALERT,
131                     "parent pointer is NULL!  Dying" );
132             exit(-1);
133         }
134     } else {
135         FG_LOG( FG_TERRAIN, FG_ALERT,
136                 "Parent count is zero for an ssg tile!  Dying" );
137         exit(-1);
138     }
139 }
140
141
142 // when a tile is still in the cache, but not in the immediate draw
143 // list, it can still remain in the scene graph, but we use a range
144 // selector to disable it from ever being drawn.
145 void 
146 FGTileEntry::ssg_disable() {
147     // cout << "TILE STATE = " << state << endl;
148     if ( state == Scheduled_for_use ) {
149         state = Scheduled_for_cache;
150     } else if ( state == Scheduled_for_cache ) {
151         // do nothing
152     } else if ( (state == Loaded) || (state == Cached) ) {
153         state = Cached;
154         // cout << "DISABLING SSG NODE" << endl;
155         select_ptr->select(0);
156     } else {
157         FG_LOG( FG_TERRAIN, FG_ALERT,
158                 "Trying to disable an unused tile!  Dying" );
159         exit(-1);
160     }   
161     // cout << "TILE STATE = " << state << endl;
162 }