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