]> git.mxchange.org Git - flightgear.git/blob - src/Scenery/newcache.cxx
Clear chat messages when an aircraft becomes inactive in the property tree.
[flightgear.git] / src / Scenery / newcache.cxx
1 // newcache.cxx -- routines to handle scenery tile caching
2 //
3 // Written by Curtis Olson, started December 2000.
4 //
5 // Copyright (C) 2000  Curtis L. Olson  - http://www.flightgear.org/~curt
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21 // $Id$
22
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #include <simgear/bucket/newbucket.hxx>
29 #include <simgear/debug/logstream.hxx>
30 #include <simgear/misc/sg_path.hxx>
31
32 #include <Main/globals.hxx>
33 #include <Main/viewer.hxx>
34 #include <Scenery/scenery.hxx>  // for scenery.center
35
36 #include "newcache.hxx"
37 #include "tileentry.hxx"
38
39
40 SG_USING_NAMESPACE(std);
41
42
43 // Constructor
44 FGNewCache::FGNewCache( void ) :
45     max_cache_size(100)
46 {
47     tile_cache.clear();
48 }
49
50
51 // Destructor
52 FGNewCache::~FGNewCache( void ) {
53     clear_cache();
54 }
55
56
57 // Free a tile cache entry
58 void FGNewCache::entry_free( long cache_index ) {
59     SG_LOG( SG_TERRAIN, SG_DEBUG, "FREEING CACHE ENTRY = " << cache_index );
60     FGTileEntry *tile = tile_cache[cache_index];
61     tile->disconnect_ssg_nodes();
62
63 #ifdef WISH_PLIB_WAS_THREADED // but it isn't
64     tile->sched_removal();
65 #else
66     tile->free_tile();
67     delete tile;
68 #endif
69
70     tile_cache.erase( cache_index );
71 }
72
73
74 // Initialize the tile cache subsystem
75 void FGNewCache::init( void ) {
76     SG_LOG( SG_TERRAIN, SG_INFO, "Initializing the tile cache." );
77
78     SG_LOG( SG_TERRAIN, SG_INFO, "  max cache size = " 
79             << max_cache_size );
80     SG_LOG( SG_TERRAIN, SG_INFO, "  current cache size = " 
81             << tile_cache.size() );
82
83 #if 0 // don't clear the cache
84     clear_cache();
85 #endif
86
87     SG_LOG( SG_TERRAIN, SG_INFO, "  done with init()"  );
88 }
89
90
91 // Search for the specified "bucket" in the cache
92 bool FGNewCache::exists( const SGBucket& b ) const {
93     long tile_index = b.gen_index();
94     const_tile_map_iterator it = tile_cache.find( tile_index );
95
96     return ( it != tile_cache.end() );
97 }
98
99 // Return the index of the oldest tile in the cache, return -1 if
100 // nothing available to be removed.
101 long FGNewCache::get_oldest_tile() {
102     // we need to free the furthest entry
103     long min_index = -1;
104     double timestamp = 0.0;
105     double max_time = 0;
106
107     tile_map_iterator current = tile_cache.begin();
108     tile_map_iterator end = tile_cache.end();
109     
110     for ( ; current != end; ++current ) {
111         long index = current->first;
112         FGTileEntry *e = current->second;
113         if ( e->is_loaded() ) {
114             
115             timestamp = e->get_timestamp();
116             if ( timestamp > max_time ) {
117                 max_time = timestamp;
118             }
119
120         } else {
121             SG_LOG( SG_TERRAIN, SG_DEBUG, "loaded = " << e->is_loaded()
122                     << " time stamp = " << e->get_timestamp() );
123         }
124     }
125
126     SG_LOG( SG_TERRAIN, SG_DEBUG, "    index = " << min_index );
127     SG_LOG( SG_TERRAIN, SG_DEBUG, "    max_time = " << max_time );
128
129     return min_index;
130 }
131
132
133 // Clear the inner ring flag for all tiles in the cache so that the
134 // external tile scheduler can flag the inner ring correctly.
135 void FGNewCache::clear_inner_ring_flags() {
136     tile_map_iterator current = tile_cache.begin();
137     tile_map_iterator end = tile_cache.end();
138     
139     for ( ; current != end; ++current ) {
140         FGTileEntry *e = current->second;
141         if ( e->is_loaded() ) {
142             e->set_inner_ring( false );
143         }
144     }
145 }
146
147 // Clear a cache entry, note that the cache only holds pointers
148 // and this does not free the object which is pointed to.
149 void FGNewCache::clear_entry( long cache_index ) {
150     tile_cache.erase( cache_index );
151 }
152
153
154 // Clear all completely loaded tiles (ignores partially loaded tiles)
155 void FGNewCache::clear_cache() {
156     std::vector<long> indexList;
157     tile_map_iterator current = tile_cache.begin();
158     tile_map_iterator end = tile_cache.end();
159     
160     for ( ; current != end; ++current ) {
161         long index = current->first;
162         SG_LOG( SG_TERRAIN, SG_DEBUG, "clearing " << index );
163         FGTileEntry *e = current->second;
164         if ( e->is_loaded() ) {
165             e->tile_bucket.make_bad();
166             // entry_free modifies tile_cache, so store index and call entry_free() later;
167             indexList.push_back( index);
168         }
169     }
170     for (unsigned int it = 0; it < indexList.size(); it++) {
171         entry_free( indexList[ it]);
172     }
173     // and ... just in case we missed something ... 
174     osg::Group* group = globals->get_scenery()->get_terrain_branch();
175     group->removeChildren(0, group->getNumChildren());
176 }
177
178
179 /**
180  * Create a new tile and schedule it for loading.
181  */
182 bool FGNewCache::insert_tile( FGTileEntry *e ) {
183     // register tile in the cache
184     long tile_index = e->get_tile_bucket().gen_index();
185     tile_cache[tile_index] = e;
186
187     return true;
188 }
189
190