1 /*
2 
3 *************************************************************************
4 
5 ArmageTron -- Just another Tron Lightcycle Game in 3D.
6 Copyright (C) 2000  Manuel Moos (manuel@moosnet.de)
7 
8 **************************************************************************
9 
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License
12 as published by the Free Software Foundation; either version 2
13 of the License, or (at your option) any later version.
14 
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 GNU General Public License for more details.
19 
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23 
24 ***************************************************************************
25 
26 */
27 
28 #ifndef ArmageTron_TIMER_H
29 #define ArmageTron_TIMER_H
30 
31 //#include <time>
32 #include "tSysTime.h"
33 #include "nNetObject.h"
34 
35 class eTimer:public nNetObject{
36 public:
37     REAL speed; // the time acceleration
38 
39     eTimer();
40     eTimer(nMessage &m);
41     virtual ~eTimer();
42     virtual void WriteSync(nMessage &m);
43     virtual void ReadSync(nMessage &m);
44     virtual nDescriptor &CreatorDescriptor() const;
45 
46     REAL Time();
TimeNoSync()47     REAL TimeNoSync(){return REAL(Time()+(tSysTimeFloat()-lastTime_)*speed);}
48 
49     void pause(bool p);
50 
51     void SyncTime();
52     void Reset(REAL t=0);
53 
AverageFPS()54     REAL AverageFPS(){return 1/(averageSpf_.GetAverage()+EPS);}
AverageFrameTime()55     REAL AverageFrameTime(){return averageSpf_.GetAverage();}
FrameTime()56     REAL FrameTime(){return spf_;}
57 
58     bool IsSynced() const; //!< returns whether the timer is synced sufficiently well to allow rendering
59 
60 private:
61     mutable double creationSystemTime_; //!< the rough system time this timer was created at
62     double smoothedSystemTime_;         //!< the smoothed system time
63     double startTime_;                  //!< when was the last game started?
64     nAverager startTimeOffset_;         //!< the smoothed average of this averager is added to the start time on the client
65     nAverager  startTimeDrift_;         //!< drift of effective start time
66     REAL startTimeSmoothedOffset_;      //!< the smoothed average of startTimeOffset_
67     nAverager qualityTester_;           //!< averager that tells us about the quality of the sync messages
68 
69     REAL lastStartTime_;                //!< last received start time
70     REAL lastRemoteTime_;               //!< last received time
71 
72     // the current game time is always smoothedSystemTime_ - ( startTime_ + startTimeSmoothedOffset_ ).
73 
74     REAL spf_;               //!< last frame time
75     nAverager averageSpf_;   //!< averager over seconds per frame
76 
77     double lastTime_;        //!< the smoothed system time of the last update
78     double nextSync_;        //!< system time of the next sync to the clients
79 };
80 
81 REAL se_GameTime();
82 REAL se_GameTimeNoSync();
83 void se_SyncGameTimer();
84 
85 void se_ResetGameTimer(REAL t=0);
86 void se_MakeGameTimer();
87 void se_KillGameTimer();
88 void se_PauseGameTimer(bool p);
89 
90 REAL se_PredictTime();
91 REAL se_AverageFrameTime();
92 REAL se_AverageFPS();
93 
94 extern eTimer *se_mainGameTimer;
95 #endif
96 
97 
98