]> git.mxchange.org Git - flightgear.git/blob - src/Scenery/FGTileLoader.cxx
Don't scale elevator by 0.5.
[flightgear.git] / src / Scenery / FGTileLoader.cxx
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 #ifdef HAVE_CONFIG_H
24 #  include <config.h>
25 #endif
26
27 #include <simgear/compiler.h>
28
29 #include <Main/globals.hxx>
30 #include "FGTileLoader.hxx"
31 #include "tileentry.hxx"
32 #include "tilemgr.hxx"
33
34 extern ssgBranch *terrain;
35 extern ssgBranch *ground;
36
37 /**
38  * 
39  */
40 FGTileLoader::FGTileLoader()
41 {
42 #ifdef ENABLE_THREADS
43     // Create and start the loader threads.
44     for (int i = 0; i < MAX_THREADS; ++i)
45     {
46         threads[i] = new LoaderThread(this);
47         threads[i]->start();
48     }
49 #endif // ENABLE_THREADS
50 }
51
52 /**
53  * Terminate all threads.
54  */
55 FGTileLoader::~FGTileLoader()
56 {
57 #ifdef ENABLE_THREADS
58     // Wake up its time to die.
59     // queue_cond.broadcast();
60
61     for (int i = 0; i < MAX_THREADS; ++i)
62     {
63         threads[i]->cancel();
64         threads[i]->join();
65     }    
66 #endif // ENABLE_THREADS
67 }
68
69
70 #if 0 // we don't ever want to do this I don't think
71 /**
72  * 
73  */
74 void FGTileLoader::reinit() {
75     while ( !tile_load_queue.empty() ) {
76         tile_load_queue.pop();
77     }
78 }
79 #endif
80
81
82 /**
83  * 
84  */
85 void
86 FGTileLoader::add( FGTileEntry* tile )
87 {
88     /**
89      * Initialise tile_path here and not in ctor to avoid problems
90      * with the initialastion order of global objects.
91      */
92     static bool beenhere = false;
93     if (!beenhere)
94     {
95         if ( !globals->get_fg_scenery().empty() ) {
96             tile_path = globals->get_fg_scenery();
97         } else {
98             SGPath tmp;
99             tmp.set( globals->get_fg_root() );
100             tmp.append( "Scenery" );
101             tile_path = tmp.str();
102         }
103         beenhere = true;
104     }
105
106     tile_load_queue.push( tile );
107 }
108
109 #ifdef WISH_PLIB_WAS_THREADED // but it isn't
110 /**
111  * 
112  */
113 void
114 FGTileLoader::remove( FGTileEntry* tile )
115 {
116     tile_free_queue.push( tile );
117 }
118 #endif
119
120 /**
121  * 
122  */
123 void
124 FGTileLoader::update()
125 {
126
127 #ifdef ENABLE_THREADS
128     // send a signal to the pager thread that it is allowed to load
129     // another tile
130     mutex.lock();
131     frame_cond.signal();
132     mutex.unlock();
133 #else
134     if ( !tile_load_queue.empty() ) {
135         // cout << "loading next tile ..." << endl;
136         // load the next tile in the queue
137         FGTileEntry* tile = tile_load_queue.front();
138         tile_load_queue.pop();
139
140         tile->load( tile_path, true );
141
142         FGTileMgr::ready_to_attach( tile );
143     }
144
145 #ifdef WISH_PLIB_WAS_THREADED // but it isn't
146     if ( !tile_free_queue.empty() ) {
147         // cout << "freeing next tile ..." << endl;
148         // free the next tile in the queue
149         FGTileEntry* tile = tile_free_queue.front();
150         tile_free_queue.pop();
151         tile->free_tile();
152         delete tile;
153     }
154 #endif
155
156 #endif // ENABLE_THREADS
157
158 }
159
160
161 #ifdef ENABLE_THREADS
162 /**
163  * 
164  */
165 void
166 FGTileLoader::LoaderThread::run()
167 {
168     pthread_cleanup_push( cleanup_handler, loader );
169     while ( true ) {
170         // Wait for a load request to be placed in the queue.
171         FGTileEntry* tile = loader->tile_load_queue.pop();
172
173         // Wait for the next frame signal before we load a tile from the queue
174         loader->mutex.lock();
175         loader->frame_cond.wait( loader->mutex );
176         loader->mutex.unlock();
177
178         set_cancel( SGThread::CANCEL_DISABLE );
179         tile->load( loader->tile_path, true );
180         set_cancel( SGThread::CANCEL_DEFERRED );
181
182         FGTileMgr::ready_to_attach( tile );
183
184 #ifdef WISH_PLIB_WAS_THREADED // but it isn't
185         // Handle and pending removals
186         while ( !loader->tile_free_queue.empty() ) {
187             // cout << "freeing next tile ..." << endl;
188             // free the next tile in the queue
189             FGTileEntry* tile = loader->tile_free_queue.pop();
190             tile->free_tile();
191             delete tile;
192         }
193 #endif
194
195     }
196     pthread_cleanup_pop(1);
197 }
198
199 /**
200  * Ensure mutex is unlocked.
201  */
202 void 
203 cleanup_handler( void* arg )
204 {
205     FGTileLoader* loader = (FGTileLoader*) arg;
206     loader->mutex.unlock();
207 }
208 #endif // ENABLE_THREADS