openscenegraph
Timer
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
19namespace osg {
20
21#if defined(_MSC_VER)
22 typedef unsigned __int64 Timer_t;
23#else
24 typedef unsigned long long Timer_t;
25#endif
26
29
30 public:
31
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 (t2>t1) ? (double)(t2 - t1)*_secsPerTick : -(double)(t1 - t2)*_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
77};
78
81{
82 public:
83 inline ElapsedTime(double* elapsedTime, osg::Timer* timer = 0):
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 {
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;
142
144 }
145
146 double* _time;
149
150};
151
152}
153#endif
Definition Timer:81
~ElapsedTime()
Definition Timer:95
void finish()
Definition Timer:129
void init(osg::Timer *timer)
Definition Timer:138
void reset()
Definition Timer:100
Timer * _timer
Definition Timer:147
double elapsedTime_u() const
Definition Timer:118
double elapsedTime() const
Definition Timer:106
double * _time
Definition Timer:146
Timer_t _startTick
Definition Timer:148
ElapsedTime(osg::Timer *timer=0)
Definition Timer:89
ElapsedTime(double *elapsedTime, osg::Timer *timer=0)
Definition Timer:83
double elapsedTime_n() const
Definition Timer:124
double elapsedTime_m() const
Definition Timer:112
Definition Timer:28
~Timer()
Definition Timer:33
double _secsPerTick
Definition Timer:76
double time_u() const
Definition Timer:53
double getSecondsPerTick() const
Definition Timer:71
double delta_n(Timer_t t1, Timer_t t2) const
Definition Timer:68
double time_s() const
Definition Timer:47
Timer_t tick() const
double delta_u(Timer_t t1, Timer_t t2) const
Definition Timer:65
void setStartTick()
Definition Timer:41
void setStartTick(Timer_t t)
Definition Timer:42
static Timer * instance()
double time_m() const
Definition Timer:50
Timer_t getStartTick() const
Definition Timer:43
Timer_t _startTick
Definition Timer:75
double delta_s(Timer_t t1, Timer_t t2) const
Definition Timer:59
double time_n() const
Definition Timer:56
double delta_m(Timer_t t1, Timer_t t2) const
Definition Timer:62
author: Julien Valentin 2017 (mp3butcher@hotmail.com)
Definition AlphaFunc:19
unsigned long long Timer_t
Definition Timer:24
#define OSG_EXPORT
Definition osg/Export:39