Tickable.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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 "platform/Tickable.h"
  23. // The statics
  24. U32 Tickable::smLastTick = 0;
  25. U32 Tickable::smLastTime = 0;
  26. U32 Tickable::smLastDelta = 0;
  27. const U32 Tickable::smTickShift = 4;
  28. const U32 Tickable::smTickMs = ( 1 << smTickShift );
  29. const F32 Tickable::smTickSec = ( F32( Tickable::smTickMs ) / 1000.f );
  30. const U32 Tickable::smTickMask = ( smTickMs - 1 );
  31. //------------------------------------------------------------------------------
  32. Tickable::Tickable() :
  33. mProcessTick( false )
  34. {
  35. // We should always start with processing of ticks off!
  36. }
  37. //------------------------------------------------------------------------------
  38. Tickable::~Tickable()
  39. {
  40. setProcessTicks( false );
  41. }
  42. //------------------------------------------------------------------------------
  43. Vector<Tickable *>& Tickable::getProcessList()
  44. {
  45. // This helps to avoid the static initialization order fiasco
  46. static Vector<Tickable *> smProcessList; ///< List of tick controls
  47. return smProcessList;
  48. }
  49. //------------------------------------------------------------------------------
  50. void Tickable::setProcessTicks( bool tick /* = true */ )
  51. {
  52. // Ignore no change.
  53. if ( tick == mProcessTick )
  54. return;
  55. // Update tick flag.
  56. mProcessTick = tick;
  57. // Are we processing ticks?
  58. if ( mProcessTick )
  59. {
  60. // Yes, so add to process list.
  61. getProcessList().push_back( this );
  62. return;
  63. }
  64. // No, so remove from process list.
  65. for( ProcessListIterator i = getProcessList().begin(); i != getProcessList().end(); i++ )
  66. {
  67. if( (*i) == this )
  68. {
  69. getProcessList().erase( i );
  70. return;
  71. }
  72. }
  73. }
  74. //------------------------------------------------------------------------------
  75. bool Tickable::advanceTime( U32 timeDelta )
  76. {
  77. U32 targetTime = smLastTime + timeDelta;
  78. U32 targetTick = ( targetTime + smTickMask ) & ~smTickMask;
  79. U32 tickCount = ( targetTick - smLastTick ) >> smTickShift;
  80. static Vector<Tickable*> safeProcessList;
  81. // Process ticks.
  82. if( tickCount )
  83. {
  84. // Fetch a copy of the process list.
  85. safeProcessList = getProcessList();
  86. for( ; smLastTick != targetTick; smLastTick += smTickMs )
  87. {
  88. for( ProcessListIterator i = safeProcessList.begin(); i != safeProcessList.end(); i++ )
  89. {
  90. (*i)->processTick();
  91. }
  92. }
  93. }
  94. smLastDelta = ( smTickMs - ( targetTime & smTickMask ) ) & smTickMask;
  95. F32 dt = smLastDelta / F32( smTickMs );
  96. // Fetch a copy of the process list.
  97. safeProcessList = getProcessList();
  98. // Interpolate tick.
  99. for( ProcessListIterator i = safeProcessList.begin(); i != safeProcessList.end(); i++ )
  100. {
  101. (*i)->interpolateTick( dt );
  102. }
  103. // Fetch a copy of the process list.
  104. safeProcessList = getProcessList();
  105. dt = F32( timeDelta ) / 1000.f;
  106. for( ProcessListIterator i = safeProcessList.begin(); i != safeProcessList.end(); i++ )
  107. {
  108. (*i)->advanceTime( dt );
  109. }
  110. smLastTime = targetTime;
  111. return tickCount != 0;
  112. }