platformTimer.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 "platform/platformTimer.h"
  23. #include "core/util/journal/process.h"
  24. #include "console/engineAPI.h"
  25. void TimeManager::_updateTime()
  26. {
  27. // Calculate & filter time delta since last event.
  28. // How long since last update?
  29. S32 delta = mTimer->getElapsedMs();
  30. // Now - we want to try to sleep until the time threshold will hit.
  31. S32 msTillThresh = (mBackground ? mBackgroundThreshold : mForegroundThreshold) - delta;
  32. if(msTillThresh > 0)
  33. {
  34. // There's some time to go, so let's sleep.
  35. Platform::sleep( msTillThresh );
  36. }
  37. // Ok - let's grab the new elapsed and send that out.
  38. S32 finalDelta = mTimer->getElapsedMs();
  39. mTimer->reset();
  40. timeEvent.trigger(finalDelta);
  41. }
  42. TimeManager::TimeManager()
  43. {
  44. mBackground = false;
  45. mTimer = PlatformTimer::create();
  46. Process::notify(this, &TimeManager::_updateTime, PROCESS_TIME_ORDER);
  47. mForegroundThreshold = 5;
  48. mBackgroundThreshold = 10;
  49. }
  50. TimeManager::~TimeManager()
  51. {
  52. Process::remove(this, &TimeManager::_updateTime);
  53. delete mTimer;
  54. }
  55. void TimeManager::setForegroundThreshold(const S32 msInterval)
  56. {
  57. AssertFatal(msInterval > 0, "TimeManager::setForegroundThreshold - should have at least 1 ms between time events to avoid math problems!");
  58. mForegroundThreshold = msInterval;
  59. }
  60. const S32 TimeManager::getForegroundThreshold() const
  61. {
  62. return mForegroundThreshold;
  63. }
  64. void TimeManager::setBackgroundThreshold(const S32 msInterval)
  65. {
  66. AssertFatal(msInterval > 0, "TimeManager::setBackgroundThreshold - should have at least 1 ms between time events to avoid math problems!");
  67. mBackgroundThreshold = msInterval;
  68. }
  69. const S32 TimeManager::getBackgroundThreshold() const
  70. {
  71. return mBackgroundThreshold;
  72. }
  73. //----------------------------------------------------------------------------------
  74. PlatformTimer::PlatformTimer()
  75. {
  76. }
  77. PlatformTimer::~PlatformTimer()
  78. {
  79. }
  80. // Exposes PlatformTimer to script for when high precision is needed.
  81. #include "core/util/tDictionary.h"
  82. #include "console/console.h"
  83. class ScriptTimerMan
  84. {
  85. public:
  86. ScriptTimerMan();
  87. ~ScriptTimerMan();
  88. S32 startTimer();
  89. S32 stopTimer( S32 id );
  90. protected:
  91. static S32 smNextId;
  92. typedef Map<S32,PlatformTimer*> TimerMap;
  93. TimerMap mTimers;
  94. };
  95. S32 ScriptTimerMan::smNextId = 1;
  96. ScriptTimerMan::ScriptTimerMan()
  97. {
  98. }
  99. ScriptTimerMan::~ScriptTimerMan()
  100. {
  101. TimerMap::Iterator itr = mTimers.begin();
  102. for ( ; itr != mTimers.end(); itr++ )
  103. delete itr->value;
  104. mTimers.clear();
  105. }
  106. S32 ScriptTimerMan::startTimer()
  107. {
  108. PlatformTimer *timer = PlatformTimer::create();
  109. mTimers.insert( smNextId, timer );
  110. smNextId++;
  111. return ( smNextId - 1 );
  112. }
  113. S32 ScriptTimerMan::stopTimer( S32 id )
  114. {
  115. TimerMap::Iterator itr = mTimers.find( id );
  116. if ( itr == mTimers.end() )
  117. return -1;
  118. PlatformTimer *timer = itr->value;
  119. S32 elapsed = timer->getElapsedMs();
  120. mTimers.erase( itr );
  121. delete timer;
  122. return elapsed;
  123. }
  124. ScriptTimerMan gScriptTimerMan;
  125. DefineEngineFunction( startPrecisionTimer, S32, (), , "startPrecisionTimer() - Create and start a high resolution platform timer. Returns the timer id." )
  126. {
  127. return gScriptTimerMan.startTimer();
  128. }
  129. DefineEngineFunction( stopPrecisionTimer, S32, ( S32 id), , "stopPrecisionTimer( S32 id ) - Stop and destroy timer with the passed id. Returns the elapsed milliseconds." )
  130. {
  131. return gScriptTimerMan.stopTimer( id );
  132. }