floating_text.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 "object/floating_text.hpp"
  17. #include <stdio.h>
  18. #include "supertux/resources.hpp"
  19. #include "video/drawing_context.hpp"
  20. FloatingText::FloatingText(const Vector& pos, const std::string& text_) :
  21. position(pos),
  22. text(text_),
  23. timer()
  24. {
  25. timer.start(.1f);
  26. position.x -= static_cast<float>(text.size()) * 8.0f;
  27. }
  28. FloatingText::FloatingText(const Vector& pos, int score) :
  29. position(pos),
  30. text(std::to_string(score)),
  31. timer()
  32. {
  33. timer.start(.1f);
  34. position.x -= static_cast<float>(text.size()) * 8.0f;
  35. }
  36. void
  37. FloatingText::update(float dt_sec)
  38. {
  39. position.y -= 1.4f * dt_sec;
  40. if (timer.check())
  41. remove_me();
  42. }
  43. const float FADING_TIME = .350f;
  44. void
  45. FloatingText::draw(DrawingContext& context)
  46. {
  47. // Make an alpha animation when disappearing.
  48. float alpha;
  49. if (timer.get_timeleft() < FADING_TIME)
  50. alpha = timer.get_timeleft() * 255.0f / FADING_TIME;
  51. else
  52. alpha = 255.0f;
  53. context.push_transform();
  54. context.set_alpha(alpha);
  55. context.color().draw_text(Resources::normal_font, text, position, ALIGN_LEFT, LAYER_OBJECTS+1, FloatingText::text_color);
  56. context.pop_transform();
  57. }
  58. /* EOF */