]> git.mxchange.org Git - flightgear.git/blob - src/Scenery/newcache.cxx
841fb4bac071b0cefe90a30ab4e972f7996b3da3
[flightgear.git] / src / Scenery / newcache.cxx
1 // newcache.cxx -- routines to handle scenery tile caching
2 //
3 // Written by Curtis Olson, started December 2000.
4 //
5 // Copyright (C) 2000  Curtis L. Olson  - curt@flightgear.org
6 //
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.
11 //
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.
16 //
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.
20 //
21 // $Id$
22
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #ifdef HAVE_WINDOWS_H
29 #  include <windows.h>
30 #endif
31
32 #include <GL/glut.h>
33 #include <GL/gl.h>
34
35 #include <plib/ssg.h>           // plib include
36
37 #include <simgear/bucket/newbucket.hxx>
38 #include <simgear/debug/logstream.hxx>
39 #include <simgear/misc/sg_path.hxx>
40
41 #include <Main/globals.hxx>
42 #include <Scenery/scenery.hxx>  // for scenery.center
43
44 #include "newcache.hxx"
45 #include "tileentry.hxx"
46
47
48 SG_USING_NAMESPACE(std);
49
50
51 // Constructor
52 FGNewCache::FGNewCache( void ) {
53     tile_cache.clear();
54 }
55
56
57 // Destructor
58 FGNewCache::~FGNewCache( void ) {
59 }
60
61
62 // Free a tile cache entry
63 void FGNewCache::entry_free( long cache_index ) {
64     SG_LOG( SG_TERRAIN, SG_DEBUG, "FREEING CACHE ENTRY = " << cache_index );
65     FGTileEntry *tile = tile_cache[cache_index];
66     tile->disconnect_ssg_nodes();
67
68 #ifdef WISH_PLIB_WAS_THREADED
69     tile->sched_removal();
70 #else // plib isn't threaded so we always go here
71     tile->free_tile();
72     delete tile;
73 #endif
74
75     tile_cache.erase( cache_index );
76 }
77
78
79 // Initialize the tile cache subsystem
80 void FGNewCache::init( void ) {
81     // This is a hack that should really get cleaned up at some point
82     extern ssgBranch *terrain;
83
84     SG_LOG( SG_TERRAIN, SG_INFO, "Initializing the tile cache." );
85
86     // expand cache if needed.  For best results ... i.e. to avoid
87     // tile load problems and blank areas: 
88     max_cache_size = 50;        // a random number to start with
89     SG_LOG( SG_TERRAIN, SG_INFO, "  max cache size = " 
90             << max_cache_size );
91     SG_LOG( SG_TERRAIN, SG_INFO, "  current cache size = " 
92             << tile_cache.size() );
93     
94     tile_map_iterator current = tile_cache.begin();
95     tile_map_iterator end = tile_cache.end();
96     
97     for ( ; current != end; ++current ) {
98         long index = current->first;
99         SG_LOG( SG_TERRAIN, SG_DEBUG, "clearing " << index );
100         FGTileEntry *e = current->second;
101         e->tile_bucket.make_bad();
102         entry_free(index);
103     }
104
105     // and ... just in case we missed something ... 
106     terrain->removeAllKids();
107
108     SG_LOG( SG_TERRAIN, SG_INFO, "  done with init()"  );
109 }
110
111
112 // Search for the specified "bucket" in the cache
113 bool FGNewCache::exists( const SGBucket& b ) const {
114     long tile_index = b.gen_index();
115     const_tile_map_iterator it = tile_cache.find( tile_index );
116
117     return ( it != tile_cache.end() );
118 }
119
120
121 // depricated for threading
122 #if 0
123 // Fill in a tile cache entry with real data for the specified bucket
124 void FGNewCache::fill_in( const SGBucket& b ) {
125     SG_LOG( SG_TERRAIN, SG_DEBUG, "FILL IN CACHE ENTRY = " << b.gen_index() );
126
127     // clear out a distant entry in the cache if needed.
128     make_space();
129
130     // create the entry
131     FGTileEntry *e = new FGTileEntry( b );
132
133     // register it in the cache
134     long tile_index = b.gen_index();
135     tile_cache[tile_index] = e;
136
137     SGPath tile_path;
138     if ( globals->get_fg_scenery() != (string)"" ) {
139         tile_path.set( globals->get_fg_scenery() );
140     } else {
141         tile_path.set( globals->get_fg_root() );
142         tile_path.append( "Scenery" );
143     }
144     
145     // Load the appropriate data file
146     e->load( tile_path, true );
147 }
148 #endif
149
150
151 // Ensure at least one entry is free in the cache
152 void FGNewCache::make_space() {
153     SG_LOG( SG_TERRAIN, SG_DEBUG, "Make space in cache" );
154     SG_LOG( SG_TERRAIN, SG_DEBUG, "cache entries = " << tile_cache.size() );
155     SG_LOG( SG_TERRAIN, SG_DEBUG, "max size = " << max_cache_size );
156
157     if ( (int)tile_cache.size() < max_cache_size ) {
158         // space in the cache, return
159         return;
160     }
161
162     while ( (int)tile_cache.size() >= max_cache_size ) {
163         sgdVec3 abs_view_pos;
164         float dist;
165         float max_dist = 0.0;
166         int max_index = -1;
167
168         // we need to free the furthest entry
169         tile_map_iterator current = tile_cache.begin();
170         tile_map_iterator end = tile_cache.end();
171     
172         for ( ; current != end; ++current ) {
173             long index = current->first;
174             FGTileEntry *e = current->second;
175
176             if ( e->is_loaded() && e->get_pending_models() == 0 ) {
177                 // calculate approximate distance from view point
178                 sgdCopyVec3( abs_view_pos,
179                              globals->get_current_view()->get_abs_view_pos() );
180
181                 SG_LOG( SG_TERRAIN, SG_DEBUG, "DIST Abs view pos = " 
182                         << abs_view_pos[0] << ","
183                         << abs_view_pos[1] << ","
184                         << abs_view_pos[2] );
185                 SG_LOG( SG_TERRAIN, SG_DEBUG,
186                         "    ref point = " << e->center );
187
188                 sgdVec3 center;
189                 sgdSetVec3( center,
190                             e->center.x(), e->center.y(), e->center.z() );
191                 dist = sgdDistanceVec3( center, abs_view_pos );
192
193                 SG_LOG( SG_TERRAIN, SG_DEBUG, "    distance = " << dist );
194
195                 if ( dist > max_dist ) {
196                     max_dist = dist;
197                     max_index = index;
198                 }
199             }
200         }
201
202         // If we made it this far, then there were no open cache entries.
203         // We will instead free the furthest cache entry and return it's
204         // index.
205
206         if ( max_index >= 0 ) {
207             SG_LOG( SG_TERRAIN, SG_DEBUG, "    max_dist = " << max_dist );
208             SG_LOG( SG_TERRAIN, SG_DEBUG, "    index = " << max_index );
209             entry_free( max_index );
210         } else {
211             SG_LOG( SG_TERRAIN, SG_ALERT, "WHOOPS!!! Dying in next_avail()" );
212             exit( -1 );
213         }
214     }
215 }
216
217
218 /**
219  * Create a new tile and schedule it for loading.
220  */
221 void
222 FGNewCache::insert_tile( FGTileEntry *e )
223 {
224     // clear out a distant entry in the cache if needed.
225     make_space();
226
227     // register it in the cache
228     long tile_index = e->get_tile_bucket().gen_index();
229     tile_cache[tile_index] = e;
230
231 }