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