]> git.mxchange.org Git - flightgear.git/blob - src/Main/globals.cxx
20c1c20768bc55181bcba5c3a3a2023107817098
[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
121 SGSubsystemMgr *
122 FGGlobals::get_subsystem_mgr () const
123 {
124     return subsystem_mgr;
125 }
126
127 SGSubsystem *
128 FGGlobals::get_subsystem (const char * name)
129 {
130     return subsystem_mgr->get_subsystem(name);
131 }
132
133 void
134 FGGlobals::add_subsystem (const char * name,
135                           SGSubsystem * subsystem,
136                           SGSubsystemMgr::GroupType type,
137                           double min_time_sec)
138 {
139     subsystem_mgr->add(name, subsystem, type, min_time_sec);
140 }
141
142
143 SGEventMgr *
144 FGGlobals::get_event_mgr () const
145 {
146     return event_mgr;
147 }
148
149
150 // Save the current state as the initial state.
151 void
152 FGGlobals::saveInitialState ()
153 {
154   delete initial_state;
155   initial_state = new SGPropertyNode();
156
157   if (!copyProperties(props, initial_state))
158     SG_LOG(SG_GENERAL, SG_ALERT, "Error saving initial state");
159 }
160
161
162 // Restore the saved initial state, if any
163 void
164 FGGlobals::restoreInitialState ()
165 {
166     if ( initial_state == 0 ) {
167         SG_LOG(SG_GENERAL, SG_ALERT,
168                "No initial state available to restore!!!");
169         return;
170     }
171
172     SGPropertyNode *currentPresets = new SGPropertyNode;
173     SGPropertyNode *targetNode = fgGetNode( "/sim/presets" );
174
175     // stash the /sim/presets tree
176     if ( !copyProperties(targetNode, currentPresets) ) {
177         SG_LOG( SG_GENERAL, SG_ALERT, "Failed to save /sim/presets subtree" );
178     }
179     
180     if ( copyProperties(initial_state, props) ) {
181         SG_LOG( SG_GENERAL, SG_INFO, "Initial state restored successfully" );
182     } else {
183         SG_LOG( SG_GENERAL, SG_INFO,
184                 "Some errors restoring initial state (read-only props?)" );
185     }
186
187     // recover the /sim/presets tree
188     if ( !copyProperties(currentPresets, targetNode) ) {
189         SG_LOG( SG_GENERAL, SG_ALERT,
190                 "Failed to restore /sim/presets subtree" );
191     }
192
193    delete currentPresets;
194 }
195
196 FGViewer *
197 FGGlobals::get_current_view () const
198 {
199   return viewmgr->get_current_view();
200 }
201
202 // end of globals.cxx