]> git.mxchange.org Git - flightgear.git/blob - src/Main/globals.cxx
Remove unnecessary inclusions of sg.h ans ssg.h
[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     renderer( new FGRenderer ),
46     subsystem_mgr( new SGSubsystemMgr ),
47     event_mgr( new SGEventMgr ),
48     sim_time_sec( 0.0 ),
49     fg_root( "" ),
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( NULL ),
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     delete renderer;
100   
101     // make sure only to delete the initial waypoints list if it acually
102     // still exists. 
103     if (initial_waypoints)
104         delete initial_waypoints;
105 }
106
107
108 // set the fg_root path
109 void FGGlobals::set_fg_root (const string &root) {
110     fg_root = root;
111     
112     // append /data to root if it exists
113     SGPath tmp( fg_root );
114     tmp.append( "data" );
115     tmp.append( "version" );
116     if ( ulFileExists( tmp.c_str() ) ) {
117         fg_root += "/data";
118         }
119 }
120
121 void FGGlobals::set_fg_scenery (const string &scenery) {
122     SGPath s;
123     if (scenery.empty()) {
124         s.set( fg_root );
125         s.append( "Scenery" );
126     } else
127         s.set( scenery );
128
129     string_list path_list = sgPathSplit( s.str() );
130     fg_scenery.clear();
131
132     for (unsigned i = 0; i < path_list.size(); i++) {
133
134         ulDir *d = ulOpenDir( path_list[i].c_str() );
135         if (d == NULL)
136             continue;
137         ulCloseDir( d );
138
139         SGPath pt( path_list[i] ), po( path_list[i] );
140         pt.append("Terrain");
141         po.append("Objects");
142
143         ulDir *td = ulOpenDir( pt.c_str() );
144         ulDir *od = ulOpenDir( po.c_str() );
145
146         if (td == NULL && od == NULL)
147             fg_scenery.push_back( path_list[i] );
148         else {
149             if (td != NULL) {
150                 fg_scenery.push_back( pt.str() );
151                 ulCloseDir( td );
152             }
153             if (od != NULL) {
154                 fg_scenery.push_back( po.str() );
155                 ulCloseDir( od );
156             }
157         }
158     }
159 }
160
161
162 FGRenderer *
163 FGGlobals::get_renderer () const
164 {
165    return renderer;
166 }
167
168 SGSubsystemMgr *
169 FGGlobals::get_subsystem_mgr () const
170 {
171     return subsystem_mgr;
172 }
173
174 SGSubsystem *
175 FGGlobals::get_subsystem (const char * name)
176 {
177     return subsystem_mgr->get_subsystem(name);
178 }
179
180 void
181 FGGlobals::add_subsystem (const char * name,
182                           SGSubsystem * subsystem,
183                           SGSubsystemMgr::GroupType type,
184                           double min_time_sec)
185 {
186     subsystem_mgr->add(name, subsystem, type, min_time_sec);
187 }
188
189
190 SGEventMgr *
191 FGGlobals::get_event_mgr () const
192 {
193     return event_mgr;
194 }
195
196
197 // Save the current state as the initial state.
198 void
199 FGGlobals::saveInitialState ()
200 {
201   delete initial_state;
202   initial_state = new SGPropertyNode();
203
204   if (!copyProperties(props, initial_state))
205     SG_LOG(SG_GENERAL, SG_ALERT, "Error saving initial state");
206 }
207
208
209 // Restore the saved initial state, if any
210 void
211 FGGlobals::restoreInitialState ()
212 {
213     if ( initial_state == 0 ) {
214         SG_LOG(SG_GENERAL, SG_ALERT,
215                "No initial state available to restore!!!");
216         return;
217     }
218
219     SGPropertyNode *currentPresets = new SGPropertyNode;
220     SGPropertyNode *targetNode = fgGetNode( "/sim/presets" );
221
222     // stash the /sim/presets tree
223     if ( !copyProperties(targetNode, currentPresets) ) {
224         SG_LOG( SG_GENERAL, SG_ALERT, "Failed to save /sim/presets subtree" );
225     }
226     
227     if ( copyProperties(initial_state, props) ) {
228         SG_LOG( SG_GENERAL, SG_INFO, "Initial state restored successfully" );
229     } else {
230         SG_LOG( SG_GENERAL, SG_INFO,
231                 "Some errors restoring initial state (read-only props?)" );
232     }
233
234     // recover the /sim/presets tree
235     if ( !copyProperties(currentPresets, targetNode) ) {
236         SG_LOG( SG_GENERAL, SG_ALERT,
237                 "Failed to restore /sim/presets subtree" );
238     }
239
240    delete currentPresets;
241 }
242
243 FGViewer *
244 FGGlobals::get_current_view () const
245 {
246   return viewmgr->get_current_view();
247 }
248
249 // end of globals.cxx