]> git.mxchange.org Git - flightgear.git/blob - src/Main/globals.cxx
Removed FGEnvironmentMgr as a special case in globals, initialization,
[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/misc/commands.hxx>
25 #include <simgear/misc/sg_path.hxx>
26
27 #include "globals.hxx"
28 #include "viewmgr.hxx"
29
30 #include "fg_props.hxx"
31 #include "fg_io.hxx"
32
33 \f
34 ////////////////////////////////////////////////////////////////////////
35 // Implementation of FGGlobals.
36 ////////////////////////////////////////////////////////////////////////
37
38 // global global :-)
39 FGGlobals *globals;
40
41
42 // Constructor
43 FGGlobals::FGGlobals() :
44     subsystem_mgr( new FGSubsystemMgr ),
45     sim_time_sec( 0.0 ),
46     fg_root( "" ),
47     fg_scenery( "" ),
48 #if defined(FX) && defined(XMESA)
49     fullscreen( true ),
50 #endif
51     warp( 0 ),
52     warp_delta( 0 ),
53     time_params( NULL ),
54     ephem( NULL ),
55     mag( NULL ),
56     autopilot( NULL ),
57     route( NULL ),
58     current_panel( NULL ),
59     soundmgr( NULL ),
60     ATC_mgr( NULL ),
61     ATC_display( NULL ),
62     AI_mgr( NULL ),
63     controls( NULL ),
64     viewmgr( NULL ),
65     props( new SGPropertyNode ),
66     initial_state( NULL ),
67     locale( NULL ),
68     commands( new SGCommandMgr ),
69     model_lib( NULL ),
70     acmodel( NULL ),
71     model_mgr( NULL ),
72     channel_options_list( NULL ),
73     scenery( NULL ),
74     tile_mgr( NULL ),
75     io( new FGIO )
76 {
77 }
78
79
80 // Destructor
81 FGGlobals::~FGGlobals() 
82 {
83   delete subsystem_mgr;
84   delete initial_state;
85   delete props;
86   delete commands;
87   delete io;
88 }
89
90
91 // set the fg_root path
92 void FGGlobals::set_fg_root (const string &root) {
93     fg_root = root;
94     
95     // append /data to root if it exists
96     SGPath tmp( fg_root );
97     tmp.append( "data" );
98     tmp.append( "version" );
99     if ( ulFileExists( tmp.c_str() ) ) {
100         fg_root += "/data";
101         }
102 }
103
104
105 FGSubsystemMgr *
106 FGGlobals::get_subsystem_mgr () const
107 {
108     return subsystem_mgr;
109 }
110
111 FGSubsystem *
112 FGGlobals::get_subsystem (const char * name)
113 {
114     return subsystem_mgr->get_subsystem(name);
115 }
116
117 void
118 FGGlobals::add_subsystem (const char * name,
119                           FGSubsystem * subsystem,
120                           FGSubsystemMgr::GroupType type,
121                           double min_time_sec)
122 {
123     subsystem_mgr->add(name, subsystem, type, min_time_sec);
124 }
125
126
127 // Save the current state as the initial state.
128 void
129 FGGlobals::saveInitialState ()
130 {
131   delete initial_state;
132   initial_state = new SGPropertyNode();
133   if (!copyProperties(props, initial_state))
134     SG_LOG(SG_GENERAL, SG_ALERT, "Error saving initial state");
135 }
136
137
138 // Restore the saved initial state, if any
139 void
140 FGGlobals::restoreInitialState ()
141 {
142     if ( initial_state == 0 ) {
143         SG_LOG(SG_GENERAL, SG_ALERT,
144                "No initial state available to restore!!!");
145         return;
146     }
147
148     SGPropertyNode *currentPresets = new SGPropertyNode;
149     SGPropertyNode *targetNode = fgGetNode( "/sim/presets" );
150
151     // stash the /sim/presets tree
152     if ( !copyProperties(targetNode, currentPresets) ) {
153         SG_LOG( SG_GENERAL, SG_ALERT, "Failed to save /sim/presets subtree" );
154     }
155     
156     if ( copyProperties(initial_state, props) ) {
157         SG_LOG( SG_GENERAL, SG_INFO, "Initial state restored successfully" );
158     } else {
159         SG_LOG( SG_GENERAL, SG_INFO,
160                 "Some errors restoring initial state (read-only props?)" );
161     }
162
163     // recover the /sim/presets tree
164     if ( !copyProperties(currentPresets, targetNode) ) {
165         SG_LOG( SG_GENERAL, SG_ALERT,
166                 "Failed to restore /sim/presets subtree" );
167     }
168
169    delete currentPresets;
170 }
171
172 FGViewer *
173 FGGlobals::get_current_view () const
174 {
175   return viewmgr->get_current_view();
176 }
177
178 // end of globals.cxx