TrueReality  v0.1.1912
Timer.h
Go to the documentation of this file.
1 /*
2 * True Reality Open Source Game and Simulation Engine
3 * Copyright © 2021 Acid Rain Studios LLC
4 *
5 * This library is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU Lesser General Public License as published by the Free
7 * Software Foundation; either version 3.0 of the License, or (at your option)
8 * any later version.
9 *
10 * This library is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this library; if not, write to the Free Software Foundation, Inc.,
17 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * @author Maxim Serebrennik
20 */
21 #pragma once
22 
23 #include <trUtil/Export.h>
24 
25 #include <trUtil/PlatformMacros.h>
26 
27 #include <osg/Timer>
28 
34 namespace trUtil
35 {
36 
37 #if defined(TR_WIN)
38 
39  using TimeTicks = unsigned __int64;
40 #else
41 
42  using TimeTicks = unsigned long long;
43 #endif
44 
56  void TR_UTIL_EXPORT AppSleep(unsigned int milliseconds);
57 
66  {
67  public:
68 
74  Timer();
75 
81  ~Timer();
82 
91  static const Timer* Instance();
92 
101  {
102  mOldTicks = mNewTicks;
103  mNewTicks = (mTimer.tick() + mCustomTicks);
104  return mNewTicks;
105  }
106 
112  void SetStartTick() { mTimer.setStartTick(); }
113 
121  void SetStartTick(TimeTicks t);
122 
130  TimeTicks GetStartTick() const { return mTimer.getStartTick(); }
131 
142  double DeltaSec(TimeTicks t1, TimeTicks t2) const { return mTimer.delta_s(t1, t2); }
143 
154  double DeltaMil(TimeTicks t1, TimeTicks t2) const { return mTimer.delta_m(t1, t2); }
155 
166  double DeltaMicro(TimeTicks t1, TimeTicks t2) const { return mTimer.delta_u(t1, t2); }
167 
178  double DeltaNano(TimeTicks t1, TimeTicks t2) const { return mTimer.delta_n(t1, t2); }
179 
187  double ElapsedSeconds() const { return mTimer.time_s(); }
188 
196  double ElapsedMilliseconds() const { return mTimer.time_m(); }
197 
205  double ElapsedMicroseconds() const { return mTimer.time_u(); }
206 
214  double ElapsedNanoseconds() const { return mTimer.time_n(); }
215 
223  double GetSecondsPerCPUTick() const { return mTimer.getSecondsPerTick(); }
224 
232  double GetSecondsPerTick() const { return (double)(mNewTicks - mOldTicks)*GetSecondsPerCPUTick(); }
233 
234  private:
235  osg::Timer mTimer;
236  TimeTicks mOldTicks = 0;
237  TimeTicks mNewTicks = 0;
238  TimeTicks mCustomTicks = 0;
239  };
240 }
TimeTicks GetStartTick() const
Get the value of the start tick.
Definition: Timer.h:130
double ElapsedMicroseconds() const
Get elapsed time in microseconds.
Definition: Timer.h:205
double GetSecondsPerCPUTick() const
Get the number of seconds per CPU tick.
Definition: Timer.h:223
TimeTicks Tick()
Get the timers tick value.
Definition: Timer.h:100
double DeltaNano(TimeTicks t1, TimeTicks t2) const
Get the time in nanoseconds between timer ticks t1 and t2.
Definition: Timer.h:178
double ElapsedSeconds() const
Get elapsed time in seconds.
Definition: Timer.h:187
void TR_UTIL_EXPORT AppSleep(unsigned int milliseconds)
Puts the current application thread to sleep for a given number of milliseconds.
Definition: Timer.cpp:30
double DeltaSec(TimeTicks t1, TimeTicks t2) const
Get the time in seconds between timer ticks t1 and t2.
Definition: Timer.h:142
Timer class is used for measuring elapsed time or time between two points.
Definition: Timer.h:65
double GetSecondsPerTick() const
Get the number of seconds per tick.
Definition: Timer.h:232
osg::Timer mTimer
Definition: Timer.h:235
A class that represents date time utility.
void SetStartTick()
Set the start tick.
Definition: Timer.h:112
Namespace that holds various utility classes for the engine.
Definition: SmrtPtr.h:208
double ElapsedNanoseconds() const
Get elapsed time in nanoseconds.
Definition: Timer.h:214
double ElapsedMilliseconds() const
Get elapsed time in milliseconds.
Definition: Timer.h:196
double DeltaMicro(TimeTicks t1, TimeTicks t2) const
Get the time in microseconds between timer ticks t1 and t2.
Definition: Timer.h:166
double DeltaMil(TimeTicks t1, TimeTicks t2) const
Get the time in milliseconds between timer ticks t1 and t2.
Definition: Timer.h:154
unsigned long long TimeTicks
64bit Integer to hold the Timers time ticks.
Definition: Timer.h:42