]> git.mxchange.org Git - flightgear.git/blobdiff - src/Main/fgfs.hxx
Moved some of the low level scene graph construction code over to simgear.
[flightgear.git] / src / Main / fgfs.hxx
index 79dec566d0998d25efd2230296954069503c196a..bc435893121212964b0742d56877fe6b2de7818d 100644 (file)
 #  include <config.h>
 #endif
 
-#ifdef SG_MATH_EXCEPTION_CLASH
-#  include <math.h>
-#endif
+#include <simgear/compiler.h>
+
+// #ifdef SG_MATH_EXCEPTION_CLASH
+// #  include <math.h>
+// #endif
 
 #ifdef HAVE_WINDOWS_H
 #  include <windows.h>                     
 #  include <float.h>                    
 #endif
 
+#include STL_STRING
+SG_USING_STD(string);
+
+#include <vector>
+SG_USING_STD(vector);
+
+#include <map>
+SG_USING_STD(map);
+
+#include <simgear/props/props.hxx>
+
 
 \f
 /**
  * <pre>
  * void MySubsystem::bind ()
  * {
- *   fgTie("/controls/elevator", &_elevator);
- *   fgSetArchivable("/controls/elevator");
+ *   fgTie("/controls/flight/elevator", &_elevator);
+ *   fgSetArchivable("/controls/flight/elevator");
  * }
  *
  * void MySubsystem::unbind ()
  * {
- *   fgUntie("/controls/elevator");
+ *   fgUntie("/controls/flight/elevator");
  * }
  * </pre>
  *
  *   _errorNode = fgGetNode("/display/error-margin-pct", true);
  * }
  *
- * void MySubsystem::update ()
+ * void MySubsystem::update (double delta_time_sec)
  * {
  *   do_something(_errorNode.getFloatValue());
  * }
  * <p>The node returned will always be a pointer to SGPropertyNode,
  * and the subsystem should <em>not</em> delete it in its destructor
  * (the pointer belongs to the property tree, not the subsystem).</p>
+ *
+ * <p>The program may ask the subsystem to suspend or resume
+ * sim-time-dependent operations; by default, the suspend() and
+ * resume() methods set the protected variable <var>_suspended</var>,
+ * which the subsystem can reference in its update() method, but
+ * subsystems may also override the suspend() and resume() methods to
+ * take different actions.</p>
  */
 class FGSubsystem
 {
 public:
 
+  /**
+   * Default constructor.
+   */
+  FGSubsystem ();
+
   /**
    * Virtual destructor to ensure that subclass destructors are called.
    */
@@ -120,7 +145,16 @@ public:
    * in the constructor, so that FlightGear can control the
    * initialization order.</p>
    */
-  virtual void init () = 0;
+  virtual void init ();
+
+
+  /**
+   * Reinitialize the subsystem.
+   *
+   * <p>This method should cause the subsystem to reinitialize itself,
+   * and (normally) to reload any configuration files.</p>
+   */
+  virtual void reinit ();
 
 
   /**
@@ -130,7 +164,7 @@ public:
    * publishes.  It will be invoked after init, but before any
    * invocations of update.</p>
    */
-  virtual void bind () = 0;
+  virtual void bind ();
 
 
   /**
@@ -140,21 +174,182 @@ public:
    * publishes.  It will be invoked by FlightGear (not the destructor)
    * just before the subsystem is removed.</p>
    */
-  virtual void unbind () = 0;
+  virtual void unbind ();
 
 
   /**
    * Update the subsystem.
    *
    * <p>FlightGear invokes this method every time the subsystem should
-   * update its state.  If the subsystem requires delta time information,
-   * it should track it itself.</p>
+   * update its state.</p>
+   *
+   * @param delta_time_sec The delta time, in seconds, since the last
+   * update.  On first update, delta time will be 0.
+   */
+  virtual void update (double delta_time_sec) = 0;
+
+
+  /**
+   * Suspend operation of this subsystem.
+   *
+   * <p>This method instructs the subsystem to suspend
+   * sim-time-dependent operations until asked to resume.  The update
+   * method will still be invoked so that the subsystem can take any
+   * non-time-dependent actions, such as updating the display.</p>
+   *
+   * <p>It is not an error for the suspend method to be invoked when
+   * the subsystem is already suspended; the invocation should simply
+   * be ignored.</p>
+   */
+  virtual void suspend ();
+
+
+  /**
+   * Suspend or resum operation of this subsystem.
+   *
+   * @param suspended true if the subsystem should be suspended, false
+   * otherwise.
+   */
+  virtual void suspend (bool suspended);
+
+
+  /**
+   * Resume operation of this subsystem.
+   *
+   * <p>This method instructs the subsystem to resume
+   * sim-time-depended operations.  It is not an error for the resume
+   * method to be invoked when the subsystem is not suspended; the
+   * invocation should simply be ignored.</p>
+   */
+  virtual void resume ();
+
+
+  /**
+   * Test whether this subsystem is suspended.
+   *
+   * @return true if the subsystem is suspended, false if it is not.
    */
-  virtual void update () = 0;
+  virtual bool is_suspended () const;
+
+
+protected:
+
+  mutable SGPropertyNode_ptr _freeze_master_node;
+  bool _suspended;
 
 };
 
 
+\f
+/**
+ * A group of FlightGear subsystems.
+ */
+class FGSubsystemGroup : public FGSubsystem
+{
+public:
+
+    FGSubsystemGroup ();
+    virtual ~FGSubsystemGroup ();
+
+    virtual void init ();
+    virtual void reinit ();
+    virtual void bind ();
+    virtual void unbind ();
+    virtual void update (double delta_time_sec);
+    virtual void suspend ();
+    virtual void resume ();
+    virtual bool is_suspended () const;
+
+    virtual void set_subsystem (const string &name,
+                                FGSubsystem * subsystem,
+                                double min_step_sec = 0);
+    virtual FGSubsystem * get_subsystem (const string &name);
+    virtual void remove_subsystem (const string &name);
+    virtual bool has_subsystem (const string &name) const;
+
+private:
+
+    struct Member {
+
+        Member ();
+        Member (const Member &member);
+        virtual ~Member ();
+
+        virtual void update (double delta_time_sec);
+
+        string name;
+        FGSubsystem * subsystem;
+        double min_step_sec;
+        double elapsed_sec;
+    };
+
+    Member * get_member (const string &name, bool create = false);
+
+    vector<Member *> _members;
+};
+
+
+\f
+/**
+ * Manage subsystems for FlightGear.
+ *
+ * This top-level subsystem will eventually manage all of the
+ * subsystems in FlightGear: it broadcasts its life-cycle events
+ * (init, bind, etc.) to all of the subsystems it manages.  Subsystems
+ * are grouped to guarantee order of initialization and execution --
+ * currently, the only two groups are INIT and GENERAL, but others
+ * will appear in the future.
+ *
+ * All subsystems are named as well as grouped, and subsystems can be
+ * looked up by name and cast to the appropriate subtype when another
+ * subsystem needs to invoke specialized methods.
+ *
+ * The subsystem manager owns the pointers to all the subsystems in
+ * it.
+ */
+class FGSubsystemMgr : public FGSubsystem
+{
+public:
+
+    /**
+     * Types of subsystem groups.
+     */
+    enum GroupType {
+        INIT = 0,
+        GENERAL,
+        MAX_GROUPS
+    };
+
+    FGSubsystemMgr ();
+    virtual ~FGSubsystemMgr ();
+
+    virtual void init ();
+    virtual void reinit ();
+    virtual void bind ();
+    virtual void unbind ();
+    virtual void update (double delta_time_sec);
+    virtual void suspend ();
+    virtual void resume ();
+    virtual bool is_suspended () const;
+
+    virtual void add (const char * name,
+                      FGSubsystem * subsystem,
+                      GroupType group = GENERAL, 
+                      double min_time_sec = 0);
+
+    virtual FGSubsystemGroup * get_group (GroupType group);
+
+    virtual FGSubsystem * get_subsystem(const string &name);
+
+private:
+
+    FGSubsystemGroup _groups[MAX_GROUPS];
+    map<string,FGSubsystem *> _subsystem_map;
+
+};
+
+
+
 #endif // __FGFS_HXX
 
 // end of fgfs.hxx