1 // newcache.cxx -- routines to handle scenery tile caching
3 // Written by Curtis Olson, started December 2000.
5 // Copyright (C) 2000 Curtis L. Olson - curt@flightgear.org
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.
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.
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.
35 #include <plib/ssg.h> // plib include
37 #include <simgear/bucket/newbucket.hxx>
38 #include <simgear/debug/logstream.hxx>
39 #include <simgear/misc/sg_path.hxx>
41 #include <Main/globals.hxx>
42 #include <Scenery/scenery.hxx> // for scenery.center
44 #include "newcache.hxx"
45 #include "tileentry.hxx"
48 SG_USING_NAMESPACE(std);
52 FGNewCache::FGNewCache( void ) {
58 FGNewCache::~FGNewCache( void ) {
62 // Free a tile cache entry
63 void FGNewCache::entry_free( long cache_index ) {
64 SG_LOG( SG_TERRAIN, SG_DEBUG, "FREEING CACHE ENTRY = " << cache_index );
65 FGTileEntry *e = tile_cache[cache_index];
68 tile_cache.erase( cache_index );
72 // Initialize the tile cache subsystem
73 void FGNewCache::init( void ) {
74 // This is a hack that should really get cleaned up at some point
75 extern ssgBranch *terrain;
77 SG_LOG( SG_TERRAIN, SG_INFO, "Initializing the tile cache." );
79 // expand cache if needed. For best results ... i.e. to avoid
80 // tile load problems and blank areas:
81 max_cache_size = 50; // a random number to start with
82 SG_LOG( SG_TERRAIN, SG_INFO, " max cache size = "
84 SG_LOG( SG_TERRAIN, SG_INFO, " current cache size = "
85 << tile_cache.size() );
87 tile_map_iterator current = tile_cache.begin();
88 tile_map_iterator end = tile_cache.end();
90 for ( ; current != end; ++current ) {
91 long index = current->first;
92 SG_LOG( SG_TERRAIN, SG_DEBUG, "clearing " << index );
93 FGTileEntry *e = current->second;
94 e->tile_bucket.make_bad();
98 // and ... just in case we missed something ...
99 terrain->removeAllKids();
101 SG_LOG( SG_TERRAIN, SG_INFO, " done with init()" );
105 // Search for the specified "bucket" in the cache
106 bool FGNewCache::exists( const SGBucket& b ) const {
107 long tile_index = b.gen_index();
108 const_tile_map_iterator it = tile_cache.find( tile_index );
110 return ( it != tile_cache.end() );
114 // depricated for threading
116 // Fill in a tile cache entry with real data for the specified bucket
117 void FGNewCache::fill_in( const SGBucket& b ) {
118 SG_LOG( SG_TERRAIN, SG_DEBUG, "FILL IN CACHE ENTRY = " << b.gen_index() );
120 // clear out a distant entry in the cache if needed.
124 FGTileEntry *e = new FGTileEntry( b );
126 // register it in the cache
127 long tile_index = b.gen_index();
128 tile_cache[tile_index] = e;
131 if ( globals->get_fg_scenery() != (string)"" ) {
132 tile_path.set( globals->get_fg_scenery() );
134 tile_path.set( globals->get_fg_root() );
135 tile_path.append( "Scenery" );
138 // Load the appropriate data file
139 e->load( tile_path, true );
144 // Ensure at least one entry is free in the cache
145 void FGNewCache::make_space() {
146 SG_LOG( SG_TERRAIN, SG_DEBUG, "Make space in cache" );
147 SG_LOG( SG_TERRAIN, SG_DEBUG, "cache entries = " << tile_cache.size() );
148 SG_LOG( SG_TERRAIN, SG_DEBUG, "max size = " << max_cache_size );
150 if ( (int)tile_cache.size() < max_cache_size ) {
151 // space in the cache, return
155 while ( (int)tile_cache.size() >= max_cache_size ) {
156 sgdVec3 abs_view_pos;
158 float max_dist = 0.0;
161 // we need to free the furthest entry
162 tile_map_iterator current = tile_cache.begin();
163 tile_map_iterator end = tile_cache.end();
165 for ( ; current != end; ++current ) {
166 long index = current->first;
167 FGTileEntry *e = current->second;
169 if ( e->is_loaded() ) {
170 // calculate approximate distance from view point
171 sgdCopyVec3( abs_view_pos,
172 globals->get_current_view()->get_abs_view_pos() );
174 SG_LOG( SG_TERRAIN, SG_DEBUG, "DIST Abs view pos = "
175 << abs_view_pos[0] << ","
176 << abs_view_pos[1] << ","
177 << abs_view_pos[2] );
178 SG_LOG( SG_TERRAIN, SG_DEBUG,
179 " ref point = " << e->center );
183 e->center.x(), e->center.y(), e->center.z() );
184 dist = sgdDistanceVec3( center, abs_view_pos );
186 SG_LOG( SG_TERRAIN, SG_DEBUG, " distance = " << dist );
188 if ( dist > max_dist ) {
195 // If we made it this far, then there were no open cache entries.
196 // We will instead free the furthest cache entry and return it's
199 if ( max_index >= 0 ) {
200 SG_LOG( SG_TERRAIN, SG_DEBUG, " max_dist = " << max_dist );
201 SG_LOG( SG_TERRAIN, SG_DEBUG, " index = " << max_index );
202 entry_free( max_index );
204 SG_LOG( SG_TERRAIN, SG_ALERT, "WHOOPS!!! Dying in next_avail()" );
212 * Create a new tile and schedule it for loading.
215 FGNewCache::insert_tile( FGTileEntry *e )
217 // clear out a distant entry in the cache if needed.
220 // register it in the cache
221 long tile_index = e->get_tile_bucket().gen_index();
222 tile_cache[tile_index] = e;