fade_helper.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // SuperTux
  2. // Copyright (C) 2021 A. Semphris <semphris@protonmail.com>
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. #include "util/fade_helper.hpp"
  17. FadeHelper::FadeHelper(float time, float target_value,
  18. float start_value, easing ease) :
  19. m_value(nullptr),
  20. m_progress(start_value),
  21. m_start(start_value),
  22. m_target(target_value),
  23. m_time(0.f),
  24. m_total_time(time),
  25. m_ease(ease)
  26. {
  27. }
  28. FadeHelper::FadeHelper(float* value, float time,
  29. float target_value, easing ease) :
  30. m_value(value),
  31. m_progress(*value),
  32. m_start(*value),
  33. m_target(target_value),
  34. m_time(0.f),
  35. m_total_time(time),
  36. m_ease(ease)
  37. {
  38. }
  39. float
  40. FadeHelper::update(float dt_sec)
  41. {
  42. m_time += dt_sec;
  43. if (completed())
  44. {
  45. m_time = m_total_time;
  46. m_progress = m_target;
  47. if (m_value)
  48. *m_value = m_target;
  49. return m_target;
  50. }
  51. // FLOAT/DOUBLE CONVERSION
  52. // If at some point in development, floats are changed to doubles, or the
  53. // ease funciton return type change from double to float, remove the casts
  54. // here (it takes a lot of space). ~ Semphris
  55. float progress = m_start + (m_target - m_start) * static_cast<float>(m_ease(
  56. static_cast<double>(m_time / m_total_time)));
  57. m_progress = progress;
  58. if (m_value)
  59. *m_value = progress;
  60. return progress;
  61. }
  62. bool
  63. FadeHelper::completed() const
  64. {
  65. return m_time >= m_total_time;
  66. }
  67. float
  68. FadeHelper::get_value() const
  69. {
  70. return m_progress;
  71. }
  72. /* EOF */