]> git.mxchange.org Git - flightgear.git/blob - src/Main/globals.cxx
Make loadxml use an absolute path, like savexml (for consistency reasons, and
[flightgear.git] / src / Main / globals.cxx
1 // globals.cxx -- Global state that needs to be shared among the sim modules
2 //
3 // Written by Curtis Olson, started July 2000.
4 //
5 // Copyright (C) 2000  Curtis L. Olson - http://www.flightgear.org/~curt
6 //
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.
11 //
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.
16 //
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.
20 //
21 // $Id$
22
23 #ifdef HAVE_CONFIG_H
24 #  include <config.h>
25 #endif
26
27 #include <simgear/sound/soundmgr_openal.hxx>
28 #include <simgear/structure/commands.hxx>
29 #include <simgear/misc/sg_path.hxx>
30
31 #include <GUI/new_gui.hxx>
32
33 #include "globals.hxx"
34 #include "renderer.hxx"
35 #include "viewmgr.hxx"
36
37 #include "fg_props.hxx"
38 #include "fg_io.hxx"
39
40 \f
41 ////////////////////////////////////////////////////////////////////////
42 // Implementation of FGGlobals.
43 ////////////////////////////////////////////////////////////////////////
44
45 // global global :-)
46 FGGlobals *globals;
47
48
49 // Constructor
50 FGGlobals::FGGlobals() :
51     renderer( new FGRenderer ),
52     subsystem_mgr( new SGSubsystemMgr ),
53     event_mgr( new SGEventMgr ),
54     sim_time_sec( 0.0 ),
55     fg_root( "" ),
56 #if defined(FX) && defined(XMESA)
57     fullscreen( true ),
58 #endif
59     warp( 0 ),
60     warp_delta( 0 ),
61     time_params( NULL ),
62     ephem( NULL ),
63     mag( NULL ),
64     route_mgr( NULL ),
65     current_panel( NULL ),
66     soundmgr( NULL ),
67     airports( NULL ),
68     ATC_mgr( NULL ),
69     AI_mgr( NULL ),
70     controls( NULL ),
71     viewmgr( NULL ),
72     props( new SGPropertyNode ),
73     initial_state( NULL ),
74     locale( NULL ),
75     commands( SGCommandMgr::instance() ),
76     model_lib( NULL ),
77     acmodel( NULL ),
78     model_mgr( NULL ),
79     channel_options_list( NULL ),
80     initial_waypoints( NULL ),
81     scenery( NULL ),
82     tile_mgr( NULL ),
83     io( new FGIO ),
84     fontcache ( new FGFontCache ),
85     navlist( NULL ),
86     loclist( NULL ),
87     gslist( NULL ),
88     dmelist( NULL ),
89     mkrlist( NULL ),
90     tacanlist( NULL ),
91     carrierlist( NULL ), 
92     fixlist( NULL )
93 {
94 }
95
96
97 // Destructor
98 FGGlobals::~FGGlobals() 
99 {
100     delete soundmgr;
101     delete subsystem_mgr;
102     delete event_mgr;
103     delete initial_state;
104     delete props;
105     delete io;
106     delete fontcache;
107     delete renderer;
108     delete initial_waypoints;
109 }
110
111
112 // set the fg_root path
113 void FGGlobals::set_fg_root (const string &root) {
114     fg_root = root;
115     
116     // append /data to root if it exists
117     SGPath tmp( fg_root );
118     tmp.append( "data" );
119     tmp.append( "version" );
120     if ( ulFileExists( tmp.c_str() ) ) {
121         fg_root += "/data";
122     }
123
124     fgSetString("/sim/fg-root", fg_root.c_str());   
125 }
126
127 void FGGlobals::set_fg_scenery (const string &scenery) {
128     SGPath s;
129     if (scenery.empty()) {
130         s.set( fg_root );
131         s.append( "Scenery" );
132     } else
133         s.set( scenery );
134
135     string_list path_list = sgPathSplit( s.str() );
136     fg_scenery.clear();
137
138     for (unsigned i = 0; i < path_list.size(); i++) {
139
140         ulDir *d = ulOpenDir( path_list[i].c_str() );
141         if (d == NULL)
142             continue;
143         ulCloseDir( d );
144
145         SGPath pt( path_list[i] ), po( path_list[i] );
146         pt.append("Terrain");
147         po.append("Objects");
148
149         ulDir *td = ulOpenDir( pt.c_str() );
150         ulDir *od = ulOpenDir( po.c_str() );
151
152         if (td == NULL && od == NULL)
153             fg_scenery.push_back( path_list[i] );
154         else {
155             if (td != NULL) {
156                 fg_scenery.push_back( pt.str() );
157                 ulCloseDir( td );
158             }
159             if (od != NULL) {
160                 fg_scenery.push_back( po.str() );
161                 ulCloseDir( od );
162             }
163         }
164         // insert a marker for FGTileEntry::load(), so that
165         // FG_SCENERY=A:B becomes list ["A/Terrain", "A/Objects", "",
166         // "B/Terrain", "B/Objects", ""]
167         fg_scenery.push_back("");
168     }
169 }
170
171
172 FGRenderer *
173 FGGlobals::get_renderer () const
174 {
175    return renderer;
176 }
177
178 SGSubsystemMgr *
179 FGGlobals::get_subsystem_mgr () const
180 {
181     return subsystem_mgr;
182 }
183
184 SGSubsystem *
185 FGGlobals::get_subsystem (const char * name)
186 {
187     return subsystem_mgr->get_subsystem(name);
188 }
189
190 void
191 FGGlobals::add_subsystem (const char * name,
192                           SGSubsystem * subsystem,
193                           SGSubsystemMgr::GroupType type,
194                           double min_time_sec)
195 {
196     subsystem_mgr->add(name, subsystem, type, min_time_sec);
197 }
198
199
200 SGEventMgr *
201 FGGlobals::get_event_mgr () const
202 {
203     return event_mgr;
204 }
205
206
207 // Save the current state as the initial state.
208 void
209 FGGlobals::saveInitialState ()
210 {
211   delete initial_state;
212   initial_state = new SGPropertyNode();
213
214   if (!copyProperties(props, initial_state))
215     SG_LOG(SG_GENERAL, SG_ALERT, "Error saving initial state");
216 }
217
218
219 // Restore the saved initial state, if any
220 void
221 FGGlobals::restoreInitialState ()
222 {
223     if ( initial_state == 0 ) {
224         SG_LOG(SG_GENERAL, SG_ALERT,
225                "No initial state available to restore!!!");
226         return;
227     }
228
229     SGPropertyNode *currentPresets = new SGPropertyNode;
230     SGPropertyNode *targetNode = fgGetNode( "/sim/presets" );
231
232     // stash the /sim/presets tree
233     if ( !copyProperties(targetNode, currentPresets) ) {
234         SG_LOG( SG_GENERAL, SG_ALERT, "Failed to save /sim/presets subtree" );
235     }
236     
237     if ( copyProperties(initial_state, props) ) {
238         SG_LOG( SG_GENERAL, SG_INFO, "Initial state restored successfully" );
239     } else {
240         SG_LOG( SG_GENERAL, SG_INFO,
241                 "Some errors restoring initial state (read-only props?)" );
242     }
243
244     // recover the /sim/presets tree
245     if ( !copyProperties(currentPresets, targetNode) ) {
246         SG_LOG( SG_GENERAL, SG_ALERT,
247                 "Failed to restore /sim/presets subtree" );
248     }
249
250    delete currentPresets;
251 }
252
253 FGViewer *
254 FGGlobals::get_current_view () const
255 {
256   return viewmgr->get_current_view();
257 }
258
259 // end of globals.cxx