TimeValue.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #ifndef CRYINCLUDE_CRYCOMMON_TIMEVALUE_H
  9. #define CRYINCLUDE_CRYCOMMON_TIMEVALUE_H
  10. #pragma once
  11. #include <CryCommon/BaseTypes.h>
  12. #include <CryCommon/platform.h>
  13. class CTimeValue
  14. {
  15. public:
  16. static const int64 TIMEVALUE_PRECISION = 100000; // one second
  17. public:
  18. // Default constructor.
  19. ILINE CTimeValue()
  20. {
  21. m_lValue = 0;
  22. }
  23. // Constructor.
  24. ILINE CTimeValue(const float fSeconds)
  25. {
  26. SetSeconds(fSeconds);
  27. }
  28. ILINE CTimeValue(const double fSeconds)
  29. {
  30. SetSeconds(fSeconds);
  31. }
  32. // Constructor.
  33. // Arguments:
  34. // inllValue - positive negative, absolute or relative in 1 second= TIMEVALUE_PRECISION units.
  35. ILINE CTimeValue(const int64 inllValue)
  36. {
  37. m_lValue = inllValue;
  38. }
  39. // Copy constructor.
  40. ILINE CTimeValue(const CTimeValue& inValue)
  41. {
  42. m_lValue = inValue.m_lValue;
  43. }
  44. // Destructor.
  45. ILINE ~CTimeValue() {}
  46. // Description:
  47. // Assignment operator.
  48. // Arguments:
  49. // inRhs - Right side.
  50. ILINE CTimeValue& operator=(const CTimeValue& inRhs)
  51. {
  52. m_lValue = inRhs.m_lValue;
  53. return *this;
  54. };
  55. // Use only for relative value, absolute values suffer a lot from precision loss.
  56. ILINE float GetSeconds() const
  57. {
  58. return m_lValue * (1.f / TIMEVALUE_PRECISION);
  59. }
  60. // Get relative time difference in seconds - call on the endTime object: endTime.GetDifferenceInSeconds( startTime );
  61. ILINE float GetDifferenceInSeconds(const CTimeValue& startTime) const
  62. {
  63. return (m_lValue - startTime.m_lValue) * (1.f / TIMEVALUE_PRECISION);
  64. }
  65. //
  66. ILINE void SetSeconds(const float infSec)
  67. {
  68. m_lValue = (int64)(infSec * TIMEVALUE_PRECISION);
  69. }
  70. //
  71. ILINE void SetSeconds(const double infSec)
  72. {
  73. m_lValue = (int64)(infSec * TIMEVALUE_PRECISION);
  74. }
  75. //
  76. ILINE void SetSeconds(const int64 indwSec)
  77. {
  78. m_lValue = indwSec * TIMEVALUE_PRECISION;
  79. }
  80. //
  81. ILINE void SetMilliSeconds(const int64 indwMilliSec)
  82. {
  83. m_lValue = indwMilliSec * (TIMEVALUE_PRECISION / 1000);
  84. }
  85. // Use only for relative value, absolute values suffer a lot from precision loss.
  86. ILINE float GetMilliSeconds() const
  87. {
  88. return m_lValue * (1000.f / TIMEVALUE_PRECISION);
  89. }
  90. ILINE int64 GetMilliSecondsAsInt64() const
  91. {
  92. return m_lValue * 1000 / TIMEVALUE_PRECISION;
  93. }
  94. ILINE int64 GetMicroSecondsAsInt64() const
  95. {
  96. return m_lValue * (1000 * 1000) / TIMEVALUE_PRECISION;
  97. }
  98. ILINE int64 GetValue() const
  99. {
  100. return m_lValue;
  101. }
  102. ILINE void SetValue(int64 val)
  103. {
  104. m_lValue = val;
  105. }
  106. // Description:
  107. // Useful for periodic events (e.g. water wave, blinking).
  108. // Changing TimePeriod can results in heavy changes in the returned value.
  109. // Return Value:
  110. // [0..1[
  111. float GetPeriodicFraction(const CTimeValue TimePeriod) const
  112. {
  113. // todo: change float implement to int64 for more precision
  114. float fAbs = GetSeconds() / TimePeriod.GetSeconds();
  115. return fAbs - (int)(fAbs);
  116. }
  117. // math operations -----------------------
  118. // Minus.
  119. ILINE CTimeValue operator-(const CTimeValue& inRhs) const { CTimeValue ret; ret.m_lValue = m_lValue - inRhs.m_lValue; return ret; };
  120. // Plus.
  121. ILINE CTimeValue operator+(const CTimeValue& inRhs) const { CTimeValue ret; ret.m_lValue = m_lValue + inRhs.m_lValue; return ret; };
  122. // Unary minus.
  123. ILINE CTimeValue operator-() const { CTimeValue ret; ret.m_lValue = -m_lValue; return ret; };
  124. ILINE CTimeValue& operator+=(const CTimeValue& inRhs) { m_lValue += inRhs.m_lValue; return *this; }
  125. ILINE CTimeValue& operator-=(const CTimeValue& inRhs) { m_lValue -= inRhs.m_lValue; return *this; }
  126. ILINE CTimeValue& operator/=(int inRhs) { m_lValue /= inRhs; return *this; }
  127. // comparison -----------------------
  128. ILINE bool operator<(const CTimeValue& inRhs) const { return m_lValue < inRhs.m_lValue; };
  129. ILINE bool operator>(const CTimeValue& inRhs) const { return m_lValue > inRhs.m_lValue; };
  130. ILINE bool operator>=(const CTimeValue& inRhs) const { return m_lValue >= inRhs.m_lValue; };
  131. ILINE bool operator<=(const CTimeValue& inRhs) const { return m_lValue <= inRhs.m_lValue; };
  132. ILINE bool operator==(const CTimeValue& inRhs) const { return m_lValue == inRhs.m_lValue; };
  133. ILINE bool operator!=(const CTimeValue& inRhs) const { return m_lValue != inRhs.m_lValue; };
  134. private: // ----------------------------------------------------------
  135. int64 m_lValue; // absolute or relative value in 1/TIMEVALUE_PRECISION, might be negative
  136. friend class CTimer;
  137. };
  138. #endif // CRYINCLUDE_CRYCOMMON_TIMEVALUE_H