iTickable.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. #include "core/iTickable.h"
  23. // The statics
  24. U32 ITickable::smLastTick = 0;
  25. U32 ITickable::smLastTime = 0;
  26. U32 ITickable::smLastDelta = 0;
  27. U32 ITickable::smTickShift = 5;
  28. U32 ITickable::smTickMs = ( 1 << smTickShift );
  29. F32 ITickable::smTickSec = ( F32( ITickable::smTickMs ) / 1000.f );
  30. U32 ITickable::smTickMask = ( smTickMs - 1 );
  31. //------------------------------------------------------------------------------
  32. ITickable::ITickable() : mProcessTick( true )
  33. {
  34. getProcessList().push_back( this );
  35. }
  36. //------------------------------------------------------------------------------
  37. ITickable::~ITickable()
  38. {
  39. for( ProcessListIterator i = getProcessList().begin(); i != getProcessList().end(); i++ )
  40. {
  41. if( (*i) == this )
  42. {
  43. getProcessList().erase( i );
  44. return;
  45. }
  46. }
  47. }
  48. //------------------------------------------------------------------------------
  49. void ITickable::init( const U32 tickShift )
  50. {
  51. // Sanity!
  52. AssertFatal( tickShift == 0 || tickShift <= 31, "ITickable::init() - Invalid 'tickShift' parameter!" );
  53. // Calculate tick constants.
  54. smTickShift = tickShift;
  55. smTickMs = ( 1 << smTickShift );
  56. smTickSec = ( F32( smTickMs ) / 1000.f );
  57. smTickMask = ( smTickMs - 1 );
  58. }
  59. //------------------------------------------------------------------------------
  60. Vector<ITickable *>& ITickable::getProcessList()
  61. {
  62. // This helps to avoid the static initialization order fiasco
  63. static Vector<ITickable *> smProcessList( __FILE__, __LINE__ ); ///< List of tick controls
  64. return smProcessList;
  65. }
  66. //------------------------------------------------------------------------------
  67. bool ITickable::advanceTime( U32 timeDelta )
  68. {
  69. U32 targetTime = smLastTime + timeDelta;
  70. U32 targetTick = ( targetTime + smTickMask ) & ~smTickMask;
  71. U32 tickCount = ( targetTick - smLastTick ) >> smTickShift;
  72. // Advance objects
  73. if( tickCount )
  74. {
  75. for( ; smLastTick != targetTick; smLastTick += smTickMs )
  76. {
  77. for( U32 i=0; i < getProcessList().size(); )
  78. {
  79. ITickable* iTick = getProcessList()[i];
  80. if( iTick->isProcessingTicks() )
  81. {
  82. iTick->processTick();
  83. // Only advance counter if the tickable hasn't deleted itself
  84. if( i < getProcessList().size() && iTick == getProcessList()[i] )
  85. ++i;
  86. }
  87. else
  88. {
  89. // Move onto the next tickable
  90. ++i;
  91. }
  92. }
  93. }
  94. }
  95. smLastDelta = ( smTickMs - ( targetTime & smTickMask ) ) & smTickMask;
  96. F32 dt = smLastDelta / F32( smTickMs );
  97. // Now interpolate objects that want ticks. Note that an object should never delete
  98. // itself during an interpolateTick().
  99. for( ProcessListIterator i = getProcessList().begin(); i != getProcessList().end(); i++ )
  100. if( (*i)->isProcessingTicks() )
  101. (*i)->interpolateTick( dt );
  102. // Inform ALL objects that time was advanced
  103. dt = F32( timeDelta ) / 1000.f;
  104. for( U32 i=0; i < getProcessList().size(); )
  105. {
  106. ITickable* iTick = getProcessList()[i];
  107. iTick->advanceTime( dt );
  108. // Only advance counter if the tickable hasn't deleted itself
  109. if( i < getProcessList().size() && iTick == getProcessList()[i] )
  110. ++i;
  111. }
  112. smLastTime = targetTime;
  113. return tickCount != 0;
  114. }