iTickable.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. for( ; smLastTick != targetTick; smLastTick += smTickMs )
  75. for( ProcessListIterator i = getProcessList().begin(); i != getProcessList().end(); i++ )
  76. if( (*i)->isProcessingTicks() )
  77. (*i)->processTick();
  78. smLastDelta = ( smTickMs - ( targetTime & smTickMask ) ) & smTickMask;
  79. F32 dt = smLastDelta / F32( smTickMs );
  80. // Now interpolate objects that want ticks
  81. for( ProcessListIterator i = getProcessList().begin(); i != getProcessList().end(); i++ )
  82. if( (*i)->isProcessingTicks() )
  83. (*i)->interpolateTick( dt );
  84. // Inform ALL objects that time was advanced
  85. dt = F32( timeDelta ) / 1000.f;
  86. for( ProcessListIterator i = getProcessList().begin(); i != getProcessList().end(); i++ )
  87. (*i)->advanceTime( dt );
  88. smLastTime = targetTime;
  89. return tickCount != 0;
  90. }