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( new SGCommandMgr ),
80 channel_options_list( NULL ),
81 initial_waypoints( NULL ),
85 fontcache ( new FGFontCache ),
99 FGGlobals::~FGGlobals()
102 delete subsystem_mgr;
104 delete initial_state;
110 delete initial_waypoints;
114 // set the fg_root path
115 void FGGlobals::set_fg_root (const string &root) {
118 // append /data to root if it exists
119 SGPath tmp( fg_root );
120 tmp.append( "data" );
121 tmp.append( "version" );
122 if ( ulFileExists( tmp.c_str() ) ) {
126 fgSetString("/sim/fg-root", fg_root.c_str());
129 void FGGlobals::set_fg_scenery (const string &scenery) {
131 if (scenery.empty()) {
133 s.append( "Scenery" );
137 string_list path_list = sgPathSplit( s.str() );
140 for (unsigned i = 0; i < path_list.size(); i++) {
142 ulDir *d = ulOpenDir( path_list[i].c_str() );
147 SGPath pt( path_list[i] ), po( path_list[i] );
148 pt.append("Terrain");
149 po.append("Objects");
151 ulDir *td = ulOpenDir( pt.c_str() );
152 ulDir *od = ulOpenDir( po.c_str() );
154 if (td == NULL && od == NULL)
155 fg_scenery.push_back( path_list[i] );
158 fg_scenery.push_back( pt.str() );
162 fg_scenery.push_back( po.str() );
166 // insert a marker for FGTileEntry::load(), so that
167 // FG_SCENERY=A:B becomes list ["A/Terrain", "A/Objects", "",
168 // "B/Terrain", "B/Objects", ""]
169 fg_scenery.push_back("");
175 FGGlobals::get_renderer () const
181 FGGlobals::get_subsystem_mgr () const
183 return subsystem_mgr;
187 FGGlobals::get_subsystem (const char * name)
189 return subsystem_mgr->get_subsystem(name);
193 FGGlobals::add_subsystem (const char * name,
194 SGSubsystem * subsystem,
195 SGSubsystemMgr::GroupType type,
198 subsystem_mgr->add(name, subsystem, type, min_time_sec);
203 FGGlobals::get_event_mgr () const
209 // Save the current state as the initial state.
211 FGGlobals::saveInitialState ()
213 delete initial_state;
214 initial_state = new SGPropertyNode();
216 if (!copyProperties(props, initial_state))
217 SG_LOG(SG_GENERAL, SG_ALERT, "Error saving initial state");
221 // Restore the saved initial state, if any
223 FGGlobals::restoreInitialState ()
225 if ( initial_state == 0 ) {
226 SG_LOG(SG_GENERAL, SG_ALERT,
227 "No initial state available to restore!!!");
231 SGPropertyNode *currentPresets = new SGPropertyNode;
232 SGPropertyNode *targetNode = fgGetNode( "/sim/presets" );
234 // stash the /sim/presets tree
235 if ( !copyProperties(targetNode, currentPresets) ) {
236 SG_LOG( SG_GENERAL, SG_ALERT, "Failed to save /sim/presets subtree" );
239 if ( copyProperties(initial_state, props) ) {
240 SG_LOG( SG_GENERAL, SG_INFO, "Initial state restored successfully" );
242 SG_LOG( SG_GENERAL, SG_INFO,
243 "Some errors restoring initial state (read-only props?)" );
246 // recover the /sim/presets tree
247 if ( !copyProperties(currentPresets, targetNode) ) {
248 SG_LOG( SG_GENERAL, SG_ALERT,
249 "Failed to restore /sim/presets subtree" );
252 delete currentPresets;
256 FGGlobals::get_current_view () const
258 return viewmgr->get_current_view();
261 // end of globals.cxx