platformTimer.cpp 4.9 KB

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