Timer
Go to the documentation of this file.
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#ifndef OSG_TIMER
#define OSG_TIMER 1
#include <osg/Export>
namespace osg {
#if defined(_MSC_VER)
typedef unsigned __int64 Timer_t;
#else
typedef unsigned long long Timer_t;
#endif
class OSG_EXPORT Timer {
public:
Timer();
~Timer() {}
static Timer* instance();
Timer_t tick() const;
void setStartTick() { _startTick = tick(); }
void setStartTick(Timer_t t) { _startTick = t; }
Timer_t getStartTick() const { return _startTick; }
inline double time_s() const { return delta_s(_startTick, tick()); }
inline double time_m() const { return delta_m(_startTick, tick()); }
inline double time_u() const { return delta_u(_startTick, tick()); }
inline double time_n() const { return delta_n(_startTick, tick()); }
inline double delta_s( Timer_t t1, Timer_t t2 ) const { return (t2>t1) ? (double)(t2 - t1)*_secsPerTick : -(double)(t1 - t2)*_secsPerTick; }
inline double delta_m( Timer_t t1, Timer_t t2 ) const { return delta_s(t1,t2)*1e3; }
inline double delta_u( Timer_t t1, Timer_t t2 ) const { return delta_s(t1,t2)*1e6; }
inline double delta_n( Timer_t t1, Timer_t t2 ) const { return delta_s(t1,t2)*1e9; }
inline double getSecondsPerTick() const { return _secsPerTick; }
protected :
Timer_t _startTick;
double _secsPerTick;
};
class ElapsedTime
{
public:
inline ElapsedTime(double* elapsedTime, osg::Timer* timer = 0):
_time(elapsedTime)
{
init(timer);
}
inline ElapsedTime(osg::Timer* timer = 0):
_time(0)
{
init(timer);
}
inline ~ElapsedTime()
{
finish();
}
inline void reset()
{
_startTick = _timer->tick();
}
inline double elapsedTime() const
{
return _timer->delta_s(_startTick, _timer->tick());
}
inline double elapsedTime_m() const
{
return _timer->delta_m(_startTick, _timer->tick());
}
inline double elapsedTime_u() const
{
return _timer->delta_u(_startTick, _timer->tick());
}
inline double elapsedTime_n() const
{
return _timer->delta_n(_startTick, _timer->tick());
}
inline void finish()
{
Timer_t endTick = _timer->tick();
if (_time) *_time += _timer->delta_s(_startTick, endTick);
_startTick = endTick;
}
protected:
inline void init(osg::Timer* timer)
{
if (timer) _timer = timer;
else _timer = osg::Timer::instance();
_startTick = _timer->tick();
}
double* _time;
Timer* _timer;
Timer_t _startTick;
};
}
#endif
osg::ElapsedTime::~ElapsedTime
~ElapsedTime()
Definition Timer:95
osg::ElapsedTime::finish
void finish()
Definition Timer:129
osg::ElapsedTime::init
void init(osg::Timer *timer)
Definition Timer:138
osg::ElapsedTime::reset
void reset()
Definition Timer:100
osg::ElapsedTime::_timer
Timer * _timer
Definition Timer:147
osg::ElapsedTime::elapsedTime_u
double elapsedTime_u() const
Definition Timer:118
osg::ElapsedTime::elapsedTime
double elapsedTime() const
Definition Timer:106
osg::ElapsedTime::_time
double * _time
Definition Timer:146
osg::ElapsedTime::_startTick
Timer_t _startTick
Definition Timer:148
osg::ElapsedTime::ElapsedTime
ElapsedTime(osg::Timer *timer=0)
Definition Timer:89
osg::ElapsedTime::ElapsedTime
ElapsedTime(double *elapsedTime, osg::Timer *timer=0)
Definition Timer:83
osg::ElapsedTime::elapsedTime_n
double elapsedTime_n() const
Definition Timer:124
osg::ElapsedTime::elapsedTime_m
double elapsedTime_m() const
Definition Timer:112
osg::Timer
Definition Timer:28
osg::Timer::~Timer
~Timer()
Definition Timer:33
osg::Timer::_secsPerTick
double _secsPerTick
Definition Timer:76
osg::Timer::time_u
double time_u() const
Definition Timer:53
osg::Timer::getSecondsPerTick
double getSecondsPerTick() const
Definition Timer:71
osg::Timer::delta_n
double delta_n(Timer_t t1, Timer_t t2) const
Definition Timer:68
osg::Timer::time_s
double time_s() const
Definition Timer:47
osg::Timer::tick
Timer_t tick() const
osg::Timer::delta_u
double delta_u(Timer_t t1, Timer_t t2) const
Definition Timer:65
osg::Timer::setStartTick
void setStartTick()
Definition Timer:41
osg::Timer::setStartTick
void setStartTick(Timer_t t)
Definition Timer:42
osg::Timer::instance
static Timer * instance()
osg::Timer::time_m
double time_m() const
Definition Timer:50
osg::Timer::getStartTick
Timer_t getStartTick() const
Definition Timer:43
osg::Timer::_startTick
Timer_t _startTick
Definition Timer:75
osg::Timer::delta_s
double delta_s(Timer_t t1, Timer_t t2) const
Definition Timer:59
osg::Timer::time_n
double time_n() const
Definition Timer:56
osg::Timer::delta_m
double delta_m(Timer_t t1, Timer_t t2) const
Definition Timer:62
osg::Timer::Timer
Timer()
osg
Definition AlphaFunc:19
osg::Timer_t
unsigned long long Timer_t
Definition Timer:24
Export
OSG_EXPORT
#define OSG_EXPORT
Definition osg/Export:39