]> git.mxchange.org Git - flightgear.git/blob - src/Scenery/newcache.cxx
Move FGControls declaration to globals.hxx
[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 <Main/viewer.hxx>
43 #include <Scenery/scenery.hxx>  // for scenery.center
44
45 #include "newcache.hxx"
46 #include "tileentry.hxx"
47
48
49 SG_USING_NAMESPACE(std);
50
51
52 // Constructor
53 FGNewCache::FGNewCache( void ) :
54     max_cache_size(50)
55 {
56     tile_cache.clear();
57 }
58
59
60 // Destructor
61 FGNewCache::~FGNewCache( void ) {
62 }
63
64
65 // Free a tile cache entry
66 void FGNewCache::entry_free( long cache_index ) {
67     SG_LOG( SG_TERRAIN, SG_DEBUG, "FREEING CACHE ENTRY = " << cache_index );
68     FGTileEntry *tile = tile_cache[cache_index];
69     tile->disconnect_ssg_nodes();
70
71 #ifdef WISH_PLIB_WAS_THREADED // but it isn't
72     tile->sched_removal();
73 #else
74     tile->free_tile();
75     delete tile;
76 #endif
77
78     tile_cache.erase( cache_index );
79 }
80
81
82 // Initialize the tile cache subsystem
83 void FGNewCache::init( void ) {
84     SG_LOG( SG_TERRAIN, SG_INFO, "Initializing the tile cache." );
85
86     SG_LOG( SG_TERRAIN, SG_INFO, "  max cache size = " 
87             << max_cache_size );
88     SG_LOG( SG_TERRAIN, SG_INFO, "  current cache size = " 
89             << tile_cache.size() );
90
91 #if 0 // don't clear the cache right now
92     clear_cache();
93 #endif
94
95     SG_LOG( SG_TERRAIN, SG_INFO, "  done with init()"  );
96 }
97
98
99 // Search for the specified "bucket" in the cache
100 bool FGNewCache::exists( const SGBucket& b ) const {
101     long tile_index = b.gen_index();
102     const_tile_map_iterator it = tile_cache.find( tile_index );
103
104     return ( it != tile_cache.end() );
105 }
106
107
108 // depricated for threading
109 #if 0
110 // Fill in a tile cache entry with real data for the specified bucket
111 void FGNewCache::fill_in( const SGBucket& b ) {
112     SG_LOG( SG_TERRAIN, SG_DEBUG, "FILL IN CACHE ENTRY = " << b.gen_index() );
113
114     // clear out a distant entry in the cache if needed.
115     make_space();
116
117     // create the entry
118     FGTileEntry *e = new FGTileEntry( b );
119
120     // register it in the cache
121     long tile_index = b.gen_index();
122     tile_cache[tile_index] = e;
123
124     SGPath tile_path;
125     if ( globals->get_fg_scenery() != (string)"" ) {
126         tile_path.set( globals->get_fg_scenery() );
127     } else {
128         tile_path.set( globals->get_fg_root() );
129         tile_path.append( "Scenery" );
130     }
131     
132     // Load the appropriate data file
133     e->load( tile_path, true );
134 }
135 #endif
136
137
138 // Ensure at least one entry is free in the cache
139 bool FGNewCache::make_space() {
140     SG_LOG( SG_TERRAIN, SG_DEBUG, "Make space in cache" );
141     SG_LOG( SG_TERRAIN, SG_DEBUG, "cache entries = " << tile_cache.size() );
142     SG_LOG( SG_TERRAIN, SG_DEBUG, "max size = " << max_cache_size );
143
144     if ( (int)tile_cache.size() < max_cache_size ) {
145         // space in the cache, return
146         return true;
147     }
148
149     while ( (int)tile_cache.size() >= max_cache_size ) {
150         sgdVec3 abs_view_pos;
151         float dist;
152         float max_dist = 0.0;
153         int max_index = -1;
154
155         // we need to free the furthest entry
156         tile_map_iterator current = tile_cache.begin();
157         tile_map_iterator end = tile_cache.end();
158     
159         for ( ; current != end; ++current ) {
160             long index = current->first;
161             FGTileEntry *e = current->second;
162
163             if ( e->is_loaded() && e->get_pending_models() == 0 ) {
164                 // calculate approximate distance from view point
165                 sgdCopyVec3( abs_view_pos,
166                              globals->get_current_view()->get_abs_view_pos() );
167
168                 SG_LOG( SG_TERRAIN, SG_DEBUG, "DIST Abs view pos = " 
169                         << abs_view_pos[0] << ","
170                         << abs_view_pos[1] << ","
171                         << abs_view_pos[2] );
172                 SG_LOG( SG_TERRAIN, SG_DEBUG,
173                         "    ref point = " << e->center );
174
175                 sgdVec3 center;
176                 sgdSetVec3( center,
177                             e->center.x(), e->center.y(), e->center.z() );
178                 dist = sgdDistanceVec3( center, abs_view_pos );
179
180                 SG_LOG( SG_TERRAIN, SG_DEBUG, "    distance = " << dist );
181
182                 if ( dist > max_dist ) {
183                     max_dist = dist;
184                     max_index = index;
185                 }
186             }
187         }
188
189         // If we made it this far, then there were no open cache entries.
190         // We will instead free the furthest cache entry and return true
191
192         if ( max_index >= 0 ) {
193             SG_LOG( SG_TERRAIN, SG_DEBUG, "    max_dist = " << max_dist );
194             SG_LOG( SG_TERRAIN, SG_DEBUG, "    index = " << max_index );
195             entry_free( max_index );
196             return true;
197         } else {
198             SG_LOG( SG_TERRAIN, SG_ALERT, "WHOOPS!!! can't make_space(), tile "
199                     "cache is full, but no entries available to removal." );
200             return false;
201         }
202     }
203 }
204
205
206 // Clear all completely loaded tiles (ignores partially loaded tiles)
207 void FGNewCache::clear_cache() {
208     // This is a hack that should really get cleaned up at some point
209     extern ssgBranch *terrain;
210
211     tile_map_iterator current = tile_cache.begin();
212     tile_map_iterator end = tile_cache.end();
213     
214     for ( ; current != end; ++current ) {
215         long index = current->first;
216         SG_LOG( SG_TERRAIN, SG_DEBUG, "clearing " << index );
217         FGTileEntry *e = current->second;
218         if ( e->is_loaded() && e->get_pending_models() == 0 ) {
219             e->tile_bucket.make_bad();
220             entry_free(index);
221         }
222     }
223
224     // and ... just in case we missed something ... 
225     terrain->removeAllKids();
226 }
227
228
229 /**
230  * Create a new tile and schedule it for loading.
231  */
232 bool FGNewCache::insert_tile( FGTileEntry *e ) {
233     // clear out a distant entry in the cache if needed.
234     if ( make_space() ) {
235       // register it in the cache
236       long tile_index = e->get_tile_bucket().gen_index();
237       tile_cache[tile_index] = e;
238
239       return true;
240     } else {
241       // failed to find cache space
242
243       return false;
244     }
245 }