]> git.mxchange.org Git - flightgear.git/commitdiff
Introduce some structure in fgMainLoop.
authorThorstenB <brehmt@gmail.com>
Sun, 20 Nov 2011 15:20:05 +0000 (16:20 +0100)
committerThorstenB <brehmt@gmail.com>
Sun, 20 Nov 2011 15:21:15 +0000 (16:21 +0100)
- Move some code to sub functions.
- Make sound manager a subsystem-manager member (and make sure it's
still processed last).

src/Main/fg_init.cxx
src/Main/globals.cxx
src/Main/globals.hxx
src/Main/main.cxx

index 8d01e88e66fd7588044e0be827eead47eaf0deb8..807606916fe2dfd61e2b8ef942d0398ce8a5c909 100644 (file)
@@ -1181,6 +1181,15 @@ bool fgInitSubsystems() {
     SG_LOG( SG_GENERAL, SG_INFO, "Initialize Subsystems");
     SG_LOG( SG_GENERAL, SG_INFO, "========== ==========");
 
+    ////////////////////////////////////////////////////////////////////
+    // Initialize the sound subsystem.
+    ////////////////////////////////////////////////////////////////////
+    // Sound manager uses an own subsystem group "SOUND" which is the last
+    // to be updated in every loop.
+    // Sound manager is updated last so it can use the CPU while the GPU
+    // is processing the scenery (doubled the frame-rate for me) -EMH-
+    globals->add_subsystem("sound", new SGSoundMgr, SGSubsystemMgr::SOUND);
+
     ////////////////////////////////////////////////////////////////////
     // Initialize the event manager subsystem.
     ////////////////////////////////////////////////////////////////////
index e2ea2954bbc00ed9ba492790a54fbbab464b4e54..00dd943dc49378181e4ab2fd40686ab26d14b173 100644 (file)
@@ -125,7 +125,6 @@ FGGlobals::FGGlobals() :
     renderer( new FGRenderer ),
     subsystem_mgr( new SGSubsystemMgr ),
     event_mgr( new SGEventMgr ),
-    soundmgr( new SGSoundMgr ),
     sim_time_sec( 0.0 ),
     fg_root( "" ),
     time_params( NULL ),
@@ -151,7 +150,7 @@ FGGlobals::FGGlobals() :
     dmelist( NULL ),
     tacanlist( NULL ),
     carrierlist( NULL ),
-    channellist( NULL )    
+    channellist( NULL )
 {
   simgear::ResourceManager::instance()->addProvider(new AircraftResourceProvider());
   simgear::PropertyObjectBase::setDefaultRoot(props);
@@ -206,9 +205,6 @@ FGGlobals::~FGGlobals()
     delete tacanlist;
     delete carrierlist;
     delete channellist;
-
-    soundmgr->unbind();
-    delete soundmgr;
 }
 
 
@@ -360,7 +356,10 @@ FGGlobals::add_subsystem (const char * name,
 SGSoundMgr *
 FGGlobals::get_soundmgr () const
 {
-    return soundmgr;
+    if (subsystem_mgr)
+        return (SGSoundMgr*) subsystem_mgr->get_subsystem("sound");
+
+    return NULL;
 }
 
 SGEventMgr *
index 52df7f514927ef67ef245150ed71fd54777b3558..11dc124dc3129d41983f94233db6275f9ee19501 100644 (file)
@@ -91,7 +91,6 @@ private:
     FGRenderer *renderer;
     SGSubsystemMgr *subsystem_mgr;
     SGEventMgr *event_mgr;
-    SGSoundMgr *soundmgr;
 
     // Number of milliseconds elapsed since the start of the program.
     double sim_time_sec;
index 1cfdd7dbfe3489bd3c8643d21ea068077fa1fd3b..d6036771af883222ab7729f731727da150c3e408 100644 (file)
@@ -93,42 +93,40 @@ using std::cerr;
 // splash screen up and running right away.
 int idle_state = 0;
 
-
-void fgInitSoundManager();
-void fgSetNewSoundDevice(const char *);
-
 // The atexit() function handler should know when the graphical subsystem
 // is initialized.
 extern int _bootstrap_OSInit;
 
-// What should we do when we have nothing else to do?  Let's get ready
-// for the next move and update the display?
-static void fgMainLoop( void )
+
+void fgInitSoundManager()
 {
-    static SGPropertyNode_ptr frame_signal
-        = fgGetNode("/sim/signals/frame", true);
+    SGSoundMgr *smgr = globals->get_soundmgr();
 
-    frame_signal->fireValueChanged();
-    
-    SG_LOG( SG_GENERAL, SG_DEBUG, "Running Main Loop");
-    SG_LOG( SG_GENERAL, SG_DEBUG, "======= ==== ====");
-    
-    
-  // update "time"
-    double sim_dt, real_dt;
-    TimeManager* timeMgr = (TimeManager*) globals->get_subsystem("time");
-    // compute simulated time (allowing for pause, warp, etc) and
-    // real elapsed time
-    timeMgr->computeTimeDeltas(sim_dt, real_dt);
+    smgr->bind();
+    smgr->init(fgGetString("/sim/sound/device-name", NULL));
 
-    // update magvar model
-    globals->get_mag()->update( globals->get_aircraft_position(),
-                                globals->get_time_params()->getJD() );
+    vector <const char*>devices = smgr->get_available_devices();
+    for (unsigned int i=0; i<devices.size(); i++) {
+        SGPropertyNode *p = fgGetNode("/sim/sound/devices/device", i, true);
+        p->setStringValue(devices[i]);
+    }
+    devices.clear();
+}
 
-    globals->get_subsystem_mgr()->update(sim_dt);
+void fgSetNewSoundDevice(const char *device)
+{
+    SGSoundMgr *smgr = globals->get_soundmgr();
+    smgr->suspend();
+    smgr->stop();
+    smgr->init(device);
+    smgr->resume();
+}
 
-    // Update the sound manager last so it can use the CPU while the GPU
-    // is processing the scenery (doubled the frame-rate for me) -EMH-
+// Update sound manager state (init/suspend/resume) and propagate property values,
+// since the sound manager doesn't read any properties itself.
+// Actual sound update is triggered by the subsystem manager.
+static void fgUpdateSound(double dt)
+{
 #ifdef ENABLE_AUDIO_SUPPORT
     static bool smgr_init = true;
     static SGPropertyNode *sound_working = fgGetNode("/sim/sound/working");
@@ -142,7 +140,7 @@ static void fgMainLoop( void )
         static SGSoundMgr *smgr = globals->get_soundmgr();
         static bool smgr_enabled = true;
 
-        if (sound_working->getBoolValue() == false) {  // request to reinit
+        if (sound_working->getBoolValue() == false) {   // request to reinit
            smgr->reinit();
            smgr->resume();
            sound_working->setBoolValue(true);
@@ -161,14 +159,17 @@ static void fgMainLoop( void )
         if (smgr_enabled == true) {
             static SGPropertyNode *volume = fgGetNode("/sim/sound/volume");
             smgr->set_volume(volume->getFloatValue());
-            smgr->update(sim_dt);
         }
     }
 #endif
+}
 
-    // END Tile Manager updates
-    bool scenery_loaded = fgGetBool("sim/sceneryloaded");
-    if (!scenery_loaded)
+static void fgLoadInitialScenery()
+{
+    static SGPropertyNode_ptr scenery_loaded
+        = fgGetNode("sim/sceneryloaded", true);
+
+    if (!scenery_loaded->getBoolValue())
     {
         if (globals->get_tile_mgr()->isSceneryLoaded()
              && fgGetBool("sim/fdm-initialized")) {
@@ -187,33 +188,43 @@ static void fgMainLoop( void )
             SGTimeStamp::sleepForMSec(500);
         }
     }
-
-    simgear::AtomicChangeListener::fireChangeListeners();
-
-    SG_LOG( SG_GENERAL, SG_DEBUG, "" );
 }
 
-void fgInitSoundManager()
+// What should we do when we have nothing else to do?  Let's get ready
+// for the next move and update the display?
+static void fgMainLoop( void )
 {
-    SGSoundMgr *smgr = globals->get_soundmgr();
+    static SGPropertyNode_ptr frame_signal
+        = fgGetNode("/sim/signals/frame", true);
 
-    smgr->bind();
-    smgr->init(fgGetString("/sim/sound/device-name", NULL));
+    frame_signal->fireValueChanged();
 
-    vector <const char*>devices = smgr->get_available_devices();
-    for (unsigned int i=0; i<devices.size(); i++) {
-        SGPropertyNode *p = fgGetNode("/sim/sound/devices/device", i, true);
-        p->setStringValue(devices[i]);
-    }
-    devices.clear();
-}
+    SG_LOG( SG_GENERAL, SG_DEBUG, "Running Main Loop");
+    SG_LOG( SG_GENERAL, SG_DEBUG, "======= ==== ====");
 
-void fgSetNewSoundDevice(const char *device)
-{
-    globals->get_soundmgr()->suspend();
-    globals->get_soundmgr()->stop();
-    globals->get_soundmgr()->init(device);
-    globals->get_soundmgr()->resume();
+    // compute simulated time (allowing for pause, warp, etc) and
+    // real elapsed time
+    double sim_dt, real_dt;
+    TimeManager* timeMgr = (TimeManager*) globals->get_subsystem("time");
+    timeMgr->computeTimeDeltas(sim_dt, real_dt);
+
+    // update magvar model
+    globals->get_mag()->update( globals->get_aircraft_position(),
+                                globals->get_time_params()->getJD() );
+
+    // Propagate sound manager properties (note: actual update is triggered
+    // by the subsystem manager).
+    fgUpdateSound(sim_dt);
+
+    // update all subsystems
+    globals->get_subsystem_mgr()->update(sim_dt);
+
+    // END Tile Manager updates
+    fgLoadInitialScenery();
+
+    simgear::AtomicChangeListener::fireChangeListeners();
+
+    SG_LOG( SG_GENERAL, SG_DEBUG, "" );
 }
 
 // Operation for querying OpenGL parameters. This must be done in a