ParabolicEase.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. ** Command & Conquer Generals Zero Hour(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. // ============================================================================
  19. // Copyright (C) 2003, 2004 Electronic Arts
  20. //
  21. // ParabolicEase.cpp
  22. // Ease in and out based on a parabolic function.
  23. // Author: Robert Minsk May 12, 2003
  24. // ============================================================================
  25. #include "PreRTS.h" // This must go first in EVERY cpp file in the GameEngine
  26. #include "GameClient/ParabolicEase.h"
  27. // ============================================================================
  28. #ifdef _INTERNAL
  29. // for occasional debugging...
  30. // #pragma optimize("", off)
  31. // #pragma MESSAGE("************************************** WARNING, optimization disabled for debugging purposes")
  32. #endif // _INTERNAL
  33. // ============================================================================
  34. namespace {
  35. // ============================================================================
  36. template <typename TYPE>
  37. inline TYPE
  38. clamp(TYPE val, TYPE min = TYPE(0), TYPE max = TYPE(1))
  39. {
  40. if (val < min) {
  41. val = min;
  42. } else if (val > max) {
  43. val = max;
  44. }
  45. return val;
  46. }
  47. // ============================================================================
  48. } // namespace
  49. // ============================================================================
  50. void
  51. ParabolicEase::setEaseTimes(Real easeInTime, Real easeOutTime)
  52. {
  53. m_in = easeInTime;
  54. if (m_in < 0.0f || m_in > 1.0f) {
  55. DEBUG_CRASH(("Ease-in out of range (in = %g)\n", m_in));
  56. m_in = clamp(m_in);
  57. }
  58. m_out = 1.0f - easeOutTime;
  59. if (m_out < 0.0f || m_out > 1.0f) {
  60. DEBUG_CRASH(("Ease-out out of range (out = %g)\n", m_out));
  61. m_out = clamp(m_out);
  62. }
  63. if (m_in > m_out) {
  64. DEBUG_CRASH(("Ease-in and ease-out overlap (in = %g, out = %g)\n", m_in, m_out));
  65. m_in = m_out;
  66. }
  67. }
  68. // ----------------------------------------------------------------------------
  69. Real
  70. ParabolicEase::operator ()(Real param) const
  71. {
  72. if (param < 0.0f || param > 1.0f) {
  73. DEBUG_CRASH(("Ease-in/ease-out parameter out of range (param = %g)\n", param));
  74. param = clamp(param);
  75. }
  76. #if 0
  77. const Real v0 = 2.0f/(1.0f + out - in);
  78. if (param < m_in) {
  79. // "param < in" avoids a division by zero at param = 0.0f and in = 0.0f
  80. return v0*param*param/(2.0f*m_in);
  81. } else if (param <= m_out) {
  82. // "param <= out" avoid a division by zero when param = 1.0f and out = 1.0f
  83. return v0*(m_in/2.0f + (param - m_in));
  84. } else {
  85. return v0*(m_in/2.0f + (m_out - m_in) +
  86. (param - m_out + (m_out*m_out - param*param)/2.0f)/(1.0f - m_out));
  87. }
  88. #else
  89. const Real v0 = 1.0f + m_out - m_in;
  90. if (param < m_in) {
  91. // "param < in" avoids a division by zero at param = 0.0f and in = 0.0f
  92. return param*param/(v0*m_in);
  93. } else if (param <= m_out) {
  94. // "param <= out" avoid a division by zero when param = 1.0f and out = 1.0f
  95. return (m_in + 2.0*(param - m_in))/v0;
  96. } else {
  97. return (m_in + 2.0*(m_out - m_in) + (2.0*(param - m_out) + m_out*m_out - param*param)/(1.0f - m_out))/v0;
  98. }
  99. #endif
  100. }