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 *tile = tile_cache[cache_index];
66 tile->disconnect_ssg_nodes();
67 tile->sched_removal();
74 tile_cache.erase( cache_index );
78 // Initialize the tile cache subsystem
79 void FGNewCache::init( void ) {
80 // This is a hack that should really get cleaned up at some point
81 extern ssgBranch *terrain;
83 SG_LOG( SG_TERRAIN, SG_INFO, "Initializing the tile cache." );
85 // expand cache if needed. For best results ... i.e. to avoid
86 // tile load problems and blank areas:
87 max_cache_size = 50; // a random number to start with
88 SG_LOG( SG_TERRAIN, SG_INFO, " max cache size = "
90 SG_LOG( SG_TERRAIN, SG_INFO, " current cache size = "
91 << tile_cache.size() );
93 tile_map_iterator current = tile_cache.begin();
94 tile_map_iterator end = tile_cache.end();
96 for ( ; current != end; ++current ) {
97 long index = current->first;
98 SG_LOG( SG_TERRAIN, SG_DEBUG, "clearing " << index );
99 FGTileEntry *e = current->second;
100 e->tile_bucket.make_bad();
104 // and ... just in case we missed something ...
105 terrain->removeAllKids();
107 SG_LOG( SG_TERRAIN, SG_INFO, " done with init()" );
111 // Search for the specified "bucket" in the cache
112 bool FGNewCache::exists( const SGBucket& b ) const {
113 long tile_index = b.gen_index();
114 const_tile_map_iterator it = tile_cache.find( tile_index );
116 return ( it != tile_cache.end() );
120 // depricated for threading
122 // Fill in a tile cache entry with real data for the specified bucket
123 void FGNewCache::fill_in( const SGBucket& b ) {
124 SG_LOG( SG_TERRAIN, SG_DEBUG, "FILL IN CACHE ENTRY = " << b.gen_index() );
126 // clear out a distant entry in the cache if needed.
130 FGTileEntry *e = new FGTileEntry( b );
132 // register it in the cache
133 long tile_index = b.gen_index();
134 tile_cache[tile_index] = e;
137 if ( globals->get_fg_scenery() != (string)"" ) {
138 tile_path.set( globals->get_fg_scenery() );
140 tile_path.set( globals->get_fg_root() );
141 tile_path.append( "Scenery" );
144 // Load the appropriate data file
145 e->load( tile_path, true );
150 // Ensure at least one entry is free in the cache
151 void FGNewCache::make_space() {
152 SG_LOG( SG_TERRAIN, SG_DEBUG, "Make space in cache" );
153 SG_LOG( SG_TERRAIN, SG_DEBUG, "cache entries = " << tile_cache.size() );
154 SG_LOG( SG_TERRAIN, SG_DEBUG, "max size = " << max_cache_size );
156 if ( (int)tile_cache.size() < max_cache_size ) {
157 // space in the cache, return
161 while ( (int)tile_cache.size() >= max_cache_size ) {
162 sgdVec3 abs_view_pos;
164 float max_dist = 0.0;
167 // we need to free the furthest entry
168 tile_map_iterator current = tile_cache.begin();
169 tile_map_iterator end = tile_cache.end();
171 for ( ; current != end; ++current ) {
172 long index = current->first;
173 FGTileEntry *e = current->second;
175 if ( e->is_loaded() && e->get_pending_models() == 0 ) {
176 // calculate approximate distance from view point
177 sgdCopyVec3( abs_view_pos,
178 globals->get_current_view()->get_abs_view_pos() );
180 SG_LOG( SG_TERRAIN, SG_DEBUG, "DIST Abs view pos = "
181 << abs_view_pos[0] << ","
182 << abs_view_pos[1] << ","
183 << abs_view_pos[2] );
184 SG_LOG( SG_TERRAIN, SG_DEBUG,
185 " ref point = " << e->center );
189 e->center.x(), e->center.y(), e->center.z() );
190 dist = sgdDistanceVec3( center, abs_view_pos );
192 SG_LOG( SG_TERRAIN, SG_DEBUG, " distance = " << dist );
194 if ( dist > max_dist ) {
201 // If we made it this far, then there were no open cache entries.
202 // We will instead free the furthest cache entry and return it's
205 if ( max_index >= 0 ) {
206 SG_LOG( SG_TERRAIN, SG_DEBUG, " max_dist = " << max_dist );
207 SG_LOG( SG_TERRAIN, SG_DEBUG, " index = " << max_index );
208 entry_free( max_index );
210 SG_LOG( SG_TERRAIN, SG_ALERT, "WHOOPS!!! Dying in next_avail()" );
218 * Create a new tile and schedule it for loading.
221 FGNewCache::insert_tile( FGTileEntry *e )
223 // clear out a distant entry in the cache if needed.
226 // register it in the cache
227 long tile_index = e->get_tile_bucket().gen_index();
228 tile_cache[tile_index] = e;