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