]> git.mxchange.org Git - flightgear.git/blob - src/Scenery/tileentry.cxx
Add a key mapping (F3) for taking a screen snap shot.
[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 // recurse an ssg tree and call removeKid() on every node from the
62 // bottom up.  Leaves the original branch in existance, but empty so
63 // it can be removed by the calling routine.
64 static void my_remove_branch( ssgBranch * branch ) {
65     for ( ssgEntity *k = branch->getKid( 0 );
66           k != NULL; 
67           k = branch->getNextKid() )
68     {
69         if ( k -> isAKindOf ( ssgTypeBranch() ) ) {
70             my_remove_branch( (ssgBranch *)k );
71             branch -> removeKid ( k );
72         } else if ( k -> isAKindOf ( ssgTypeLeaf() ) ) {
73             branch -> removeKid ( k ) ;
74         }
75     }
76 }
77
78
79 // Step through the fragment list, deleting the display list, then the
80 // fragment, until the list is empty.  Also delete the arrays used by
81 // ssg as well as the whole ssg branch
82 void
83 FGTileEntry::free_tile()
84 {
85     FG_LOG( FG_TERRAIN, FG_DEBUG,
86             "FREEING TILE = (" << tile_bucket << ")" );
87
88     // mark tile unused
89     mark_unused();
90
91     // delete fragment list and node list
92     FG_LOG( FG_TERRAIN, FG_DEBUG,
93             "  deleting " << fragment_list.size() << " fragments" );
94     fragment_list.clear();
95     FG_LOG( FG_TERRAIN, FG_DEBUG,
96             "  deleting " << nodes.size() << " nodes" );
97     nodes.clear();
98
99     // delete the ssg structures
100     FG_LOG( FG_TERRAIN, FG_DEBUG,
101             "  deleting vertex, normal, and texture coordinate arrays" );
102     FG_LOG( FG_TERRAIN, FG_DEBUG,
103             "    deleting vertex array" );
104     if ( vtlist != NULL ) {
105         delete vtlist;
106         vtlist = NULL;
107     }
108     FG_LOG( FG_TERRAIN, FG_DEBUG,
109             "    deleting normal array" );
110     if ( vnlist != NULL ) {
111         delete vnlist;
112         vnlist = NULL;
113     }
114     FG_LOG( FG_TERRAIN, FG_DEBUG,
115             "    deleting texture coordinate array" );
116     if ( tclist != NULL ) {
117         delete tclist;
118         tclist = NULL;
119     }
120     for ( int i = 0; i < (int)free_ptrs.size(); ++i ) {
121         delete free_ptrs[i];
122     }
123     free_ptrs.clear();
124
125     // delete the ssg branch
126
127     int pcount = select_ptr->getNumParents();
128     if ( pcount > 0 ) {
129         // find the first parent (should only be one)
130         ssgBranch *parent = select_ptr->getParent( 0 ) ;
131         if( parent ) {
132             my_remove_branch( select_ptr );
133             parent->removeKid( select_ptr );
134         } else {
135             FG_LOG( FG_TERRAIN, FG_ALERT,
136                     "parent pointer is NULL!  Dying" );
137             exit(-1);
138         }
139     } else {
140         FG_LOG( FG_TERRAIN, FG_ALERT,
141                 "Parent count is zero for an ssg tile!  Dying" );
142         exit(-1);
143     }
144 }
145
146
147 // when a tile is still in the cache, but not in the immediate draw
148 // list, it can still remain in the scene graph, but we use a range
149 // selector to disable it from ever being drawn.
150 void 
151 FGTileEntry::ssg_disable() {
152     // cout << "TILE STATE = " << state << endl;
153     if ( state == Scheduled_for_use ) {
154         state = Scheduled_for_cache;
155     } else if ( state == Scheduled_for_cache ) {
156         // do nothing
157     } else if ( (state == Loaded) || (state == Cached) ) {
158         state = Cached;
159         // cout << "DISABLING SSG NODE" << endl;
160         select_ptr->select(0);
161     } else {
162         FG_LOG( FG_TERRAIN, FG_ALERT,
163                 "Trying to disable an unused tile!  Dying" );
164         exit(-1);
165     }   
166     // cout << "TILE STATE = " << state << endl;
167 }