OSG  3.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Timer.h
Go to the documentation of this file.
1 /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2  *
3  * This library is open source and may be redistributed and/or modified under
4  * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5  * (at your option) any later version. The full license is in LICENSE file
6  * included with this distribution, and on the openscenegraph.org website.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * OpenSceneGraph Public License for more details.
12 */
13 
14 #ifndef OSG_TIMER
15 #define OSG_TIMER 1
16 
17 #include <osg/Export>
18 
19 namespace osg {
20 
21 #if defined(_MSC_VER)
22  typedef __int64 Timer_t;
23 #else
24  typedef unsigned long long Timer_t;
25 #endif
26 
29 
30  public:
31 
32  Timer();
33  ~Timer() {}
34 
35  static Timer* instance();
36 
38  Timer_t tick() const;
39 
41  void setStartTick() { _startTick = tick(); }
42  void setStartTick(Timer_t t) { _startTick = t; }
43  Timer_t getStartTick() const { return _startTick; }
44 
45 
47  inline double time_s() const { return delta_s(_startTick, tick()); }
48 
50  inline double time_m() const { return delta_m(_startTick, tick()); }
51 
53  inline double time_u() const { return delta_u(_startTick, tick()); }
54 
56  inline double time_n() const { return delta_n(_startTick, tick()); }
57 
59  inline double delta_s( Timer_t t1, Timer_t t2 ) const { return (double)(t2 - t1)*_secsPerTick; }
60 
62  inline double delta_m( Timer_t t1, Timer_t t2 ) const { return delta_s(t1,t2)*1e3; }
63 
65  inline double delta_u( Timer_t t1, Timer_t t2 ) const { return delta_s(t1,t2)*1e6; }
66 
68  inline double delta_n( Timer_t t1, Timer_t t2 ) const { return delta_s(t1,t2)*1e9; }
69 
71  inline double getSecondsPerTick() const { return _secsPerTick; }
72 
73  protected :
74 
75  Timer_t _startTick;
76  double _secsPerTick;
77 };
78 
81 {
82  public:
83  inline ElapsedTime(double* elapsedTime, osg::Timer* timer = 0):
84  _time(elapsedTime)
85  {
86  init(timer);
87  }
88 
89  inline ElapsedTime(osg::Timer* timer = 0):
90  _time(0)
91  {
92  init(timer);
93  }
94 
95  inline ~ElapsedTime()
96  {
97  finish();
98  }
99 
100  inline void reset()
101  {
102  _startTick = _timer->tick();
103  }
104 
106  inline double elapsedTime() const
107  {
108  return _timer->delta_s(_startTick, _timer->tick());
109  }
110 
112  inline double elapsedTime_m() const
113  {
114  return _timer->delta_m(_startTick, _timer->tick());
115  }
116 
118  inline double elapsedTime_u() const
119  {
120  return _timer->delta_u(_startTick, _timer->tick());
121  }
122 
124  inline double elapsedTime_n() const
125  {
126  return _timer->delta_n(_startTick, _timer->tick());
127  }
128 
129  inline void finish()
130  {
131  Timer_t endTick = _timer->tick();
132  if (_time) *_time += _timer->delta_s(_startTick, endTick);
133  _startTick = endTick;
134  }
135 
136  protected:
137 
138  inline void init(osg::Timer* timer)
139  {
140  if (timer) _timer = timer;
141  else _timer = osg::Timer::instance();
142 
143  _startTick = _timer->tick();
144  }
145 
146  double* _time;
148  Timer_t _startTick;
149 
150 };
151 
152 }
153 #endif
ElapsedTime(double *elapsedTime, osg::Timer *timer=0)
Definition: Timer.h:83
Timer * _timer
Definition: Timer.h:147
#define OSG_EXPORT
Definition: Export.h:43
Timer_t _startTick
Definition: Timer.h:75
double elapsedTime_n() const
Definition: Timer.h:124
Timer_t _startTick
Definition: Timer.h:148
double delta_s(Timer_t t1, Timer_t t2) const
Definition: Timer.h:59
void setStartTick(Timer_t t)
Definition: Timer.h:42
double _secsPerTick
Definition: Timer.h:76
double * _time
Definition: Timer.h:146
unsigned long long Timer_t
Definition: Timer.h:24
void setStartTick()
Definition: Timer.h:41
double elapsedTime_m() const
Definition: Timer.h:112
double delta_m(Timer_t t1, Timer_t t2) const
Definition: Timer.h:62
static Timer * instance()
double time_n() const
Definition: Timer.h:56
~Timer()
Definition: Timer.h:33
double delta_n(Timer_t t1, Timer_t t2) const
Definition: Timer.h:68
Timer_t tick() const
double time_u() const
Definition: Timer.h:53
double time_s() const
Definition: Timer.h:47
void init(osg::Timer *timer)
Definition: Timer.h:138
double elapsedTime() const
Definition: Timer.h:106
Definition: AlphaFunc.h:19
Timer_t getStartTick() const
Definition: Timer.h:43
void reset()
Definition: Timer.h:100
double getSecondsPerTick() const
Definition: Timer.h:71
double elapsedTime_u() const
Definition: Timer.h:118
ElapsedTime(osg::Timer *timer=0)
Definition: Timer.h:89
double time_m() const
Definition: Timer.h:50
void finish()
Definition: Timer.h:129
double delta_u(Timer_t t1, Timer_t t2) const
Definition: Timer.h:65