From e1c1a28288183236d2244b15cd494276e94fe59c Mon Sep 17 00:00:00 2001 From: ThorstenB Date: Sun, 20 Nov 2011 16:20:05 +0100 Subject: [PATCH] Introduce some structure in fgMainLoop. - 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 | 9 ++++ src/Main/globals.cxx | 11 ++-- src/Main/globals.hxx | 1 - src/Main/main.cxx | 117 +++++++++++++++++++++++-------------------- 4 files changed, 78 insertions(+), 60 deletions(-) diff --git a/src/Main/fg_init.cxx b/src/Main/fg_init.cxx index 8d01e88e6..807606916 100644 --- a/src/Main/fg_init.cxx +++ b/src/Main/fg_init.cxx @@ -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. //////////////////////////////////////////////////////////////////// diff --git a/src/Main/globals.cxx b/src/Main/globals.cxx index e2ea2954b..00dd943dc 100644 --- a/src/Main/globals.cxx +++ b/src/Main/globals.cxx @@ -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 * diff --git a/src/Main/globals.hxx b/src/Main/globals.hxx index 52df7f514..11dc124dc 100644 --- a/src/Main/globals.hxx +++ b/src/Main/globals.hxx @@ -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; diff --git a/src/Main/main.cxx b/src/Main/main.cxx index 1cfdd7dbf..d6036771a 100644 --- a/src/Main/main.cxx +++ b/src/Main/main.cxx @@ -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 devices = smgr->get_available_devices(); + for (unsigned int i=0; isetStringValue(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 devices = smgr->get_available_devices(); - for (unsigned int i=0; isetStringValue(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 -- 2.39.5