]> git.mxchange.org Git - flightgear.git/blob - src/Main/globals.cxx
Miscellaneous tweaks and Bugfixes. Mostly memory leaks ported from the plib
[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/scene/model/modellib.hxx>
28 #include <simgear/sound/soundmgr_openal.hxx>
29 #include <simgear/structure/commands.hxx>
30 #include <simgear/misc/sg_path.hxx>
31 #include <simgear/timing/sg_time.hxx>
32 #include <simgear/ephemeris/ephemeris.hxx>
33 #include <simgear/magvar/magvar.hxx>
34 #include <simgear/scene/material/matlib.hxx>
35
36 #include <Aircraft/controls.hxx>
37 #include <Airports/runways.hxx>
38 #include <ATC/AIMgr.hxx>
39 #include <ATC/ATCmgr.hxx>
40 #include <Autopilot/route_mgr.hxx>
41 #include <Cockpit/panel.hxx>
42 #include <GUI/new_gui.hxx>
43 #include <Model/acmodel.hxx>
44 #include <Model/modelmgr.hxx>
45 #include <MultiPlayer/multiplaymgr.hxx>
46 #include <Navaids/awynet.hxx>
47 #include <Scenery/scenery.hxx>
48 #include <Scenery/tilemgr.hxx>
49
50 #include "globals.hxx"
51 #include "renderer.hxx"
52 #include "viewmgr.hxx"
53
54 #include "fg_props.hxx"
55 #include "fg_io.hxx"
56
57 \f
58 ////////////////////////////////////////////////////////////////////////
59 // Implementation of FGGlobals.
60 ////////////////////////////////////////////////////////////////////////
61
62 // global global :-)
63 FGGlobals *globals;
64
65
66 // Constructor
67 FGGlobals::FGGlobals() :
68     renderer( new FGRenderer ),
69     subsystem_mgr( new SGSubsystemMgr ),
70     event_mgr( new SGEventMgr ),
71     sim_time_sec( 0.0 ),
72     fg_root( "" ),
73 #if defined(FX) && defined(XMESA)
74     fullscreen( true ),
75 #endif
76     warp( 0 ),
77     warp_delta( 0 ),
78     time_params( NULL ),
79     ephem( NULL ),
80     mag( NULL ),
81     route_mgr( NULL ),
82     current_panel( NULL ),
83     soundmgr( NULL ),
84     airports( NULL ),
85     ATC_mgr( NULL ),
86     AI_mgr( NULL ),
87     controls( NULL ),
88     viewmgr( NULL ),
89     props( new SGPropertyNode ),
90     initial_state( NULL ),
91     locale( NULL ),
92     commands( SGCommandMgr::instance() ),
93     model_lib( NULL ),
94     acmodel( NULL ),
95     model_mgr( NULL ),
96     channel_options_list( NULL ),
97     initial_waypoints( NULL ),
98     scenery( NULL ),
99     tile_mgr( NULL ),
100     io( new FGIO ),
101     fontcache ( new FGFontCache ),
102     navlist( NULL ),
103     loclist( NULL ),
104     gslist( NULL ),
105     dmelist( NULL ),
106     mkrlist( NULL ),
107     tacanlist( NULL ),
108     carrierlist( NULL ), 
109     fixlist( NULL )
110 {
111 }
112
113
114 // Destructor
115 FGGlobals::~FGGlobals() 
116 {
117     delete renderer;
118 // The AIModels manager performs a number of actions upon
119     // Shutdown that implicitly assume that other subsystems
120     // are still operational (Due to the dynamic allocation and
121     // deallocation of AIModel objects. To ensure we can safely
122     // shut down all subsystems, make sure we take down the 
123     // AIModels system first.
124     subsystem_mgr->get_group(SGSubsystemMgr::GENERAL)->remove_subsystem("ai_model");
125      delete subsystem_mgr;
126      delete event_mgr;
127      delete time_params;
128      delete ephem;
129      delete mag;
130      delete matlib;
131      delete route_mgr;
132      delete current_panel;
133      delete soundmgr;
134      delete airports;
135
136      delete runways;
137      delete ATC_mgr;
138      delete AI_mgr;
139      delete controls;
140      delete viewmgr;
141
142      delete initial_state;
143 //     //delete locale; Don't delete locale
144 //     delete commands;
145      delete model_lib;
146      delete acmodel;
147      delete model_mgr;
148      delete channel_options_list;
149      delete initial_waypoints;
150      delete scenery;
151      //delete tile_mgr; // Don't delete tile manager yet, because loader thread problems
152      delete io;
153      delete fontcache;
154
155     delete navlist;
156     delete loclist;
157     delete gslist;
158     delete dmelist;
159     delete mkrlist;
160     delete tacanlist;
161     delete carrierlist;
162     delete channellist;
163     delete fixlist;
164     delete airwaynet;
165     delete multiplayer_mgr;
166  
167     delete props;
168 }
169
170
171 // set the fg_root path
172 void FGGlobals::set_fg_root (const string &root) {
173     fg_root = root;
174     
175     // append /data to root if it exists
176     SGPath tmp( fg_root );
177     tmp.append( "data" );
178     tmp.append( "version" );
179     if ( ulFileExists( tmp.c_str() ) ) {
180         fg_root += "/data";
181     }
182
183     fgSetString("/sim/fg-root", fg_root.c_str());   
184 }
185
186 void FGGlobals::set_fg_scenery (const string &scenery) {
187     SGPath s;
188     if (scenery.empty()) {
189         s.set( fg_root );
190         s.append( "Scenery" );
191     } else
192         s.set( scenery );
193
194     string_list path_list = sgPathSplit( s.str() );
195     fg_scenery.clear();
196
197     for (unsigned i = 0; i < path_list.size(); i++) {
198
199         ulDir *d = ulOpenDir( path_list[i].c_str() );
200         if (d == NULL)
201             continue;
202         ulCloseDir( d );
203
204         SGPath pt( path_list[i] ), po( path_list[i] );
205         pt.append("Terrain");
206         po.append("Objects");
207
208         ulDir *td = ulOpenDir( pt.c_str() );
209         ulDir *od = ulOpenDir( po.c_str() );
210
211         if (td == NULL && od == NULL)
212             fg_scenery.push_back( path_list[i] );
213         else {
214             if (td != NULL) {
215                 fg_scenery.push_back( pt.str() );
216                 ulCloseDir( td );
217             }
218             if (od != NULL) {
219                 fg_scenery.push_back( po.str() );
220                 ulCloseDir( od );
221             }
222         }
223         // insert a marker for FGTileEntry::load(), so that
224         // FG_SCENERY=A:B becomes list ["A/Terrain", "A/Objects", "",
225         // "B/Terrain", "B/Objects", ""]
226         fg_scenery.push_back("");
227     }
228 }
229
230
231 FGRenderer *
232 FGGlobals::get_renderer () const
233 {
234    return renderer;
235 }
236
237 SGSubsystemMgr *
238 FGGlobals::get_subsystem_mgr () const
239 {
240     return subsystem_mgr;
241 }
242
243 SGSubsystem *
244 FGGlobals::get_subsystem (const char * name)
245 {
246     return subsystem_mgr->get_subsystem(name);
247 }
248
249 void
250 FGGlobals::add_subsystem (const char * name,
251                           SGSubsystem * subsystem,
252                           SGSubsystemMgr::GroupType type,
253                           double min_time_sec)
254 {
255     subsystem_mgr->add(name, subsystem, type, min_time_sec);
256 }
257
258
259 SGEventMgr *
260 FGGlobals::get_event_mgr () const
261 {
262     return event_mgr;
263 }
264
265
266 // Save the current state as the initial state.
267 void
268 FGGlobals::saveInitialState ()
269 {
270   delete initial_state;
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