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