]> git.mxchange.org Git - flightgear.git/blob - src/Main/fgfs.hxx
Added a "Presets" menu.
[flightgear.git] / src / Main / fgfs.hxx
1 // fgfs.hxx -- top level include file for FlightGear.
2 //
3 // Written by David Megginson, started 2000-12
4 //
5 // Copyright (C) 2000  David Megginson, david@megginson.com
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 #ifndef __FGFS_HXX
25 #define __FGFS_HXX 1
26
27
28 #ifdef HAVE_CONFIG_H
29 #  include <config.h>
30 #endif
31
32 #include <simgear/compiler.h>
33
34 // #ifdef SG_MATH_EXCEPTION_CLASH
35 // #  include <math.h>
36 // #endif
37
38 #ifdef HAVE_WINDOWS_H
39 #  include <windows.h>                     
40 #  include <float.h>                    
41 #endif
42
43 #include STL_STRING
44 SG_USING_STD(string);
45
46 #include <vector>
47 SG_USING_STD(vector);
48
49 #include <map>
50 SG_USING_STD(map);
51
52 #include <simgear/misc/props.hxx>
53
54
55 \f
56 /**
57  * Basic interface for all FlightGear subsystems.
58  *
59  * <p>This is an abstract interface that all FlightGear subsystems
60  * will eventually implement.  It defines the basic operations for
61  * each subsystem: initialization, property binding and unbinding, and
62  * updating.  Interfaces may define additional methods, but the
63  * preferred way of exchanging information with other subsystems is
64  * through the property tree.</p>
65  *
66  * <p>To publish information through a property, a subsystem should
67  * bind it to a variable or (if necessary) a getter/setter pair in the
68  * bind() method, and release the property in the unbind() method:</p>
69  *
70  * <pre>
71  * void MySubsystem::bind ()
72  * {
73  *   fgTie("/controls/elevator", &_elevator);
74  *   fgSetArchivable("/controls/elevator");
75  * }
76  *
77  * void MySubsystem::unbind ()
78  * {
79  *   fgUntie("/controls/elevator");
80  * }
81  * </pre>
82  *
83  * <p>To reference a property (possibly) from another subsystem, there
84  * are two alternatives.  If the property will be referenced only
85  * infrequently (say, in the init() method), then the fgGet* methods
86  * declared in fg_props.hxx are the simplest:</p>
87  *
88  * <pre>
89  * void MySubsystem::init ()
90  * {
91  *   _errorMargin = fgGetFloat("/display/error-margin-pct");
92  * }
93  * </pre>
94  *
95  * <p>On the other hand, if the property will be referenced frequently
96  * (say, in the update() method), then the hash-table lookup required
97  * by the fgGet* methods might be too expensive; instead, the
98  * subsystem should obtain a reference to the actual property node in
99  * its init() function and use that reference in the main loop:</p>
100  *
101  * <pre>
102  * void MySubsystem::init ()
103  * {
104  *   _errorNode = fgGetNode("/display/error-margin-pct", true);
105  * }
106  *
107  * void MySubsystem::update (double delta_time_sec)
108  * {
109  *   do_something(_errorNode.getFloatValue());
110  * }
111  * </pre>
112  *
113  * <p>The node returned will always be a pointer to SGPropertyNode,
114  * and the subsystem should <em>not</em> delete it in its destructor
115  * (the pointer belongs to the property tree, not the subsystem).</p>
116  *
117  * <p>The program may ask the subsystem to suspend or resume
118  * sim-time-dependent operations; by default, the suspend() and
119  * resume() methods set the protected variable <var>_suspended</var>,
120  * which the subsystem can reference in its update() method, but
121  * subsystems may also override the suspend() and resume() methods to
122  * take different actions.</p>
123  */
124 class FGSubsystem
125 {
126 public:
127
128   /**
129    * Default constructor.
130    */
131   FGSubsystem ();
132
133   /**
134    * Virtual destructor to ensure that subclass destructors are called.
135    */
136   virtual ~FGSubsystem ();
137
138
139   /**
140    * Initialize the subsystem.
141    *
142    * <p>This method should set up the state of the subsystem, but
143    * should not bind any properties.  Note that any dependencies on
144    * the state of other subsystems should be placed here rather than
145    * in the constructor, so that FlightGear can control the
146    * initialization order.</p>
147    */
148   virtual void init ();
149
150
151   /**
152    * Acquire the subsystem's property bindings.
153    *
154    * <p>This method should bind all properties that the subsystem
155    * publishes.  It will be invoked after init, but before any
156    * invocations of update.</p>
157    */
158   virtual void bind ();
159
160
161   /**
162    * Release the subsystem's property bindings.
163    *
164    * <p>This method should release all properties that the subsystem
165    * publishes.  It will be invoked by FlightGear (not the destructor)
166    * just before the subsystem is removed.</p>
167    */
168   virtual void unbind ();
169
170
171   /**
172    * Update the subsystem.
173    *
174    * <p>FlightGear invokes this method every time the subsystem should
175    * update its state.</p>
176    *
177    * @param delta_time_sec The delta time, in seconds, since the last
178    * update.  On first update, delta time will be 0.
179    */
180   virtual void update (double delta_time_sec) = 0;
181
182
183   /**
184    * Suspend operation of this subsystem.
185    *
186    * <p>This method instructs the subsystem to suspend
187    * sim-time-dependent operations until asked to resume.  The update
188    * method will still be invoked so that the subsystem can take any
189    * non-time-dependent actions, such as updating the display.</p>
190    *
191    * <p>It is not an error for the suspend method to be invoked when
192    * the subsystem is already suspended; the invocation should simply
193    * be ignored.</p>
194    */
195   virtual void suspend ();
196
197
198   /**
199    * Suspend or resum operation of this subsystem.
200    *
201    * @param suspended true if the subsystem should be suspended, false
202    * otherwise.
203    */
204   virtual void suspend (bool suspended);
205
206
207   /**
208    * Resume operation of this subsystem.
209    *
210    * <p>This method instructs the subsystem to resume
211    * sim-time-depended operations.  It is not an error for the resume
212    * method to be invoked when the subsystem is not suspended; the
213    * invocation should simply be ignored.</p>
214    */
215   virtual void resume ();
216
217
218   /**
219    * Test whether this subsystem is suspended.
220    *
221    * @return true if the subsystem is suspended, false if it is not.
222    */
223   virtual bool is_suspended () const;
224
225
226 protected:
227
228   mutable SGPropertyNode_ptr _freeze_master_node;
229   bool _suspended;
230
231 };
232
233
234 \f
235 /**
236  * A group of FlightGear subsystems.
237  */
238 class FGSubsystemGroup : public FGSubsystem
239 {
240 public:
241
242     FGSubsystemGroup ();
243     virtual ~FGSubsystemGroup ();
244
245     virtual void init ();
246     virtual void bind ();
247     virtual void unbind ();
248     virtual void update (double delta_time_sec);
249
250     virtual void set_subsystem (const string &name,
251                                 FGSubsystem * subsystem,
252                                 double min_step_sec = 0);
253     virtual FGSubsystem * get_subsystem (const string &name);
254     virtual void remove_subsystem (const string &name);
255     virtual bool has_subsystem (const string &name) const;
256
257 private:
258
259     struct Member {
260
261         Member ();
262         Member (const Member &member);
263         virtual ~Member ();
264
265         virtual void update (double delta_time_sec);
266
267         string name;
268         FGSubsystem * subsystem;
269         double min_step_sec;
270         double elapsed_sec;
271     };
272
273     Member * get_member (const string &name, bool create = false);
274
275     vector<Member *> _members;
276 };
277
278
279 \f
280 /**
281  * Manage subsystems for the application.
282  */
283 class FGSubsystemMgr : public FGSubsystem
284 {
285 public:
286
287     enum GroupType {
288         INIT = 0,
289         GENERAL,
290         MAX_GROUPS
291     };
292
293     FGSubsystemMgr ();
294     virtual ~FGSubsystemMgr ();
295
296     virtual void init ();
297     virtual void bind ();
298     virtual void unbind ();
299     virtual void update (double delta_time_sec);
300
301     virtual void add (GroupType group, const string &name,
302                       FGSubsystem * subsystem,
303                       double min_time_sec = 0);
304
305     virtual FGSubsystemGroup * get_group (GroupType group);
306
307 private:
308
309     FGSubsystemGroup _groups[MAX_GROUPS];
310
311 };
312
313
314
315 #endif // __FGFS_HXX
316
317 // end of fgfs.hxx