]> git.mxchange.org Git - simgear.git/blob - simgear/scene/tgdb/TileCache.hxx
Merge branch 'jmt/ref_ptr-conv'
[simgear.git] / simgear / scene / tgdb / TileCache.hxx
1 // newcache.hxx -- routines to handle scenery tile caching
2 //
3 // Written by Curtis Olson, started December 2000.
4 //
5 // Copyright (C) 2000  Curtis L. Olson  - http://www.flightgear.org/~curt
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21 // $Id$
22
23
24 #ifndef _TILECACHE_HXX
25 #define _TILECACHE_HXX
26
27 #include <map>
28
29 #include <simgear/bucket/newbucket.hxx>
30 #include <simgear/math/point3d.hxx>
31 #include <simgear/scene/tgdb/TileEntry.hxx>
32
33 using std::map;
34
35 namespace simgear {
36
37 // A class to store and manage a pile of tiles
38 class TileCache {
39 public:
40     typedef map < long, simgear::TileEntry * > tile_map;
41     typedef tile_map::iterator tile_map_iterator;
42     typedef tile_map::const_iterator const_tile_map_iterator;
43 private:
44     // cache storage space
45     tile_map tile_cache;
46
47     // maximum cache size
48     int max_cache_size;
49
50     // pointers to allow an external linear traversal of cache entries
51     tile_map_iterator current;
52
53     // Free a tile cache entry
54     void entry_free( long cache_index );
55
56     double current_time;
57
58 public:
59     tile_map_iterator begin() { return tile_cache.begin(); }
60     tile_map_iterator end() { return tile_cache.end(); }
61     const_tile_map_iterator begin() const { return tile_cache.begin(); }
62     const_tile_map_iterator end() const { return tile_cache.end(); }
63
64     // Constructor
65     TileCache();
66
67     // Destructor
68     ~TileCache();
69
70     // Initialize the tile cache subsystem
71     void init( void );
72
73     // Check if the specified "bucket" exists in the cache
74     bool exists( const SGBucket& b ) const;
75
76     // Return the index of the oldest tile in the cache, return -1 if
77     // nothing available to be removed.
78     long get_oldest_tile();
79
80     // Clear the inner ring flag for all tiles in the cache so that
81     // the external tile scheduler can flag the inner ring correctly.
82     void clear_inner_ring_flags();
83
84     // Clear a cache entry, note that the cache only holds pointers
85     // and this does not free the object which is pointed to.
86     void clear_entry( long cache_entry );
87
88     // Clear all completely loaded tiles (ignores partially loaded tiles)
89     void clear_cache();
90
91     // Return a pointer to the specified tile cache entry
92     inline simgear::TileEntry *get_tile( const long tile_index ) const {
93         const_tile_map_iterator it = tile_cache.find( tile_index );
94         if ( it != tile_cache.end() ) {
95             return it->second;
96         } else {
97             return NULL;
98         }
99     }
100
101     // Return a pointer to the specified tile cache entry
102     inline simgear::TileEntry *get_tile( const SGBucket& b ) const {
103         return get_tile( b.gen_index() );
104     }
105
106     // Return the cache size
107     inline size_t get_size() const { return tile_cache.size(); }
108
109     // External linear traversal of cache
110     inline void reset_traversal() { current = tile_cache.begin(); }
111     inline bool at_end() { return current == tile_cache.end(); }
112     inline simgear::TileEntry *get_current() const {
113         // cout << "index = " << current->first << endl;
114         return current->second;
115     }
116     inline void next() { ++current; }
117
118     inline int get_max_cache_size() const { return max_cache_size; }
119     inline void set_max_cache_size( int m ) { max_cache_size = m; }
120
121     /**
122      * Create a new tile and enqueue it for loading.
123      * @param b
124      * @return success/failure
125      */
126     bool insert_tile( simgear::TileEntry* e );
127
128     void set_current_time(double val) { current_time = val; }
129     double get_current_time() const { return current_time; }
130 };
131
132 }
133
134 #endif // _TILECACHE_HXX