]> git.mxchange.org Git - flightgear.git/blob - src/Scenery/FGTileLoader.hxx
Tweaks so tile loading still works in non-threaded mode.
[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 #endif
34
35 // Forward reference.
36 class FGTileEntry;
37
38 /**
39  * Queues tiles for loading, possibly by a separate thread.
40  */
41 class FGTileLoader
42 {
43 public:
44
45     /**
46      * Constructor.
47      */
48     FGTileLoader();
49
50     /**
51      * Destructor.
52      */
53     ~FGTileLoader();
54
55     /**
56      * Add a tile to the end of the load queue.
57      * @param tile The tile to be loaded from disk.
58      * @param vis Current visibilty (in feet?) (see FGTileMgr::vis).
59      */
60     void add( FGTileEntry* tile );
61
62     /**
63      * The tile loader thread will only load one tile per call to the
64      * update() method.  This is a way to spread out the work of the
65      * tile loader and slow it down so it is less intrusive.  For
66      * systems built without thead support this is a no-op.
67      */
68     void update();
69
70     /**
71      * Returns whether the load queue is empty (contains no elements).
72      * @return true if load queue is empty otherwise returns false.
73      */
74     // bool empty() const { return tile_queue.empty(); }
75
76 private:
77
78 private:
79
80 #ifdef ENABLE_THREADS
81     /**
82      * FIFO queue of tiles to load from data files.
83      */
84     SGBlockingQueue< FGTileEntry* > tile_queue;
85 #endif
86
87     /**
88      * Base name of directory containing tile data file.
89      */
90     SGPath tile_path;
91
92 #ifdef ENABLE_THREADS
93     /**
94      * Maximum number of threads to create for loading tiles.
95      */
96     enum { MAX_THREADS = 1 };
97
98     /**
99      * This class represents the thread of execution responsible for
100      * loading a tile.
101      */
102     class LoaderThread : public SGThread
103     {
104     public:
105         LoaderThread( FGTileLoader* l ) : loader(l) {}
106         ~LoaderThread() {}
107
108         /**
109          * Reads the tile from disk.
110          */
111         void run();
112
113     private:
114         FGTileLoader* loader;
115
116     private:
117         // not implemented.
118         LoaderThread();
119         LoaderThread( const LoaderThread& );
120         LoaderThread& operator=( const LoaderThread& );
121     };
122
123     friend class LoaderThread;
124
125     /**
126      * Array of loading threads.
127      */
128     LoaderThread* threads[ MAX_THREADS ];
129     
130     /**
131      * Lock and synchronize access to tile queue.
132      */
133     SGMutex mutex;
134     SGCondition frame_cond;
135
136     /**
137      * Thread cleanup handler.
138      */
139     friend void cleanup_handler( void* );
140 #endif // ENABLE_THREADS
141 };
142
143 #endif // FG_TILE_LOADER_HXX