]> git.mxchange.org Git - flightgear.git/blob - src/Scenery/tilecache.hxx
Canvas: update for new bounding box getters.
[flightgear.git] / src / Scenery / tilecache.hxx
1 // TileCache.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 "tileentry.hxx"
31
32 using std::map;
33
34 // A class to store and manage a pile of tiles
35 class TileCache {
36 public:
37     typedef map < long, TileEntry * > tile_map;
38     typedef tile_map::iterator tile_map_iterator;
39     typedef tile_map::const_iterator const_tile_map_iterator;
40 private:
41     // cache storage space
42     tile_map tile_cache;
43
44     // maximum cache size
45     int max_cache_size;
46
47     // pointers to allow an external linear traversal of cache entries
48     tile_map_iterator current;
49
50     double current_time;
51
52     // Free a tile cache entry
53     void entry_free( long cache_index );
54
55 public:
56     tile_map_iterator begin() { return tile_cache.begin(); }
57     tile_map_iterator end() { return tile_cache.end(); }
58     const_tile_map_iterator begin() const { return tile_cache.begin(); }
59     const_tile_map_iterator end() const { return tile_cache.end(); }
60
61     // Constructor
62     TileCache();
63
64     // Destructor
65     ~TileCache();
66
67     // Initialize the tile cache subsystem
68     void init( void );
69
70     // Check if the specified "bucket" exists in the cache
71     bool exists( const SGBucket& b ) const;
72
73     // Return the index of a tile to be dropped from the cache, return -1 if
74     // nothing available to be removed.
75     long get_drop_tile();
76     
77     // Clear all flags indicating tiles belonging to the current view
78     void clear_current_view();
79
80     // Clear a cache entry, note that the cache only holds pointers
81     // and this does not free the object which is pointed to.
82     void clear_entry( long cache_entry );
83
84     // Clear all completely loaded tiles (ignores partially loaded tiles)
85     void clear_cache();
86
87     // Return a pointer to the specified tile cache entry
88     inline TileEntry *get_tile( const long tile_index ) const {
89         const_tile_map_iterator it = tile_cache.find( tile_index );
90         if ( it != tile_cache.end() ) {
91             return it->second;
92         } else {
93             return NULL;
94         }
95     }
96
97     // Return a pointer to the specified tile cache entry
98     inline TileEntry *get_tile( const SGBucket& b ) const {
99         return get_tile( b.gen_index() );
100     }
101
102     // Return the cache size
103     inline size_t get_size() const { return tile_cache.size(); }
104
105     // External linear traversal of cache
106     inline void reset_traversal() { current = tile_cache.begin(); }
107     inline bool at_end() { return current == tile_cache.end(); }
108     inline TileEntry *get_current() const {
109         // cout << "index = " << current->first << endl;
110         return current->second;
111     }
112     inline void next() { ++current; }
113
114     inline int get_max_cache_size() const { return max_cache_size; }
115     inline void set_max_cache_size( int m ) { max_cache_size = m; }
116
117     /**
118      * Create a new tile and enqueue it for loading.
119      * @param b
120      * @return success/failure
121      */
122     bool insert_tile( TileEntry* e );
123
124     void set_current_time(double val) { current_time = val; }
125     double get_current_time() const { return current_time; }
126
127     // update tile's priority and expiry time according to current request
128     void request_tile(TileEntry* t,float priority,bool current_view,double requesttime);
129 };
130
131 #endif // _TILECACHE_HXX