AnimationFrame.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #include "AnimationFrame.h"
  2. #include "res/ResAnim.h"
  3. namespace oxygine
  4. {
  5. void AnimationFrame::init(ResAnim* rs, const Diffuse& df, const RectF& srcRect, const RectF& destRect, const Vector2& frame_size)
  6. {
  7. _resAnim = rs;
  8. _diffuse = df;
  9. _srcRect = srcRect;
  10. _destRect = destRect;
  11. _frameSize = frame_size.cast<PointS>();
  12. }
  13. void AnimationFrame::init2(ResAnim* rs, short col, short row, const Diffuse& df, const RectF& srcRect, const RectF& destRect, const Vector2& frame_size)
  14. {
  15. _column = col;
  16. _row = row;
  17. _resAnim = rs;
  18. _diffuse = df;
  19. _srcRect = srcRect;
  20. _destRect = destRect;
  21. _frameSize = frame_size.cast<PointS>();
  22. }
  23. AnimationFrame AnimationFrame::getClipped(const RectF& rect) const
  24. {
  25. AnimationFrame f = *this;
  26. float w = (float)_diffuse.base->getWidth();
  27. float h = (float)_diffuse.base->getHeight();
  28. f._destRect.clip(rect);
  29. if (f._destRect.isEmpty())
  30. f._destRect = RectF(0, 0, 0, 0);
  31. RectF srcRect = _srcRect * Vector2(w, h);
  32. float sc = 1.0f;//(float)srcRect.getWidth() / f._destRect.getWidth();
  33. if (_resAnim)
  34. sc = _resAnim->getScaleFactor();
  35. f._srcRect.pos = srcRect.pos - (_destRect.pos - f._destRect.pos) * sc;
  36. f._srcRect.size = srcRect.size - (_destRect.size - f._destRect.size) * sc;
  37. f._srcRect = f._srcRect / Vector2(w, h);
  38. f._frameSize = rect.size;
  39. f._destRect.pos -= rect.pos;
  40. return f;
  41. }
  42. AnimationFrame AnimationFrame::getFlipped(bool vertical, bool horizontal) const
  43. {
  44. AnimationFrame f = *this;
  45. if (vertical)
  46. f.flipY();
  47. if (horizontal)
  48. f.flipX();
  49. return f;
  50. }
  51. void AnimationFrame::flipX()
  52. {
  53. _srcRect.setX(_srcRect.getRight());
  54. _srcRect.setWidth(-_srcRect.getWidth());
  55. _destRect.pos.x = _frameSize.x - _destRect.getRight();
  56. }
  57. void AnimationFrame::flipY()
  58. {
  59. _srcRect.setY(_srcRect.getBottom());
  60. _srcRect.setHeight(-_srcRect.getHeight());
  61. _destRect.pos.y = _frameSize.y - _destRect.getBottom();
  62. }
  63. AnimationFrame::AnimationFrame(spNativeTexture t)
  64. {
  65. _row = _column = 0;
  66. _resAnim = 0;
  67. _diffuse.base = t;
  68. _diffuse.premultiplied = true;
  69. _srcRect = RectF(0, 0, 1, 1);
  70. _destRect = RectF(0, 0, (float)t->getWidth(), (float)t->getHeight());
  71. _frameSize = Vector2((float)t->getWidth(), (float)t->getHeight());
  72. }
  73. }