]> git.mxchange.org Git - simgear.git/blob - simgear/scene/tgdb/TileCache.cxx
Win32 fix
[simgear.git] / simgear / scene / tgdb / TileCache.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 #ifdef HAVE_CONFIG_H
24 #  include <simgear_config.h>
25 #endif
26
27 #include <simgear/bucket/newbucket.hxx>
28 #include <simgear/debug/logstream.hxx>
29 #include <simgear/misc/sg_path.hxx>
30
31 #include "TileEntry.hxx"
32 #include "TileCache.hxx"
33
34 SG_USING_NAMESPACE(std);
35
36 using simgear::TileEntry;
37 using simgear::TileCache;
38
39 TileCache::TileCache( void ) :
40     max_cache_size(100)
41 {
42     tile_cache.clear();
43 }
44
45
46 TileCache::~TileCache( void ) {
47     clear_cache();
48 }
49
50
51 // Free a tile cache entry
52 void TileCache::entry_free( long cache_index ) {
53     SG_LOG( SG_TERRAIN, SG_DEBUG, "FREEING CACHE ENTRY = " << cache_index );
54     TileEntry *tile = tile_cache[cache_index];
55     tile->removeFromSceneGraph();
56
57     tile->free_tile();
58     delete tile;
59
60     tile_cache.erase( cache_index );
61 }
62
63
64 // Initialize the tile cache subsystem
65 void TileCache::init( void ) {
66     SG_LOG( SG_TERRAIN, SG_INFO, "Initializing the tile cache." );
67
68     SG_LOG( SG_TERRAIN, SG_INFO, "  max cache size = "
69             << max_cache_size );
70     SG_LOG( SG_TERRAIN, SG_INFO, "  current cache size = "
71             << tile_cache.size() );
72
73 #if 0 // don't clear the cache
74     clear_cache();
75 #endif
76
77     SG_LOG( SG_TERRAIN, SG_INFO, "  done with init()"  );
78 }
79
80
81 // Search for the specified "bucket" in the cache
82 bool TileCache::exists( const SGBucket& b ) const {
83     long tile_index = b.gen_index();
84     const_tile_map_iterator it = tile_cache.find( tile_index );
85
86     return ( it != tile_cache.end() );
87 }
88
89
90 // Return the index of the oldest tile in the cache, return -1 if
91 // nothing available to be removed.
92 long TileCache::get_oldest_tile() {
93     // we need to free the furthest entry
94     long min_index = -1;
95     double timestamp = 0.0;
96     double min_time = DBL_MAX;
97
98     tile_map_iterator current = tile_cache.begin();
99     tile_map_iterator end = tile_cache.end();
100
101     for ( ; current != end; ++current ) {
102         long index = current->first;
103         TileEntry *e = current->second;
104         if ( e->is_loaded() ) {
105             timestamp = e->get_timestamp();
106             if ( timestamp < min_time ) {
107                 min_time = timestamp;
108                 min_index = index;
109             }
110         } else {
111             SG_LOG( SG_TERRAIN, SG_DEBUG, "loaded = " << e->is_loaded()
112                     << " time stamp = " << e->get_timestamp() );
113         }
114     }
115
116     SG_LOG( SG_TERRAIN, SG_DEBUG, "    index = " << min_index );
117     SG_LOG( SG_TERRAIN, SG_DEBUG, "    min_time = " << min_time );
118
119     return min_index;
120 }
121
122
123 // Clear the inner ring flag for all tiles in the cache so that the
124 // external tile scheduler can flag the inner ring correctly.
125 void TileCache::clear_inner_ring_flags() {
126     tile_map_iterator current = tile_cache.begin();
127     tile_map_iterator end = tile_cache.end();
128
129     for ( ; current != end; ++current ) {
130         TileEntry *e = current->second;
131         if ( e->is_loaded() ) {
132             e->set_inner_ring( false );
133         }
134     }
135 }
136
137 // Clear a cache entry, note that the cache only holds pointers
138 // and this does not free the object which is pointed to.
139 void TileCache::clear_entry( long cache_index ) {
140     tile_cache.erase( cache_index );
141 }
142
143
144 // Clear all completely loaded tiles (ignores partially loaded tiles)
145 void TileCache::clear_cache() {
146     std::vector<long> indexList;
147     tile_map_iterator current = tile_cache.begin();
148     tile_map_iterator end = tile_cache.end();
149
150     for ( ; current != end; ++current ) {
151         long index = current->first;
152         SG_LOG( SG_TERRAIN, SG_DEBUG, "clearing " << index );
153         TileEntry *e = current->second;
154         if ( e->is_loaded() ) {
155             e->tile_bucket.make_bad();
156             // entry_free modifies tile_cache, so store index and call entry_free() later;
157             indexList.push_back( index);
158         }
159     }
160     for (unsigned int it = 0; it < indexList.size(); it++) {
161         entry_free( indexList[ it]);
162     }
163 }
164
165 /**
166  * Create a new tile and schedule it for loading.
167  */
168 bool TileCache::insert_tile( TileEntry *e ) {
169     // register tile in the cache
170     long tile_index = e->get_tile_bucket().gen_index();
171     tile_cache[tile_index] = e;
172
173     return true;
174 }
175