]> git.mxchange.org Git - flightgear.git/blobdiff - src/Scenery/FGTileLoader.cxx
Make use of the ground material types
[flightgear.git] / src / Scenery / FGTileLoader.cxx
index 3465e0f2bf110daa9e0040ae705d36bc73a5bf7b..e8fee269a1e105902b516b583e86de91b2faaa3e 100644 (file)
@@ -16,7 +16,7 @@
 //
 // You should have received a copy of the GNU General Public License
 // along with this program; if not, write to the Free Software
-// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 //
 // $Id$
 
 #  include <config.h>
 #endif
 
+#include <simgear/compiler.h>
+#include <simgear/structure/exception.hxx>
+
 #include <Main/globals.hxx>
 #include "FGTileLoader.hxx"
 #include "tileentry.hxx"
+#include "tilemgr.hxx"
+
+extern ssgBranch *terrain;
+extern ssgBranch *ground;
+
 
 /**
  * 
  */
 FGTileLoader::FGTileLoader()
 {
-#ifdef ENABLE_THREADS
+#if defined(ENABLE_THREADS)
     // Create and start the loader threads.
     for (int i = 0; i < MAX_THREADS; ++i)
     {
        threads[i] = new LoaderThread(this);
-       threads[i]->start();
+       threads[i]->start( 1 );
     }
 #endif // ENABLE_THREADS
 }
@@ -48,9 +56,9 @@ FGTileLoader::FGTileLoader()
  */
 FGTileLoader::~FGTileLoader()
 {
-#ifdef ENABLE_THREADS
+#if defined(ENABLE_THREADS)
     // Wake up its time to die.
-    queue_cond.broadcast();
+    // queue_cond.broadcast();
 
     for (int i = 0; i < MAX_THREADS; ++i)
     {
@@ -60,6 +68,19 @@ FGTileLoader::~FGTileLoader()
 #endif // ENABLE_THREADS
 }
 
+
+#if 0 // we don't ever want to do this I don't think
+/**
+ * 
+ */
+void FGTileLoader::reinit() {
+    while ( !tile_load_queue.empty() ) {
+       tile_load_queue.pop();
+    }
+}
+#endif
+
+
 /**
  * 
  */
@@ -68,30 +89,30 @@ FGTileLoader::add( FGTileEntry* tile )
 {
     /**
      * Initialise tile_path here and not in ctor to avoid problems
-     * with the initialastion order of global objects.
+     * with the initialisation order of global objects.
      */
     static bool beenhere = false;
-    if (!beenhere)
-    {
-       if ( globals->get_fg_scenery() != (string)"" ) {
-           tile_path.set( globals->get_fg_scenery() );
-       } else {
-           tile_path.set( globals->get_fg_root() );
-           tile_path.append( "Scenery" );
-       }
-       beenhere = true;
+    if (!beenhere) {
+        tile_path = globals->get_fg_scenery();
+        if (!tile_path.size())
+            throw sg_throwable(string("No valid scenery path defined!"));
+
+        beenhere = true;
     }
 
-#ifdef ENABLE_THREADS
-    mutex.lock();
-    tile_queue.push( tile );
-    // Signal waiting working threads.
-    queue_cond.signal();
-    mutex.unlock();
-#else
-    tile->load( tile_path, true );
-#endif // ENABLE_THREADS
+    tile_load_queue.push( tile );
+}
+
+#ifdef WISH_PLIB_WAS_THREADED // but it isn't
+/**
+ * 
+ */
+void
+FGTileLoader::remove( FGTileEntry* tile )
+{
+    tile_free_queue.push( tile );
 }
+#endif
 
 /**
  * 
@@ -99,15 +120,52 @@ FGTileLoader::add( FGTileEntry* tile )
 void
 FGTileLoader::update()
 {
-#ifdef ENABLE_THREADS
+
+#if defined(ENABLE_THREADS)
+    // send a signal to the pager thread that it is allowed to load
+    // another tile
     mutex.lock();
     frame_cond.signal();
     mutex.unlock();
+#else
+    if ( !tile_load_queue.empty() ) {
+        // cout << "loading next tile ..." << endl;
+        // load the next tile in the queue
+        FGTileEntry* tile = tile_load_queue.front();
+        tile_load_queue.pop();
+
+        tile->load( tile_path, true );
+
+        FGTileMgr::ready_to_attach( tile );
+    }
+
+#ifdef WISH_PLIB_WAS_THREADED // but it isn't
+    if ( !tile_free_queue.empty() ) {
+        // cout << "freeing next tile ..." << endl;
+        // free the next tile in the queue
+        FGTileEntry* tile = tile_free_queue.front();
+        tile_free_queue.pop();
+       tile->free_tile();
+       delete tile;
+    }
+#endif
+
 #endif // ENABLE_THREADS
+
 }
 
 
-#ifdef ENABLE_THREADS
+#if defined(ENABLE_THREADS)
+/**
+ * Ensure mutex is unlocked.
+ */
+void 
+cleanup_handler( void* arg )
+{
+    FGTileLoader* loader = (FGTileLoader*) arg;
+    loader->mutex.unlock();
+}
+
 /**
  * 
  */
@@ -117,43 +175,31 @@ FGTileLoader::LoaderThread::run()
     pthread_cleanup_push( cleanup_handler, loader );
     while ( true ) {
        // Wait for a load request to be placed in the queue.
-       loader->mutex.lock();
-       while (loader->empty())
-       {
-           loader->queue_cond.wait( loader->mutex );
-       }
-
-       // Have we been canceled - exits if yes.
-       //pthread_testcancel();
-       if (loader->empty())
-       {
-           loader->mutex.unlock();
-           pthread_exit( PTHREAD_CANCELED );
-       }
+       FGTileEntry* tile = loader->tile_load_queue.pop();
 
         // Wait for the next frame signal before we load a tile from the queue
-        // Note that loader->mutex is already locked at this point.
+        loader->mutex.lock();
         loader->frame_cond.wait( loader->mutex );
-
-       // Grab the tile to load and release the mutex.
-       FGTileEntry* tile = loader->tile_queue.front();
-       loader->tile_queue.pop();
-       loader->mutex.unlock();
+        loader->mutex.unlock();
 
        set_cancel( SGThread::CANCEL_DISABLE );
        tile->load( loader->tile_path, true );
        set_cancel( SGThread::CANCEL_DEFERRED );
+
+       FGTileMgr::ready_to_attach( tile );
+
+#ifdef WISH_PLIB_WAS_THREADED // but it isn't
+       // Handle and pending removals
+       while ( !loader->tile_free_queue.empty() ) {
+           // cout << "freeing next tile ..." << endl;
+           // free the next tile in the queue
+           FGTileEntry* tile = loader->tile_free_queue.pop();
+           tile->free_tile();
+           delete tile;
+       }
+#endif
+
     }
     pthread_cleanup_pop(1);
 }
-
-/**
- * Ensure mutex is unlocked.
- */
-void 
-cleanup_handler( void* arg )
-{
-    FGTileLoader* loader = (FGTileLoader*) arg;
-    loader->mutex.unlock();
-}
 #endif // ENABLE_THREADS