ambient_light.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. LayerObject(mapping),
  35. m_ambient_light(1.0f, 1.0f, 1.0f, 1.0f),
  36. m_ambient_light_fading(false),
  37. m_source_ambient_light(1.0f, 1.0f, 1.0f, 1.0f),
  38. m_target_ambient_light(1.0f, 1.0f, 1.0f, 1.0f),
  39. m_ambient_light_fade_duration(0.0f),
  40. m_ambient_light_fade_accum(0.0f)
  41. {
  42. std::vector<float> color_vec;
  43. if (mapping.get("color", color_vec))
  44. {
  45. if (color_vec.size() < 3) {
  46. log_warning << "'ambient-light' requires three float arguments" << std::endl;
  47. } else {
  48. m_ambient_light = Color(color_vec);
  49. }
  50. }
  51. }
  52. void
  53. AmbientLight::update(float dt_sec)
  54. {
  55. if (m_ambient_light_fading)
  56. {
  57. m_ambient_light_fade_accum += dt_sec;
  58. float percent_done = m_ambient_light_fade_accum / m_ambient_light_fade_duration * 1.0f;
  59. float r = (1.0f - percent_done) * m_source_ambient_light.red + percent_done * m_target_ambient_light.red;
  60. float g = (1.0f - percent_done) * m_source_ambient_light.green + percent_done * m_target_ambient_light.green;
  61. float b = (1.0f - percent_done) * m_source_ambient_light.blue + percent_done * m_target_ambient_light.blue;
  62. r = math::clamp(r, 0.0f, 1.0f);
  63. g = math::clamp(g, 0.0f, 1.0f);
  64. b = math::clamp(b, 0.0f, 1.0f);
  65. m_ambient_light = Color(r, g, b);
  66. if (m_ambient_light_fade_accum >= m_ambient_light_fade_duration)
  67. {
  68. m_ambient_light = m_target_ambient_light;
  69. m_ambient_light_fading = false;
  70. m_ambient_light_fade_accum = 0;
  71. }
  72. }
  73. }
  74. void
  75. AmbientLight::draw(DrawingContext& context)
  76. {
  77. context.set_ambient_color(m_ambient_light);
  78. }
  79. void
  80. AmbientLight::fade_to_ambient_light(float red, float green, float blue, float seconds)
  81. {
  82. if (seconds == 0)
  83. {
  84. m_ambient_light = Color(red, green, blue);
  85. return;
  86. }
  87. m_ambient_light_fading = true;
  88. m_ambient_light_fade_accum = 0;
  89. m_ambient_light_fade_duration = seconds;
  90. m_source_ambient_light = m_ambient_light;
  91. m_target_ambient_light = Color(red, green, blue);
  92. }
  93. ObjectSettings
  94. AmbientLight::get_settings()
  95. {
  96. ObjectSettings result = GameObject::get_settings();
  97. result.add_color(_("Color"), &m_ambient_light, "color");
  98. return result;
  99. }
  100. /* EOF */