AnimationState.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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/Animation.h>
  29. #include <spine/AnimationState.h>
  30. #include <spine/AnimationStateData.h>
  31. #include <spine/Event.h>
  32. #include <spine/extension.h>
  33. #include <spine/Skeleton.h>
  34. #include <spine/SkeletonData.h>
  35. #include <string.h>
  36. spTrackEntry* _spTrackEntry_create () {
  37. spTrackEntry* entry = NEW(spTrackEntry);
  38. entry->timeScale = 1;
  39. entry->lastTime = -1;
  40. return entry;
  41. }
  42. void _spTrackEntry_dispose (spTrackEntry* entry) {
  43. FREE(entry);
  44. }
  45. void _spTrackEntry_disposeAll (spTrackEntry* entry) {
  46. while (entry) {
  47. spTrackEntry* next = entry->next;
  48. _spTrackEntry_dispose(entry);
  49. entry = next;
  50. }
  51. }
  52. /**/
  53. typedef struct {
  54. spAnimationState super;
  55. spEvent** events;
  56. } _spAnimationState;
  57. void _spAnimationState_setCurrent (spAnimationState* self, int index, spTrackEntry* entry);
  58. spAnimationState* spAnimationState_create (spAnimationStateData* data) {
  59. _spAnimationState* internal = NEW(_spAnimationState);
  60. spAnimationState* self = SUPER(internal);
  61. internal->events = MALLOC(spEvent*, 64);
  62. self->timeScale = 1;
  63. CONST_CAST(spAnimationStateData*, self->data) = data;
  64. return self;
  65. }
  66. void spAnimationState_dispose (spAnimationState* self) {
  67. int i;
  68. _spAnimationState* internal = SUB_CAST(_spAnimationState, self);
  69. FREE(internal->events);
  70. for (i = 0; i < self->trackCount; i++)
  71. _spTrackEntry_disposeAll(self->tracks[i]);
  72. FREE(self->tracks);
  73. FREE(self);
  74. }
  75. void spAnimationState_update (spAnimationState* self, float delta) {
  76. int i;
  77. float trackDelta;
  78. delta *= self->timeScale;
  79. for (i = 0; i < self->trackCount; i++) {
  80. spTrackEntry* current = self->tracks[i];
  81. if (!current) continue;
  82. trackDelta = delta * current->timeScale;
  83. current->time += trackDelta;
  84. if (current->previous) {
  85. current->previous->time += trackDelta;
  86. current->mixTime += trackDelta;
  87. }
  88. if (current->next) {
  89. if (current->lastTime >= current->next->delay) _spAnimationState_setCurrent(self, i, current->next);
  90. } else {
  91. /* End non-looping animation when it reaches its end time and there is no next entry. */
  92. if (!current->loop && current->lastTime >= current->endTime) spAnimationState_clearTrack(self, i);
  93. }
  94. }
  95. }
  96. void spAnimationState_apply (spAnimationState* self, spSkeleton* skeleton) {
  97. _spAnimationState* internal = SUB_CAST(_spAnimationState, self);
  98. int i, ii;
  99. int eventCount;
  100. float time;
  101. spTrackEntry* previous;
  102. for (i = 0; i < self->trackCount; i++) {
  103. spTrackEntry* current = self->tracks[i];
  104. if (!current) continue;
  105. eventCount = 0;
  106. time = current->time;
  107. if (!current->loop && time > current->endTime) time = current->endTime;
  108. previous = current->previous;
  109. if (!previous) {
  110. spAnimation_apply(current->animation, skeleton, current->lastTime, time, current->loop, internal->events, &eventCount);
  111. } else {
  112. float alpha = current->mixTime / current->mixDuration;
  113. float previousTime = previous->time;
  114. if (!previous->loop && previousTime > previous->endTime) previousTime = previous->endTime;
  115. spAnimation_apply(previous->animation, skeleton, previousTime, previousTime, previous->loop, 0, 0);
  116. if (alpha >= 1) {
  117. alpha = 1;
  118. _spTrackEntry_dispose(current->previous);
  119. current->previous = 0;
  120. }
  121. spAnimation_mix(current->animation, skeleton, current->lastTime, time, current->loop, internal->events, &eventCount,
  122. alpha);
  123. }
  124. for (ii = 0; ii < eventCount; ii++) {
  125. spEvent* event = internal->events[ii];
  126. if (current->listener) current->listener(self, i, ANIMATION_EVENT, event, 0);
  127. if (self->listener) self->listener(self, i, ANIMATION_EVENT, event, 0);
  128. }
  129. /* Check if completed the animation or a loop iteration. */
  130. if (current->loop ? (FMOD(current->lastTime, current->endTime) > FMOD(time, current->endTime))
  131. : (current->lastTime < current->endTime && time >= current->endTime)) {
  132. int count = (int)(time / current->endTime);
  133. if (current->listener) current->listener(self, i, ANIMATION_COMPLETE, 0, count);
  134. if (self->listener) self->listener(self, i, ANIMATION_COMPLETE, 0, count);
  135. if (i >= self->trackCount || self->tracks[i] != current) continue;
  136. }
  137. if (i >= self->trackCount || self->tracks[i] != current) continue;
  138. current->lastTime = current->time;
  139. }
  140. }
  141. void spAnimationState_clearTracks (spAnimationState* self) {
  142. int i;
  143. for (i = 0; i < self->trackCount; i++)
  144. spAnimationState_clearTrack(self, i);
  145. self->trackCount = 0;
  146. }
  147. void spAnimationState_clearTrack (spAnimationState* self, int trackIndex) {
  148. spTrackEntry* current;
  149. if (trackIndex >= self->trackCount) return;
  150. current = self->tracks[trackIndex];
  151. if (!current) return;
  152. if (current->listener) current->listener(self, trackIndex, ANIMATION_END, 0, 0);
  153. if (self->listener) self->listener(self, trackIndex, ANIMATION_END, 0, 0);
  154. self->tracks[trackIndex] = 0;
  155. if (current->previous) _spTrackEntry_dispose(current->previous);
  156. _spTrackEntry_disposeAll(current);
  157. }
  158. spTrackEntry* _spAnimationState_expandToIndex (spAnimationState* self, int index) {
  159. spTrackEntry** newTracks;
  160. if (index < self->trackCount) return self->tracks[index];
  161. newTracks = CALLOC(spTrackEntry*, index + 1);
  162. memcpy(newTracks, self->tracks, self->trackCount * sizeof(spTrackEntry*));
  163. FREE(self->tracks);
  164. self->tracks = newTracks;
  165. self->trackCount = index + 1;
  166. return 0;
  167. }
  168. void _spAnimationState_setCurrent (spAnimationState* self, int index, spTrackEntry* entry) {
  169. spTrackEntry* current = _spAnimationState_expandToIndex(self, index);
  170. if (current) {
  171. spTrackEntry* previous = current->previous;
  172. current->previous = 0;
  173. if (current->listener) current->listener(self, index, ANIMATION_END, 0, 0);
  174. if (self->listener) self->listener(self, index, ANIMATION_END, 0, 0);
  175. entry->mixDuration = spAnimationStateData_getMix(self->data, current->animation, entry->animation);
  176. if (entry->mixDuration > 0) {
  177. entry->mixTime = 0;
  178. /* If a mix is in progress, mix from the closest animation. */
  179. if (previous && current->mixTime / current->mixDuration < 0.5f) {
  180. entry->previous = previous;
  181. previous = current;
  182. } else
  183. entry->previous = current;
  184. } else
  185. _spTrackEntry_dispose(current);
  186. if (previous) _spTrackEntry_dispose(previous);
  187. }
  188. self->tracks[index] = entry;
  189. if (entry->listener) entry->listener(self, index, ANIMATION_START, 0, 0);
  190. if (self->listener) self->listener(self, index, ANIMATION_START, 0, 0);
  191. }
  192. spTrackEntry* spAnimationState_setAnimationByName (spAnimationState* self, int trackIndex, const char* animationName,
  193. int/*bool*/loop) {
  194. spAnimation* animation = spSkeletonData_findAnimation(self->data->skeletonData, animationName);
  195. return spAnimationState_setAnimation(self, trackIndex, animation, loop);
  196. }
  197. spTrackEntry* spAnimationState_setAnimation (spAnimationState* self, int trackIndex, spAnimation* animation, int/*bool*/loop) {
  198. spTrackEntry* entry;
  199. spTrackEntry* current = _spAnimationState_expandToIndex(self, trackIndex);
  200. if (current) _spTrackEntry_disposeAll(current->next);
  201. entry = _spTrackEntry_create();
  202. entry->animation = animation;
  203. entry->loop = loop;
  204. entry->endTime = animation->duration;
  205. _spAnimationState_setCurrent(self, trackIndex, entry);
  206. return entry;
  207. }
  208. spTrackEntry* spAnimationState_addAnimationByName (spAnimationState* self, int trackIndex, const char* animationName,
  209. int/*bool*/loop, float delay) {
  210. spAnimation* animation = spSkeletonData_findAnimation(self->data->skeletonData, animationName);
  211. return spAnimationState_addAnimation(self, trackIndex, animation, loop, delay);
  212. }
  213. spTrackEntry* spAnimationState_addAnimation (spAnimationState* self, int trackIndex, spAnimation* animation, int/*bool*/loop,
  214. float delay) {
  215. spTrackEntry* last;
  216. spTrackEntry* entry = _spTrackEntry_create();
  217. entry->animation = animation;
  218. entry->loop = loop;
  219. entry->endTime = animation->duration;
  220. last = _spAnimationState_expandToIndex(self, trackIndex);
  221. if (last) {
  222. while (last->next)
  223. last = last->next;
  224. last->next = entry;
  225. } else
  226. self->tracks[trackIndex] = entry;
  227. if (delay <= 0) {
  228. if (last)
  229. delay += last->endTime - spAnimationStateData_getMix(self->data, last->animation, animation);
  230. else
  231. delay = 0;
  232. }
  233. entry->delay = delay;
  234. return entry;
  235. }
  236. spTrackEntry* spAnimationState_getCurrent (spAnimationState* self, int trackIndex) {
  237. if (trackIndex >= self->trackCount) return 0;
  238. return self->tracks[trackIndex];
  239. }