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