platformTimer.h 3.5 KB

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