ambient_light.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // SuperTux
  2. // Copyright (C) 2018 Ingo Ruhnke <grumbel@gmail.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 "object/ambient_light.hpp"
  17. #include <sexp/io.hpp>
  18. #include <sexp/value.hpp>
  19. #include "math/util.hpp"
  20. #include "util/log.hpp"
  21. #include "util/reader_mapping.hpp"
  22. #include "util/writer.hpp"
  23. #include "video/drawing_context.hpp"
  24. AmbientLight::AmbientLight(const Color& color) :
  25. m_ambient_light(color),
  26. m_ambient_light_fading(false),
  27. m_source_ambient_light(color),
  28. m_target_ambient_light(color),
  29. m_ambient_light_fade_duration(0.0f),
  30. m_ambient_light_fade_accum(0.0f)
  31. {
  32. }
  33. AmbientLight::AmbientLight(const ReaderMapping& mapping) :
  34. m_ambient_light(1.0f, 1.0f, 1.0f, 1.0f),
  35. m_ambient_light_fading(false),
  36. m_source_ambient_light(1.0f, 1.0f, 1.0f, 1.0f),
  37. m_target_ambient_light(1.0f, 1.0f, 1.0f, 1.0f),
  38. m_ambient_light_fade_duration(0.0f),
  39. m_ambient_light_fade_accum(0.0f)
  40. {
  41. std::vector<float> color_vec;
  42. if (mapping.get("color", color_vec))
  43. {
  44. if (color_vec.size() < 3) {
  45. log_warning << "'ambient-light' requires three float arguments" << std::endl;
  46. } else {
  47. m_ambient_light = Color(color_vec);
  48. }
  49. }
  50. }
  51. void
  52. AmbientLight::update(float dt_sec)
  53. {
  54. if (m_ambient_light_fading)
  55. {
  56. m_ambient_light_fade_accum += dt_sec;
  57. float percent_done = m_ambient_light_fade_accum / m_ambient_light_fade_duration * 1.0f;
  58. float r = (1.0f - percent_done) * m_source_ambient_light.red + percent_done * m_target_ambient_light.red;
  59. float g = (1.0f - percent_done) * m_source_ambient_light.green + percent_done * m_target_ambient_light.green;
  60. float b = (1.0f - percent_done) * m_source_ambient_light.blue + percent_done * m_target_ambient_light.blue;
  61. r = math::clamp(r, 0.0f, 1.0f);
  62. g = math::clamp(g, 0.0f, 1.0f);
  63. b = math::clamp(b, 0.0f, 1.0f);
  64. m_ambient_light = Color(r, g, b);
  65. if (m_ambient_light_fade_accum >= m_ambient_light_fade_duration)
  66. {
  67. m_ambient_light = m_target_ambient_light;
  68. m_ambient_light_fading = false;
  69. m_ambient_light_fade_accum = 0;
  70. }
  71. }
  72. }
  73. void
  74. AmbientLight::draw(DrawingContext& context)
  75. {
  76. context.set_ambient_color(m_ambient_light);
  77. }
  78. void
  79. AmbientLight::set_ambient_light(const Color& ambient_light)
  80. {
  81. m_ambient_light = ambient_light;
  82. }
  83. Color
  84. AmbientLight::get_ambient_light() const
  85. {
  86. return m_ambient_light;
  87. }
  88. void
  89. AmbientLight::fade_to_ambient_light(float red, float green, float blue, float seconds)
  90. {
  91. if (seconds == 0)
  92. {
  93. m_ambient_light = Color(red, green, blue);
  94. return;
  95. }
  96. m_ambient_light_fading = true;
  97. m_ambient_light_fade_accum = 0;
  98. m_ambient_light_fade_duration = seconds;
  99. m_source_ambient_light = m_ambient_light;
  100. m_target_ambient_light = Color(red, green, blue);
  101. }
  102. ObjectSettings
  103. AmbientLight::get_settings()
  104. {
  105. ObjectSettings result = GameObject::get_settings();
  106. result.add_color(_("Color"), &m_ambient_light, "color");
  107. return result;
  108. }
  109. /* EOF */