Bone.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /******************************************************************************
  2. * Spine Runtimes Software License
  3. * Version 2
  4. *
  5. * Copyright (c) 2013, Esoteric Software
  6. * All rights reserved.
  7. *
  8. * You are granted a perpetual, non-exclusive, non-sublicensable and
  9. * non-transferable license to install, execute and perform the Spine Runtimes
  10. * Software (the "Software") solely for internal use. Without the written
  11. * permission of Esoteric Software, you may not (a) modify, translate, adapt or
  12. * otherwise create derivative works, improvements of the Software or develop
  13. * new applications using the Software or (b) remove, delete, alter or obscure
  14. * any trademarks or any copyright, trademark, patent or other intellectual
  15. * property or proprietary rights notices on or in the Software, including
  16. * any copy thereof. Redistributions in binary or source form must include
  17. * this license and terms. THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE
  18. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  19. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  20. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTARE BE LIABLE FOR ANY
  21. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. *****************************************************************************/
  28. #include <spine/Bone.h>
  29. #include <spine/extension.h>
  30. static int yDown;
  31. void spBone_setYDown (int value) {
  32. yDown = value;
  33. }
  34. spBone* spBone_create (spBoneData* data, spBone* parent) {
  35. spBone* self = NEW(spBone);
  36. CONST_CAST(spBoneData*, self->data) = data;
  37. CONST_CAST(spBone*, self->parent) = parent;
  38. spBone_setToSetupPose(self);
  39. return self;
  40. }
  41. void spBone_dispose (spBone* self) {
  42. FREE(self);
  43. }
  44. void spBone_setToSetupPose (spBone* self) {
  45. self->x = self->data->x;
  46. self->y = self->data->y;
  47. self->rotation = self->data->rotation;
  48. self->scaleX = self->data->scaleX;
  49. self->scaleY = self->data->scaleY;
  50. }
  51. void spBone_updateWorldTransform (spBone* self, int flipX, int flipY) {
  52. float radians, cosine, sine;
  53. if (self->parent) {
  54. CONST_CAST(float, self->worldX) = self->x * self->parent->m00 + self->y * self->parent->m01 + self->parent->worldX;
  55. CONST_CAST(float, self->worldY) = self->x * self->parent->m10 + self->y * self->parent->m11 + self->parent->worldY;
  56. if (self->data->inheritScale) {
  57. CONST_CAST(float, self->worldScaleX) = self->parent->worldScaleX * self->scaleX;
  58. CONST_CAST(float, self->worldScaleY) = self->parent->worldScaleY * self->scaleY;
  59. } else {
  60. CONST_CAST(float, self->worldScaleX) = self->scaleX;
  61. CONST_CAST(float, self->worldScaleY) = self->scaleY;
  62. }
  63. CONST_CAST(float, self->worldRotation) =
  64. self->data->inheritRotation ? self->parent->worldRotation + self->rotation : self->rotation;
  65. } else {
  66. CONST_CAST(float, self->worldX) = flipX ? -self->x : self->x;
  67. CONST_CAST(float, self->worldY) = flipY != yDown ? -self->y : self->y;
  68. CONST_CAST(float, self->worldScaleX) = self->scaleX;
  69. CONST_CAST(float, self->worldScaleY) = self->scaleY;
  70. CONST_CAST(float, self->worldRotation) = self->rotation;
  71. }
  72. radians = (float)(self->worldRotation * 3.1415926535897932385 / 180);
  73. #ifdef __STDC_VERSION__
  74. cosine = cosf(radians);
  75. sine = sinf(radians);
  76. #else
  77. cosine = (float)cos(radians);
  78. sine = (float)sin(radians);
  79. #endif
  80. CONST_CAST(float, self->m00) = cosine * self->worldScaleX;
  81. CONST_CAST(float, self->m10) = sine * self->worldScaleX;
  82. CONST_CAST(float, self->m01) = -sine * self->worldScaleY;
  83. CONST_CAST(float, self->m11) = cosine * self->worldScaleY;
  84. if (flipX) {
  85. CONST_CAST(float, self->m00) = -self->m00;
  86. CONST_CAST(float, self->m01) = -self->m01;
  87. }
  88. if (flipY != yDown) {
  89. CONST_CAST(float, self->m10) = -self->m10;
  90. CONST_CAST(float, self->m11) = -self->m11;
  91. }
  92. }