Transform.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /**
  2. * Copyright (c) 2006-2017 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. #include "Transform.h"
  21. namespace love
  22. {
  23. namespace math
  24. {
  25. love::Type Transform::type("Transform", &Object::type);
  26. Transform::Transform()
  27. : matrix()
  28. , inverseDirty(true)
  29. , inverseMatrix()
  30. {
  31. }
  32. Transform::Transform(const Matrix4 &m)
  33. : matrix(m)
  34. , inverseDirty(true)
  35. , inverseMatrix()
  36. {
  37. }
  38. Transform::Transform(float x, float y, float a, float sx, float sy, float ox, float oy, float kx, float ky)
  39. : matrix(x, y, a, sx, sy, ox, oy, kx, ky)
  40. , inverseDirty(true)
  41. , inverseMatrix()
  42. {
  43. }
  44. Transform::~Transform()
  45. {
  46. }
  47. Transform *Transform::clone()
  48. {
  49. return new Transform(*this);
  50. }
  51. Transform *Transform::inverse()
  52. {
  53. return new Transform(getInverseMatrix());
  54. }
  55. void Transform::apply(Transform *other)
  56. {
  57. matrix *= other->getMatrix();
  58. inverseDirty = true;
  59. }
  60. void Transform::translate(float x, float y)
  61. {
  62. matrix.translate(x, y);
  63. inverseDirty = true;
  64. }
  65. void Transform::rotate(float angle)
  66. {
  67. matrix.rotate(angle);
  68. inverseDirty = true;
  69. }
  70. void Transform::scale(float x, float y)
  71. {
  72. matrix.scale(x, y);
  73. inverseDirty = true;
  74. }
  75. void Transform::shear(float x, float y)
  76. {
  77. matrix.shear(x, y);
  78. inverseDirty = true;
  79. }
  80. void Transform::reset()
  81. {
  82. matrix.setIdentity();
  83. inverseDirty = true;
  84. }
  85. void Transform::setTransformation(float x, float y, float a, float sx, float sy, float ox, float oy, float kx, float ky)
  86. {
  87. matrix.setTransformation(x, y, a, sx, sy, ox, oy, kx, ky);
  88. inverseDirty = true;
  89. }
  90. love::Vector2 Transform::transformPoint(love::Vector2 p) const
  91. {
  92. love::Vector2 result;
  93. matrix.transformXY(&result, &p, 1);
  94. return result;
  95. }
  96. love::Vector2 Transform::inverseTransformPoint(love::Vector2 p)
  97. {
  98. love::Vector2 result;
  99. getInverseMatrix().transformXY(&result, &p, 1);
  100. return result;
  101. }
  102. const Matrix4 &Transform::getMatrix() const
  103. {
  104. return matrix;
  105. }
  106. void Transform::setMatrix(const Matrix4 &m)
  107. {
  108. matrix = m;
  109. inverseDirty = true;
  110. }
  111. bool Transform::getConstant(const char *in, MatrixLayout &out)
  112. {
  113. return matrixLayouts.find(in, out);
  114. }
  115. bool Transform::getConstant(MatrixLayout in, const char *&out)
  116. {
  117. return matrixLayouts.find(in, out);
  118. }
  119. StringMap<Transform::MatrixLayout, Transform::MATRIX_MAX_ENUM>::Entry Transform::matrixLayoutEntries[] =
  120. {
  121. { "row", MATRIX_ROW_MAJOR },
  122. { "column", MATRIX_COLUMN_MAJOR },
  123. };
  124. StringMap<Transform::MatrixLayout, Transform::MATRIX_MAX_ENUM> Transform::matrixLayouts(Transform::matrixLayoutEntries, sizeof(Transform::matrixLayoutEntries));
  125. } // math
  126. } // love