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 <Airports/simple.hxx>
51 #include <Navaids/navlist.hxx>
52 #include <Navaids/fixlist.hxx>
54 #include "globals.hxx"
55 #include "renderer.hxx"
56 #include "viewmgr.hxx"
58 #include "fg_props.hxx"
62 ////////////////////////////////////////////////////////////////////////
63 // Implementation of FGGlobals.
64 ////////////////////////////////////////////////////////////////////////
71 FGGlobals::FGGlobals() :
72 renderer( new FGRenderer ),
73 subsystem_mgr( new SGSubsystemMgr ),
74 event_mgr( new SGEventMgr ),
84 current_panel( NULL ),
91 props( new SGPropertyNode ),
92 initial_state( NULL ),
94 commands( SGCommandMgr::instance() ),
97 channel_options_list( NULL ),
98 initial_waypoints( NULL ),
102 fontcache ( new FGFontCache ),
111 multiplayer_mgr( NULL )
117 FGGlobals::~FGGlobals()
120 // The AIModels manager performs a number of actions upon
121 // Shutdown that implicitly assume that other subsystems
122 // are still operational (Due to the dynamic allocation and
123 // deallocation of AIModel objects. To ensure we can safely
124 // shut down all subsystems, make sure we take down the
125 // AIModels system first.
126 subsystem_mgr->get_group(SGSubsystemMgr::GENERAL)->remove_subsystem("ai_model");
127 delete subsystem_mgr;
134 delete current_panel;
146 delete channel_options_list;
147 delete initial_waypoints;
161 delete multiplayer_mgr;
165 // set the fg_root path
166 void FGGlobals::set_fg_root (const string &root) {
169 // append /data to root if it exists
170 SGPath tmp( fg_root );
171 tmp.append( "data" );
172 tmp.append( "version" );
173 if ( ulFileExists( tmp.c_str() ) ) {
175 SG_LOG(SG_GENERAL, SG_WARN, "Warning: changing bad FG_ROOT/--fg-root to '"
179 // remove /sim/fg-root before writing to prevent hijacking
180 SGPropertyNode *n = fgGetNode("/sim", true);
181 n->removeChild("fg-root", 0, false);
182 n = n->getChild("fg-root", 0, true);
183 n->setStringValue(fg_root.c_str());
184 n->setAttribute(SGPropertyNode::WRITE, false);
187 void FGGlobals::set_fg_scenery (const string &scenery) {
189 if (scenery.empty()) {
191 s.append( "Scenery" );
195 string_list path_list = sgPathSplit( s.str() );
198 for (unsigned i = 0; i < path_list.size(); i++) {
200 ulDir *d = ulOpenDir( path_list[i].c_str() );
205 SGPath pt( path_list[i] ), po( path_list[i] );
206 pt.append("Terrain");
207 po.append("Objects");
209 ulDir *td = ulOpenDir( pt.c_str() );
210 ulDir *od = ulOpenDir( po.c_str() );
212 if (td == NULL && od == NULL)
213 fg_scenery.push_back( path_list[i] );
216 fg_scenery.push_back( pt.str() );
220 fg_scenery.push_back( po.str() );
224 // insert a marker for FGTileEntry::load(), so that
225 // FG_SCENERY=A:B becomes list ["A/Terrain", "A/Objects", "",
226 // "B/Terrain", "B/Objects", ""]
227 fg_scenery.push_back("");
233 FGGlobals::get_renderer () const
239 FGGlobals::get_subsystem_mgr () const
241 return subsystem_mgr;
245 FGGlobals::get_subsystem (const char * name)
247 return subsystem_mgr->get_subsystem(name);
251 FGGlobals::add_subsystem (const char * name,
252 SGSubsystem * subsystem,
253 SGSubsystemMgr::GroupType type,
256 subsystem_mgr->add(name, subsystem, type, min_time_sec);
261 FGGlobals::get_event_mgr () const
267 // Save the current state as the initial state.
269 FGGlobals::saveInitialState ()
271 initial_state = new SGPropertyNode();
273 if (!copyProperties(props, initial_state))
274 SG_LOG(SG_GENERAL, SG_ALERT, "Error saving initial state");
278 // Restore the saved initial state, if any
280 FGGlobals::restoreInitialState ()
282 if ( initial_state == 0 ) {
283 SG_LOG(SG_GENERAL, SG_ALERT,
284 "No initial state available to restore!!!");
288 SGPropertyNode *currentPresets = new SGPropertyNode;
289 SGPropertyNode *targetNode = fgGetNode( "/sim/presets" );
291 // stash the /sim/presets tree
292 if ( !copyProperties(targetNode, currentPresets) ) {
293 SG_LOG( SG_GENERAL, SG_ALERT, "Failed to save /sim/presets subtree" );
296 if ( copyProperties(initial_state, props) ) {
297 SG_LOG( SG_GENERAL, SG_INFO, "Initial state restored successfully" );
299 SG_LOG( SG_GENERAL, SG_INFO,
300 "Some errors restoring initial state (read-only props?)" );
303 // recover the /sim/presets tree
304 if ( !copyProperties(currentPresets, targetNode) ) {
305 SG_LOG( SG_GENERAL, SG_ALERT,
306 "Failed to restore /sim/presets subtree" );
309 delete currentPresets;
313 FGGlobals::get_current_view () const
315 return viewmgr->get_current_view();
318 // end of globals.cxx