sprite_animation.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * Copyright (c) 2012-2014 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #include "sprite_animation.h"
  6. #include "log.h"
  7. namespace crown
  8. {
  9. SpriteAnimation::SpriteAnimation(const SpriteAnimationResource* sar)
  10. : m_resource(sar)
  11. , m_animation(NULL)
  12. , m_frames(sprite_animation_resource::get_animation_frames(sar))
  13. , m_cur_time(0)
  14. , m_cur_frame(0)
  15. , m_loop(false)
  16. {
  17. }
  18. void SpriteAnimation::play(StringId32 name, bool loop)
  19. {
  20. if (m_animation)
  21. return;
  22. m_animation = sprite_animation_resource::get_animation(m_resource, name);
  23. m_cur_time = 0;
  24. m_cur_frame = 0;
  25. m_loop = loop;
  26. }
  27. void SpriteAnimation::stop()
  28. {
  29. m_animation = NULL;
  30. m_cur_time = 0;
  31. m_cur_frame = 0;
  32. }
  33. void SpriteAnimation::update(float dt)
  34. {
  35. if (!m_animation)
  36. return;
  37. const uint32_t frame = m_animation->first_frame + uint32_t(m_animation->num_frames * (m_cur_time / m_animation->time));
  38. m_cur_frame = m_frames[frame];
  39. m_cur_time += dt;
  40. if (m_cur_time >= m_animation->time)
  41. {
  42. if (m_loop)
  43. {
  44. m_cur_time = 0;
  45. return;
  46. }
  47. else
  48. {
  49. stop();
  50. return;
  51. }
  52. }
  53. }
  54. } // namespace crown