| 1 | // TimeManager.hxx -- simulation-wide time management |
| 2 | // |
| 3 | // Written by James Turner, started July 2010. |
| 4 | // |
| 5 | // Copyright (C) 2010 Curtis L. Olson - http://www.flightgear.org/~curt |
| 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 20 | |
| 21 | #ifndef FG_TIME_TIMEMANAGER_HXX |
| 22 | #define FG_TIME_TIMEMANAGER_HXX |
| 23 | |
| 24 | #include <simgear/props/props.hxx> |
| 25 | #include <simgear/structure/subsystem_mgr.hxx> |
| 26 | #include <simgear/math/SGVec3.hxx> |
| 27 | |
| 28 | // forward decls |
| 29 | class SGTime; |
| 30 | |
| 31 | class TimeManager : public SGSubsystem, |
| 32 | public SGPropertyChangeListener |
| 33 | { |
| 34 | public: |
| 35 | TimeManager(); |
| 36 | virtual ~TimeManager(); |
| 37 | |
| 38 | // Subsystem API. |
| 39 | void init() override; |
| 40 | void postinit() override; |
| 41 | void reinit() override; |
| 42 | void shutdown() override; |
| 43 | void unbind() override; |
| 44 | void update(double dt) override; |
| 45 | |
| 46 | void reposition(); |
| 47 | |
| 48 | // Subsystem identification. |
| 49 | static const char* staticSubsystemClassId() { return "time"; } |
| 50 | |
| 51 | void computeTimeDeltas(double& simDt, double& realDt); |
| 52 | |
| 53 | void computeTimeDeltasSimple(double& simDt, double& realDt); |
| 54 | |
| 55 | // SGPropertyChangeListener overrides |
| 56 | void valueChanged(SGPropertyNode *) override; |
| 57 | |
| 58 | void setTimeOffset(const std::string& offset_type, long int offset); |
| 59 | |
| 60 | inline double getMPProtocolClockSec() const { return _mpProtocolClock; } |
| 61 | inline double getSteadyClockSec() const { return _steadyClock; } |
| 62 | |
| 63 | double getSimSpeedUpFactor() const; |
| 64 | |
| 65 | private: |
| 66 | // test class is a friend so we can fake elapsed system time |
| 67 | friend class TimeManagerTests; |
| 68 | |
| 69 | /** |
| 70 | * Ensure a consistent update-rate using a combination of |
| 71 | * sleep()-ing and busy-waiting. |
| 72 | */ |
| 73 | void throttleUpdateRate(); |
| 74 | |
| 75 | /** |
| 76 | * Compute frame (update) rate and write it to a property |
| 77 | */ |
| 78 | void computeFrameRate(); |
| 79 | |
| 80 | void updateLocalTime(); |
| 81 | |
| 82 | void updateLocalTimeString(); |
| 83 | |
| 84 | // set up a time offset (aka warp) if one is specified |
| 85 | void initTimeOffset(); |
| 86 | |
| 87 | bool _inited = false; |
| 88 | SGTime* _impl = nullptr; |
| 89 | SGTimeStamp _lastStamp; |
| 90 | SGTimeStamp _systemStamp; |
| 91 | bool _firstUpdate = true; |
| 92 | double _dtRemainder = 0; |
| 93 | SGPropertyNode_ptr _maxDtPerFrame; |
| 94 | SGPropertyNode_ptr _clockFreeze; |
| 95 | SGPropertyNode_ptr _timeOverride; |
| 96 | SGPropertyNode_ptr _warp; |
| 97 | SGPropertyNode_ptr _warpDelta; |
| 98 | SGPropertyNode_ptr _simTimeFactor; |
| 99 | SGPropertyNode_ptr _mpProtocolClockNode; |
| 100 | SGPropertyNode_ptr _steadyClockNode; |
| 101 | SGPropertyNode_ptr _frameTimeOffsetNode; |
| 102 | SGPropertyNode_ptr _dtRemainderNode; |
| 103 | SGPropertyNode_ptr _mpClockOffset; |
| 104 | SGPropertyNode_ptr _steadyClockDrift; |
| 105 | SGPropertyNode_ptr _computeDrift; |
| 106 | SGPropertyNode_ptr _frameWait; |
| 107 | SGPropertyNode_ptr _maxFrameRate; |
| 108 | SGPropertyNode_ptr _localTimeStringNode; |
| 109 | SGPropertyNode_ptr _localTimeZoneNode; |
| 110 | SGPropertyNode_ptr _frameNumber; |
| 111 | SGPropertyNode_ptr _simFixedDt; |
| 112 | |
| 113 | bool _lastClockFreeze = false; |
| 114 | bool _adjustWarpOnUnfreeze = false; |
| 115 | |
| 116 | // frame-rate / worst-case latency / update-rate counters |
| 117 | SGPropertyNode_ptr _frameRate; |
| 118 | SGPropertyNode_ptr _frameRateWorst; |
| 119 | SGPropertyNode_ptr _frameLatency; |
| 120 | time_t _lastFrameTime = 0; |
| 121 | double _frameLatencyMax = 0; |
| 122 | double _mpProtocolClock = 0; |
| 123 | double _steadyClock = 0; |
| 124 | int _frameCount = 0; |
| 125 | |
| 126 | // we update TZ after moving more than a threshold distance |
| 127 | SGVec3d _lastTimeZoneCheckPosition; |
| 128 | |
| 129 | SGPropertyNode_ptr _sceneryLoaded; |
| 130 | SGPropertyNode_ptr _modelHz; |
| 131 | SGPropertyNode_ptr _timeDelta; |
| 132 | SGPropertyNode_ptr _simTimeDelta; |
| 133 | |
| 134 | bool _simpleTimeEnabledPrev = false; |
| 135 | SGPropertyNode_ptr _simpleTimeEnabled; |
| 136 | SGPropertyNode_ptr _simpleTimeUtc; |
| 137 | SGPropertyNode_ptr _simpleTimeFdm; |
| 138 | double _simple_time_utc = 0; |
| 139 | double _simple_time_fdm = 0; |
| 140 | }; |
| 141 | |
| 142 | #endif // of FG_TIME_TIMEMANAGER_HXX |