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