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 <Debug/logstream.hxx>
36 #include <Airports/genapt.hxx>
37 #include <Bucket/newbucket.hxx>
38 #include <Main/options.hxx>
39 #include <Main/views.hxx>
40 #include <Misc/fgpath.hxx>
41 #include <Objects/obj.hxx>
43 #include "tilecache.hxx"
44 #include "tileentry.hxx"
48 FGTileCache global_tile_cache;
52 FGTileCache::FGTileCache( void ) {
57 // Initialize the tile cache subsystem
59 FGTileCache::init( void )
63 FG_LOG( FG_TERRAIN, FG_INFO, "Initializing the tile cache." );
65 // expand cache if needed. For best results ... i.e. to avoid
66 // tile load problems and blank areas:
68 // target_cache_size >= (current.options.tile_diameter + 1) ** 2
70 int side = current_options.get_tile_diameter() + 2;
71 int target_cache_size = (side*side);
72 FG_LOG( FG_TERRAIN, FG_DEBUG, " target cache size = "
73 << target_cache_size );
74 FG_LOG( FG_TERRAIN, FG_DEBUG, " current cache size = "
75 << tile_cache.size() );
77 FG_LOG( FG_TERRAIN, FG_DEBUG, " size of tile = "
79 if ( target_cache_size > (int)tile_cache.size() ) {
82 int expansion_amt = target_cache_size - (int)tile_cache.size();
83 for ( i = 0; i < expansion_amt; ++i ) {
84 tile_cache.push_back( e );
85 FG_LOG( FG_TERRAIN, FG_DEBUG, " expanding cache size = "
86 << tile_cache.size() );
89 FG_LOG( FG_TERRAIN, FG_DEBUG, " done expanding cache, size = "
90 << tile_cache.size() );
92 for ( i = 0; i < (int)tile_cache.size(); i++ ) {
93 if ( !tile_cache[i].is_unused() ) {
96 tile_cache[i].mark_unused();
97 tile_cache[i].tile_bucket.make_bad();
99 FG_LOG( FG_TERRAIN, FG_DEBUG, " done with init()" );
103 // Search for the specified "bucket" in the cache
105 FGTileCache::exists( const FGBucket& p )
109 for ( i = 0; i < (int)tile_cache.size(); i++ ) {
110 if ( tile_cache[i].tile_bucket == p ) {
111 FG_LOG( FG_TERRAIN, FG_DEBUG,
112 "TILE EXISTS in cache ... index = " << i );
121 // Fill in a tile cache entry with real data for the specified bucket
123 FGTileCache::fill_in( int index, const FGBucket& p )
125 // Load the appropriate data file and build tile fragment list
126 FGPath tile_path( current_options.get_fg_root() );
127 tile_path.append( "Scenery" );
128 tile_path.append( p.gen_base_path() );
129 tile_path.append( p.gen_index_str() );
131 tile_cache[index].mark_loaded();
132 tile_cache[index].tile_bucket = p;
133 fgObjLoad( tile_path.str(), &tile_cache[index] );
134 // tile_cache[ index ].ObjLoad( tile_path, p );
136 // cout << " ncount before = " << tile_cache[index].ncount << "\n";
137 // cout << " fragments before = " << tile_cache[index].fragment_list.size()
140 string apt_path = tile_path.str();
142 fgAptGenerate( apt_path, &tile_cache[index] );
144 // cout << " ncount after = " << tile_cache[index].ncount << "\n";
145 // cout << " fragments after = " << tile_cache[index].fragment_list.size()
150 // Free a tile cache entry
152 FGTileCache::entry_free( int index )
154 tile_cache[index].release_fragments();
158 // Return index of next available slot in tile cache
160 FGTileCache::next_avail( void )
162 Point3D delta, abs_view_pos;
164 float max, med, min, tmp;
165 float dist, max_dist;
171 for ( i = 0; i < (int)tile_cache.size(); i++ ) {
172 // only look at freeing NON-scheduled (i.e. ready to load
173 // cache entries. This assumes that the cache is always big
174 // enough for our tile radius!
176 if ( tile_cache[i].is_unused() ) {
177 // favor unused cache slots
179 } else if ( tile_cache[i].is_loaded() ) {
180 // calculate approximate distance from view point
181 abs_view_pos = current_view.get_abs_view_pos();
183 FG_LOG( FG_TERRAIN, FG_DEBUG,
184 "DIST Abs view pos = " << abs_view_pos );
185 FG_LOG( FG_TERRAIN, FG_DEBUG,
186 " ref point = " << tile_cache[i].center );
188 delta.setx( fabs(tile_cache[i].center.x() - abs_view_pos.x() ) );
189 delta.sety( fabs(tile_cache[i].center.y() - abs_view_pos.y() ) );
190 delta.setz( fabs(tile_cache[i].center.z() - abs_view_pos.z() ) );
192 max = delta.x(); med = delta.y(); min = delta.z();
194 tmp = max; max = med; med = tmp;
197 tmp = max; max = min; min = tmp;
199 dist = max + (med + min) / 4;
201 FG_LOG( FG_TERRAIN, FG_DEBUG, " distance = " << dist );
203 if ( dist > max_dist ) {
210 // If we made it this far, then there were no open cache entries.
211 // We will instead free the furthest cache entry and return it's
214 entry_free( max_index );
220 FGTileCache::~FGTileCache( void ) {