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/scene/model/modellib.hxx>
28 #include <simgear/sound/soundmgr_openal.hxx>
29 #include <simgear/structure/commands.hxx>
30 #include <simgear/misc/sg_path.hxx>
31 #include <simgear/timing/sg_time.hxx>
32 #include <simgear/ephemeris/ephemeris.hxx>
33 #include <simgear/magvar/magvar.hxx>
34 #include <simgear/scene/material/matlib.hxx>
36 #include <Aircraft/controls.hxx>
37 #include <Airports/runways.hxx>
38 #include <ATC/AIMgr.hxx>
39 #include <ATC/ATCmgr.hxx>
40 #include <Autopilot/route_mgr.hxx>
41 #include <Cockpit/panel.hxx>
42 #include <GUI/new_gui.hxx>
43 #include <Model/acmodel.hxx>
44 #include <Model/modelmgr.hxx>
45 #include <MultiPlayer/multiplaymgr.hxx>
46 #include <Navaids/awynet.hxx>
47 #include <Scenery/scenery.hxx>
48 #include <Scenery/tilemgr.hxx>
50 #include "globals.hxx"
51 #include "renderer.hxx"
52 #include "viewmgr.hxx"
54 #include "fg_props.hxx"
58 ////////////////////////////////////////////////////////////////////////
59 // Implementation of FGGlobals.
60 ////////////////////////////////////////////////////////////////////////
67 FGGlobals::FGGlobals() :
68 renderer( new FGRenderer ),
69 subsystem_mgr( new SGSubsystemMgr ),
70 event_mgr( new SGEventMgr ),
73 #if defined(FX) && defined(XMESA)
82 current_panel( NULL ),
89 props( new SGPropertyNode ),
90 initial_state( NULL ),
92 commands( SGCommandMgr::instance() ),
96 channel_options_list( NULL ),
97 initial_waypoints( NULL ),
101 fontcache ( new FGFontCache ),
115 FGGlobals::~FGGlobals()
118 // The AIModels manager performs a number of actions upon
119 // Shutdown that implicitly assume that other subsystems
120 // are still operational (Due to the dynamic allocation and
121 // deallocation of AIModel objects. To ensure we can safely
122 // shut down all subsystems, make sure we take down the
123 // AIModels system first.
124 subsystem_mgr->get_group(SGSubsystemMgr::GENERAL)->remove_subsystem("ai_model");
125 delete subsystem_mgr;
132 delete current_panel;
142 delete initial_state;
143 // //delete locale; Don't delete locale
148 delete channel_options_list;
149 delete initial_waypoints;
151 //delete tile_mgr; // Don't delete tile manager yet, because loader thread problems
165 delete multiplayer_mgr;
171 // set the fg_root path
172 void FGGlobals::set_fg_root (const string &root) {
175 // append /data to root if it exists
176 SGPath tmp( fg_root );
177 tmp.append( "data" );
178 tmp.append( "version" );
179 if ( ulFileExists( tmp.c_str() ) ) {
183 fgSetString("/sim/fg-root", fg_root.c_str());
186 void FGGlobals::set_fg_scenery (const string &scenery) {
188 if (scenery.empty()) {
190 s.append( "Scenery" );
194 string_list path_list = sgPathSplit( s.str() );
197 for (unsigned i = 0; i < path_list.size(); i++) {
199 ulDir *d = ulOpenDir( path_list[i].c_str() );
204 SGPath pt( path_list[i] ), po( path_list[i] );
205 pt.append("Terrain");
206 po.append("Objects");
208 ulDir *td = ulOpenDir( pt.c_str() );
209 ulDir *od = ulOpenDir( po.c_str() );
211 if (td == NULL && od == NULL)
212 fg_scenery.push_back( path_list[i] );
215 fg_scenery.push_back( pt.str() );
219 fg_scenery.push_back( po.str() );
223 // insert a marker for FGTileEntry::load(), so that
224 // FG_SCENERY=A:B becomes list ["A/Terrain", "A/Objects", "",
225 // "B/Terrain", "B/Objects", ""]
226 fg_scenery.push_back("");
232 FGGlobals::get_renderer () const
238 FGGlobals::get_subsystem_mgr () const
240 return subsystem_mgr;
244 FGGlobals::get_subsystem (const char * name)
246 return subsystem_mgr->get_subsystem(name);
250 FGGlobals::add_subsystem (const char * name,
251 SGSubsystem * subsystem,
252 SGSubsystemMgr::GroupType type,
255 subsystem_mgr->add(name, subsystem, type, min_time_sec);
260 FGGlobals::get_event_mgr () const
266 // Save the current state as the initial state.
268 FGGlobals::saveInitialState ()
270 delete initial_state;
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