]> git.mxchange.org Git - flightgear.git/blob - src/Main/globals.cxx
d000cc954b89e88e33dc6caa6e750f42324a1a93
[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     fixlist( NULL ),
81     ilslist( NULL ),
82     beacons( NULL )
83 {
84 }
85
86
87 // Destructor
88 FGGlobals::~FGGlobals() 
89 {
90     delete soundmgr;
91     delete subsystem_mgr;
92     delete event_mgr;
93     delete initial_state;
94     delete props;
95     delete commands;
96     delete io;
97   
98     // make sure only to delete the initial waypoints list if it acually
99     // still exists. 
100     if (initial_waypoints)
101         delete initial_waypoints;
102 }
103
104
105 // set the fg_root path
106 void FGGlobals::set_fg_root (const string &root) {
107     fg_root = root;
108     
109     // append /data to root if it exists
110     SGPath tmp( fg_root );
111     tmp.append( "data" );
112     tmp.append( "version" );
113     if ( ulFileExists( tmp.c_str() ) ) {
114         fg_root += "/data";
115         }
116 }
117
118
119 SGSubsystemMgr *
120 FGGlobals::get_subsystem_mgr () const
121 {
122     return subsystem_mgr;
123 }
124
125 SGSubsystem *
126 FGGlobals::get_subsystem (const char * name)
127 {
128     return subsystem_mgr->get_subsystem(name);
129 }
130
131 void
132 FGGlobals::add_subsystem (const char * name,
133                           SGSubsystem * subsystem,
134                           SGSubsystemMgr::GroupType type,
135                           double min_time_sec)
136 {
137     subsystem_mgr->add(name, subsystem, type, min_time_sec);
138 }
139
140
141 SGEventMgr *
142 FGGlobals::get_event_mgr () const
143 {
144     return event_mgr;
145 }
146
147
148 // Save the current state as the initial state.
149 void
150 FGGlobals::saveInitialState ()
151 {
152   delete initial_state;
153   initial_state = new SGPropertyNode();
154
155   if (!copyProperties(props, initial_state))
156     SG_LOG(SG_GENERAL, SG_ALERT, "Error saving initial state");
157 }
158
159
160 // Restore the saved initial state, if any
161 void
162 FGGlobals::restoreInitialState ()
163 {
164     if ( initial_state == 0 ) {
165         SG_LOG(SG_GENERAL, SG_ALERT,
166                "No initial state available to restore!!!");
167         return;
168     }
169
170     SGPropertyNode *currentPresets = new SGPropertyNode;
171     SGPropertyNode *targetNode = fgGetNode( "/sim/presets" );
172
173     // stash the /sim/presets tree
174     if ( !copyProperties(targetNode, currentPresets) ) {
175         SG_LOG( SG_GENERAL, SG_ALERT, "Failed to save /sim/presets subtree" );
176     }
177     
178     if ( copyProperties(initial_state, props) ) {
179         SG_LOG( SG_GENERAL, SG_INFO, "Initial state restored successfully" );
180     } else {
181         SG_LOG( SG_GENERAL, SG_INFO,
182                 "Some errors restoring initial state (read-only props?)" );
183     }
184
185     // recover the /sim/presets tree
186     if ( !copyProperties(currentPresets, targetNode) ) {
187         SG_LOG( SG_GENERAL, SG_ALERT,
188                 "Failed to restore /sim/presets subtree" );
189     }
190
191    delete currentPresets;
192 }
193
194 FGViewer *
195 FGGlobals::get_current_view () const
196 {
197   return viewmgr->get_current_view();
198 }
199
200 // end of globals.cxx