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