]> git.mxchange.org Git - flightgear.git/blob - src/Main/globals.cxx
warning fixes
[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 #include <simgear/timing/sg_time.hxx>
31 #include <simgear/ephemeris/ephemeris.hxx>
32 #include <simgear/magvar/magvar.hxx>
33 #include <simgear/scene/material/matlib.hxx>
34 #include <simgear/structure/subsystem_mgr.hxx>
35 #include <simgear/structure/event_mgr.hxx>
36
37 #include <Aircraft/controls.hxx>
38 #include <Airports/runways.hxx>
39 #include <ATCDCL/AIMgr.hxx>
40 #include <ATCDCL/ATCmgr.hxx>
41 #include <Autopilot/route_mgr.hxx>
42 #include <Cockpit/panel.hxx>
43 #include <GUI/new_gui.hxx>
44 #include <Model/acmodel.hxx>
45 #include <Model/modelmgr.hxx>
46 #include <MultiPlayer/multiplaymgr.hxx>
47 #include <Navaids/awynet.hxx>
48 #include <Scenery/scenery.hxx>
49 #include <Scenery/tilemgr.hxx>
50 #include <Navaids/navlist.hxx>
51
52 #include "globals.hxx"
53 #include "renderer.hxx"
54 #include "viewmgr.hxx"
55
56 #include "fg_props.hxx"
57 #include "fg_io.hxx"
58
59 \f
60 ////////////////////////////////////////////////////////////////////////
61 // Implementation of FGGlobals.
62 ////////////////////////////////////////////////////////////////////////
63
64 // global global :-)
65 FGGlobals *globals;
66
67
68 // Constructor
69 FGGlobals::FGGlobals() :
70     renderer( new FGRenderer ),
71     subsystem_mgr( new SGSubsystemMgr ),
72     event_mgr( new SGEventMgr ),
73     sim_time_sec( 0.0 ),
74     fg_root( "" ),
75     warp( 0 ),
76     warp_delta( 0 ),
77     time_params( NULL ),
78     ephem( NULL ),
79     mag( NULL ),
80     matlib( NULL ),
81     route_mgr( NULL ),
82     current_panel( NULL ),
83     soundmgr( NULL ),
84     ATC_mgr( NULL ),
85     AI_mgr( NULL ),
86     controls( NULL ),
87     viewmgr( NULL ),
88     props( new SGPropertyNode ),
89     initial_state( NULL ),
90     locale( NULL ),
91     commands( SGCommandMgr::instance() ),
92     acmodel( NULL ),
93     model_mgr( NULL ),
94     channel_options_list( NULL ),
95     initial_waypoints( NULL ),
96     scenery( NULL ),
97     tile_mgr( NULL ),
98     io( new FGIO ),
99     fontcache ( new FGFontCache ),
100     navlist( NULL ),
101     loclist( NULL ),
102     gslist( NULL ),
103     dmelist( NULL ),
104     tacanlist( NULL ),
105     carrierlist( NULL ),
106     channellist( NULL ),
107     airwaynet( NULL ),
108     multiplayer_mgr( NULL )
109 {
110 }
111
112
113 // Destructor
114 FGGlobals::~FGGlobals() 
115 {
116     delete renderer;
117 // The AIModels manager performs a number of actions upon
118     // Shutdown that implicitly assume that other subsystems
119     // are still operational (Due to the dynamic allocation and
120     // deallocation of AIModel objects. To ensure we can safely
121     // shut down all subsystems, make sure we take down the 
122     // AIModels system first.
123     subsystem_mgr->get_group(SGSubsystemMgr::GENERAL)->remove_subsystem("ai_model");
124     delete subsystem_mgr;
125     delete event_mgr;
126     delete time_params;
127     delete ephem;
128     delete mag;
129     delete matlib;
130     delete route_mgr;
131     delete current_panel;
132     delete soundmgr;
133
134     delete ATC_mgr;
135     delete AI_mgr;
136     delete controls;
137     delete viewmgr;
138
139 //     delete commands;
140     delete acmodel;
141     delete model_mgr;
142     delete channel_options_list;
143     delete initial_waypoints;
144     delete tile_mgr;
145     delete scenery;
146     delete io;
147     delete fontcache;
148
149     delete navlist;
150     delete loclist;
151     delete gslist;
152     delete dmelist;
153     delete tacanlist;
154     delete carrierlist;
155     delete channellist;
156     delete airwaynet;
157     delete multiplayer_mgr;
158 }
159
160
161 // set the fg_root path
162 void FGGlobals::set_fg_root (const string &root) {
163     fg_root = root;
164
165     // append /data to root if it exists
166     SGPath tmp( fg_root );
167     tmp.append( "data" );
168     tmp.append( "version" );
169     if ( ulFileExists( tmp.c_str() ) ) {
170         fgGetNode("BAD_FG_ROOT", true)->setStringValue(fg_root);
171         fg_root += "/data";
172         fgGetNode("GOOD_FG_ROOT", true)->setStringValue(fg_root);
173         SG_LOG(SG_GENERAL, SG_ALERT, "***\n***\n*** Warning: changing bad FG_ROOT/--fg-root to '"
174                 << fg_root << "'\n***\n***");
175     }
176
177     // remove /sim/fg-root before writing to prevent hijacking
178     SGPropertyNode *n = fgGetNode("/sim", true);
179     n->removeChild("fg-root", 0, false);
180     n = n->getChild("fg-root", 0, true);
181     n->setStringValue(fg_root.c_str());
182     n->setAttribute(SGPropertyNode::WRITE, false);
183 }
184
185 void FGGlobals::set_fg_scenery (const string &scenery) {
186     SGPath s;
187     if (scenery.empty()) {
188         s.set( fg_root );
189         s.append( "Scenery" );
190     } else
191         s.set( scenery );
192
193     string_list path_list = sgPathSplit( s.str() );
194     fg_scenery.clear();
195
196     for (unsigned i = 0; i < path_list.size(); i++) {
197
198         ulDir *d = ulOpenDir( path_list[i].c_str() );
199         if (d == NULL)
200             continue;
201         ulCloseDir( d );
202
203         SGPath pt( path_list[i] ), po( path_list[i] );
204         pt.append("Terrain");
205         po.append("Objects");
206
207         ulDir *td = ulOpenDir( pt.c_str() );
208         ulDir *od = ulOpenDir( po.c_str() );
209
210         // "Terrain" and "Airports" directory don't exist. add directory as is
211         // otherwise, automatically append either Terrain, Objects, or both
212         //if (td == NULL && od == NULL)
213             fg_scenery.push_back( path_list[i] );
214         //else {
215             if (td != NULL) {
216                 fg_scenery.push_back( pt.str() );
217                 ulCloseDir( td );
218             }
219             if (od != NULL) {
220                 fg_scenery.push_back( po.str() );
221                 ulCloseDir( od );
222             }
223         //}
224         // insert a marker for FGTileEntry::load(), so that
225         // FG_SCENERY=A:B becomes list ["A/Terrain", "A/Objects", "",
226         // "B/Terrain", "B/Objects", ""]
227         fg_scenery.push_back("");
228     }
229 }
230
231
232 FGRenderer *
233 FGGlobals::get_renderer () const
234 {
235    return renderer;
236 }
237
238 SGSubsystemMgr *
239 FGGlobals::get_subsystem_mgr () const
240 {
241     return subsystem_mgr;
242 }
243
244 SGSubsystem *
245 FGGlobals::get_subsystem (const char * name)
246 {
247     return subsystem_mgr->get_subsystem(name);
248 }
249
250 void
251 FGGlobals::add_subsystem (const char * name,
252                           SGSubsystem * subsystem,
253                           SGSubsystemMgr::GroupType type,
254                           double min_time_sec)
255 {
256     subsystem_mgr->add(name, subsystem, type, min_time_sec);
257 }
258
259
260 SGEventMgr *
261 FGGlobals::get_event_mgr () const
262 {
263     return event_mgr;
264 }
265
266
267 // Save the current state as the initial state.
268 void
269 FGGlobals::saveInitialState ()
270 {
271   initial_state = new SGPropertyNode();
272
273   if (!copyProperties(props, initial_state))
274     SG_LOG(SG_GENERAL, SG_ALERT, "Error saving initial state");
275 }
276
277
278 // Restore the saved initial state, if any
279 void
280 FGGlobals::restoreInitialState ()
281 {
282     if ( initial_state == 0 ) {
283         SG_LOG(SG_GENERAL, SG_ALERT,
284                "No initial state available to restore!!!");
285         return;
286     }
287
288     SGPropertyNode *currentPresets = new SGPropertyNode;
289     SGPropertyNode *targetNode = fgGetNode( "/sim/presets" );
290
291     // stash the /sim/presets tree
292     if ( !copyProperties(targetNode, currentPresets) ) {
293         SG_LOG( SG_GENERAL, SG_ALERT, "Failed to save /sim/presets subtree" );
294     }
295     
296     if ( copyProperties(initial_state, props) ) {
297         SG_LOG( SG_GENERAL, SG_INFO, "Initial state restored successfully" );
298     } else {
299         SG_LOG( SG_GENERAL, SG_INFO,
300                 "Some errors restoring initial state (read-only props?)" );
301     }
302
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" );
307     }
308
309    delete currentPresets;
310 }
311
312 FGViewer *
313 FGGlobals::get_current_view () const
314 {
315   return viewmgr->get_current_view();
316 }
317
318 // end of globals.cxx