timeSource.h

Engine/source/core/util/timeSource.h

More...

Classes:

class

Timer that queries the real-time ticker.

class
class

Timer that queries the simulation-time ticker.

Detailed Description

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#ifndef _TIMESOURCE_H_
 25#define _TIMESOURCE_H_
 26
 27#ifndef _PLATFORM_H_
 28#include "platform/platform.h"
 29#endif
 30
 31#ifndef _TSTREAM_H_
 32#include "core/stream/tStream.h"
 33#endif
 34
 35#ifndef _SIM_H_
 36#include "console/sim.h"
 37#endif
 38
 39
 40/// Timer that queries the real-time ticker.
 41struct RealMSTimer
 42{
 43   typedef U32 TickType;
 44   static TickType getTick()
 45   {
 46      return Platform::getRealMilliseconds();
 47   }
 48};
 49
 50/// Timer that queries the simulation-time ticker.
 51struct VirtualMSTimer
 52{
 53   typedef U32 TickType;
 54   static TickType getTick()
 55   {
 56      return Platform::getVirtualMilliseconds();
 57   }
 58};
 59
 60/// Timer that queries Sim::getCurrentTime().
 61struct SimMSTimer
 62{
 63   typedef U32 TickType;
 64   static TickType getTick()
 65   {
 66      return Sim::getCurrentTime();
 67   }
 68};
 69
 70
 71///
 72template< class Timer = RealMSTimer, typename Tick = typename Timer::TickType >
 73class GenericTimeSource : public IPositionable< Tick >,
 74                          public IProcess,
 75                          public IResettable
 76{
 77   public:
 78
 79      typedef IPositionable< Tick> Parent;
 80      typedef Tick TickType;
 81
 82   protected:
 83
 84      ///
 85      TickType mStartTime;
 86
 87      ///
 88      TickType mPauseTime;
 89
 90      ///
 91      Timer mTimer;
 92
 93   public:
 94
 95      GenericTimeSource()
 96         : mStartTime( TypeTraits< TickType >::MAX ),
 97           mPauseTime( TypeTraits< TickType >::MAX ) {}
 98
 99      bool isStarted() const
100      {
101         return ( mStartTime != TypeTraits< TickType >::MAX );
102      }
103      bool isPaused() const
104      {
105         return ( mPauseTime != TypeTraits< TickType >::MAX );
106      }
107
108      /// Return the number of ticks since the time source
109      /// has been started.
110      TickType getPosition() const
111      {
112         if( !isStarted() )
113            return TypeTraits< TickType >::ZERO;
114         else if( isPaused() )
115            return ( mPauseTime - mStartTime );
116         else
117            return ( mTimer.getTick() - mStartTime );
118      }
119
120      ///
121      void setPosition( TickType pos )
122      {
123         if( !isStarted() )
124            mStartTime = pos;
125         else
126         {
127            TickType savedStartTime = mStartTime;
128
129            mStartTime = ( mTimer.getTick() - pos );
130            if( isPaused() )
131               mPauseTime = ( mStartTime + ( mPauseTime - savedStartTime ) );
132         }
133      }
134
135      // IResettable.
136      virtual void reset()
137      {
138         mStartTime = TypeTraits< TickType >::MAX;
139         mPauseTime = TypeTraits< TickType >::MAX;
140      }
141
142      // IProcess.
143      virtual void start()
144      {
145         if( !isStarted() )
146         {
147            TickType now = mTimer.getTick();
148
149            if( isPaused() )
150            {
151               mStartTime += now - mPauseTime;
152               mPauseTime = TypeTraits< TickType >::MAX;
153            }
154            else
155               mStartTime = now;
156         }
157      }
158      virtual void stop()
159      {
160         reset();
161      }
162      virtual void pause()
163      {
164         if( !isPaused() )
165            mPauseTime = mTimer.getTick();
166      }
167};
168
169#endif // _TIMESOURCE_H_
170