1 // globals.cxx -- Global state that needs to be shared among the sim modules
3 // Written by Curtis Olson, started July 2000.
5 // Copyright (C) 2000 Curtis L. Olson - http://www.flightgear.org/~curt
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 #include <simgear/sound/soundmgr_openal.hxx>
28 #include <simgear/structure/commands.hxx>
29 #include <simgear/misc/sg_path.hxx>
30 #include <simgear/timing/sg_time.hxx>
31 #include <simgear/ephemeris/ephemeris.hxx>
32 #include <simgear/magvar/magvar.hxx>
33 #include <simgear/scene/material/matlib.hxx>
35 #include <Aircraft/controls.hxx>
36 #include <Airports/runways.hxx>
37 #include <ATCDCL/AIMgr.hxx>
38 #include <ATCDCL/ATCmgr.hxx>
39 #include <Autopilot/route_mgr.hxx>
40 #include <Cockpit/panel.hxx>
41 #include <GUI/new_gui.hxx>
42 #include <Model/acmodel.hxx>
43 #include <Model/modelmgr.hxx>
44 #include <MultiPlayer/multiplaymgr.hxx>
45 #include <Navaids/awynet.hxx>
46 #include <Scenery/scenery.hxx>
47 #include <Scenery/tilemgr.hxx>
49 #include "globals.hxx"
50 #include "renderer.hxx"
51 #include "viewmgr.hxx"
53 #include "fg_props.hxx"
57 ////////////////////////////////////////////////////////////////////////
58 // Implementation of FGGlobals.
59 ////////////////////////////////////////////////////////////////////////
66 FGGlobals::FGGlobals() :
67 renderer( new FGRenderer ),
68 subsystem_mgr( new SGSubsystemMgr ),
69 event_mgr( new SGEventMgr ),
72 #if defined(FX) && defined(XMESA)
82 current_panel( NULL ),
90 props( new SGPropertyNode ),
91 initial_state( NULL ),
93 commands( SGCommandMgr::instance() ),
96 channel_options_list( NULL ),
97 initial_waypoints( NULL ),
101 fontcache ( new FGFontCache ),
112 multiplayer_mgr( NULL )
118 FGGlobals::~FGGlobals()
121 // The AIModels manager performs a number of actions upon
122 // Shutdown that implicitly assume that other subsystems
123 // are still operational (Due to the dynamic allocation and
124 // deallocation of AIModel objects. To ensure we can safely
125 // shut down all subsystems, make sure we take down the
126 // AIModels system first.
127 subsystem_mgr->get_group(SGSubsystemMgr::GENERAL)->remove_subsystem("ai_model");
128 delete subsystem_mgr;
135 delete current_panel;
148 delete channel_options_list;
149 delete initial_waypoints;
165 delete multiplayer_mgr;
169 // set the fg_root path
170 void FGGlobals::set_fg_root (const string &root) {
173 // append /data to root if it exists
174 SGPath tmp( fg_root );
175 tmp.append( "data" );
176 tmp.append( "version" );
177 if ( ulFileExists( tmp.c_str() ) ) {
181 fgSetString("/sim/fg-root", fg_root.c_str());
184 void FGGlobals::set_fg_scenery (const string &scenery) {
186 if (scenery.empty()) {
188 s.append( "Scenery" );
192 string_list path_list = sgPathSplit( s.str() );
195 for (unsigned i = 0; i < path_list.size(); i++) {
197 ulDir *d = ulOpenDir( path_list[i].c_str() );
202 SGPath pt( path_list[i] ), po( path_list[i] );
203 pt.append("Terrain");
204 po.append("Objects");
206 ulDir *td = ulOpenDir( pt.c_str() );
207 ulDir *od = ulOpenDir( po.c_str() );
209 if (td == NULL && od == NULL)
210 fg_scenery.push_back( path_list[i] );
213 fg_scenery.push_back( pt.str() );
217 fg_scenery.push_back( po.str() );
221 // insert a marker for FGTileEntry::load(), so that
222 // FG_SCENERY=A:B becomes list ["A/Terrain", "A/Objects", "",
223 // "B/Terrain", "B/Objects", ""]
224 fg_scenery.push_back("");
230 FGGlobals::get_renderer () const
236 FGGlobals::get_subsystem_mgr () const
238 return subsystem_mgr;
242 FGGlobals::get_subsystem (const char * name)
244 return subsystem_mgr->get_subsystem(name);
248 FGGlobals::add_subsystem (const char * name,
249 SGSubsystem * subsystem,
250 SGSubsystemMgr::GroupType type,
253 subsystem_mgr->add(name, subsystem, type, min_time_sec);
258 FGGlobals::get_event_mgr () const
264 // Save the current state as the initial state.
266 FGGlobals::saveInitialState ()
268 initial_state = new SGPropertyNode();
270 if (!copyProperties(props, initial_state))
271 SG_LOG(SG_GENERAL, SG_ALERT, "Error saving initial state");
275 // Restore the saved initial state, if any
277 FGGlobals::restoreInitialState ()
279 if ( initial_state == 0 ) {
280 SG_LOG(SG_GENERAL, SG_ALERT,
281 "No initial state available to restore!!!");
285 SGPropertyNode *currentPresets = new SGPropertyNode;
286 SGPropertyNode *targetNode = fgGetNode( "/sim/presets" );
288 // stash the /sim/presets tree
289 if ( !copyProperties(targetNode, currentPresets) ) {
290 SG_LOG( SG_GENERAL, SG_ALERT, "Failed to save /sim/presets subtree" );
293 if ( copyProperties(initial_state, props) ) {
294 SG_LOG( SG_GENERAL, SG_INFO, "Initial state restored successfully" );
296 SG_LOG( SG_GENERAL, SG_INFO,
297 "Some errors restoring initial state (read-only props?)" );
300 // recover the /sim/presets tree
301 if ( !copyProperties(currentPresets, targetNode) ) {
302 SG_LOG( SG_GENERAL, SG_ALERT,
303 "Failed to restore /sim/presets subtree" );
306 delete currentPresets;
310 FGGlobals::get_current_view () const
312 return viewmgr->get_current_view();
315 // end of globals.cxx