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