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>
31 #include <GUI/new_gui.hxx>
33 #include "globals.hxx"
34 #include "renderer.hxx"
35 #include "viewmgr.hxx"
37 #include "fg_props.hxx"
41 ////////////////////////////////////////////////////////////////////////
42 // Implementation of FGGlobals.
43 ////////////////////////////////////////////////////////////////////////
50 FGGlobals::FGGlobals() :
51 renderer( new FGRenderer ),
52 subsystem_mgr( new SGSubsystemMgr ),
53 event_mgr( new SGEventMgr ),
56 #if defined(FX) && defined(XMESA)
65 current_panel( NULL ),
73 props( new SGPropertyNode ),
74 initial_state( NULL ),
76 commands( SGCommandMgr::instance() ),
80 channel_options_list( NULL ),
81 initial_waypoints( NULL ),
85 fontcache ( new FGFontCache ),
99 FGGlobals::~FGGlobals()
102 delete subsystem_mgr;
104 delete initial_state;
109 delete initial_waypoints;
113 // set the fg_root path
114 void FGGlobals::set_fg_root (const string &root) {
117 // append /data to root if it exists
118 SGPath tmp( fg_root );
119 tmp.append( "data" );
120 tmp.append( "version" );
121 if ( ulFileExists( tmp.c_str() ) ) {
125 fgSetString("/sim/fg-root", fg_root.c_str());
128 void FGGlobals::set_fg_scenery (const string &scenery) {
130 if (scenery.empty()) {
132 s.append( "Scenery" );
136 string_list path_list = sgPathSplit( s.str() );
139 for (unsigned i = 0; i < path_list.size(); i++) {
141 ulDir *d = ulOpenDir( path_list[i].c_str() );
146 SGPath pt( path_list[i] ), po( path_list[i] );
147 pt.append("Terrain");
148 po.append("Objects");
150 ulDir *td = ulOpenDir( pt.c_str() );
151 ulDir *od = ulOpenDir( po.c_str() );
153 if (td == NULL && od == NULL)
154 fg_scenery.push_back( path_list[i] );
157 fg_scenery.push_back( pt.str() );
161 fg_scenery.push_back( po.str() );
165 // insert a marker for FGTileEntry::load(), so that
166 // FG_SCENERY=A:B becomes list ["A/Terrain", "A/Objects", "",
167 // "B/Terrain", "B/Objects", ""]
168 fg_scenery.push_back("");
174 FGGlobals::get_renderer () const
180 FGGlobals::get_subsystem_mgr () const
182 return subsystem_mgr;
186 FGGlobals::get_subsystem (const char * name)
188 return subsystem_mgr->get_subsystem(name);
192 FGGlobals::add_subsystem (const char * name,
193 SGSubsystem * subsystem,
194 SGSubsystemMgr::GroupType type,
197 subsystem_mgr->add(name, subsystem, type, min_time_sec);
202 FGGlobals::get_event_mgr () const
208 // Save the current state as the initial state.
210 FGGlobals::saveInitialState ()
212 delete initial_state;
213 initial_state = new SGPropertyNode();
215 if (!copyProperties(props, initial_state))
216 SG_LOG(SG_GENERAL, SG_ALERT, "Error saving initial state");
220 // Restore the saved initial state, if any
222 FGGlobals::restoreInitialState ()
224 if ( initial_state == 0 ) {
225 SG_LOG(SG_GENERAL, SG_ALERT,
226 "No initial state available to restore!!!");
230 SGPropertyNode *currentPresets = new SGPropertyNode;
231 SGPropertyNode *targetNode = fgGetNode( "/sim/presets" );
233 // stash the /sim/presets tree
234 if ( !copyProperties(targetNode, currentPresets) ) {
235 SG_LOG( SG_GENERAL, SG_ALERT, "Failed to save /sim/presets subtree" );
238 if ( copyProperties(initial_state, props) ) {
239 SG_LOG( SG_GENERAL, SG_INFO, "Initial state restored successfully" );
241 SG_LOG( SG_GENERAL, SG_INFO,
242 "Some errors restoring initial state (read-only props?)" );
245 // recover the /sim/presets tree
246 if ( !copyProperties(currentPresets, targetNode) ) {
247 SG_LOG( SG_GENERAL, SG_ALERT,
248 "Failed to restore /sim/presets subtree" );
251 delete currentPresets;
255 FGGlobals::get_current_view () const
257 return viewmgr->get_current_view();
260 // end of globals.cxx