Torque3D Documentation / _generateds / platformTimer.cpp

platformTimer.cpp

Engine/source/platform/platformTimer.cpp

More...

Classes:

Public Functions

DefineEngineFunction(startPrecisionTimer , S32 , () , "startPrecisionTimer() - Create and start <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> high resolution platform timer. Returns the timer id." )
DefineEngineFunction(stopPrecisionTimer , S32 , (S32 id) , "stopPrecisionTimer( <a href="/coding/file/types_8h/#types_8h_1a57a2244776e01ad620c556de58eb7880">S32</a> <a href="/coding/file/win32cursorcontroller_8cpp/#win32cursorcontroller_8cpp_1ab38592509822a5f4674447022cc62efe">id</a> ) - Stop and destroy timer with the passed id. Returns the elapsed milliseconds." )

Detailed Description

Public Variables

ScriptTimerMan gScriptTimerMan 

Public Functions

DefineEngineFunction(startPrecisionTimer , S32 , () , "startPrecisionTimer() - Create and start <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> high resolution platform timer. Returns the timer id." )

DefineEngineFunction(stopPrecisionTimer , S32 , (S32 id) , "stopPrecisionTimer( <a href="/coding/file/types_8h/#types_8h_1a57a2244776e01ad620c556de58eb7880">S32</a> <a href="/coding/file/win32cursorcontroller_8cpp/#win32cursorcontroller_8cpp_1ab38592509822a5f4674447022cc62efe">id</a> ) - Stop and destroy timer with the passed id. Returns the elapsed milliseconds." )

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#include "platform/platformTimer.h"
 25#include "core/util/journal/process.h"
 26#include "console/engineAPI.h"
 27
 28void TimeManager::_updateTime()
 29{
 30   // Calculate & filter time delta since last event.
 31
 32   // How long since last update?
 33   S32 delta = mTimer->getElapsedMs();
 34
 35   // Now - we want to try to sleep until the time threshold will hit.
 36   S32 msTillThresh = (mBackground ? mBackgroundThreshold : mForegroundThreshold) - delta;
 37
 38   if(msTillThresh > 0)
 39   {
 40      // There's some time to go, so let's sleep.
 41      Platform::sleep( msTillThresh );
 42   }
 43
 44   // Ok - let's grab the new elapsed and send that out.
 45   S32 finalDelta = mTimer->getElapsedMs();
 46   mTimer->reset();
 47
 48   timeEvent.trigger(finalDelta);
 49}
 50
 51TimeManager::TimeManager()
 52{
 53   mBackground = false;
 54   mTimer = PlatformTimer::create();
 55   Process::notify(this, &TimeManager::_updateTime, PROCESS_TIME_ORDER);
 56   
 57   mForegroundThreshold = 5;
 58   mBackgroundThreshold = 10;
 59}
 60
 61TimeManager::~TimeManager()
 62{
 63   Process::remove(this, &TimeManager::_updateTime);
 64   delete mTimer;
 65}
 66
 67void TimeManager::setForegroundThreshold(const S32 msInterval)
 68{
 69   AssertFatal(msInterval > 0, "TimeManager::setForegroundThreshold - should have at least 1 ms between time events to avoid math problems!");
 70   mForegroundThreshold = msInterval;
 71}
 72
 73const S32 TimeManager::getForegroundThreshold() const
 74{
 75   return mForegroundThreshold;
 76}
 77
 78void TimeManager::setBackgroundThreshold(const S32 msInterval)
 79{
 80   AssertFatal(msInterval > 0, "TimeManager::setBackgroundThreshold - should have at least 1 ms between time events to avoid math problems!");
 81   mBackgroundThreshold = msInterval;
 82}
 83
 84const S32 TimeManager::getBackgroundThreshold() const
 85{
 86   return mBackgroundThreshold;
 87}
 88
 89//----------------------------------------------------------------------------------
 90PlatformTimer::PlatformTimer()
 91{
 92}
 93
 94PlatformTimer::~PlatformTimer()
 95{
 96}
 97
 98
 99// Exposes PlatformTimer to script for when high precision is needed.
100
101#include "core/util/tDictionary.h"
102#include "console/console.h"
103
104class ScriptTimerMan
105{
106public:
107
108   ScriptTimerMan();
109   ~ScriptTimerMan();
110
111   S32 startTimer();
112   S32 stopTimer( S32 id );
113
114protected:
115
116   static S32 smNextId;
117
118   typedef Map<S32,PlatformTimer*> TimerMap;
119
120   TimerMap mTimers;
121};
122
123S32 ScriptTimerMan::smNextId = 1;
124
125ScriptTimerMan::ScriptTimerMan()
126{
127}
128
129ScriptTimerMan::~ScriptTimerMan()
130{
131   TimerMap::Iterator itr = mTimers.begin();
132
133   for ( ; itr != mTimers.end(); itr++ )   
134      delete itr->value;
135   
136   mTimers.clear();
137}
138
139S32 ScriptTimerMan::startTimer()
140{
141   PlatformTimer *timer = PlatformTimer::create();
142   mTimers.insert( smNextId, timer );
143   smNextId++;
144   return ( smNextId - 1 );
145}
146
147S32 ScriptTimerMan::stopTimer( S32 id )
148{
149   TimerMap::Iterator itr = mTimers.find( id );
150   if ( itr == mTimers.end() )
151      return -1;
152
153   PlatformTimer *timer = itr->value;
154   S32 elapsed = timer->getElapsedMs();
155
156   mTimers.erase( itr );
157   delete timer;
158
159   return elapsed;
160}
161
162ScriptTimerMan gScriptTimerMan;
163
164DefineEngineFunction( startPrecisionTimer, S32, (), , "startPrecisionTimer() - Create and start a high resolution platform timer. Returns the timer id." )
165{
166   return gScriptTimerMan.startTimer();
167}
168
169DefineEngineFunction( stopPrecisionTimer, S32, ( S32 id), , "stopPrecisionTimer( S32 id ) - Stop and destroy timer with the passed id.  Returns the elapsed milliseconds." )
170{
171   return gScriptTimerMan.stopTimer( id );
172}
173