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