Skeleton.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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/Skeleton.h>
  29. #include <string.h>
  30. #include <spine/extension.h>
  31. spSkeleton* spSkeleton_create (spSkeletonData* data) {
  32. int i, ii;
  33. spSkeleton* self = NEW(spSkeleton);
  34. CONST_CAST(spSkeletonData*, self->data) = data;
  35. self->boneCount = self->data->boneCount;
  36. self->bones = MALLOC(spBone*, self->boneCount);
  37. for (i = 0; i < self->boneCount; ++i) {
  38. spBoneData* boneData = self->data->bones[i];
  39. spBone* parent = 0;
  40. if (boneData->parent) {
  41. /* Find parent bone. */
  42. for (ii = 0; ii < self->boneCount; ++ii) {
  43. if (data->bones[ii] == boneData->parent) {
  44. parent = self->bones[ii];
  45. break;
  46. }
  47. }
  48. }
  49. self->bones[i] = spBone_create(boneData, parent);
  50. }
  51. CONST_CAST(spBone*, self->root) = self->bones[0];
  52. self->slotCount = data->slotCount;
  53. self->slots = MALLOC(spSlot*, self->slotCount);
  54. for (i = 0; i < self->slotCount; ++i) {
  55. spSlotData *slotData = data->slots[i];
  56. /* Find bone for the slotData's boneData. */
  57. spBone* bone = 0;
  58. for (ii = 0; ii < self->boneCount; ++ii) {
  59. if (data->bones[ii] == slotData->boneData) {
  60. bone = self->bones[ii];
  61. break;
  62. }
  63. }
  64. self->slots[i] = spSlot_create(slotData, self, bone);
  65. }
  66. self->drawOrder = MALLOC(spSlot*, self->slotCount);
  67. memcpy(self->drawOrder, self->slots, sizeof(spSlot*) * self->slotCount);
  68. self->r = 1;
  69. self->g = 1;
  70. self->b = 1;
  71. self->a = 1;
  72. return self;
  73. }
  74. void spSkeleton_dispose (spSkeleton* self) {
  75. int i;
  76. for (i = 0; i < self->boneCount; ++i)
  77. spBone_dispose(self->bones[i]);
  78. FREE(self->bones);
  79. for (i = 0; i < self->slotCount; ++i)
  80. spSlot_dispose(self->slots[i]);
  81. FREE(self->slots);
  82. FREE(self->drawOrder);
  83. FREE(self);
  84. }
  85. void spSkeleton_updateWorldTransform (const spSkeleton* self) {
  86. int i;
  87. for (i = 0; i < self->boneCount; ++i)
  88. spBone_updateWorldTransform(self->bones[i], self->flipX, self->flipY);
  89. }
  90. void spSkeleton_setToSetupPose (const spSkeleton* self) {
  91. spSkeleton_setBonesToSetupPose(self);
  92. spSkeleton_setSlotsToSetupPose(self);
  93. }
  94. void spSkeleton_setBonesToSetupPose (const spSkeleton* self) {
  95. int i;
  96. for (i = 0; i < self->boneCount; ++i)
  97. spBone_setToSetupPose(self->bones[i]);
  98. }
  99. void spSkeleton_setSlotsToSetupPose (const spSkeleton* self) {
  100. int i;
  101. memcpy(self->drawOrder, self->slots, self->slotCount * sizeof(spSlot*));
  102. for (i = 0; i < self->slotCount; ++i)
  103. spSlot_setToSetupPose(self->slots[i]);
  104. }
  105. spBone* spSkeleton_findBone (const spSkeleton* self, const char* boneName) {
  106. int i;
  107. for (i = 0; i < self->boneCount; ++i)
  108. if (strcmp(self->data->bones[i]->name, boneName) == 0) return self->bones[i];
  109. return 0;
  110. }
  111. int spSkeleton_findBoneIndex (const spSkeleton* self, const char* boneName) {
  112. int i;
  113. for (i = 0; i < self->boneCount; ++i)
  114. if (strcmp(self->data->bones[i]->name, boneName) == 0) return i;
  115. return -1;
  116. }
  117. spSlot* spSkeleton_findSlot (const spSkeleton* self, const char* slotName) {
  118. int i;
  119. for (i = 0; i < self->slotCount; ++i)
  120. if (strcmp(self->data->slots[i]->name, slotName) == 0) return self->slots[i];
  121. return 0;
  122. }
  123. int spSkeleton_findSlotIndex (const spSkeleton* self, const char* slotName) {
  124. int i;
  125. for (i = 0; i < self->slotCount; ++i)
  126. if (strcmp(self->data->slots[i]->name, slotName) == 0) return i;
  127. return -1;
  128. }
  129. int spSkeleton_setSkinByName (spSkeleton* self, const char* skinName) {
  130. spSkin *skin;
  131. if (!skinName) {
  132. spSkeleton_setSkin(self, 0);
  133. return 1;
  134. }
  135. skin = spSkeletonData_findSkin(self->data, skinName);
  136. if (!skin) return 0;
  137. spSkeleton_setSkin(self, skin);
  138. return 1;
  139. }
  140. void spSkeleton_setSkin (spSkeleton* self, spSkin* newSkin) {
  141. if (self->skin && newSkin) spSkin_attachAll(newSkin, self, self->skin);
  142. CONST_CAST(spSkin*, self->skin) = newSkin;
  143. }
  144. spAttachment* spSkeleton_getAttachmentForSlotName (const spSkeleton* self, const char* slotName, const char* attachmentName) {
  145. int slotIndex = spSkeletonData_findSlotIndex(self->data, slotName);
  146. return spSkeleton_getAttachmentForSlotIndex(self, slotIndex, attachmentName);
  147. }
  148. spAttachment* spSkeleton_getAttachmentForSlotIndex (const spSkeleton* self, int slotIndex, const char* attachmentName) {
  149. if (slotIndex == -1) return 0;
  150. if (self->skin) {
  151. spAttachment *attachment = spSkin_getAttachment(self->skin, slotIndex, attachmentName);
  152. if (attachment) return attachment;
  153. }
  154. if (self->data->defaultSkin) {
  155. spAttachment *attachment = spSkin_getAttachment(self->data->defaultSkin, slotIndex, attachmentName);
  156. if (attachment) return attachment;
  157. }
  158. return 0;
  159. }
  160. int spSkeleton_setAttachment (spSkeleton* self, const char* slotName, const char* attachmentName) {
  161. int i;
  162. for (i = 0; i < self->slotCount; ++i) {
  163. spSlot *slot = self->slots[i];
  164. if (strcmp(slot->data->name, slotName) == 0) {
  165. if (!attachmentName)
  166. spSlot_setAttachment(slot, 0);
  167. else {
  168. spAttachment* attachment = spSkeleton_getAttachmentForSlotIndex(self, i, attachmentName);
  169. if (!attachment) return 0;
  170. spSlot_setAttachment(slot, attachment);
  171. }
  172. return 1;
  173. }
  174. }
  175. return 0;
  176. }
  177. void spSkeleton_update (spSkeleton* self, float deltaTime) {
  178. self->time += deltaTime;
  179. }