1 // tilecache.cxx -- routines to handle scenery tile caching
3 // Written by Curtis Olson, started January 1998.
5 // Copyright (C) 1997 Curtis L. Olson - curt@infoplane.com
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 <ssg.h> // plib include
37 #include <Debug/logstream.hxx>
38 #include <Airports/genapt.hxx>
39 #include <Bucket/newbucket.hxx>
40 #include <Main/options.hxx>
41 #include <Main/views.hxx>
42 #include <Misc/fgpath.hxx>
43 #include <Objects/obj.hxx>
45 #include "tilecache.hxx"
46 #include "tileentry.hxx"
49 // a cheesy hack (to be fixed later)
50 extern ssgBranch *terrain;
51 extern ssgEntity *penguin;
55 FGTileCache global_tile_cache;
59 FGTileCache::FGTileCache( void ) {
64 // Initialize the tile cache subsystem
66 FGTileCache::init( void )
70 FG_LOG( FG_TERRAIN, FG_INFO, "Initializing the tile cache." );
72 // expand cache if needed. For best results ... i.e. to avoid
73 // tile load problems and blank areas:
75 // target_cache_size >= (current.options.tile_diameter + 1) ** 2
77 int side = current_options.get_tile_diameter() + 2;
78 int target_cache_size = (side*side);
79 FG_LOG( FG_TERRAIN, FG_DEBUG, " target cache size = "
80 << target_cache_size );
81 FG_LOG( FG_TERRAIN, FG_DEBUG, " current cache size = "
82 << tile_cache.size() );
84 FG_LOG( FG_TERRAIN, FG_DEBUG, " size of tile = "
86 if ( target_cache_size > (int)tile_cache.size() ) {
89 int expansion_amt = target_cache_size - (int)tile_cache.size();
90 for ( i = 0; i < expansion_amt; ++i ) {
91 tile_cache.push_back( e );
92 FG_LOG( FG_TERRAIN, FG_DEBUG, " expanding cache size = "
93 << tile_cache.size() );
96 FG_LOG( FG_TERRAIN, FG_DEBUG, " done expanding cache, size = "
97 << tile_cache.size() );
99 for ( i = 0; i < (int)tile_cache.size(); i++ ) {
100 if ( !tile_cache[i].is_unused() ) {
103 tile_cache[i].mark_unused();
104 tile_cache[i].tile_bucket.make_bad();
106 FG_LOG( FG_TERRAIN, FG_DEBUG, " done with init()" );
110 // Search for the specified "bucket" in the cache
112 FGTileCache::exists( const FGBucket& p )
116 for ( i = 0; i < (int)tile_cache.size(); i++ ) {
117 if ( tile_cache[i].tile_bucket == p ) {
118 FG_LOG( FG_TERRAIN, FG_DEBUG,
119 "TILE EXISTS in cache ... index = " << i );
128 // Fill in a tile cache entry with real data for the specified bucket
130 FGTileCache::fill_in( int index, const FGBucket& p )
132 cout << "FILL IN CACHE ENTRY = " << index << endl;
134 // Force some values in case the tile fails to load (i.e. fill
136 tile_cache[index].center = Point3D( 0.0 );
137 tile_cache[index].vtlist = NULL;
138 tile_cache[index].vnlist = NULL;
139 tile_cache[index].tclist = NULL;
141 // Load the appropriate data file and build tile fragment list
142 FGPath tile_path( current_options.get_fg_root() );
143 tile_path.append( "Scenery" );
144 tile_path.append( p.gen_base_path() );
145 tile_path.append( p.gen_index_str() );
147 tile_cache[index].tile_bucket = p;
149 tile_cache[index].select_ptr = new ssgSelector;
150 tile_cache[index].transform_ptr = new ssgTransform;
151 tile_cache[index].range_ptr = new ssgRangeSelector;
153 ssgBranch *new_tile = fgObjLoad( tile_path.str(), &tile_cache[index] );
154 if ( new_tile != NULL ) {
155 tile_cache[index].range_ptr->addKid( new_tile );
157 tile_cache[index].transform_ptr->addKid( tile_cache[index].range_ptr );
158 tile_cache[index].select_ptr->addKid( tile_cache[index].transform_ptr );
159 terrain->addKid( tile_cache[index].select_ptr );
161 if ( tile_cache[index].is_scheduled_for_cache() ) {
162 cout << "FOUND ONE SCHEDULED FOR CACHE" << endl;
163 // load, but not needed now so disable
164 tile_cache[index].mark_loaded();
165 tile_cache[index].ssg_disable();
166 tile_cache[index].select_ptr->select(0);
168 cout << "FOUND ONE READY TO LOAD" << endl;
169 tile_cache[index].mark_loaded();
170 tile_cache[index].select_ptr->select(1);
175 // Free a tile cache entry
177 FGTileCache::entry_free( int cache_index )
179 cout << "FREEING CACHE ENTRY = " << cache_index << endl;
180 tile_cache[cache_index].free_tile();
184 // Return index of next available slot in tile cache
186 FGTileCache::next_avail( void )
189 Point3D abs_view_pos;
191 // float max, med, min, tmp;
192 float dist, max_dist;
198 for ( i = 0; i < (int)tile_cache.size(); i++ ) {
199 // only look at freeing NON-scheduled (i.e. ready to load
200 // cache entries. This assumes that the cache is always big
201 // enough for our tile radius!
203 if ( tile_cache[i].is_unused() ) {
204 // favor unused cache slots
206 } else if ( tile_cache[i].is_loaded() || tile_cache[i].is_cached() ) {
207 // calculate approximate distance from view point
208 abs_view_pos = current_view.get_abs_view_pos();
210 FG_LOG( FG_TERRAIN, FG_DEBUG,
211 "DIST Abs view pos = " << abs_view_pos );
212 FG_LOG( FG_TERRAIN, FG_DEBUG,
213 " ref point = " << tile_cache[i].center );
216 delta.setx( fabs(tile_cache[i].center.x() - abs_view_pos.x() ) );
217 delta.sety( fabs(tile_cache[i].center.y() - abs_view_pos.y() ) );
218 delta.setz( fabs(tile_cache[i].center.z() - abs_view_pos.z() ) );
220 max = delta.x(); med = delta.y(); min = delta.z();
222 tmp = max; max = med; med = tmp;
225 tmp = max; max = min; min = tmp;
227 dist = max + (med + min) / 4;
230 dist = tile_cache[i].center.distance3D( abs_view_pos );
232 FG_LOG( FG_TERRAIN, FG_DEBUG, " distance = " << dist );
234 if ( dist > max_dist ) {
241 // If we made it this far, then there were no open cache entries.
242 // We will instead free the furthest cache entry and return it's
245 if ( max_index >=0 ) {
246 FG_LOG( FG_TERRAIN, FG_INFO, " max_dist = " << max_dist );
247 FG_LOG( FG_TERRAIN, FG_INFO, " index = " << max_index );
248 entry_free( max_index );
251 FG_LOG( FG_TERRAIN, FG_ALERT, "WHOOPS!!! Dying in next_avail()" );
258 FGTileCache::~FGTileCache( void ) {