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