]> git.mxchange.org Git - flightgear.git/blobdiff - src/Main/fg_init.cxx
Also properly reinit previously set time of day upon reset.
[flightgear.git] / src / Main / fg_init.cxx
index fb94e6179dcd333994cd599d324ff4ad5ad1588a..6035a54ce930259daba9925fe16e9670c29afd1a 100644 (file)
 #endif
 
 #include <simgear/compiler.h>
-#include <simgear/misc/exception.hxx>
 
 #include STL_STRING
 
 #include <simgear/constants.h>
 #include <simgear/debug/logstream.hxx>
+#include <simgear/structure/exception.hxx>
+#include <simgear/structure/event_mgr.hxx>
 #include <simgear/math/point3d.hxx>
 #include <simgear/math/polar3d.hxx>
 #include <simgear/math/sg_geodesy.hxx>
 #endif
 #include <Sound/fg_fx.hxx>
 #include <Systems/system_mgr.hxx>
-#include <Time/FGEventMgr.hxx>
 #include <Time/light.hxx>
 #include <Time/moonpos.hxx>
 #include <Time/sunpos.hxx>
@@ -268,8 +268,27 @@ bool fgInitFGRoot ( int argc, char **argv ) {
         root = "/FlightGear";
 #elif defined( WIN32 )
         root = "\\FlightGear";
-#elif defined( macintosh )
-        root = "";
+#elif defined(OSX_BUNDLE) 
+        /* the following code looks for the base package directly inside
+            the application bundle. This can be changed fairly easily by
+            fiddling with the code below. And yes, I know it's ugly and verbose.
+        */
+        CFBundleRef appBundle = CFBundleGetMainBundle();
+        CFURLRef appUrl = CFBundleCopyBundleURL(appBundle);
+        CFRelease(appBundle);
+
+        // look for a 'data' subdir directly inside the bundle : is there
+        // a better place? maybe in Resources? I don't know ...
+        CFURLRef dataDir = CFURLCreateCopyAppendingPathComponent(NULL, appUrl, CFSTR("data"), true);
+
+        // now convert down to a path, and the a c-string
+        CFStringRef path = CFURLCopyFileSystemPath(dataDir, kCFURLPOSIXPathStyle);
+        root = CFStringGetCStringPtr(path, CFStringGetSystemEncoding());
+
+        // tidy up.
+        CFRelease(appBundle);
+        CFRelease(dataDir);
+        CFRelease(path);
 #else
         root = PKGLIBDIR;
 #endif
@@ -508,6 +527,38 @@ do_options (int argc, char ** argv)
 }
 
 
+static string fgFindAircraftPath( const SGPath &path, const string &aircraft ) {
+    ulDirEnt* dire;
+    ulDir *dirp = ulOpenDir(path.str().c_str());
+    if (dirp == NULL) {
+        cerr << "Unable to open aircraft directory." << endl;
+        exit(-1);
+    }
+
+    while ((dire = ulReadDir(dirp)) != NULL) {
+        if (dire->d_isdir) {
+            if ( strcmp("CVS", dire->d_name) && strcmp(".", dire->d_name)
+                 && strcmp("..", dire->d_name) )
+            {
+                SGPath next = path;
+                next.append(dire->d_name);
+
+                string result = fgFindAircraftPath( next, aircraft );
+                if ( ! result.empty() ) {
+                    return result;
+                }
+            }
+        } else if ( !strcmp(dire->d_name, aircraft.c_str()) ) {
+            return path.str();
+        }
+    }
+
+    ulCloseDir(dirp);
+
+    return "";
+}
+
+
 // Read in configuration (file and command line)
 bool fgInitConfig ( int argc, char **argv ) {
 
@@ -527,42 +578,36 @@ bool fgInitConfig ( int argc, char **argv ) {
     // Scan user config files and command line for a specified aircraft.
     fgInitFGAircraft(argc, argv);
 
-    string aircraft = fgGetString("/sim/aircraft", "");
+    string aircraft = fgGetString( "/sim/aircraft", "" );
     if ( aircraft.size() > 0 ) {
-        SGPath aircraft_path(globals->get_fg_root());
-        aircraft_path.append("Aircraft");
-        aircraft_path.append(aircraft);
-        aircraft_path.concat("-set.xml");
-
-        if ( !ulFileExists(aircraft_path.c_str()) ) {
-            string adir = aircraft;
-            int pos, alen = adir.length();
-
-            if ( ((pos = adir.rfind("-jsbsim")) != string::npos) ||
-                 ((pos = adir.rfind("-yasim")) != string::npos) ||
-                 ((pos = adir.rfind("-uiuc")) != string::npos) )
-            {
-                adir.erase(pos, alen);
+        SGPath aircraft_search( globals->get_fg_root() );
+        aircraft_search.append( "Aircraft" );
+
+        string aircraft_set = aircraft + "-set.xml";
+
+        string result = fgFindAircraftPath( aircraft_search, aircraft_set );
+        if ( !result.empty() ) {
+            fgSetString( "/sim/aircraft-dir", result.c_str() );
+            SGPath full_name( result );
+            full_name.append( aircraft_set );
+
+            SG_LOG(SG_INPUT, SG_INFO, "Reading default aircraft: " << aircraft
+                   << " from " << full_name.str());
+            try {
+                readProperties( full_name.str(), globals->get_props() );
+            } catch ( const sg_exception &e ) {
+                string message = "Error reading default aircraft: ";
+                message += e.getFormattedMessage();
+                SG_LOG(SG_INPUT, SG_ALERT, message);
+                exit(2);
             }
-
-            aircraft_path = globals->get_fg_root();
-            aircraft_path.append("Aircraft");
-            aircraft_path.append(adir);
-            aircraft_path.append(aircraft);
-            aircraft_path.concat("-set.xml");
-        }
-        SG_LOG(SG_INPUT, SG_INFO, "Reading default aircraft: " << aircraft
-               << " from " << aircraft_path.str());
-        try {
-            readProperties(aircraft_path.str(), globals->get_props());
-        } catch (const sg_exception &e) {
-            string message = "Error reading default aircraft: ";
-            message += e.getFormattedMessage();
-            SG_LOG(SG_INPUT, SG_ALERT, message);
-            exit(2);
+        } else {
+            SG_LOG( SG_INPUT, SG_ALERT, "Cannot find specified aircraft: "
+                    << aircraft );
         }
+
     } else {
-        SG_LOG(SG_INPUT, SG_ALERT, "No default aircraft specified");
+        SG_LOG( SG_INPUT, SG_ALERT, "No default aircraft specified" );
     }
 
     // parse options after loading aircraft to ensure any user
@@ -1365,8 +1410,6 @@ void fgInitTimeOffset() {
 
     SG_LOG( SG_GENERAL, SG_INFO, "After fgInitTimeOffset(): warp = " 
             << globals->get_warp() );
-
-    fgUpdateSkyAndLightingParams();
 }
 
 // This is the top level init routine which calls all the other
@@ -1381,11 +1424,23 @@ bool fgInitSubsystems() {
     // static const SGPropertyNode *altitude
     //     = fgGetNode("/sim/presets/altitude-ft");
 
-    fgLIGHT *l = &cur_light_params;
+    FGLight *l = (FGLight *)(globals->get_subsystem("lighting"));
 
     SG_LOG( SG_GENERAL, SG_INFO, "Initialize Subsystems");
     SG_LOG( SG_GENERAL, SG_INFO, "========== ==========");
 
+    ////////////////////////////////////////////////////////////////////
+    // Initialize the event manager subsystem.
+    ////////////////////////////////////////////////////////////////////
+
+     globals->get_event_mgr()->bind();
+     globals->get_event_mgr()->init();
+
+     // Output event stats every 60 seconds
+     globals->get_event_mgr()->add( "SGEventMgr::print_stats()",
+                                    globals->get_event_mgr(),
+                                    &SGEventMgr::print_stats,
+                                    60000 );
 
     ////////////////////////////////////////////////////////////////////
     // Initialize the material property subsystem.
@@ -1398,17 +1453,6 @@ bool fgInitSubsystems() {
         exit(-1);
     }
 
-    ////////////////////////////////////////////////////////////////////
-    // Initialize the event manager subsystem.
-    ////////////////////////////////////////////////////////////////////
-
-    global_events.init();
-
-    // Output event stats every 60 seconds
-    global_events.Register( "FGEventMgr::print_stats()",
-                            &global_events, &FGEventMgr::print_stats,
-                            60000 );
-
 
     ////////////////////////////////////////////////////////////////////
     // Initialize the scenery management subsystem.
@@ -1425,10 +1469,10 @@ bool fgInitSubsystems() {
     }
 
     // cause refresh of viewer scenery timestamps every 15 seconds...
-    global_events.Register( "FGTileMgr::refresh_view_timestamps()",
-                            globals->get_tile_mgr(),
-                            &FGTileMgr::refresh_view_timestamps,
-                            15000 );
+    globals->get_event_mgr()->add( "FGTileMgr::refresh_view_timestamps()",
+                                   globals->get_tile_mgr(),
+                                   &FGTileMgr::refresh_view_timestamps,
+                                   15000 );
 
     SG_LOG( SG_GENERAL, SG_DEBUG,
             "Current terrain elevation after tile mgr init " <<
@@ -1453,31 +1497,6 @@ bool fgInitSubsystems() {
     fgInitView();
 
 
-    ////////////////////////////////////////////////////////////////////
-    // Initialize the lighting subsystem.
-    ////////////////////////////////////////////////////////////////////
-
-    // fgUpdateSunPos() needs a few position and view parameters set
-    // so it can calculate local relative sun angle and a few other
-    // things for correctly orienting the sky.
-    fgUpdateSunPos();
-    fgUpdateMoonPos();
-    global_events.Register( "fgUpdateSunPos()", &fgUpdateSunPos,
-                            60000);
-    global_events.Register( "fgUpdateMoonPos()", &fgUpdateMoonPos,
-                            60000);
-
-    // Initialize Lighting interpolation tables
-    l->Init();
-
-    // force one lighting update to make it right to start with...
-    l->Update();
-    // update the lighting parameters (based on sun angle)
-    global_events.Register( "fgLight::Update()",
-                            &cur_light_params, &fgLIGHT::Update,
-                            30000 );
-
-
     ////////////////////////////////////////////////////////////////////
     // Create and register the logger.
     ////////////////////////////////////////////////////////////////////
@@ -1498,7 +1517,7 @@ bool fgInitSubsystems() {
     // Create and register the XML GUI.
     ////////////////////////////////////////////////////////////////////
 
-    globals->add_subsystem("gui", new NewGUI, FGSubsystemMgr::INIT);
+    globals->add_subsystem("gui", new NewGUI, SGSubsystemMgr::INIT);
 
 
     ////////////////////////////////////////////////////////////////////
@@ -1506,8 +1525,8 @@ bool fgInitSubsystems() {
     ////////////////////////////////////////////////////////////////////
 
     // update the current timezone each 30 minutes
-    global_events.Register( "fgUpdateLocalTime()", &fgUpdateLocalTime,
-                            30*60*1000 );
+    globals->get_event_mgr()->add( "fgUpdateLocalTime()",
+                                   &fgUpdateLocalTime, 30*60*1000 );
 
 
     ////////////////////////////////////////////////////////////////////
@@ -1553,12 +1572,20 @@ bool fgInitSubsystems() {
     WeatherDatabase = FGLocalWeatherDatabase::theFGLocalWeatherDatabase;
 
     // register the periodic update of the weather
-    global_events.Register( "weather update", &fgUpdateWeatherDatabase,
-                            30000);
+    globals->get_event_mgr()->add( "weather update",
+                                   &fgUpdateWeatherDatabase, 30000);
 #else
     globals->add_subsystem("environment", new FGEnvironmentMgr);
 #endif
 
+
+    ////////////////////////////////////////////////////////////////////
+    // Initialize the lighting subsystem.
+    ////////////////////////////////////////////////////////////////////
+
+    globals->add_subsystem("lighting", new FGLight);
+
+
 #ifdef FG_USE_CLOUDS_3D
     ////////////////////////////////////////////////////////////////////
     // Initialize the 3D cloud subsystem.
@@ -1735,7 +1762,6 @@ bool fgInitSubsystems() {
     globals->get_subsystem_mgr()->bind();
     globals->get_subsystem_mgr()->init();
 
-
 #ifdef FG_MPLAYER_AS
     ////////////////////////////////////////////////////////////////////
     // Initialize multiplayer subsystem
@@ -1797,11 +1823,11 @@ void fgReInitSubsystems()
     globals->get_controls()->reset_all();
     globals->get_autopilot()->reset();
 
-    fgUpdateSunPos();
-    fgUpdateMoonPos();
-    cur_light_params.Update();
     fgUpdateLocalTime();
 
+    // re-init to proper time of day setting
+    fgInitTimeOffset();
+
     if ( !freeze ) {
         fgSetBool("/sim/freeze/master", false);
     }