]> git.mxchange.org Git - flightgear.git/blob - src/Scenery/FGTileLoader.hxx
Tile pager tweaks.
[flightgear.git] / src / Scenery / FGTileLoader.hxx
1 // FGTileLoader - Queue scenery tiles for loading.
2 //
3 // Written by Bernie Bright, started March 2001.
4 //
5 // Copyright (C) 2001  Bernard Bright - bbright@bigpond.net.au
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 #ifndef FG_TILE_LOADER_HXX
25 #define FG_TILE_LOADER_HXX
26
27 #include <simgear/bucket/newbucket.hxx>
28 #include <simgear/misc/sg_path.hxx>
29
30 #ifdef ENABLE_THREADS
31 #  include <simgear/threads/SGThread.hxx>
32 #  include <simgear/threads/SGQueue.hxx>
33 #else
34 #  include <queue>
35 #endif
36
37 // Forward reference.
38 class FGTileEntry;
39
40 /**
41  * Queues tiles for loading, possibly by a separate thread.
42  */
43 class FGTileLoader
44 {
45 public:
46
47     /**
48      * Constructor.
49      */
50     FGTileLoader();
51
52     /**
53      * Destructor.
54      */
55     ~FGTileLoader();
56
57     /**
58      * Flush anything in pending load queue without doing the work
59      * Leave the free queue intact since that's are only record of
60      * things we need to remove.
61      */
62     void reinit();
63     
64     /**
65      * Add a tile to the end of the load queue.
66      * @param tile The tile to be loaded from disk.
67      */
68     void add( FGTileEntry* tile );
69
70 #ifdef WISH_PLIB_WAS_THREADED // but it isn't
71     /**
72      * Remove a tile from memory.
73      * @param tile The tile to be removed from memory.
74      */
75     void remove( FGTileEntry* tile );
76 #endif
77
78     /**
79      * The tile loader thread will only load one tile per call to the
80      * update() method.  This is a way to spread out the work of the
81      * tile loader and slow it down so it is less intrusive.  For
82      * systems built without thead support this is a no-op.
83      */
84     void update();
85
86     /**
87      * Returns whether the load queue is empty (contains no elements).
88      * @return true if load queue is empty otherwise returns false.
89      */
90     // bool empty() const { return tile_load_queue.empty(); }
91
92 private:
93
94 private:
95
96 #ifdef ENABLE_THREADS
97     /**
98      * FIFO queue of tiles to load from data files.
99      */
100     SGBlockingQueue< FGTileEntry * > tile_load_queue;
101     // SGBlockingQueue< FGTileEntry * > tile_free_queue;
102 #else
103     queue< FGTileEntry * > tile_load_queue;
104     // queue< FGTileEntry * > tile_free_queue;
105 #endif
106
107     /**
108      * Base name of directory containing tile data file.
109      */
110     SGPath tile_path;
111
112 #ifdef ENABLE_THREADS
113     /**
114      * Maximum number of threads to create for loading tiles.
115      */
116     enum { MAX_THREADS = 1 };
117
118     /**
119      * This class represents the thread of execution responsible for
120      * loading a tile.
121      */
122     class LoaderThread : public SGThread
123     {
124     public:
125         LoaderThread( FGTileLoader* l ) : loader(l) {}
126         ~LoaderThread() {}
127
128         /**
129          * Reads the tile from disk.
130          */
131         void run();
132
133     private:
134         FGTileLoader* loader;
135
136     private:
137         // not implemented.
138         LoaderThread();
139         LoaderThread( const LoaderThread& );
140         LoaderThread& operator=( const LoaderThread& );
141     };
142
143     friend class LoaderThread;
144
145     /**
146      * Array of loading threads.
147      */
148     LoaderThread* threads[ MAX_THREADS ];
149     
150     /**
151      * Lock and synchronize access to tile queue.
152      */
153     SGMutex mutex;
154     SGCondition frame_cond;
155
156     /**
157      * Thread cleanup handler.
158      */
159     friend void cleanup_handler( void* );
160 #endif // ENABLE_THREADS
161 };
162
163 #endif // FG_TILE_LOADER_HXX