platformTimer.h
Engine/source/platform/platformTimer.h
Classes:
class
class
Platform-specific timer class.
class
Utility class to fire journalled time-delta events at regular intervals.
Detailed Description
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#ifndef _PLATFORM_PLATFORMTIMER_H_ 25#define _PLATFORM_PLATFORMTIMER_H_ 26 27#include "platform/platform.h" 28#include "core/util/journal/journaledSignal.h" 29 30/// Platform-specific timer class. 31/// 32/// This exists primarily as support for the TimeManager, but may be useful 33/// elsewhere. 34class PlatformTimer 35{ 36protected: 37 PlatformTimer(); 38public: 39 virtual ~PlatformTimer(); 40 41 /// Get the number of MS that have elapsed since creation or the last 42 /// reset call. 43 virtual const S32 getElapsedMs()=0; 44 45 /// Reset elapsed ms back to zero. 46 virtual void reset()=0; 47 48 /// Create a new PlatformTimer. 49 static PlatformTimer *create(); 50}; 51 52/// Utility class to fire journalled time-delta events at regular intervals. 53/// 54/// Most games and simulations need the ability to update their state based on 55/// a time-delta. However, tracking time accurately and sending out well-conditioned 56/// events (for instance, allowing no events with delta=0) tends to be platform 57/// specific. This class provides an abstraction around this platform mojo. 58/// 59/// In addition, a well behaved application may want to alter how frequently 60/// it processes time advancement depending on its execution state. For instance, 61/// a game running in the background can significantly reduce CPU usage 62/// by only updating every 100ms, instead of trying to maintain a 1ms update 63/// update rate. 64class TimeManager 65{ 66 PlatformTimer *mTimer; 67 S32 mForegroundThreshold, mBackgroundThreshold; 68 bool mBackground; 69 70 void _updateTime(); 71 72public: 73 74 TimeManagerEvent timeEvent; 75 76 TimeManager(); 77 ~TimeManager(); 78 79 void setForegroundThreshold(const S32 msInterval); 80 const S32 getForegroundThreshold() const; 81 82 void setBackgroundThreshold(const S32 msInterval); 83 const S32 getBackgroundThreshold() const; 84 85 void setBackground(const bool isBackground) { mBackground = isBackground; }; 86 const bool getBackground() const { return mBackground; }; 87 88}; 89 90class DefaultPlatformTimer : public PlatformTimer 91{ 92 S32 mLastTime, mNextTime; 93 94public: 95 DefaultPlatformTimer() 96 { 97 mLastTime = mNextTime = Platform::getRealMilliseconds(); 98 } 99 100 const S32 getElapsedMs() 101 { 102 mNextTime = Platform::getRealMilliseconds(); 103 return (mNextTime - mLastTime); 104 } 105 106 void reset() 107 { 108 mLastTime = mNextTime; 109 } 110}; 111 112#endif 113