vrecko
virtual reality framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Timer.h
Go to the documentation of this file.
1 #ifndef VRECKO_TIMER_H
2 #define VRECKO_TIMER_H
3 
4 // More reliable (and slightly less precise) version of a timer than that from the OSG.
5 // ------------------------------------------------------------------------------------
6 // Uses timeGetTime() function instead of QueryPerformanceCounter().
7 // Calculates time with 1 ms precision and works correctly even on multicore multiprocessor
8 // machines unlike QPC, which can jump back and forth randomly. The jumps we experienced
9 // were usually in the range of 50 ms, but much higher values were also reported
10 // on the internet forums.
11 
12 
13 #include <vrecko/Export>
14 #include <osg/Timer>
15 
16 namespace vrecko {
17 
18 /* Could be derived from osg::Timer IF the tick() method was virtual :(
19 
20  Therefore it was directly copied from osg::Timer and changed a bit.
21  The interface is identical to osg::Timer (at least in OSG 2.4.0). */
22 
23 
24 class VRECKO_EXPORT Timer {
25  public:
26 
27  Timer();
28  ~Timer();
29 
30  static Timer* instance();
31 
33  osg::Timer_t tick() const;
34 
36  void setStartTick() { _startTick = tick(); }
37  void setStartTick(osg::Timer_t t) { _startTick = t; }
38  osg::Timer_t getStartTick() const { return _startTick; }
39 
41  inline double time_s() const { return delta_s(_startTick, tick()); }
42 
44  inline double time_m() const { return delta_m(_startTick, tick()); }
45 
47  inline double time_u() const { return delta_u(_startTick, tick()); }
48 
50  inline double time_n() const { return delta_n(_startTick, tick()); }
51 
53  inline double delta_s( osg::Timer_t t1, osg::Timer_t t2 ) const { return (double)(t2 - t1)*_secsPerTick; }
54 
56  inline double delta_m( osg::Timer_t t1, osg::Timer_t t2 ) const { return delta_s(t1,t2)*1e3; }
57 
59  inline double delta_u( osg::Timer_t t1, osg::Timer_t t2 ) const { return delta_s(t1,t2)*1e6; }
60 
62  inline double delta_n( osg::Timer_t t1, osg::Timer_t t2 ) const { return delta_s(t1,t2)*1e9; }
63 
65  inline double getSecondsPerTick() const { return _secsPerTick; }
66 
67  protected :
68  osg::Timer_t _startTick;
69  double _secsPerTick;
70 };
71 
72 
73 };
74 
75 #endif