AnimationState.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  1. /******************************************************************************
  2. * Spine Runtimes Software License v2.5
  3. *
  4. * Copyright (c) 2013-2016, Esoteric Software
  5. * All rights reserved.
  6. *
  7. * You are granted a perpetual, non-exclusive, non-sublicensable, and
  8. * non-transferable license to use, install, execute, and perform the Spine
  9. * Runtimes software and derivative works solely for personal or internal
  10. * use. Without the written permission of Esoteric Software (see Section 2 of
  11. * the Spine Software License Agreement), you may not (a) modify, translate,
  12. * adapt, or develop new applications using the Spine Runtimes or otherwise
  13. * create derivative works or improvements of the Spine Runtimes or (b) remove,
  14. * delete, alter, or obscure any trademarks or any copyright, trademark, patent,
  15. * or other intellectual property or proprietary rights notices on or in the
  16. * Software, including any copy thereof. Redistributions in binary or source
  17. * form must include this license and terms.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE "AS IS" AND ANY EXPRESS OR
  20. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  22. * EVENT SHALL ESOTERIC SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, BUSINESS INTERRUPTION, OR LOSS OF
  25. * USE, DATA, OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  26. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. *****************************************************************************/
  30. #include <spine/AnimationState.h>
  31. #include <spine/extension.h>
  32. #include <limits.h>
  33. static spAnimation* SP_EMPTY_ANIMATION = 0;
  34. void spAnimationState_disposeStatics () {
  35. if (SP_EMPTY_ANIMATION) spAnimation_dispose(SP_EMPTY_ANIMATION);
  36. SP_EMPTY_ANIMATION = 0;
  37. }
  38. /* Forward declaration of some "private" functions so we can keep
  39. the same function order in C as we have method order in Java */
  40. void _spAnimationState_disposeTrackEntry (spTrackEntry* entry);
  41. void _spAnimationState_disposeTrackEntries (spAnimationState* state, spTrackEntry* entry);
  42. void _spAnimationState_updateMixingFrom (spAnimationState* self, spTrackEntry* entry, float delta);
  43. float _spAnimationState_applyMixingFrom (spAnimationState* self, spTrackEntry* entry, spSkeleton* skeleton);
  44. void _spAnimationState_applyRotateTimeline (spAnimationState* self, spTimeline* timeline, spSkeleton* skeleton, float time, float alpha, int /*boolean*/ setupPose, float* timelinesRotation, int i, int /*boolean*/ firstFrame);
  45. void _spAnimationState_queueEvents (spAnimationState* self, spTrackEntry* entry, float animationTime);
  46. void _spAnimationState_setCurrent (spAnimationState* self, int index, spTrackEntry* current, int /*boolean*/ interrupt);
  47. spTrackEntry* _spAnimationState_expandToIndex (spAnimationState* self, int index);
  48. spTrackEntry* _spAnimationState_trackEntry (spAnimationState* self, int trackIndex, spAnimation* animation, int /*boolean*/ loop, spTrackEntry* last);
  49. void _spAnimationState_disposeNext (spAnimationState* self, spTrackEntry* entry);
  50. void _spAnimationState_animationsChanged (spAnimationState* self);
  51. float* _spAnimationState_resizeTimelinesRotation(spTrackEntry* entry, int newSize);
  52. int* _spAnimationState_resizeTimelinesFirst(spTrackEntry* entry, int newSize);
  53. void _spAnimationState_ensureCapacityPropertyIDs(spAnimationState* self, int capacity);
  54. int _spAnimationState_addPropertyID(spAnimationState* self, int id);
  55. void _spAnimationState_setTimelinesFirst (spAnimationState* self, spTrackEntry* entry);
  56. void _spAnimationState_checkTimelinesFirst (spAnimationState* self, spTrackEntry* entry);
  57. void _spAnimationState_checkTimelinesUsage (spAnimationState* self, spTrackEntry* entry);
  58. _spEventQueue* _spEventQueue_create (_spAnimationState* state) {
  59. _spEventQueue *self = CALLOC(_spEventQueue, 1);
  60. self->state = state;
  61. self->objectsCount = 0;
  62. self->objectsCapacity = 16;
  63. self->objects = CALLOC(_spEventQueueItem, self->objectsCapacity);
  64. self->drainDisabled = 0;
  65. return self;
  66. }
  67. void _spEventQueue_free (_spEventQueue* self) {
  68. FREE(self->objects);
  69. FREE(self);
  70. }
  71. void _spEventQueue_ensureCapacity (_spEventQueue* self, int newElements) {
  72. if (self->objectsCount + newElements > self->objectsCapacity) {
  73. _spEventQueueItem* newObjects;
  74. self->objectsCapacity <<= 1;
  75. newObjects = CALLOC(_spEventQueueItem, self->objectsCapacity);
  76. memcpy(newObjects, self->objects, sizeof(_spEventQueueItem) * self->objectsCount);
  77. FREE(self->objects);
  78. self->objects = newObjects;
  79. }
  80. }
  81. void _spEventQueue_addType (_spEventQueue* self, spEventType type) {
  82. _spEventQueue_ensureCapacity(self, 1);
  83. self->objects[self->objectsCount++].type = type;
  84. }
  85. void _spEventQueue_addEntry (_spEventQueue* self, spTrackEntry* entry) {
  86. _spEventQueue_ensureCapacity(self, 1);
  87. self->objects[self->objectsCount++].entry = entry;
  88. }
  89. void _spEventQueue_addEvent (_spEventQueue* self, spEvent* event) {
  90. _spEventQueue_ensureCapacity(self, 1);
  91. self->objects[self->objectsCount++].event = event;
  92. }
  93. void _spEventQueue_start (_spEventQueue* self, spTrackEntry* entry) {
  94. _spEventQueue_addType(self, SP_ANIMATION_START);
  95. _spEventQueue_addEntry(self, entry);
  96. self->state->animationsChanged = 1;
  97. }
  98. void _spEventQueue_interrupt (_spEventQueue* self, spTrackEntry* entry) {
  99. _spEventQueue_addType(self, SP_ANIMATION_INTERRUPT);
  100. _spEventQueue_addEntry(self, entry);
  101. }
  102. void _spEventQueue_end (_spEventQueue* self, spTrackEntry* entry) {
  103. _spEventQueue_addType(self, SP_ANIMATION_END);
  104. _spEventQueue_addEntry(self, entry);
  105. self->state->animationsChanged = 1;
  106. }
  107. void _spEventQueue_dispose (_spEventQueue* self, spTrackEntry* entry) {
  108. _spEventQueue_addType(self, SP_ANIMATION_DISPOSE);
  109. _spEventQueue_addEntry(self, entry);
  110. }
  111. void _spEventQueue_complete (_spEventQueue* self, spTrackEntry* entry) {
  112. _spEventQueue_addType(self, SP_ANIMATION_COMPLETE);
  113. _spEventQueue_addEntry(self, entry);
  114. }
  115. void _spEventQueue_event (_spEventQueue* self, spTrackEntry* entry, spEvent* event) {
  116. _spEventQueue_addType(self, SP_ANIMATION_EVENT);
  117. _spEventQueue_addEntry(self, entry);
  118. _spEventQueue_addEvent(self, event);
  119. }
  120. void _spEventQueue_clear (_spEventQueue* self) {
  121. self->objectsCount = 0;
  122. }
  123. void _spEventQueue_drain (_spEventQueue* self) {
  124. int i;
  125. if (self->drainDisabled) return;
  126. self->drainDisabled = 1;
  127. for (i = 0; i < self->objectsCount; i += 2) {
  128. spEventType type = self->objects[i].type;
  129. spTrackEntry* entry = self->objects[i+1].entry;
  130. spEvent* event;
  131. switch (type) {
  132. case SP_ANIMATION_START:
  133. case SP_ANIMATION_INTERRUPT:
  134. case SP_ANIMATION_COMPLETE:
  135. if (entry->listener) entry->listener(SUPER(self->state), type, entry, 0);
  136. if (self->state->super.listener) self->state->super.listener(SUPER(self->state), type, entry, 0);
  137. break;
  138. case SP_ANIMATION_END:
  139. if (entry->listener) entry->listener(SUPER(self->state), type, entry, 0);
  140. if (self->state->super.listener) self->state->super.listener(SUPER(self->state), type, entry, 0);
  141. /* Fall through. */
  142. case SP_ANIMATION_DISPOSE:
  143. if (entry->listener) entry->listener(SUPER(self->state), SP_ANIMATION_DISPOSE, entry, 0);
  144. if (self->state->super.listener) self->state->super.listener(SUPER(self->state), SP_ANIMATION_DISPOSE, entry, 0);
  145. _spAnimationState_disposeTrackEntry(entry);
  146. break;
  147. case SP_ANIMATION_EVENT:
  148. event = self->objects[i+2].event;
  149. if (entry->listener) entry->listener(SUPER(self->state), type, entry, event);
  150. if (self->state->super.listener) self->state->super.listener(SUPER(self->state), type, entry, event);
  151. i++;
  152. break;
  153. }
  154. }
  155. _spEventQueue_clear(self);
  156. self->drainDisabled = 0;
  157. }
  158. void _spAnimationState_disposeTrackEntry (spTrackEntry* entry) {
  159. FREE(entry->timelinesFirst);
  160. FREE(entry->timelinesRotation);
  161. FREE(entry);
  162. }
  163. void _spAnimationState_disposeTrackEntries (spAnimationState* state, spTrackEntry* entry) {
  164. while (entry) {
  165. spTrackEntry* next = entry->next;
  166. spTrackEntry* from = entry->mixingFrom;
  167. while (from) {
  168. spTrackEntry* nextFrom = from->mixingFrom;
  169. _spAnimationState_disposeTrackEntry(from);
  170. from = nextFrom;
  171. }
  172. _spAnimationState_disposeTrackEntry(entry);
  173. entry = next;
  174. }
  175. }
  176. spAnimationState* spAnimationState_create (spAnimationStateData* data) {
  177. _spAnimationState* internal;
  178. spAnimationState* self;
  179. if (!SP_EMPTY_ANIMATION) {
  180. SP_EMPTY_ANIMATION = (spAnimation*)1; /* dirty trick so we can recursively call spAnimation_create */
  181. SP_EMPTY_ANIMATION = spAnimation_create("<empty>", 0);
  182. }
  183. internal = NEW(_spAnimationState);
  184. self = SUPER(internal);
  185. CONST_CAST(spAnimationStateData*, self->data) = data;
  186. self->timeScale = 1;
  187. internal->queue = _spEventQueue_create(internal);
  188. internal->events = CALLOC(spEvent*, 128);
  189. internal->propertyIDs = CALLOC(int, 128);
  190. internal->propertyIDsCapacity = 128;
  191. return self;
  192. }
  193. void spAnimationState_dispose (spAnimationState* self) {
  194. int i;
  195. _spAnimationState* internal = SUB_CAST(_spAnimationState, self);
  196. for (i = 0; i < self->tracksCount; i++)
  197. _spAnimationState_disposeTrackEntries(self, self->tracks[i]);
  198. FREE(self->tracks);
  199. _spEventQueue_free(internal->queue);
  200. FREE(internal->events);
  201. FREE(internal->propertyIDs);
  202. FREE(internal);
  203. }
  204. void spAnimationState_update (spAnimationState* self, float delta) {
  205. int i, n;
  206. _spAnimationState* internal = SUB_CAST(_spAnimationState, self);
  207. delta *= self->timeScale;
  208. for (i = 0, n = self->tracksCount; i < n; i++) {
  209. float currentDelta;
  210. spTrackEntry* current = self->tracks[i];
  211. spTrackEntry* next;
  212. if (!current) continue;
  213. current->animationLast = current->nextAnimationLast;
  214. current->trackLast = current->nextTrackLast;
  215. currentDelta = delta * current->timeScale;
  216. if (current->delay > 0) {
  217. current->delay -= currentDelta;
  218. if (current->delay > 0) continue;
  219. currentDelta = -current->delay;
  220. current->delay = 0;
  221. }
  222. next = current->next;
  223. if (next) {
  224. /* When the next entry's delay is passed, change to the next entry, preserving leftover time. */
  225. float nextTime = current->trackLast - next->delay;
  226. if (nextTime >= 0) {
  227. next->delay = 0;
  228. next->trackTime = nextTime + delta * next->timeScale;
  229. current->trackTime += currentDelta;
  230. _spAnimationState_setCurrent(self, i, next, 1);
  231. while (next->mixingFrom) {
  232. next->mixTime += currentDelta;
  233. next = next->mixingFrom;
  234. }
  235. continue;
  236. }
  237. } else {
  238. /* Clear the track when there is no next entry, the track end time is reached, and there is no mixingFrom. */
  239. if (current->trackLast >= current->trackEnd && current->mixingFrom == 0) {
  240. self->tracks[i] = 0;
  241. _spEventQueue_end(internal->queue, current);
  242. _spAnimationState_disposeNext(self, current);
  243. continue;
  244. }
  245. }
  246. _spAnimationState_updateMixingFrom(self, current, delta);
  247. current->trackTime += currentDelta;
  248. }
  249. _spEventQueue_drain(internal->queue);
  250. }
  251. void _spAnimationState_updateMixingFrom (spAnimationState* self, spTrackEntry* entry, float delta) {
  252. spTrackEntry* from = entry->mixingFrom;
  253. _spAnimationState* internal = SUB_CAST(_spAnimationState, self);
  254. if (!from) return;
  255. _spAnimationState_updateMixingFrom(self, from, delta);
  256. if (entry->mixTime >= entry->mixDuration && from->mixingFrom == 0 && entry->mixTime > 0) {
  257. entry->mixingFrom = 0;
  258. _spEventQueue_end(internal->queue, from);
  259. return;
  260. }
  261. from->animationLast = from->nextAnimationLast;
  262. from->trackLast = from->nextTrackLast;
  263. from->trackTime += delta * from->timeScale;
  264. entry->mixTime += delta * entry->timeScale;
  265. }
  266. void spAnimationState_apply (spAnimationState* self, spSkeleton* skeleton) {
  267. _spAnimationState* internal = SUB_CAST(_spAnimationState, self);
  268. spTrackEntry* current;
  269. int i, ii, n;
  270. float animationLast, animationTime;
  271. int timelineCount;
  272. spTimeline** timelines;
  273. int /*boolean*/ firstFrame;
  274. float* timelinesRotation;
  275. int* timelinesFirst;
  276. spTimeline* timeline;
  277. if (internal->animationsChanged) _spAnimationState_animationsChanged(self);
  278. for (i = 0, n = self->tracksCount; i < n; i++) {
  279. float mix;
  280. current = self->tracks[i];
  281. if (!current || current->delay > 0) continue;
  282. /* Apply mixing from entries first. */
  283. mix = current->alpha;
  284. if (current->mixingFrom)
  285. mix *= _spAnimationState_applyMixingFrom(self, current, skeleton);
  286. else if (current->trackTime >= current->trackEnd)
  287. mix = 0;
  288. /* Apply current entry. */
  289. animationLast = current->animationLast; animationTime = spTrackEntry_getAnimationTime(current);
  290. timelineCount = current->animation->timelinesCount;
  291. timelines = current->animation->timelines;
  292. if (mix == 1) {
  293. for (ii = 0; ii < timelineCount; ii++)
  294. spTimeline_apply(timelines[ii], skeleton, animationLast, animationTime, internal->events, &internal->eventsCount, 1, 1, 0);
  295. } else {
  296. firstFrame = current->timelinesRotationCount == 0;
  297. if (firstFrame) _spAnimationState_resizeTimelinesRotation(current, timelineCount << 1);
  298. timelinesRotation = current->timelinesRotation;
  299. timelinesFirst = current->timelinesFirst;
  300. for (ii = 0; ii < timelineCount; ii++) {
  301. timeline = timelines[ii];
  302. if (timeline->type == SP_TIMELINE_ROTATE)
  303. _spAnimationState_applyRotateTimeline(self, timeline, skeleton, animationTime, mix, timelinesFirst[ii], timelinesRotation, ii << 1, firstFrame);
  304. else
  305. spTimeline_apply(timeline, skeleton, animationLast, animationTime, internal->events, &internal->eventsCount, mix, timelinesFirst[ii], 0);
  306. }
  307. }
  308. _spAnimationState_queueEvents(self, current, animationTime);
  309. internal->eventsCount = 0;
  310. current->nextAnimationLast = animationTime;
  311. current->nextTrackLast = current->trackTime;
  312. }
  313. _spEventQueue_drain(internal->queue);
  314. }
  315. float _spAnimationState_applyMixingFrom (spAnimationState* self, spTrackEntry* entry, spSkeleton* skeleton) {
  316. _spAnimationState* internal = SUB_CAST(_spAnimationState, self);
  317. float mix;
  318. spEvent** events;
  319. int /*boolean*/ attachments;
  320. int /*boolean*/ drawOrder;
  321. float animationLast;
  322. float animationTime;
  323. int timelineCount;
  324. spTimeline** timelines;
  325. int* timelinesFirst;
  326. float alpha;
  327. int /*boolean*/ firstFrame;
  328. float* timelinesRotation;
  329. spTimeline* timeline;
  330. int /*boolean*/ setupPose;
  331. int i;
  332. spTrackEntry* from = entry->mixingFrom;
  333. if (from->mixingFrom) _spAnimationState_applyMixingFrom(self, from, skeleton);
  334. if (entry->mixDuration == 0) /* Single frame mix to undo mixingFrom changes. */
  335. mix = 1;
  336. else {
  337. mix = entry->mixTime / entry->mixDuration;
  338. if (mix > 1) mix = 1;
  339. }
  340. events = mix < from->eventThreshold ? internal->events : 0;
  341. attachments = mix < from->attachmentThreshold;
  342. drawOrder = mix < from->drawOrderThreshold;
  343. animationLast = from->animationLast;
  344. animationTime = spTrackEntry_getAnimationTime(from);
  345. timelineCount = from->animation->timelinesCount;
  346. timelines = from->animation->timelines;
  347. timelinesFirst = from->timelinesFirst;
  348. alpha = from->alpha * entry->mixAlpha * (1 - mix);
  349. firstFrame = from->timelinesRotationCount == 0;
  350. if (firstFrame) _spAnimationState_resizeTimelinesRotation(from, timelineCount << 1);
  351. timelinesRotation = from->timelinesRotation;
  352. for (i = 0; i < timelineCount; i++) {
  353. timeline = timelines[i];
  354. setupPose = timelinesFirst[i];
  355. if (timeline->type == SP_TIMELINE_ROTATE)
  356. _spAnimationState_applyRotateTimeline(self, timeline, skeleton, animationTime, alpha, setupPose, timelinesRotation, i << 1, firstFrame);
  357. else {
  358. if (!setupPose) {
  359. if (!attachments && timeline->type == SP_TIMELINE_ATTACHMENT) continue;
  360. if (!drawOrder && timeline->type == SP_TIMELINE_DRAWORDER) continue;
  361. }
  362. spTimeline_apply(timeline, skeleton, animationLast, animationTime, events, &internal->eventsCount, alpha, setupPose, 1);
  363. }
  364. }
  365. if (entry->mixDuration > 0) _spAnimationState_queueEvents(self, from, animationTime);
  366. internal->eventsCount = 0;
  367. from->nextAnimationLast = animationTime;
  368. from->nextTrackLast = from->trackTime;
  369. return mix;
  370. }
  371. void _spAnimationState_applyRotateTimeline (spAnimationState* self, spTimeline* timeline, spSkeleton* skeleton, float time, float alpha, int /*boolean*/ setupPose, float* timelinesRotation, int i, int /*boolean*/ firstFrame) {
  372. spRotateTimeline *rotateTimeline;
  373. float *frames;
  374. spBone* bone;
  375. float r1, r2;
  376. int frame;
  377. float prevRotation;
  378. float frameTime;
  379. float percent;
  380. float total, diff;
  381. int /*boolean*/ current, dir;
  382. if (firstFrame) timelinesRotation[i] = 0;
  383. if (alpha == 1) {
  384. spTimeline_apply(timeline, skeleton, 0, time, 0, 0, 1, setupPose, 0);
  385. return;
  386. }
  387. rotateTimeline = SUB_CAST(spRotateTimeline, timeline);
  388. frames = rotateTimeline->frames;
  389. bone = skeleton->bones[rotateTimeline->boneIndex];
  390. if (time < frames[0]) {
  391. if (setupPose) {
  392. bone->rotation = bone->data->rotation;
  393. }
  394. return; /* Time is before first frame. */
  395. }
  396. if (time >= frames[rotateTimeline->framesCount - ROTATE_ENTRIES]) /* Time is after last frame. */
  397. r2 = bone->data->rotation + frames[rotateTimeline->framesCount + ROTATE_PREV_ROTATION];
  398. else {
  399. /* Interpolate between the previous frame and the current frame. */
  400. frame = _spCurveTimeline_binarySearch(frames, rotateTimeline->framesCount, time, ROTATE_ENTRIES);
  401. prevRotation = frames[frame + ROTATE_PREV_ROTATION];
  402. frameTime = frames[frame];
  403. percent = spCurveTimeline_getCurvePercent(SUPER(rotateTimeline), (frame >> 1) - 1,
  404. 1 - (time - frameTime) / (frames[frame + ROTATE_PREV_TIME] - frameTime));
  405. r2 = frames[frame + ROTATE_ROTATION] - prevRotation;
  406. r2 -= (16384 - (int)(16384.499999999996 - r2 / 360)) * 360;
  407. r2 = prevRotation + r2 * percent + bone->data->rotation;
  408. r2 -= (16384 - (int)(16384.499999999996 - r2 / 360)) * 360;
  409. }
  410. /* Mix between rotations using the direction of the shortest route on the first frame while detecting crosses. */
  411. r1 = setupPose ? bone->data->rotation : bone->rotation;
  412. diff = r2 - r1;
  413. if (diff == 0) {
  414. total = timelinesRotation[i];
  415. } else {
  416. float lastTotal, lastDiff;
  417. diff -= (16384 - (int)(16384.499999999996 - diff / 360)) * 360;
  418. if (firstFrame) {
  419. lastTotal = 0;
  420. lastDiff = diff;
  421. } else {
  422. lastTotal = timelinesRotation[i]; /* Angle and direction of mix, including loops. */
  423. lastDiff = timelinesRotation[i + 1]; /* Difference between bones. */
  424. }
  425. current = diff > 0;
  426. dir = lastTotal >= 0;
  427. /* Detect cross at 0 (not 180). */
  428. if (SIGNUM(lastDiff) != SIGNUM(diff) && ABS(lastDiff) <= 90) {
  429. /* A cross after a 360 rotation is a loop. */
  430. if (ABS(lastTotal) > 180) lastTotal += 360 * SIGNUM(lastTotal);
  431. dir = current;
  432. }
  433. total = diff + lastTotal - FMOD(lastTotal, 360); /* Store loops as part of lastTotal. */
  434. if (dir != current) total += 360 * SIGNUM(lastTotal);
  435. timelinesRotation[i] = total;
  436. }
  437. timelinesRotation[i + 1] = diff;
  438. r1 += total * alpha;
  439. bone->rotation = r1 - (16384 - (int)(16384.499999999996 - r1 / 360)) * 360;
  440. }
  441. void _spAnimationState_queueEvents (spAnimationState* self, spTrackEntry* entry, float animationTime) {
  442. spEvent** events;
  443. spEvent* event;
  444. _spAnimationState* internal = SUB_CAST(_spAnimationState, self);
  445. int i, n;
  446. float animationStart = entry->animationStart, animationEnd = entry->animationEnd;
  447. float duration = animationEnd - animationStart;
  448. float trackLastWrapped = FMOD(entry->trackLast, duration);
  449. /* Queue events before complete. */
  450. events = internal->events;
  451. for (i = 0, n = internal->eventsCount; i < n; i++) {
  452. event = events[i];
  453. if (event->time < trackLastWrapped) break;
  454. if (event->time > animationEnd) continue; /* Discard events outside animation start/end. */
  455. _spEventQueue_event(internal->queue, entry, event);
  456. }
  457. /* Queue complete if completed a loop iteration or the animation. */
  458. if (entry->loop ? (trackLastWrapped > FMOD(entry->trackTime, duration))
  459. : (animationTime >= animationEnd && entry->animationLast < animationEnd)) {
  460. _spEventQueue_complete(internal->queue, entry);
  461. }
  462. /* Queue events after complete. */
  463. for (; i < n; i++) {
  464. event = events[i];
  465. if (event->time < animationStart) continue; /* Discard events outside animation start/end. */
  466. _spEventQueue_event(internal->queue, entry, event);
  467. }
  468. }
  469. void spAnimationState_clearTracks (spAnimationState* self) {
  470. _spAnimationState* internal = SUB_CAST(_spAnimationState, self);
  471. int i, n;
  472. internal->queue->drainDisabled = 1;
  473. for (i = 0, n = self->tracksCount; i < n; i++)
  474. spAnimationState_clearTrack(self, i);
  475. self->tracksCount = 0;
  476. internal->queue->drainDisabled = 0;
  477. _spEventQueue_drain(internal->queue);
  478. }
  479. void spAnimationState_clearTrack (spAnimationState* self, int trackIndex) {
  480. spTrackEntry* current;
  481. spTrackEntry* entry;
  482. spTrackEntry* from;
  483. _spAnimationState* internal = SUB_CAST(_spAnimationState, self);
  484. if (trackIndex >= self->tracksCount) return;
  485. current = self->tracks[trackIndex];
  486. if (!current) return;
  487. _spEventQueue_end(internal->queue, current);
  488. _spAnimationState_disposeNext(self, current);
  489. entry = current;
  490. while (1) {
  491. from = entry->mixingFrom;
  492. if (!from) break;
  493. _spEventQueue_end(internal->queue, from);
  494. entry->mixingFrom = 0;
  495. entry = from;
  496. }
  497. self->tracks[current->trackIndex] = 0;
  498. _spEventQueue_drain(internal->queue);
  499. }
  500. void _spAnimationState_setCurrent (spAnimationState* self, int index, spTrackEntry* current, int /*boolean*/ interrupt) {
  501. _spAnimationState* internal = SUB_CAST(_spAnimationState, self);
  502. spTrackEntry* from = _spAnimationState_expandToIndex(self, index);
  503. self->tracks[index] = current;
  504. if (from) {
  505. if (interrupt) _spEventQueue_interrupt(internal->queue, from);
  506. current->mixingFrom = from;
  507. current->mixTime = 0;
  508. from->timelinesRotationCount = 0;
  509. /* If not completely mixed in, set mixAlpha so mixing out happens from current mix to zero. */
  510. if (from->mixingFrom && from->mixDuration > 0) current->mixAlpha *= MIN(from->mixTime / from->mixDuration, 1);
  511. }
  512. _spEventQueue_start(internal->queue, current);
  513. }
  514. /** Set the current animation. Any queued animations are cleared. */
  515. spTrackEntry* spAnimationState_setAnimationByName (spAnimationState* self, int trackIndex, const char* animationName,
  516. int/*bool*/loop) {
  517. spAnimation* animation = spSkeletonData_findAnimation(self->data->skeletonData, animationName);
  518. return spAnimationState_setAnimation(self, trackIndex, animation, loop);
  519. }
  520. spTrackEntry* spAnimationState_setAnimation (spAnimationState* self, int trackIndex, spAnimation* animation, int/*bool*/loop) {
  521. spTrackEntry* entry;
  522. _spAnimationState* internal = SUB_CAST(_spAnimationState, self);
  523. int interrupt = 1;
  524. spTrackEntry* current = _spAnimationState_expandToIndex(self, trackIndex);
  525. if (current) {
  526. if (current->nextTrackLast == -1) {
  527. /* Don't mix from an entry that was never applied. */
  528. self->tracks[trackIndex] = current->mixingFrom;
  529. _spEventQueue_interrupt(internal->queue, current);
  530. _spEventQueue_end(internal->queue, current);
  531. _spAnimationState_disposeNext(self, current);
  532. current = current->mixingFrom;
  533. interrupt = 0;
  534. } else
  535. _spAnimationState_disposeNext(self, current);
  536. }
  537. entry = _spAnimationState_trackEntry(self, trackIndex, animation, loop, current);
  538. _spAnimationState_setCurrent(self, trackIndex, entry, interrupt);
  539. _spEventQueue_drain(internal->queue);
  540. return entry;
  541. }
  542. /** Adds an animation to be played delay seconds after the current or last queued animation, taking into account any mix
  543. * duration. */
  544. spTrackEntry* spAnimationState_addAnimationByName (spAnimationState* self, int trackIndex, const char* animationName,
  545. int/*bool*/loop, float delay) {
  546. spAnimation* animation = spSkeletonData_findAnimation(self->data->skeletonData, animationName);
  547. return spAnimationState_addAnimation(self, trackIndex, animation, loop, delay);
  548. }
  549. spTrackEntry* spAnimationState_addAnimation (spAnimationState* self, int trackIndex, spAnimation* animation, int/*bool*/loop,
  550. float delay) {
  551. spTrackEntry* entry;
  552. _spAnimationState* internal = SUB_CAST(_spAnimationState, self);
  553. spTrackEntry* last = _spAnimationState_expandToIndex(self, trackIndex);
  554. if (last) {
  555. while (last->next)
  556. last = last->next;
  557. }
  558. entry = _spAnimationState_trackEntry(self, trackIndex, animation, loop, last);
  559. if (!last) {
  560. _spAnimationState_setCurrent(self, trackIndex, entry, 1);
  561. _spEventQueue_drain(internal->queue);
  562. } else {
  563. last->next = entry;
  564. if (delay <= 0) {
  565. float duration = last->animationEnd - last->animationStart;
  566. if (duration != 0)
  567. delay += duration * (1 + (int)(last->trackTime / duration)) - spAnimationStateData_getMix(self->data, last->animation, animation);
  568. else
  569. delay = 0;
  570. }
  571. }
  572. entry->delay = delay;
  573. return entry;
  574. }
  575. spTrackEntry* spAnimationState_setEmptyAnimation(spAnimationState* self, int trackIndex, float mixDuration) {
  576. spTrackEntry* entry = spAnimationState_setAnimation(self, trackIndex, SP_EMPTY_ANIMATION, 0);
  577. entry->mixDuration = mixDuration;
  578. entry->trackEnd = mixDuration;
  579. return entry;
  580. }
  581. spTrackEntry* spAnimationState_addEmptyAnimation(spAnimationState* self, int trackIndex, float mixDuration, float delay) {
  582. spTrackEntry* entry;
  583. if (delay <= 0) delay -= mixDuration;
  584. entry = spAnimationState_addAnimation(self, trackIndex, SP_EMPTY_ANIMATION, 0, delay);
  585. entry->mixDuration = mixDuration;
  586. entry->trackEnd = mixDuration;
  587. return entry;
  588. }
  589. void spAnimationState_setEmptyAnimations(spAnimationState* self, float mixDuration) {
  590. int i, n;
  591. spTrackEntry* current;
  592. _spAnimationState* internal = SUB_CAST(_spAnimationState, self);
  593. internal->queue->drainDisabled = 1;
  594. for (i = 0, n = self->tracksCount; i < n; i++) {
  595. current = self->tracks[i];
  596. if (current) spAnimationState_setEmptyAnimation(self, current->trackIndex, mixDuration);
  597. }
  598. internal->queue->drainDisabled = 0;
  599. _spEventQueue_drain(internal->queue);
  600. }
  601. spTrackEntry* _spAnimationState_expandToIndex (spAnimationState* self, int index) {
  602. spTrackEntry** newTracks;
  603. if (index < self->tracksCount) return self->tracks[index];
  604. newTracks = CALLOC(spTrackEntry*, index + 1);
  605. memcpy(newTracks, self->tracks, self->tracksCount * sizeof(spTrackEntry*));
  606. FREE(self->tracks);
  607. self->tracks = newTracks;
  608. self->tracksCount = index + 1;
  609. return 0;
  610. }
  611. spTrackEntry* _spAnimationState_trackEntry (spAnimationState* self, int trackIndex, spAnimation* animation, int /*boolean*/ loop, spTrackEntry* last) {
  612. spTrackEntry* entry = NEW(spTrackEntry);
  613. entry->trackIndex = trackIndex;
  614. entry->animation = animation;
  615. entry->loop = loop;
  616. entry->eventThreshold = 0;
  617. entry->attachmentThreshold = 0;
  618. entry->drawOrderThreshold = 0;
  619. entry->animationStart = 0;
  620. entry->animationEnd = animation->duration;
  621. entry->animationLast = -1;
  622. entry->nextAnimationLast = -1;
  623. entry->delay = 0;
  624. entry->trackTime = 0;
  625. entry->trackLast = -1;
  626. entry->nextTrackLast = -1;
  627. entry->trackEnd = (float)INT_MAX;
  628. entry->timeScale = 1;
  629. entry->alpha = 1;
  630. entry->mixAlpha = 1;
  631. entry->mixTime = 0;
  632. entry->mixDuration = !last ? 0 : spAnimationStateData_getMix(self->data, last->animation, animation);
  633. return entry;
  634. }
  635. void _spAnimationState_disposeNext (spAnimationState* self, spTrackEntry* entry) {
  636. _spAnimationState* internal = SUB_CAST(_spAnimationState, self);
  637. spTrackEntry* next = entry->next;
  638. while (next) {
  639. _spEventQueue_dispose(internal->queue, next);
  640. next = next->next;
  641. }
  642. entry->next = 0;
  643. }
  644. void _spAnimationState_animationsChanged (spAnimationState* self) {
  645. _spAnimationState* internal = SUB_CAST(_spAnimationState, self);
  646. int i, n;
  647. spTrackEntry* entry;
  648. internal->animationsChanged = 0;
  649. i = 0; n = self->tracksCount;
  650. internal->propertyIDsCount = 0;
  651. for (; i < n; i++) {
  652. entry = self->tracks[i];
  653. if (!entry) continue;
  654. _spAnimationState_setTimelinesFirst(self, entry);
  655. i++;
  656. break;
  657. }
  658. for (; i < n; i++) {
  659. entry = self->tracks[i];
  660. if (entry) _spAnimationState_checkTimelinesFirst(self, entry);
  661. }
  662. }
  663. float* _spAnimationState_resizeTimelinesRotation(spTrackEntry* entry, int newSize) {
  664. if (entry->timelinesRotationCount != newSize) {
  665. float* newTimelinesRotation = CALLOC(float, newSize);
  666. FREE(entry->timelinesRotation);
  667. entry->timelinesRotation = newTimelinesRotation;
  668. entry->timelinesRotationCount = newSize;
  669. }
  670. return entry->timelinesRotation;
  671. }
  672. int* _spAnimationState_resizeTimelinesFirst(spTrackEntry* entry, int newSize) {
  673. if (entry->timelinesFirstCount != newSize) {
  674. int* newTimelinesFirst = CALLOC(int, newSize);
  675. FREE(entry->timelinesFirst);
  676. entry->timelinesFirst = newTimelinesFirst;
  677. entry->timelinesFirstCount = newSize;
  678. }
  679. return entry->timelinesFirst;
  680. }
  681. void _spAnimationState_ensureCapacityPropertyIDs(spAnimationState* self, int capacity) {
  682. _spAnimationState* internal = SUB_CAST(_spAnimationState, self);
  683. if (internal->propertyIDsCapacity < capacity) {
  684. int *newPropertyIDs = CALLOC(int, capacity << 1);
  685. memcpy(newPropertyIDs, internal->propertyIDs, sizeof(int) * internal->propertyIDsCount);
  686. FREE(internal->propertyIDs);
  687. internal->propertyIDs = newPropertyIDs;
  688. internal->propertyIDsCapacity = capacity << 1;
  689. }
  690. }
  691. int _spAnimationState_addPropertyID(spAnimationState* self, int id) {
  692. int i, n;
  693. _spAnimationState* internal = SUB_CAST(_spAnimationState, self);
  694. for (i = 0, n = internal->propertyIDsCount; i < n; i++) {
  695. if (internal->propertyIDs[i] == id) return 0;
  696. }
  697. _spAnimationState_ensureCapacityPropertyIDs(self, internal->propertyIDsCount + 1);
  698. internal->propertyIDs[internal->propertyIDsCount] = id;
  699. internal->propertyIDsCount++;
  700. return 1;
  701. }
  702. void _spAnimationState_setTimelinesFirst (spAnimationState* self, spTrackEntry* entry) {
  703. int i, n;
  704. int* usage;
  705. spTimeline** timelines;
  706. if (entry->mixingFrom) {
  707. _spAnimationState_setTimelinesFirst(self, entry->mixingFrom);
  708. _spAnimationState_checkTimelinesUsage(self, entry);
  709. return;
  710. }
  711. n = entry->animation->timelinesCount;
  712. timelines = entry->animation->timelines;
  713. usage = _spAnimationState_resizeTimelinesFirst(entry, n);
  714. for (i = 0; i < n; i++) {
  715. _spAnimationState_addPropertyID(self, spTimeline_getPropertyId(timelines[i]));
  716. usage[i] = 1;
  717. }
  718. }
  719. void _spAnimationState_checkTimelinesFirst (spAnimationState* self, spTrackEntry* entry) {
  720. if (entry->mixingFrom) _spAnimationState_checkTimelinesFirst(self, entry->mixingFrom);
  721. _spAnimationState_checkTimelinesUsage(self, entry);
  722. }
  723. void _spAnimationState_checkTimelinesUsage (spAnimationState* self, spTrackEntry* entry) {
  724. int i, n;
  725. int* usage;
  726. spTimeline** timelines;
  727. n = entry->animation->timelinesCount;
  728. timelines = entry->animation->timelines;
  729. usage = _spAnimationState_resizeTimelinesFirst(entry, n);
  730. for (i = 0; i < n; i++)
  731. usage[i] = _spAnimationState_addPropertyID(self, spTimeline_getPropertyId(timelines[i]));
  732. }
  733. spTrackEntry* spAnimationState_getCurrent (spAnimationState* self, int trackIndex) {
  734. if (trackIndex >= self->tracksCount) return 0;
  735. return self->tracks[trackIndex];
  736. }
  737. void spAnimationState_clearListenerNotifications(spAnimationState* self) {
  738. _spAnimationState* internal = SUB_CAST(_spAnimationState, self);
  739. _spEventQueue_clear(internal->queue);
  740. }
  741. float spTrackEntry_getAnimationTime (spTrackEntry* entry) {
  742. if (entry->loop) {
  743. float duration = entry->animationEnd - entry->animationStart;
  744. if (duration == 0) return entry->animationStart;
  745. return FMOD(entry->trackTime, duration) + entry->animationStart;
  746. }
  747. return MIN(entry->trackTime + entry->animationStart, entry->animationEnd);
  748. }