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/bucketutils.hxx>
38 #include <Main/options.hxx>
39 #include <Main/views.hxx>
40 #include <Objects/obj.hxx>
43 #include "tilecache.hxx"
47 fgTILECACHE global_tile_cache;
51 fgTILECACHE::fgTILECACHE( void ) {
55 // Initialize the tile cache subsystem
57 fgTILECACHE::init( void )
61 FG_LOG( FG_TERRAIN, FG_INFO, "Initializing the tile cache." );
63 for ( i = 0; i < FG_TILE_CACHE_SIZE; i++ ) {
64 tile_cache[i].used = 0;
69 // Search for the specified "bucket" in the cache
71 fgTILECACHE::exists( const FGBucket& p )
75 for ( i = 0; i < FG_TILE_CACHE_SIZE; i++ ) {
76 if ( tile_cache[i].tile_bucket == p ) {
77 FG_LOG( FG_TERRAIN, FG_DEBUG,
78 "TILE EXISTS in cache ... index = " << i );
87 // Fill in a tile cache entry with real data for the specified bucket
89 fgTILECACHE::fill_in( int index, FGBucket& p )
91 // Load the appropriate data file and build tile fragment list
92 string tile_path = current_options.get_fg_root() +
93 "/Scenery/" + p.gen_base_path() + "/" + p.gen_index_str();
95 tile_cache[index].used = true;
96 tile_cache[index].tile_bucket = p;
97 fgObjLoad( tile_path, &tile_cache[index] );
98 // tile_cache[ index ].ObjLoad( tile_path, p );
100 // cout << " ncount before = " << tile_cache[index].ncount << "\n";
101 // cout << " fragments before = " << tile_cache[index].fragment_list.size()
104 string apt_path = tile_path + ".apt";
105 fgAptGenerate( apt_path, &tile_cache[index] );
107 // cout << " ncount after = " << tile_cache[index].ncount << "\n";
108 // cout << " fragments after = " << tile_cache[index].fragment_list.size()
113 // Free a tile cache entry
115 fgTILECACHE::entry_free( int index )
117 tile_cache[index].release_fragments();
121 // Return index of next available slot in tile cache
123 fgTILECACHE::next_avail( void )
125 Point3D delta, abs_view_pos;
127 float max, med, min, tmp;
128 float dist, max_dist;
134 for ( i = 0; i < FG_TILE_CACHE_SIZE; i++ ) {
135 if ( ! tile_cache[i].used ) {
138 // calculate approximate distance from view point
139 abs_view_pos = current_view.get_abs_view_pos();
141 FG_LOG( FG_TERRAIN, FG_DEBUG,
142 "DIST Abs view pos = " << abs_view_pos );
143 FG_LOG( FG_TERRAIN, FG_DEBUG,
144 " ref point = " << tile_cache[i].center );
146 delta.setx( fabs(tile_cache[i].center.x() - abs_view_pos.x() ) );
147 delta.sety( fabs(tile_cache[i].center.y() - abs_view_pos.y() ) );
148 delta.setz( fabs(tile_cache[i].center.z() - abs_view_pos.z() ) );
150 max = delta.x(); med = delta.y(); min = delta.z();
152 tmp = max; max = med; med = tmp;
155 tmp = max; max = min; min = tmp;
157 dist = max + (med + min) / 4;
159 FG_LOG( FG_TERRAIN, FG_DEBUG, " distance = " << dist );
161 if ( dist > max_dist ) {
168 // If we made it this far, then there were no open cache entries.
169 // We will instead free the furthest cache entry and return it's
172 entry_free( max_index );
178 fgTILECACHE::~fgTILECACHE( void ) {