From e1dd52d38aa91e17060e642c6d090556bc2d4d46 Mon Sep 17 00:00:00 2001 From: curt Date: Fri, 19 Jan 2001 22:57:24 +0000 Subject: [PATCH] The FlightGear patches add new saveInitialState and restoreInitialState methods to FGGlobals, as well as the two-stage commit described above for loading saved files. fgInit now takes a snapshot of the initial state before handing off to the main loop, and the GUI reInit function restores that state explicitly before calling fgReInit. The FlightGear patches also modify fg_props.hxx to add optional useDefault arguments to all of the fgTie functions -- that lets you choose whether you want to pick up any default value in the property tree when you tie the property (the default is true). --- src/GUI/gui_local.cxx | 2 ++ src/Main/fg_init.cxx | 6 +++++- src/Main/fg_props.cxx | 11 +++++++++- src/Main/fg_props.hxx | 47 ++++++++++++++++++++++++++----------------- src/Main/globals.cxx | 46 +++++++++++++++++++++++++++++++++--------- src/Main/globals.hxx | 15 +++++++++++++- 6 files changed, 97 insertions(+), 30 deletions(-) diff --git a/src/GUI/gui_local.cxx b/src/GUI/gui_local.cxx index 398a47ac8..a68e3063e 100644 --- a/src/GUI/gui_local.cxx +++ b/src/GUI/gui_local.cxx @@ -2,6 +2,7 @@ #include // plib include +#include
#include
#include "gui.h" @@ -38,6 +39,7 @@ void reInit(puObject *cb) BusyCursor(0); Quat0(); build_rotmatrix(GuiQuat_mat, curGuiQuat); + /* check */ globals->restoreInitialState(); fgReInitSubsystems(); BusyCursor(1); } diff --git a/src/Main/fg_init.cxx b/src/Main/fg_init.cxx index 3581a5818..0c506726f 100644 --- a/src/Main/fg_init.cxx +++ b/src/Main/fg_init.cxx @@ -778,6 +778,10 @@ bool fgInitSubsystems( void ) { FG_LOG( FG_GENERAL, FG_INFO, endl); + // Save the initial state for future + // reference. + globals->saveInitialState(); + return true; } @@ -867,7 +871,7 @@ void fgReInitSubsystems( void ) << globals->get_current_view()->get_abs_view_pos()); cur_fdm_state->init(); - cur_fdm_state->bind(); +// cur_fdm_state->bind(); // cur_fdm_state->init( 1.0 / fgGetInt("/sim/model-hz") ); scenery.cur_elev = cur_fdm_state->get_Runway_altitude() * FEET_TO_METER; diff --git a/src/Main/fg_props.cxx b/src/Main/fg_props.cxx index 46bc65838..30c337e9a 100644 --- a/src/Main/fg_props.cxx +++ b/src/Main/fg_props.cxx @@ -45,7 +45,16 @@ fgSaveFlight (ostream &output) bool fgLoadFlight (istream &input) { - return readProperties(input, globals->get_props()); + SGPropertyNode props; + if (readProperties(input, &props)) { + copyProperties(&props, globals->get_props()); + // When loading a flight, make it the + // new initial state. + globals->saveInitialState(); + } else { + FG_LOG(FG_INPUT, FG_ALERT, "Error restoring flight; aborted"); + return false; + } } // end of fg_props.cxx diff --git a/src/Main/fg_props.hxx b/src/Main/fg_props.hxx index 8dd28cea4..be9e68788 100644 --- a/src/Main/fg_props.hxx +++ b/src/Main/fg_props.hxx @@ -101,50 +101,57 @@ fgUntie (const string &name) // Templates cause ambiguity here inline void -fgTie (const string &name, bool *pointer) +fgTie (const string &name, bool *pointer, bool useDefault = true) { - if (!globals->get_props()->tie(name, SGRawValuePointer(pointer))) + if (!globals->get_props()->tie(name, SGRawValuePointer(pointer), + useDefault)) FG_LOG(FG_GENERAL, FG_WARN, "Failed to tie property " << name << " to a pointer"); } inline void -fgTie (const string &name, int *pointer) +fgTie (const string &name, int *pointer, bool useDefault = true) { - if (!globals->get_props()->tie(name, SGRawValuePointer(pointer))) + if (!globals->get_props()->tie(name, SGRawValuePointer(pointer), + useDefault)) FG_LOG(FG_GENERAL, FG_WARN, "Failed to tie property " << name << " to a pointer"); } inline void -fgTie (const string &name, float *pointer) +fgTie (const string &name, float *pointer, bool useDefault = true) { - if (!globals->get_props()->tie(name, SGRawValuePointer(pointer))) + if (!globals->get_props()->tie(name, SGRawValuePointer(pointer), + useDefault)) FG_LOG(FG_GENERAL, FG_WARN, "Failed to tie property " << name << " to a pointer"); } inline void -fgTie (const string &name, double *pointer) +fgTie (const string &name, double *pointer, bool useDefault = true) { - if (!globals->get_props()->tie(name, SGRawValuePointer(pointer))) + if (!globals->get_props()->tie(name, SGRawValuePointer(pointer), + useDefault)) FG_LOG(FG_GENERAL, FG_WARN, "Failed to tie property " << name << " to a pointer"); } inline void -fgTie (const string &name, string *pointer) +fgTie (const string &name, string *pointer, bool useDefault = true) { - if (!globals->get_props()->tie(name, SGRawValuePointer(pointer))) + if (!globals->get_props()->tie(name, SGRawValuePointer(pointer), + useDefault)) FG_LOG(FG_GENERAL, FG_WARN, "Failed to tie property " << name << " to a pointer"); } template inline void -fgTie (const string &name, V (*getter)(), void (*setter)(V) = 0) +fgTie (const string &name, V (*getter)(), void (*setter)(V) = 0, + bool useDefault = true) { - if (!globals->get_props()->tie(name, SGRawValueFunctions(getter, setter))) + if (!globals->get_props()->tie(name, SGRawValueFunctions(getter, setter), + useDefault)) FG_LOG(FG_GENERAL, FG_WARN, "Failed to tie property " << name << " to functions"); } @@ -152,12 +159,13 @@ fgTie (const string &name, V (*getter)(), void (*setter)(V) = 0) template inline void fgTie (const string &name, int index, V (*getter)(int), - void (*setter)(int, V) = 0) + void (*setter)(int, V) = 0, bool useDefault = true) { if (!globals->get_props()->tie(name, SGRawValueFunctionsIndexed(index, getter, - setter))) + setter), + useDefault)) FG_LOG(FG_GENERAL, FG_WARN, "Failed to tie property " << name << " to indexed functions"); } @@ -165,10 +173,11 @@ fgTie (const string &name, int index, V (*getter)(int), template inline void fgTie (const string &name, T * obj, V (T::*getter)() const, - void (T::*setter)(V) = 0) + void (T::*setter)(V) = 0, bool useDefault = true) { if (!globals->get_props()->tie(name, - SGRawValueMethods(*obj, getter, setter))) + SGRawValueMethods(*obj, getter, setter), + useDefault)) FG_LOG(FG_GENERAL, FG_WARN, "Failed to tie property " << name << " to object methods"); } @@ -176,13 +185,15 @@ fgTie (const string &name, T * obj, V (T::*getter)() const, template inline void fgTie (const string &name, T * obj, int index, - V (T::*getter)(int) const, void (T::*setter)(int, V) = 0) + V (T::*getter)(int) const, void (T::*setter)(int, V) = 0, + bool useDefault = true) { if (!globals->get_props()->tie(name, SGRawValueMethodsIndexed(*obj, index, getter, - setter))) + setter), + useDefault)) FG_LOG(FG_GENERAL, FG_WARN, "Failed to tie property " << name << " to indexed object methods"); } diff --git a/src/Main/globals.cxx b/src/Main/globals.cxx index ab752033b..312dc0b81 100644 --- a/src/Main/globals.cxx +++ b/src/Main/globals.cxx @@ -25,6 +25,11 @@ #include "fg_props.hxx" + +//////////////////////////////////////////////////////////////////////// +// Implementation of FGGlobals. +//////////////////////////////////////////////////////////////////////// + // global global :-) FGGlobals *globals; @@ -33,21 +38,44 @@ FGGlobals *globals; FGGlobals::FGGlobals() : freeze( false ), warp( 0 ), - warp_delta( 0 ) + warp_delta( 0 ), + props(0), + initial_state(0) { - // TODO: move to a proper bind method -// fgTie("/sim/freeze", &freeze); -// fgTie("/sim/warp", &warp); -// fgTie("/sim/warp-delta", &warp_delta); } // Destructor FGGlobals::~FGGlobals() { - // TODO: move to a proper unbind method -// fgUntie("/sim/freeze"); -// fgUntie("/sim/warp"); -// fgUntie("/sim/warp-delta"); + delete initial_state; +} + + +// Save the current state as the initial state. +void +FGGlobals::saveInitialState () +{ + delete initial_state; + initial_state = new SGPropertyNode(); + if (!copyProperties(props, initial_state)) + FG_LOG(FG_GENERAL, FG_ALERT, "Error saving initial state"); } + +// Restore the saved initial state, if any +void +FGGlobals::restoreInitialState () +{ + if (initial_state == 0) { + FG_LOG(FG_GENERAL, FG_ALERT, "No initial state available to restore!!!"); + } else if (!copyProperties(initial_state, props)) { + FG_LOG(FG_GENERAL, FG_INFO, + "Some errors restoring initial state (probably just read-only props)"); + } else { + FG_LOG(FG_GENERAL, FG_INFO, "Initial state restored successfully"); + } +} + + +// end of globals.cxx diff --git a/src/Main/globals.hxx b/src/Main/globals.hxx index e092aec89..72d3433ef 100644 --- a/src/Main/globals.hxx +++ b/src/Main/globals.hxx @@ -83,6 +83,7 @@ private: // properties SGPropertyNode *props; + SGPropertyNode *initial_state; // list of serial port-like configurations string_list channel_options_list; @@ -134,7 +135,19 @@ public: inline string_list get_channel_options_list () { return channel_options_list; } - + + + /** + * Save the current state as the initial state. + */ + void saveInitialState (); + + + /** + * Restore the saved initial state, if any. + */ + void restoreInitialState (); + }; -- 2.39.5