fadetoblack.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // SuperTux
  2. // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
  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 "supertux/fadetoblack.hpp"
  17. #include "supertux/globals.hpp"
  18. #include "video/drawing_context.hpp"
  19. FadeToBlack::FadeToBlack(Direction direction, float fade_time, Color color) :
  20. m_direction(direction),
  21. m_fade_time(fade_time),
  22. m_color(color),
  23. m_accum_time(0)
  24. {
  25. }
  26. void
  27. FadeToBlack::update(float dt_sec)
  28. {
  29. m_accum_time += dt_sec;
  30. if (m_accum_time > m_fade_time)
  31. m_accum_time = m_fade_time;
  32. }
  33. void
  34. FadeToBlack::draw(DrawingContext& context)
  35. {
  36. Color col = m_color;
  37. col.alpha = m_accum_time / m_fade_time;
  38. if (m_direction != FADEOUT)
  39. col.alpha = 1.0f - col.alpha;
  40. // The colours are mixed directly in sRGB space, so change alpha for a more
  41. // linear fading (it may only work correctly with black).
  42. col.alpha = Color::remove_gamma(col.alpha);
  43. context.color().draw_filled_rect(context.get_rect(), col, LAYER_GUI + 1);
  44. }
  45. bool
  46. FadeToBlack::done() const
  47. {
  48. return m_accum_time >= m_fade_time;
  49. }
  50. /* EOF */