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 // Load the appropriate data file and build tile fragment list
135 FGPath tile_path( current_options.get_fg_root() );
136 tile_path.append( "Scenery" );
137 tile_path.append( p.gen_base_path() );
138 tile_path.append( p.gen_index_str() );
140 tile_cache[index].tile_bucket = p;
142 tile_cache[index].select_ptr = new ssgSelector;
143 tile_cache[index].transform_ptr = new ssgTransform;
144 tile_cache[index].range_ptr = new ssgRangeSelector;
146 ssgBranch *new_tile = fgObjLoad( tile_path.str(), &tile_cache[index] );
147 if ( new_tile != NULL ) {
148 tile_cache[index].range_ptr->addKid( new_tile );
150 tile_cache[index].transform_ptr->addKid( tile_cache[index].range_ptr );
151 tile_cache[index].select_ptr->addKid( tile_cache[index].transform_ptr );
152 terrain->addKid( tile_cache[index].select_ptr );
154 if ( tile_cache[index].is_scheduled_for_cache() ) {
155 // cout << "FOUND ONE SCHEDULED FOR CACHE" << endl;
156 // load, but not needed now so disable
157 tile_cache[index].mark_loaded();
158 tile_cache[index].ssg_disable();
159 tile_cache[index].select_ptr->select(0);
161 // cout << "FOUND ONE READY TO LOAD" << endl;
162 tile_cache[index].mark_loaded();
163 tile_cache[index].select_ptr->select(1);
168 // Free a tile cache entry
170 FGTileCache::entry_free( int index )
172 tile_cache[index].free_tile();
176 // Return index of next available slot in tile cache
178 FGTileCache::next_avail( void )
180 Point3D delta, abs_view_pos;
182 float max, med, min, tmp;
183 float dist, max_dist;
189 for ( i = 0; i < (int)tile_cache.size(); i++ ) {
190 // only look at freeing NON-scheduled (i.e. ready to load
191 // cache entries. This assumes that the cache is always big
192 // enough for our tile radius!
194 if ( tile_cache[i].is_unused() ) {
195 // favor unused cache slots
197 } else if ( tile_cache[i].is_loaded() ) {
198 // calculate approximate distance from view point
199 abs_view_pos = current_view.get_abs_view_pos();
201 FG_LOG( FG_TERRAIN, FG_DEBUG,
202 "DIST Abs view pos = " << abs_view_pos );
203 FG_LOG( FG_TERRAIN, FG_DEBUG,
204 " ref point = " << tile_cache[i].center );
206 delta.setx( fabs(tile_cache[i].center.x() - abs_view_pos.x() ) );
207 delta.sety( fabs(tile_cache[i].center.y() - abs_view_pos.y() ) );
208 delta.setz( fabs(tile_cache[i].center.z() - abs_view_pos.z() ) );
210 max = delta.x(); med = delta.y(); min = delta.z();
212 tmp = max; max = med; med = tmp;
215 tmp = max; max = min; min = tmp;
217 dist = max + (med + min) / 4;
219 FG_LOG( FG_TERRAIN, FG_DEBUG, " distance = " << dist );
221 if ( dist > max_dist ) {
228 // If we made it this far, then there were no open cache entries.
229 // We will instead free the furthest cache entry and return it's
232 entry_free( max_index );
238 FGTileCache::~FGTileCache( void ) {