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., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include <simgear/sound/soundmgr_openal.hxx>
28 #include <simgear/structure/commands.hxx>
29 #include <simgear/misc/sg_path.hxx>
31 #include "globals.hxx"
32 #include "renderer.hxx"
33 #include "viewmgr.hxx"
35 #include "fg_props.hxx"
39 ////////////////////////////////////////////////////////////////////////
40 // Implementation of FGGlobals.
41 ////////////////////////////////////////////////////////////////////////
48 FGGlobals::FGGlobals() :
49 renderer( new FGRenderer ),
50 subsystem_mgr( new SGSubsystemMgr ),
51 event_mgr( new SGEventMgr ),
54 #if defined(FX) && defined(XMESA)
63 current_panel( NULL ),
71 props( new SGPropertyNode ),
72 initial_state( NULL ),
74 commands( new SGCommandMgr ),
78 channel_options_list( NULL ),
79 initial_waypoints( NULL ),
96 FGGlobals::~FGGlobals()
101 delete initial_state;
106 delete initial_waypoints;
110 // set the fg_root path
111 void FGGlobals::set_fg_root (const string &root) {
114 // append /data to root if it exists
115 SGPath tmp( fg_root );
116 tmp.append( "data" );
117 tmp.append( "version" );
118 if ( ulFileExists( tmp.c_str() ) ) {
123 void FGGlobals::set_fg_scenery (const string &scenery) {
125 if (scenery.empty()) {
127 s.append( "Scenery" );
131 string_list path_list = sgPathSplit( s.str() );
134 for (unsigned i = 0; i < path_list.size(); i++) {
136 ulDir *d = ulOpenDir( path_list[i].c_str() );
141 SGPath pt( path_list[i] ), po( path_list[i] );
142 pt.append("Terrain");
143 po.append("Objects");
145 ulDir *td = ulOpenDir( pt.c_str() );
146 ulDir *od = ulOpenDir( po.c_str() );
148 if (td == NULL && od == NULL)
149 fg_scenery.push_back( path_list[i] );
152 fg_scenery.push_back( pt.str() );
156 fg_scenery.push_back( po.str() );
165 FGGlobals::get_renderer () const
171 FGGlobals::get_subsystem_mgr () const
173 return subsystem_mgr;
177 FGGlobals::get_subsystem (const char * name)
179 return subsystem_mgr->get_subsystem(name);
183 FGGlobals::add_subsystem (const char * name,
184 SGSubsystem * subsystem,
185 SGSubsystemMgr::GroupType type,
188 subsystem_mgr->add(name, subsystem, type, min_time_sec);
193 FGGlobals::get_event_mgr () const
199 // Save the current state as the initial state.
201 FGGlobals::saveInitialState ()
203 delete initial_state;
204 initial_state = new SGPropertyNode();
206 if (!copyProperties(props, initial_state))
207 SG_LOG(SG_GENERAL, SG_ALERT, "Error saving initial state");
211 // Restore the saved initial state, if any
213 FGGlobals::restoreInitialState ()
215 if ( initial_state == 0 ) {
216 SG_LOG(SG_GENERAL, SG_ALERT,
217 "No initial state available to restore!!!");
221 SGPropertyNode *currentPresets = new SGPropertyNode;
222 SGPropertyNode *targetNode = fgGetNode( "/sim/presets" );
224 // stash the /sim/presets tree
225 if ( !copyProperties(targetNode, currentPresets) ) {
226 SG_LOG( SG_GENERAL, SG_ALERT, "Failed to save /sim/presets subtree" );
229 if ( copyProperties(initial_state, props) ) {
230 SG_LOG( SG_GENERAL, SG_INFO, "Initial state restored successfully" );
232 SG_LOG( SG_GENERAL, SG_INFO,
233 "Some errors restoring initial state (read-only props?)" );
236 // recover the /sim/presets tree
237 if ( !copyProperties(currentPresets, targetNode) ) {
238 SG_LOG( SG_GENERAL, SG_ALERT,
239 "Failed to restore /sim/presets subtree" );
242 delete currentPresets;
246 FGGlobals::get_current_view () const
248 return viewmgr->get_current_view();
251 // end of globals.cxx