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 // Load the appropriate data file and build tile fragment list
133 FGPath tile_path( current_options.get_fg_root() );
134 tile_path.append( "Scenery" );
135 tile_path.append( p.gen_base_path() );
136 tile_path.append( p.gen_index_str() );
138 tile_cache[index].mark_loaded();
139 tile_cache[index].tile_bucket = p;
140 ssgBranch *new_tile = fgObjLoad( tile_path.str(), &tile_cache[index] );
141 tile_cache[index].branch_ptr = new ssgTransform;
142 tile_cache[index].branch_ptr->addKid( new_tile );
143 // tile_cache[index].branch_ptr->addKid( penguin );
144 terrain->addKid( tile_cache[index].branch_ptr );
146 // cout << " ncount before = " << tile_cache[index].ncount << "\n";
147 // cout << " fragments before = " << tile_cache[index].fragment_list.size()
150 string apt_path = tile_path.str();
152 fgAptGenerate( apt_path, &tile_cache[index] );
154 // cout << " ncount after = " << tile_cache[index].ncount << "\n";
155 // cout << " fragments after = " << tile_cache[index].fragment_list.size()
160 // Free a tile cache entry
162 FGTileCache::entry_free( int index )
164 tile_cache[index].release_fragments();
168 // Return index of next available slot in tile cache
170 FGTileCache::next_avail( void )
172 Point3D delta, abs_view_pos;
174 float max, med, min, tmp;
175 float dist, max_dist;
181 for ( i = 0; i < (int)tile_cache.size(); i++ ) {
182 // only look at freeing NON-scheduled (i.e. ready to load
183 // cache entries. This assumes that the cache is always big
184 // enough for our tile radius!
186 if ( tile_cache[i].is_unused() ) {
187 // favor unused cache slots
189 } else if ( tile_cache[i].is_loaded() ) {
190 // calculate approximate distance from view point
191 abs_view_pos = current_view.get_abs_view_pos();
193 FG_LOG( FG_TERRAIN, FG_DEBUG,
194 "DIST Abs view pos = " << abs_view_pos );
195 FG_LOG( FG_TERRAIN, FG_DEBUG,
196 " ref point = " << tile_cache[i].center );
198 delta.setx( fabs(tile_cache[i].center.x() - abs_view_pos.x() ) );
199 delta.sety( fabs(tile_cache[i].center.y() - abs_view_pos.y() ) );
200 delta.setz( fabs(tile_cache[i].center.z() - abs_view_pos.z() ) );
202 max = delta.x(); med = delta.y(); min = delta.z();
204 tmp = max; max = med; med = tmp;
207 tmp = max; max = min; min = tmp;
209 dist = max + (med + min) / 4;
211 FG_LOG( FG_TERRAIN, FG_DEBUG, " distance = " << dist );
213 if ( dist > max_dist ) {
220 // If we made it this far, then there were no open cache entries.
221 // We will instead free the furthest cache entry and return it's
224 entry_free( max_index );
230 FGTileCache::~FGTileCache( void ) {