2 * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
7 #include "TimeComputer.h"
10 TimeComputer::TimeComputer()
17 fPerformanceTimeBase(0),
27 TimeComputer::Init(float frameRate
, bigtime_t realBaseTime
)
29 fRealTime
= realBaseTime
;
32 SetFrameRate(frameRate
);
37 TimeComputer::SetFrameRate(float frameRate
)
39 if (frameRate
== fFrameRate
)
42 fFrameRate
= frameRate
;
43 fUsecsPerFrame
= (double)1000000 / fFrameRate
;
44 fResetTimeBase
= true;
51 TimeComputer::AddTimeStamp(bigtime_t realTime
, uint64 frames
)
53 bigtime_t estimatedPerformanceTime
= fPerformanceTime
54 + bigtime_t((realTime
- fRealTime
) * fDrift
);
59 // use the extrapolated performance time at the given real time
60 fPerformanceTime
= estimatedPerformanceTime
;
61 fPerformanceTimeBase
= estimatedPerformanceTime
;
63 fResetTimeBase
= false;
64 _AddEntry(fRealTime
, fPerformanceTime
);
69 bigtime_t performanceTime
= fPerformanceTimeBase
70 + bigtime_t((frames
- fFrameBase
) * fUsecsPerFrame
);
71 _AddEntry(realTime
, performanceTime
);
73 // Update performance time and drift. We don't use the given
74 // performance time directly, but average it with the estimated
76 fPerformanceTime
= (performanceTime
+ estimatedPerformanceTime
) / 2;
78 Entry
& entry
= fEntries
[fFirstEntry
];
79 fDrift
= double(fPerformanceTime
- entry
.performanceTime
)
80 / double(fRealTime
- entry
.realTime
);
85 TimeComputer::_AddEntry(bigtime_t realTime
, bigtime_t performanceTime
)
87 fLastEntry
= (fLastEntry
+ 1) % kEntryCount
;
88 Entry
& entry
= fEntries
[fLastEntry
];
89 entry
.realTime
= realTime
;
90 entry
.performanceTime
= performanceTime
;
92 if (fLastEntry
== fFirstEntry
)
93 fFirstEntry
= (fFirstEntry
+ 1) % kEntryCount
;