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>
34 #include <simgear/structure/subsystem_mgr.hxx>
35 #include <simgear/structure/event_mgr.hxx>
37 #include <Aircraft/controls.hxx>
38 #include <Airports/runways.hxx>
39 #include <ATCDCL/AIMgr.hxx>
40 #include <ATCDCL/ATCmgr.hxx>
41 #include <Autopilot/route_mgr.hxx>
42 #include <Cockpit/panel.hxx>
43 #include <GUI/new_gui.hxx>
44 #include <Model/acmodel.hxx>
45 #include <Model/modelmgr.hxx>
46 #include <MultiPlayer/multiplaymgr.hxx>
47 #include <Navaids/awynet.hxx>
48 #include <Scenery/scenery.hxx>
49 #include <Scenery/tilemgr.hxx>
50 #include <Navaids/navlist.hxx>
52 #include "globals.hxx"
53 #include "renderer.hxx"
54 #include "viewmgr.hxx"
56 #include "fg_props.hxx"
60 ////////////////////////////////////////////////////////////////////////
61 // Implementation of FGGlobals.
62 ////////////////////////////////////////////////////////////////////////
69 FGGlobals::FGGlobals() :
70 props( new SGPropertyNode ),
71 initial_state( NULL ),
73 renderer( new FGRenderer ),
74 subsystem_mgr( new SGSubsystemMgr ),
75 event_mgr( new SGEventMgr ),
85 current_panel( NULL ),
91 commands( SGCommandMgr::instance() ),
94 channel_options_list( NULL ),
95 initial_waypoints( NULL ),
99 fontcache ( new FGFontCache ),
108 multiplayer_mgr( NULL )
114 FGGlobals::~FGGlobals()
117 // The AIModels manager performs a number of actions upon
118 // Shutdown that implicitly assume that other subsystems
119 // are still operational (Due to the dynamic allocation and
120 // deallocation of AIModel objects. To ensure we can safely
121 // shut down all subsystems, make sure we take down the
122 // AIModels system first.
123 subsystem_mgr->get_group(SGSubsystemMgr::GENERAL)->remove_subsystem("ai_model");
124 // FGInput (FGInputEvent) and FGDialog calls get_subsystem() in their destructors,
125 // which is not safe since some subsystem are already deleted but can be referred.
126 // So these subsystems must be deleted prior to deleting subsystem_mgr unless
127 // ~SGSubsystemGroup and SGSubsystemMgr::get_subsystem are changed not to refer to
128 // deleted subsystems.
129 subsystem_mgr->get_group(SGSubsystemMgr::GENERAL)->remove_subsystem("input");
130 subsystem_mgr->get_group(SGSubsystemMgr::GENERAL)->remove_subsystem("gui");
131 delete subsystem_mgr;
138 delete current_panel;
149 delete channel_options_list;
150 delete initial_waypoints;
164 delete multiplayer_mgr;
168 // set the fg_root path
169 void FGGlobals::set_fg_root (const string &root) {
172 // append /data to root if it exists
173 SGPath tmp( fg_root );
174 tmp.append( "data" );
175 tmp.append( "version" );
176 if ( ulFileExists( tmp.c_str() ) ) {
177 fgGetNode("BAD_FG_ROOT", true)->setStringValue(fg_root);
179 fgGetNode("GOOD_FG_ROOT", true)->setStringValue(fg_root);
180 SG_LOG(SG_GENERAL, SG_ALERT, "***\n***\n*** Warning: changing bad FG_ROOT/--fg-root to '"
181 << fg_root << "'\n***\n***");
184 // remove /sim/fg-root before writing to prevent hijacking
185 SGPropertyNode *n = fgGetNode("/sim", true);
186 n->removeChild("fg-root", 0, false);
187 n = n->getChild("fg-root", 0, true);
188 n->setStringValue(fg_root.c_str());
189 n->setAttribute(SGPropertyNode::WRITE, false);
192 void FGGlobals::set_fg_scenery (const string &scenery) {
194 if (scenery.empty()) {
196 s.append( "Scenery" );
200 string_list path_list = sgPathSplit( s.str() );
203 for (unsigned i = 0; i < path_list.size(); i++) {
205 ulDir *d = ulOpenDir( path_list[i].c_str() );
210 SGPath pt( path_list[i] ), po( path_list[i] );
211 pt.append("Terrain");
212 po.append("Objects");
214 ulDir *td = ulOpenDir( pt.c_str() );
215 ulDir *od = ulOpenDir( po.c_str() );
217 // "Terrain" and "Airports" directory don't exist. add directory as is
218 // otherwise, automatically append either Terrain, Objects, or both
219 //if (td == NULL && od == NULL)
220 fg_scenery.push_back( path_list[i] );
223 fg_scenery.push_back( pt.str() );
227 fg_scenery.push_back( po.str() );
231 // insert a marker for FGTileEntry::load(), so that
232 // FG_SCENERY=A:B becomes list ["A/Terrain", "A/Objects", "",
233 // "B/Terrain", "B/Objects", ""]
234 fg_scenery.push_back("");
240 FGGlobals::get_renderer () const
246 FGGlobals::get_subsystem_mgr () const
248 return subsystem_mgr;
252 FGGlobals::get_subsystem (const char * name)
254 return subsystem_mgr->get_subsystem(name);
258 FGGlobals::add_subsystem (const char * name,
259 SGSubsystem * subsystem,
260 SGSubsystemMgr::GroupType type,
263 subsystem_mgr->add(name, subsystem, type, min_time_sec);
268 FGGlobals::get_event_mgr () const
274 // Save the current state as the initial state.
276 FGGlobals::saveInitialState ()
278 initial_state = new SGPropertyNode();
280 if (!copyProperties(props, initial_state))
281 SG_LOG(SG_GENERAL, SG_ALERT, "Error saving initial state");
285 // Restore the saved initial state, if any
287 FGGlobals::restoreInitialState ()
289 if ( initial_state == 0 ) {
290 SG_LOG(SG_GENERAL, SG_ALERT,
291 "No initial state available to restore!!!");
295 SGPropertyNode *currentPresets = new SGPropertyNode;
296 SGPropertyNode *targetNode = fgGetNode( "/sim/presets" );
298 // stash the /sim/presets tree
299 if ( !copyProperties(targetNode, currentPresets) ) {
300 SG_LOG( SG_GENERAL, SG_ALERT, "Failed to save /sim/presets subtree" );
303 if ( copyProperties(initial_state, props) ) {
304 SG_LOG( SG_GENERAL, SG_INFO, "Initial state restored successfully" );
306 SG_LOG( SG_GENERAL, SG_INFO,
307 "Some errors restoring initial state (read-only props?)" );
310 // recover the /sim/presets tree
311 if ( !copyProperties(currentPresets, targetNode) ) {
312 SG_LOG( SG_GENERAL, SG_ALERT,
313 "Failed to restore /sim/presets subtree" );
316 delete currentPresets;
320 FGGlobals::get_current_view () const
322 return viewmgr->get_current_view();
325 // end of globals.cxx