timeSource.h 4.2 KB

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