1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2013 Jan BOON (Kaetemi) <jan.boon@kaetemi.be>
7 // This program is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Affero General Public License as
9 // published by the Free Software Foundation, either version 3 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU Affero General Public License for more details.
17 // You should have received a copy of the GNU Affero General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <nel/misc/types_nl.h>
21 #include "game_time.h"
26 // #include <nel/misc/debug.h>
27 #include <nel/misc/value_smoother.h>
28 #include <nel/misc/config_file.h>
29 #include <nel/misc/time_nl.h>
32 #include "snowballs_client.h"
33 #include "configuration.h"
36 using namespace NLMISC
;
41 // impl note: at some point animation time can be linked to server as well
42 // see version of time.cpp in snowballs5 experimental code
44 static bool _SkipAnimationOnce
;
45 static NLMISC::TTime _TimeMs
;
46 static CValueSmootherTemplate
<float> _FpsSmoother
;
51 NLMISC::TLocalTime a_LocalTimeDelta
;
52 NL3D::TGlobalAnimationTime a_AnimationTimeDelta
;
54 } /* anonymous namespace */
56 static void cbFpsSmoothing(CConfigFile::CVar
&var
)
58 _FpsSmoother
.init((uint
)var
.asInt());
61 void CGameTime::init()
63 _SkipAnimationOnce
= true; // since we're loading ... ;)
64 _TimeMs
= NLMISC::CTime::getLocalTime();
65 LocalTime
= ((TLocalTime
)_TimeMs
) / 1000.0;
67 a_LocalTimeDelta
= 0.0;
69 AnimationTimeDelta
= 0.f
;
70 a_AnimationTimeDelta
= 0.0;
71 FramesPerSecond
= 0.f
;
72 FramesPerSecondSmooth
= 0.f
;
73 CConfiguration::setAndCallback("FpsSmoothing", cbFpsSmoothing
);
76 void CGameTime::release()
78 // could also call init again here just for fun but nothing useful otherwise
79 CConfiguration::dropCallback("FpsSmoothing");
83 void CGameTime::updateTime()
85 TTime timems
= NLMISC::CTime::getLocalTime();
86 TTime deltams
= timems
- _TimeMs
;
89 if (!deltams
) // time has not moved
91 // average of previous fps and this fps should be ok
94 a_LocalTimeDelta
= 0.f
;
95 a_AnimationTimeDelta
= 0.f
;
99 FramesPerSecond
= 1000.0f
/ (float)deltams
;
101 TLocalTime localTime
= ((TLocalTime
)timems
) / 1000.0;
102 a_LocalTimeDelta
= localTime
- LocalTime
;
104 if (_SkipAnimationOnce
)
106 a_AnimationTimeDelta
= 0.f
;
107 _SkipAnimationOnce
= false;
111 a_AnimationTimeDelta
= (TGlobalAnimationTime
)a_LocalTimeDelta
;
115 _FpsSmoother
.addValue(FramesPerSecond
);
116 FramesPerSecondSmooth
= _FpsSmoother
.getSmoothValue();
119 void CGameTime::advanceTime(double f
)
121 LocalTimeDelta
= a_LocalTimeDelta
* f
;
122 LocalTime
+= LocalTimeDelta
;
123 TGlobalAnimationTime atd
= a_AnimationTimeDelta
* f
;
124 AnimationTimeDelta
= (NL3D::TAnimationTime
)atd
;
125 AnimationTime
+= atd
;
128 void CGameTime::skipAnimationOnce()
130 _SkipAnimationOnce
= true;
133 } /* namespace SBCLIENT */