Animation.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341
  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/Animation.h>
  31. #include <spine/IkConstraint.h>
  32. #include <limits.h>
  33. #include <spine/extension.h>
  34. spAnimation* spAnimation_create (const char* name, int timelinesCount) {
  35. spAnimation* self = NEW(spAnimation);
  36. MALLOC_STR(self->name, name);
  37. self->timelinesCount = timelinesCount;
  38. self->timelines = MALLOC(spTimeline*, timelinesCount);
  39. return self;
  40. }
  41. void spAnimation_dispose (spAnimation* self) {
  42. int i;
  43. for (i = 0; i < self->timelinesCount; ++i)
  44. spTimeline_dispose(self->timelines[i]);
  45. FREE(self->timelines);
  46. FREE(self->name);
  47. FREE(self);
  48. }
  49. void spAnimation_apply (const spAnimation* self, spSkeleton* skeleton, float lastTime, float time, int loop, spEvent** events,
  50. int* eventsCount, float alpha, int setupPose, int mixingOut) {
  51. int i, n = self->timelinesCount;
  52. if (loop && self->duration) {
  53. time = FMOD(time, self->duration);
  54. if (lastTime > 0) lastTime = FMOD(lastTime, self->duration);
  55. }
  56. for (i = 0; i < n; ++i)
  57. spTimeline_apply(self->timelines[i], skeleton, lastTime, time, events, eventsCount, alpha, setupPose, mixingOut);
  58. }
  59. /**/
  60. typedef struct _spTimelineVtable {
  61. void (*apply) (const spTimeline* self, spSkeleton* skeleton, float lastTime, float time, spEvent** firedEvents,
  62. int* eventsCount, float alpha, int setupPose, int mixingOut);
  63. int (*getPropertyId) (const spTimeline* self);
  64. void (*dispose) (spTimeline* self);
  65. } _spTimelineVtable;
  66. void _spTimeline_init (spTimeline* self, spTimelineType type, /**/
  67. void (*dispose) (spTimeline* self), /**/
  68. void (*apply) (const spTimeline* self, spSkeleton* skeleton, float lastTime, float time, spEvent** firedEvents, int* eventsCount, float alpha, int setupPose, int mixingOut),
  69. int (*getPropertyId) (const spTimeline* self)) {
  70. CONST_CAST(spTimelineType, self->type) = type;
  71. CONST_CAST(_spTimelineVtable*, self->vtable) = NEW(_spTimelineVtable);
  72. VTABLE(spTimeline, self)->dispose = dispose;
  73. VTABLE(spTimeline, self)->apply = apply;
  74. VTABLE(spTimeline, self)->getPropertyId = getPropertyId;
  75. }
  76. void _spTimeline_deinit (spTimeline* self) {
  77. FREE(self->vtable);
  78. }
  79. void spTimeline_dispose (spTimeline* self) {
  80. VTABLE(spTimeline, self)->dispose(self);
  81. }
  82. void spTimeline_apply (const spTimeline* self, spSkeleton* skeleton, float lastTime, float time, spEvent** firedEvents,
  83. int* eventsCount, float alpha, int /*boolean*/ setupPose, int /*boolean*/ mixingOut) {
  84. VTABLE(spTimeline, self)->apply(self, skeleton, lastTime, time, firedEvents, eventsCount, alpha, setupPose, mixingOut);
  85. }
  86. int spTimeline_getPropertyId (const spTimeline* self) {
  87. return VTABLE(spTimeline, self)->getPropertyId(self);
  88. }
  89. /**/
  90. static const float CURVE_LINEAR = 0, CURVE_STEPPED = 1, CURVE_BEZIER = 2;
  91. static const int BEZIER_SIZE = 10 * 2 - 1;
  92. void _spCurveTimeline_init (spCurveTimeline* self, spTimelineType type, int framesCount, /**/
  93. void (*dispose) (spTimeline* self), /**/
  94. void (*apply) (const spTimeline* self, spSkeleton* skeleton, float lastTime, float time, spEvent** firedEvents, int* eventsCount, float alpha, int setupPose, int mixingOut),
  95. int (*getPropertyId)(const spTimeline* self)) {
  96. _spTimeline_init(SUPER(self), type, dispose, apply, getPropertyId);
  97. self->curves = CALLOC(float, (framesCount - 1) * BEZIER_SIZE);
  98. }
  99. void _spCurveTimeline_deinit (spCurveTimeline* self) {
  100. _spTimeline_deinit(SUPER(self));
  101. FREE(self->curves);
  102. }
  103. void spCurveTimeline_setLinear (spCurveTimeline* self, int frameIndex) {
  104. self->curves[frameIndex * BEZIER_SIZE] = CURVE_LINEAR;
  105. }
  106. void spCurveTimeline_setStepped (spCurveTimeline* self, int frameIndex) {
  107. self->curves[frameIndex * BEZIER_SIZE] = CURVE_STEPPED;
  108. }
  109. void spCurveTimeline_setCurve (spCurveTimeline* self, int frameIndex, float cx1, float cy1, float cx2, float cy2) {
  110. float tmpx = (-cx1 * 2 + cx2) * 0.03f, tmpy = (-cy1 * 2 + cy2) * 0.03f;
  111. float dddfx = ((cx1 - cx2) * 3 + 1) * 0.006f, dddfy = ((cy1 - cy2) * 3 + 1) * 0.006f;
  112. float ddfx = tmpx * 2 + dddfx, ddfy = tmpy * 2 + dddfy;
  113. float dfx = cx1 * 0.3f + tmpx + dddfx * 0.16666667f, dfy = cy1 * 0.3f + tmpy + dddfy * 0.16666667f;
  114. float x = dfx, y = dfy;
  115. int i = frameIndex * BEZIER_SIZE, n = i + BEZIER_SIZE - 1;
  116. self->curves[i++] = CURVE_BEZIER;
  117. for (; i < n; i += 2) {
  118. self->curves[i] = x;
  119. self->curves[i + 1] = y;
  120. dfx += ddfx;
  121. dfy += ddfy;
  122. ddfx += dddfx;
  123. ddfy += dddfy;
  124. x += dfx;
  125. y += dfy;
  126. }
  127. }
  128. float spCurveTimeline_getCurvePercent (const spCurveTimeline* self, int frameIndex, float percent) {
  129. float x, y;
  130. int i = frameIndex * BEZIER_SIZE, start, n;
  131. float type = self->curves[i];
  132. percent = CLAMP(percent, 0, 1);
  133. if (type == CURVE_LINEAR) return percent;
  134. if (type == CURVE_STEPPED) return 0;
  135. i++;
  136. x = 0;
  137. for (start = i, n = i + BEZIER_SIZE - 1; i < n; i += 2) {
  138. x = self->curves[i];
  139. if (x >= percent) {
  140. float prevX, prevY;
  141. if (i == start) {
  142. prevX = 0;
  143. prevY = 0;
  144. } else {
  145. prevX = self->curves[i - 2];
  146. prevY = self->curves[i - 1];
  147. }
  148. return prevY + (self->curves[i + 1] - prevY) * (percent - prevX) / (x - prevX);
  149. }
  150. }
  151. y = self->curves[i - 1];
  152. return y + (1 - y) * (percent - x) / (1 - x); /* Last point is 1,1. */
  153. }
  154. /* @param target After the first and before the last entry. */
  155. static int binarySearch (float *values, int valuesLength, float target, int step) {
  156. int low = 0, current;
  157. int high = valuesLength / step - 2;
  158. if (high == 0) return step;
  159. current = high >> 1;
  160. while (1) {
  161. if (values[(current + 1) * step] <= target)
  162. low = current + 1;
  163. else
  164. high = current;
  165. if (low == high) return (low + 1) * step;
  166. current = (low + high) >> 1;
  167. }
  168. return 0;
  169. }
  170. int _spCurveTimeline_binarySearch (float *values, int valuesLength, float target, int step) {
  171. return binarySearch(values, valuesLength, target, step);
  172. }
  173. /* @param target After the first and before the last entry. */
  174. static int binarySearch1 (float *values, int valuesLength, float target) {
  175. int low = 0, current;
  176. int high = valuesLength - 2;
  177. if (high == 0) return 1;
  178. current = high >> 1;
  179. while (1) {
  180. if (values[(current + 1)] <= target)
  181. low = current + 1;
  182. else
  183. high = current;
  184. if (low == high) return low + 1;
  185. current = (low + high) >> 1;
  186. }
  187. return 0;
  188. }
  189. /**/
  190. void _spBaseTimeline_dispose (spTimeline* timeline) {
  191. struct spBaseTimeline* self = SUB_CAST(struct spBaseTimeline, timeline);
  192. _spCurveTimeline_deinit(SUPER(self));
  193. FREE(self->frames);
  194. FREE(self);
  195. }
  196. /* Many timelines have structure identical to struct spBaseTimeline and extend spCurveTimeline. **/
  197. struct spBaseTimeline* _spBaseTimeline_create (int framesCount, spTimelineType type, int frameSize, /**/
  198. void (*apply) (const spTimeline* self, spSkeleton* skeleton, float lastTime, float time, spEvent** firedEvents,
  199. int* eventsCount, float alpha, int setupPose, int mixingOut),
  200. int (*getPropertyId) (const spTimeline* self)) {
  201. struct spBaseTimeline* self = NEW(struct spBaseTimeline);
  202. _spCurveTimeline_init(SUPER(self), type, framesCount, _spBaseTimeline_dispose, apply, getPropertyId);
  203. CONST_CAST(int, self->framesCount) = framesCount * frameSize;
  204. CONST_CAST(float*, self->frames) = CALLOC(float, self->framesCount);
  205. return self;
  206. }
  207. /**/
  208. void _spRotateTimeline_apply (const spTimeline* timeline, spSkeleton* skeleton, float lastTime, float time, spEvent** firedEvents,
  209. int* eventsCount, float alpha, int setupPose, int mixingOut) {
  210. spBone *bone;
  211. int frame;
  212. float prevRotation, frameTime, percent, r;
  213. spRotateTimeline* self = SUB_CAST(spRotateTimeline, timeline);
  214. bone = skeleton->bones[self->boneIndex];
  215. if (time < self->frames[0]) {
  216. if (setupPose) bone->rotation = bone->data->rotation;
  217. return;
  218. }
  219. if (time >= self->frames[self->framesCount - ROTATE_ENTRIES]) { /* Time is after last frame. */
  220. if (setupPose)
  221. bone->rotation = bone->data->rotation + self->frames[self->framesCount + ROTATE_PREV_ROTATION] * alpha;
  222. else {
  223. r = bone->data->rotation + self->frames[self->framesCount + ROTATE_PREV_ROTATION] - bone->rotation;
  224. r -= (16384 - (int)(16384.499999999996 - r / 360)) * 360; /* Wrap within -180 and 180. */
  225. bone->rotation += r * alpha;
  226. }
  227. return;
  228. }
  229. /* Interpolate between the previous frame and the current frame. */
  230. frame = binarySearch(self->frames, self->framesCount, time, ROTATE_ENTRIES);
  231. prevRotation = self->frames[frame + ROTATE_PREV_ROTATION];
  232. frameTime = self->frames[frame];
  233. percent = spCurveTimeline_getCurvePercent(SUPER(self), (frame >> 1) - 1, 1 - (time - frameTime) / (self->frames[frame + ROTATE_PREV_TIME] - frameTime));
  234. r = self->frames[frame + ROTATE_ROTATION] - prevRotation;
  235. r -= (16384 - (int)(16384.499999999996 - r / 360)) * 360;
  236. r = prevRotation + r * percent;
  237. if (setupPose) {
  238. r -= (16384 - (int)(16384.499999999996 - r / 360)) * 360;
  239. bone->rotation = bone->data->rotation + r * alpha;
  240. } else {
  241. r = bone->data->rotation + r - bone->rotation;
  242. r -= (16384 - (int)(16384.499999999996 - r / 360)) * 360;
  243. bone->rotation += r * alpha;
  244. }
  245. UNUSED(lastTime);
  246. UNUSED(firedEvents);
  247. UNUSED(eventsCount);
  248. }
  249. int _spRotateTimeline_getPropertyId (const spTimeline* timeline) {
  250. return (SP_TIMELINE_ROTATE << 25) + SUB_CAST(spRotateTimeline, timeline)->boneIndex;
  251. }
  252. spRotateTimeline* spRotateTimeline_create (int framesCount) {
  253. return _spBaseTimeline_create(framesCount, SP_TIMELINE_ROTATE, ROTATE_ENTRIES, _spRotateTimeline_apply, _spRotateTimeline_getPropertyId);
  254. }
  255. void spRotateTimeline_setFrame (spRotateTimeline* self, int frameIndex, float time, float degrees) {
  256. frameIndex <<= 1;
  257. self->frames[frameIndex] = time;
  258. self->frames[frameIndex + ROTATE_ROTATION] = degrees;
  259. }
  260. /**/
  261. static const int TRANSLATE_PREV_TIME = -3, TRANSLATE_PREV_X = -2, TRANSLATE_PREV_Y = -1;
  262. static const int TRANSLATE_X = 1, TRANSLATE_Y = 2;
  263. void _spTranslateTimeline_apply (const spTimeline* timeline, spSkeleton* skeleton, float lastTime, float time,
  264. spEvent** firedEvents, int* eventsCount, float alpha, int setupPose, int mixingOut) {
  265. spBone *bone;
  266. int frame;
  267. float frameTime, percent;
  268. float x, y;
  269. float *frames;
  270. int framesCount;
  271. spTranslateTimeline* self = SUB_CAST(spTranslateTimeline, timeline);
  272. bone = skeleton->bones[self->boneIndex];
  273. if (time < self->frames[0]) {
  274. if (setupPose) {
  275. bone->x = bone->data->x;
  276. bone->y = bone->data->y;
  277. }
  278. return;
  279. }
  280. frames = self->frames;
  281. framesCount = self->framesCount;
  282. if (time >= frames[framesCount - TRANSLATE_ENTRIES]) { /* Time is after last frame. */
  283. x = frames[framesCount + TRANSLATE_PREV_X];
  284. y = frames[framesCount + TRANSLATE_PREV_Y];
  285. } else {
  286. /* Interpolate between the previous frame and the current frame. */
  287. frame = binarySearch(frames, framesCount, time, TRANSLATE_ENTRIES);
  288. x = frames[frame + TRANSLATE_PREV_X];
  289. y = frames[frame + TRANSLATE_PREV_Y];
  290. frameTime = frames[frame];
  291. percent = spCurveTimeline_getCurvePercent(SUPER(self), frame / TRANSLATE_ENTRIES - 1,
  292. 1 - (time - frameTime) / (frames[frame + TRANSLATE_PREV_TIME] - frameTime));
  293. x += (frames[frame + TRANSLATE_X] - x) * percent;
  294. y += (frames[frame + TRANSLATE_Y] - y) * percent;
  295. }
  296. if (setupPose) {
  297. bone->x = bone->data->x + x * alpha;
  298. bone->y = bone->data->y + y * alpha;
  299. } else {
  300. bone->x += (bone->data->x + x - bone->x) * alpha;
  301. bone->y += (bone->data->y + y - bone->y) * alpha;
  302. }
  303. UNUSED(lastTime);
  304. UNUSED(firedEvents);
  305. UNUSED(eventsCount);
  306. }
  307. int _spTranslateTimeline_getPropertyId (const spTimeline* self) {
  308. return (SP_TIMELINE_TRANSLATE << 24) + SUB_CAST(spTranslateTimeline, self)->boneIndex;
  309. }
  310. spTranslateTimeline* spTranslateTimeline_create (int framesCount) {
  311. return _spBaseTimeline_create(framesCount, SP_TIMELINE_TRANSLATE, TRANSLATE_ENTRIES, _spTranslateTimeline_apply, _spTranslateTimeline_getPropertyId);
  312. }
  313. void spTranslateTimeline_setFrame (spTranslateTimeline* self, int frameIndex, float time, float x, float y) {
  314. frameIndex *= TRANSLATE_ENTRIES;
  315. self->frames[frameIndex] = time;
  316. self->frames[frameIndex + TRANSLATE_X] = x;
  317. self->frames[frameIndex + TRANSLATE_Y] = y;
  318. }
  319. /**/
  320. void _spScaleTimeline_apply (const spTimeline* timeline, spSkeleton* skeleton, float lastTime, float time, spEvent** firedEvents,
  321. int* eventsCount, float alpha, int setupPose, int mixingOut) {
  322. spBone *bone;
  323. int frame;
  324. float frameTime, percent, x, y;
  325. float *frames;
  326. int framesCount;
  327. spScaleTimeline* self = SUB_CAST(spScaleTimeline, timeline);
  328. bone = skeleton->bones[self->boneIndex];
  329. if (time < self->frames[0]) {
  330. if (setupPose) {
  331. bone->scaleX = bone->data->scaleX;
  332. bone->scaleY = bone->data->scaleY;
  333. }
  334. return;
  335. }
  336. frames = self->frames;
  337. framesCount = self->framesCount;
  338. if (time >= frames[framesCount - TRANSLATE_ENTRIES]) { /* Time is after last frame. */
  339. x = frames[framesCount + TRANSLATE_PREV_X] * bone->data->scaleX;
  340. y = frames[framesCount + TRANSLATE_PREV_Y] * bone->data->scaleY;
  341. } else {
  342. /* Interpolate between the previous frame and the current frame. */
  343. frame = binarySearch(frames, framesCount, time, TRANSLATE_ENTRIES);
  344. x = frames[frame + TRANSLATE_PREV_X];
  345. y = frames[frame + TRANSLATE_PREV_Y];
  346. frameTime = frames[frame];
  347. percent = spCurveTimeline_getCurvePercent(SUPER(self), frame / TRANSLATE_ENTRIES - 1,
  348. 1 - (time - frameTime) / (frames[frame + TRANSLATE_PREV_TIME] - frameTime));
  349. x = (x + (frames[frame + TRANSLATE_X] - x) * percent) * bone->data->scaleX;
  350. y = (y + (frames[frame + TRANSLATE_Y] - y) * percent) * bone->data->scaleY;
  351. }
  352. if (alpha == 1) {
  353. bone->scaleX = x;
  354. bone->scaleY = y;
  355. } else {
  356. float bx, by;
  357. if (setupPose) {
  358. bx = bone->data->scaleX;
  359. by = bone->data->scaleY;
  360. } else {
  361. bx = bone->scaleX;
  362. by = bone->scaleY;
  363. }
  364. /* Mixing out uses sign of setup or current pose, else use sign of key. */
  365. if (mixingOut) {
  366. x = ABS(x) * SIGNUM(bx);
  367. y = ABS(y) * SIGNUM(by);
  368. } else {
  369. bx = ABS(bx) * SIGNUM(x);
  370. by = ABS(by) * SIGNUM(y);
  371. }
  372. bone->scaleX = bx + (x - bx) * alpha;
  373. bone->scaleY = by + (y - by) * alpha;
  374. }
  375. UNUSED(lastTime);
  376. UNUSED(firedEvents);
  377. UNUSED(eventsCount);
  378. }
  379. int _spScaleTimeline_getPropertyId (const spTimeline* timeline) {
  380. return (SP_TIMELINE_SCALE << 24) + SUB_CAST(spScaleTimeline, timeline)->boneIndex;
  381. }
  382. spScaleTimeline* spScaleTimeline_create (int framesCount) {
  383. return _spBaseTimeline_create(framesCount, SP_TIMELINE_SCALE, TRANSLATE_ENTRIES, _spScaleTimeline_apply, _spScaleTimeline_getPropertyId);
  384. }
  385. void spScaleTimeline_setFrame (spScaleTimeline* self, int frameIndex, float time, float x, float y) {
  386. spTranslateTimeline_setFrame(self, frameIndex, time, x, y);
  387. }
  388. /**/
  389. void _spShearTimeline_apply (const spTimeline* timeline, spSkeleton* skeleton, float lastTime, float time, spEvent** firedEvents,
  390. int* eventsCount, float alpha, int setupPose, int mixingOut) {
  391. spBone *bone;
  392. int frame;
  393. float frameTime, percent, x, y;
  394. float *frames;
  395. int framesCount;
  396. spShearTimeline* self = SUB_CAST(spShearTimeline, timeline);
  397. bone = skeleton->bones[self->boneIndex];
  398. frames = self->frames;
  399. framesCount = self->framesCount;
  400. if (time < self->frames[0]) {
  401. if (setupPose) {
  402. bone->shearX = bone->data->shearX;
  403. bone->shearY = bone->data->shearY;
  404. }
  405. return;
  406. }
  407. if (time >= frames[framesCount - TRANSLATE_ENTRIES]) { /* Time is after last frame. */
  408. x = frames[framesCount + TRANSLATE_PREV_X];
  409. y = frames[framesCount + TRANSLATE_PREV_Y];
  410. } else {
  411. /* Interpolate between the previous frame and the current frame. */
  412. frame = binarySearch(frames, framesCount, time, TRANSLATE_ENTRIES);
  413. x = frames[frame + TRANSLATE_PREV_X];
  414. y = frames[frame + TRANSLATE_PREV_Y];
  415. frameTime = frames[frame];
  416. percent = spCurveTimeline_getCurvePercent(SUPER(self), frame / TRANSLATE_ENTRIES - 1,
  417. 1 - (time - frameTime) / (frames[frame + TRANSLATE_PREV_TIME] - frameTime));
  418. x = x + (frames[frame + TRANSLATE_X] - x) * percent;
  419. y = y + (frames[frame + TRANSLATE_Y] - y) * percent;
  420. }
  421. if (setupPose) {
  422. bone->shearX = bone->data->shearX + x * alpha;
  423. bone->shearY = bone->data->shearY + y * alpha;
  424. } else {
  425. bone->shearX += (bone->data->shearX + x - bone->shearX) * alpha;
  426. bone->shearY += (bone->data->shearY + y - bone->shearY) * alpha;
  427. }
  428. UNUSED(lastTime);
  429. UNUSED(firedEvents);
  430. UNUSED(eventsCount);
  431. }
  432. int _spShearTimeline_getPropertyId (const spTimeline* timeline) {
  433. return (SP_TIMELINE_SHEAR << 24) + SUB_CAST(spShearTimeline, timeline)->boneIndex;
  434. }
  435. spShearTimeline* spShearTimeline_create (int framesCount) {
  436. return (spShearTimeline*)_spBaseTimeline_create(framesCount, SP_TIMELINE_SHEAR, 3, _spShearTimeline_apply, _spShearTimeline_getPropertyId);
  437. }
  438. void spShearTimeline_setFrame (spShearTimeline* self, int frameIndex, float time, float x, float y) {
  439. spTranslateTimeline_setFrame(self, frameIndex, time, x, y);
  440. }
  441. /**/
  442. static const int COLOR_PREV_TIME = -5, COLOR_PREV_R = -4, COLOR_PREV_G = -3, COLOR_PREV_B = -2, COLOR_PREV_A = -1;
  443. static const int COLOR_R = 1, COLOR_G = 2, COLOR_B = 3, COLOR_A = 4;
  444. void _spColorTimeline_apply (const spTimeline* timeline, spSkeleton* skeleton, float lastTime, float time, spEvent** firedEvents,
  445. int* eventsCount, float alpha, int setupPose, int mixingOut) {
  446. spSlot *slot;
  447. int frame;
  448. float percent, frameTime;
  449. float r, g, b, a;
  450. spColorTimeline* self = (spColorTimeline*)timeline;
  451. slot = skeleton->slots[self->slotIndex];
  452. if (time < self->frames[0]) {
  453. if (setupPose) {
  454. slot->r = slot->data->r;
  455. slot->g = slot->data->g;
  456. slot->b = slot->data->b;
  457. slot->a = slot->data->a;
  458. }
  459. return;
  460. }
  461. if (time >= self->frames[self->framesCount - 5]) { /* Time is after last frame */
  462. int i = self->framesCount;
  463. r = self->frames[i + COLOR_PREV_R];
  464. g = self->frames[i + COLOR_PREV_G];
  465. b = self->frames[i + COLOR_PREV_B];
  466. a = self->frames[i + COLOR_PREV_A];
  467. } else {
  468. /* Interpolate between the previous frame and the current frame. */
  469. frame = binarySearch(self->frames, self->framesCount, time, COLOR_ENTRIES);
  470. r = self->frames[frame + COLOR_PREV_R];
  471. g = self->frames[frame + COLOR_PREV_G];
  472. b = self->frames[frame + COLOR_PREV_B];
  473. a = self->frames[frame + COLOR_PREV_A];
  474. frameTime = self->frames[frame];
  475. percent = spCurveTimeline_getCurvePercent(SUPER(self), frame / COLOR_ENTRIES - 1,
  476. 1 - (time - frameTime) / (self->frames[frame + COLOR_PREV_TIME] - frameTime));
  477. r += (self->frames[frame + COLOR_R] - r) * percent;
  478. g += (self->frames[frame + COLOR_G] - g) * percent;
  479. b += (self->frames[frame + COLOR_B] - b) * percent;
  480. a += (self->frames[frame + COLOR_A] - a) * percent;
  481. }
  482. if (alpha == 1) {
  483. slot->r = r;
  484. slot->g = g;
  485. slot->b = b;
  486. slot->a = a;
  487. } else {
  488. if (setupPose) {
  489. slot->r = slot->data->r;
  490. slot->g = slot->data->g;
  491. slot->b = slot->data->b;
  492. slot->a = slot->data->a;
  493. }
  494. slot->r += (r - slot->r) * alpha;
  495. slot->g += (g - slot->g) * alpha;
  496. slot->b += (b - slot->b) * alpha;
  497. slot->a += (a - slot->a) * alpha;
  498. }
  499. UNUSED(lastTime);
  500. UNUSED(firedEvents);
  501. UNUSED(eventsCount);
  502. }
  503. int _spColorTimeline_getPropertyId (const spTimeline* timeline) {
  504. return (SP_TIMELINE_COLOR << 24) + SUB_CAST(spColorTimeline, timeline)->slotIndex;
  505. }
  506. spColorTimeline* spColorTimeline_create (int framesCount) {
  507. return (spColorTimeline*)_spBaseTimeline_create(framesCount, SP_TIMELINE_COLOR, 5, _spColorTimeline_apply, _spColorTimeline_getPropertyId);
  508. }
  509. void spColorTimeline_setFrame (spColorTimeline* self, int frameIndex, float time, float r, float g, float b, float a) {
  510. frameIndex *= COLOR_ENTRIES;
  511. self->frames[frameIndex] = time;
  512. self->frames[frameIndex + COLOR_R] = r;
  513. self->frames[frameIndex + COLOR_G] = g;
  514. self->frames[frameIndex + COLOR_B] = b;
  515. self->frames[frameIndex + COLOR_A] = a;
  516. }
  517. /**/
  518. void _spAttachmentTimeline_apply (const spTimeline* timeline, spSkeleton* skeleton, float lastTime, float time,
  519. spEvent** firedEvents, int* eventsCount, float alpha, int setupPose, int mixingOut) {
  520. const char* attachmentName;
  521. spAttachmentTimeline* self = (spAttachmentTimeline*)timeline;
  522. int frameIndex;
  523. spSlot* slot = skeleton->slots[self->slotIndex];
  524. if (mixingOut && setupPose) {
  525. const char* attachmentName = slot->data->attachmentName;
  526. spSlot_setAttachment(slot, attachmentName ? spSkeleton_getAttachmentForSlotIndex(skeleton, self->slotIndex, attachmentName) : 0);
  527. return;
  528. }
  529. if (time < self->frames[0]) {
  530. if (setupPose) {
  531. attachmentName = slot->data->attachmentName;
  532. spSlot_setAttachment(skeleton->slots[self->slotIndex],
  533. attachmentName ? spSkeleton_getAttachmentForSlotIndex(skeleton, self->slotIndex, attachmentName) : 0);
  534. }
  535. return;
  536. }
  537. if (time >= self->frames[self->framesCount - 1])
  538. frameIndex = self->framesCount - 1;
  539. else
  540. frameIndex = binarySearch1(self->frames, self->framesCount, time) - 1;
  541. attachmentName = self->attachmentNames[frameIndex];
  542. spSlot_setAttachment(skeleton->slots[self->slotIndex],
  543. attachmentName ? spSkeleton_getAttachmentForSlotIndex(skeleton, self->slotIndex, attachmentName) : 0);
  544. UNUSED(lastTime);
  545. UNUSED(firedEvents);
  546. UNUSED(eventsCount);
  547. UNUSED(alpha);
  548. }
  549. int _spAttachmentTimeline_getPropertyId (const spTimeline* timeline) {
  550. return (SP_TIMELINE_ATTACHMENT << 24) + SUB_CAST(spAttachmentTimeline, timeline)->slotIndex;
  551. }
  552. void _spAttachmentTimeline_dispose (spTimeline* timeline) {
  553. spAttachmentTimeline* self = SUB_CAST(spAttachmentTimeline, timeline);
  554. int i;
  555. _spTimeline_deinit(timeline);
  556. for (i = 0; i < self->framesCount; ++i)
  557. FREE(self->attachmentNames[i]);
  558. FREE(self->attachmentNames);
  559. FREE(self->frames);
  560. FREE(self);
  561. }
  562. spAttachmentTimeline* spAttachmentTimeline_create (int framesCount) {
  563. spAttachmentTimeline* self = NEW(spAttachmentTimeline);
  564. _spTimeline_init(SUPER(self), SP_TIMELINE_ATTACHMENT, _spAttachmentTimeline_dispose, _spAttachmentTimeline_apply, _spAttachmentTimeline_getPropertyId);
  565. CONST_CAST(int, self->framesCount) = framesCount;
  566. CONST_CAST(float*, self->frames) = CALLOC(float, framesCount);
  567. CONST_CAST(char**, self->attachmentNames) = CALLOC(char*, framesCount);
  568. return self;
  569. }
  570. void spAttachmentTimeline_setFrame (spAttachmentTimeline* self, int frameIndex, float time, const char* attachmentName) {
  571. self->frames[frameIndex] = time;
  572. FREE(self->attachmentNames[frameIndex]);
  573. if (attachmentName)
  574. MALLOC_STR(self->attachmentNames[frameIndex], attachmentName);
  575. else
  576. self->attachmentNames[frameIndex] = 0;
  577. }
  578. /**/
  579. void _spDeformTimeline_apply (const spTimeline* timeline, spSkeleton* skeleton, float lastTime, float time, spEvent** firedEvents,
  580. int* eventsCount, float alpha, int setupPose, int mixingOut) {
  581. int frame, i, vertexCount;
  582. float percent, frameTime;
  583. const float* prevVertices;
  584. const float* nextVertices;
  585. float* frames;
  586. int framesCount;
  587. const float** frameVertices;
  588. float* vertices;
  589. spDeformTimeline* self = (spDeformTimeline*)timeline;
  590. spSlot *slot = skeleton->slots[self->slotIndex];
  591. if (slot->attachment != self->attachment) {
  592. if (!slot->attachment) return;
  593. switch (slot->attachment->type) {
  594. case SP_ATTACHMENT_MESH: {
  595. spMeshAttachment* mesh = SUB_CAST(spMeshAttachment, slot->attachment);
  596. if (!mesh->inheritDeform || mesh->parentMesh != (void*)self->attachment) return;
  597. break;
  598. }
  599. default:
  600. return;
  601. }
  602. }
  603. frames = self->frames;
  604. framesCount = self->framesCount;
  605. if (time < frames[0]) { /* Time is before first frame. */
  606. if (setupPose) slot->attachmentVerticesCount = 0;
  607. return;
  608. }
  609. vertexCount = self->frameVerticesCount;
  610. if (slot->attachmentVerticesCount < vertexCount) {
  611. if (slot->attachmentVerticesCapacity < vertexCount) {
  612. FREE(slot->attachmentVertices);
  613. slot->attachmentVertices = MALLOC(float, vertexCount);
  614. slot->attachmentVerticesCapacity = vertexCount;
  615. }
  616. }
  617. if (slot->attachmentVerticesCount != vertexCount) alpha = 1; /* Don't mix from uninitialized slot vertices. */
  618. slot->attachmentVerticesCount = vertexCount;
  619. frameVertices = self->frameVertices;
  620. vertices = slot->attachmentVertices;
  621. if (time >= frames[framesCount - 1]) { /* Time is after last frame. */
  622. const float* lastVertices = self->frameVertices[framesCount - 1];
  623. if (alpha == 1) {
  624. /* Vertex positions or deform offsets, no alpha. */
  625. memcpy(vertices, lastVertices, vertexCount * sizeof(float));
  626. } else if (setupPose) {
  627. spVertexAttachment* vertexAttachment = SUB_CAST(spVertexAttachment, slot->attachment);
  628. if (!vertexAttachment->bones) {
  629. /* Unweighted vertex positions, with alpha. */
  630. float* setupVertices = vertexAttachment->vertices;
  631. for (i = 0; i < vertexCount; i++) {
  632. float setup = setupVertices[i];
  633. vertices[i] = setup + (lastVertices[i] - setup) * alpha;
  634. }
  635. } else {
  636. /* Weighted deform offsets, with alpha. */
  637. for (i = 0; i < vertexCount; i++)
  638. vertices[i] = lastVertices[i] * alpha;
  639. }
  640. } else {
  641. /* Vertex positions or deform offsets, with alpha. */
  642. for (i = 0; i < vertexCount; i++)
  643. vertices[i] += (lastVertices[i] - vertices[i]) * alpha;
  644. }
  645. return;
  646. }
  647. /* Interpolate between the previous frame and the current frame. */
  648. frame = binarySearch(frames, framesCount, time, 1);
  649. prevVertices = frameVertices[frame - 1];
  650. nextVertices = frameVertices[frame];
  651. frameTime = frames[frame];
  652. percent = spCurveTimeline_getCurvePercent(SUPER(self), frame - 1, 1 - (time - frameTime) / (frames[frame - 1] - frameTime));
  653. if (alpha == 1) {
  654. /* Vertex positions or deform offsets, no alpha. */
  655. for (i = 0; i < vertexCount; i++) {
  656. float prev = prevVertices[i];
  657. vertices[i] = prev + (nextVertices[i] - prev) * percent;
  658. }
  659. } else if (setupPose) {
  660. spVertexAttachment* vertexAttachment = SUB_CAST(spVertexAttachment, slot->attachment);
  661. if (!vertexAttachment->bones) {
  662. /* Unweighted vertex positions, with alpha. */
  663. float* setupVertices = vertexAttachment->vertices;
  664. for (i = 0; i < vertexCount; i++) {
  665. float prev = prevVertices[i], setup = setupVertices[i];
  666. vertices[i] = setup + (prev + (nextVertices[i] - prev) * percent - setup) * alpha;
  667. }
  668. } else {
  669. /* Weighted deform offsets, with alpha. */
  670. for (i = 0; i < vertexCount; i++) {
  671. float prev = prevVertices[i];
  672. vertices[i] = (prev + (nextVertices[i] - prev) * percent) * alpha;
  673. }
  674. }
  675. } else {
  676. /* Vertex positions or deform offsets, with alpha. */
  677. for (i = 0; i < vertexCount; i++) {
  678. float prev = prevVertices[i];
  679. vertices[i] += (prev + (nextVertices[i] - prev) * percent - vertices[i]) * alpha;
  680. }
  681. }
  682. UNUSED(lastTime);
  683. UNUSED(firedEvents);
  684. UNUSED(eventsCount);
  685. }
  686. int _spDeformTimeline_getPropertyId (const spTimeline* timeline) {
  687. return (SP_TIMELINE_DEFORM << 24) + SUB_CAST(spDeformTimeline, timeline)->slotIndex;
  688. }
  689. void _spDeformTimeline_dispose (spTimeline* timeline) {
  690. spDeformTimeline* self = SUB_CAST(spDeformTimeline, timeline);
  691. int i;
  692. _spCurveTimeline_deinit(SUPER(self));
  693. for (i = 0; i < self->framesCount; ++i)
  694. FREE(self->frameVertices[i]);
  695. FREE(self->frameVertices);
  696. FREE(self->frames);
  697. FREE(self);
  698. }
  699. spDeformTimeline* spDeformTimeline_create (int framesCount, int frameVerticesCount) {
  700. spDeformTimeline* self = NEW(spDeformTimeline);
  701. _spCurveTimeline_init(SUPER(self), SP_TIMELINE_DEFORM, framesCount, _spDeformTimeline_dispose, _spDeformTimeline_apply, _spDeformTimeline_getPropertyId);
  702. CONST_CAST(int, self->framesCount) = framesCount;
  703. CONST_CAST(float*, self->frames) = CALLOC(float, self->framesCount);
  704. CONST_CAST(float**, self->frameVertices) = CALLOC(float*, framesCount);
  705. CONST_CAST(int, self->frameVerticesCount) = frameVerticesCount;
  706. return self;
  707. }
  708. void spDeformTimeline_setFrame (spDeformTimeline* self, int frameIndex, float time, float* vertices) {
  709. self->frames[frameIndex] = time;
  710. FREE(self->frameVertices[frameIndex]);
  711. if (!vertices)
  712. self->frameVertices[frameIndex] = 0;
  713. else {
  714. self->frameVertices[frameIndex] = MALLOC(float, self->frameVerticesCount);
  715. memcpy(CONST_CAST(float*, self->frameVertices[frameIndex]), vertices, self->frameVerticesCount * sizeof(float));
  716. }
  717. }
  718. /**/
  719. /** Fires events for frames > lastTime and <= time. */
  720. void _spEventTimeline_apply (const spTimeline* timeline, spSkeleton* skeleton, float lastTime, float time, spEvent** firedEvents,
  721. int* eventsCount, float alpha, int setupPose, int mixingOut) {
  722. spEventTimeline* self = (spEventTimeline*)timeline;
  723. int frame;
  724. if (!firedEvents) return;
  725. if (lastTime > time) { /* Fire events after last time for looped animations. */
  726. _spEventTimeline_apply(timeline, skeleton, lastTime, (float)INT_MAX, firedEvents, eventsCount, alpha, setupPose, mixingOut);
  727. lastTime = -1;
  728. } else if (lastTime >= self->frames[self->framesCount - 1]) /* Last time is after last frame. */
  729. return;
  730. if (time < self->frames[0]) return; /* Time is before first frame. */
  731. if (lastTime < self->frames[0])
  732. frame = 0;
  733. else {
  734. float frameTime;
  735. frame = binarySearch1(self->frames, self->framesCount, lastTime);
  736. frameTime = self->frames[frame];
  737. while (frame > 0) { /* Fire multiple events with the same frame. */
  738. if (self->frames[frame - 1] != frameTime) break;
  739. frame--;
  740. }
  741. }
  742. for (; frame < self->framesCount && time >= self->frames[frame]; ++frame) {
  743. firedEvents[*eventsCount] = self->events[frame];
  744. (*eventsCount)++;
  745. }
  746. }
  747. int _spEventTimeline_getPropertyId (const spTimeline* timeline) {
  748. return SP_TIMELINE_EVENT << 24;
  749. }
  750. void _spEventTimeline_dispose (spTimeline* timeline) {
  751. spEventTimeline* self = SUB_CAST(spEventTimeline, timeline);
  752. int i;
  753. _spTimeline_deinit(timeline);
  754. for (i = 0; i < self->framesCount; ++i)
  755. spEvent_dispose(self->events[i]);
  756. FREE(self->events);
  757. FREE(self->frames);
  758. FREE(self);
  759. }
  760. spEventTimeline* spEventTimeline_create (int framesCount) {
  761. spEventTimeline* self = NEW(spEventTimeline);
  762. _spTimeline_init(SUPER(self), SP_TIMELINE_EVENT, _spEventTimeline_dispose, _spEventTimeline_apply, _spEventTimeline_getPropertyId);
  763. CONST_CAST(int, self->framesCount) = framesCount;
  764. CONST_CAST(float*, self->frames) = CALLOC(float, framesCount);
  765. CONST_CAST(spEvent**, self->events) = CALLOC(spEvent*, framesCount);
  766. return self;
  767. }
  768. void spEventTimeline_setFrame (spEventTimeline* self, int frameIndex, spEvent* event) {
  769. self->frames[frameIndex] = event->time;
  770. FREE(self->events[frameIndex]);
  771. self->events[frameIndex] = event;
  772. }
  773. /**/
  774. void _spDrawOrderTimeline_apply (const spTimeline* timeline, spSkeleton* skeleton, float lastTime, float time,
  775. spEvent** firedEvents, int* eventsCount, float alpha, int setupPose, int mixingOut) {
  776. int i;
  777. int frame;
  778. const int* drawOrderToSetupIndex;
  779. spDrawOrderTimeline* self = (spDrawOrderTimeline*)timeline;
  780. if (mixingOut && setupPose) {
  781. memcpy(skeleton->drawOrder, skeleton->slots, self->slotsCount * sizeof(spSlot*));
  782. return;
  783. }
  784. if (time < self->frames[0]) {
  785. if (setupPose) memcpy(skeleton->drawOrder, skeleton->slots, self->slotsCount * sizeof(spSlot*));
  786. return;
  787. }
  788. if (time >= self->frames[self->framesCount - 1]) /* Time is after last frame. */
  789. frame = self->framesCount - 1;
  790. else
  791. frame = binarySearch1(self->frames, self->framesCount, time) - 1;
  792. drawOrderToSetupIndex = self->drawOrders[frame];
  793. if (!drawOrderToSetupIndex)
  794. memcpy(skeleton->drawOrder, skeleton->slots, self->slotsCount * sizeof(spSlot*));
  795. else {
  796. for (i = 0; i < self->slotsCount; ++i)
  797. skeleton->drawOrder[i] = skeleton->slots[drawOrderToSetupIndex[i]];
  798. }
  799. UNUSED(lastTime);
  800. UNUSED(firedEvents);
  801. UNUSED(eventsCount);
  802. UNUSED(alpha);
  803. }
  804. int _spDrawOrderTimeline_getPropertyId (const spTimeline* timeline) {
  805. return SP_TIMELINE_DRAWORDER << 24;
  806. }
  807. void _spDrawOrderTimeline_dispose (spTimeline* timeline) {
  808. spDrawOrderTimeline* self = SUB_CAST(spDrawOrderTimeline, timeline);
  809. int i;
  810. _spTimeline_deinit(timeline);
  811. for (i = 0; i < self->framesCount; ++i)
  812. FREE(self->drawOrders[i]);
  813. FREE(self->drawOrders);
  814. FREE(self->frames);
  815. FREE(self);
  816. }
  817. spDrawOrderTimeline* spDrawOrderTimeline_create (int framesCount, int slotsCount) {
  818. spDrawOrderTimeline* self = NEW(spDrawOrderTimeline);
  819. _spTimeline_init(SUPER(self), SP_TIMELINE_DRAWORDER, _spDrawOrderTimeline_dispose, _spDrawOrderTimeline_apply, _spDrawOrderTimeline_getPropertyId);
  820. CONST_CAST(int, self->framesCount) = framesCount;
  821. CONST_CAST(float*, self->frames) = CALLOC(float, framesCount);
  822. CONST_CAST(int**, self->drawOrders) = CALLOC(int*, framesCount);
  823. CONST_CAST(int, self->slotsCount) = slotsCount;
  824. return self;
  825. }
  826. void spDrawOrderTimeline_setFrame (spDrawOrderTimeline* self, int frameIndex, float time, const int* drawOrder) {
  827. self->frames[frameIndex] = time;
  828. FREE(self->drawOrders[frameIndex]);
  829. if (!drawOrder)
  830. self->drawOrders[frameIndex] = 0;
  831. else {
  832. self->drawOrders[frameIndex] = MALLOC(int, self->slotsCount);
  833. memcpy(CONST_CAST(int*, self->drawOrders[frameIndex]), drawOrder, self->slotsCount * sizeof(int));
  834. }
  835. }
  836. /**/
  837. static const int IKCONSTRAINT_PREV_TIME = -3, IKCONSTRAINT_PREV_MIX = -2, IKCONSTRAINT_PREV_BEND_DIRECTION = -1;
  838. static const int IKCONSTRAINT_MIX = 1, IKCONSTRAINT_BEND_DIRECTION = 2;
  839. void _spIkConstraintTimeline_apply (const spTimeline* timeline, spSkeleton* skeleton, float lastTime, float time,
  840. spEvent** firedEvents, int* eventsCount, float alpha, int setupPose, int mixingOut) {
  841. int frame;
  842. float frameTime, percent, mix;
  843. float *frames;
  844. int framesCount;
  845. spIkConstraint* constraint;
  846. spIkConstraintTimeline* self = (spIkConstraintTimeline*)timeline;
  847. constraint = skeleton->ikConstraints[self->ikConstraintIndex];
  848. if (time < self->frames[0]) {
  849. if (setupPose) {
  850. constraint->mix = constraint->data->mix;
  851. constraint->bendDirection = constraint->data->bendDirection;
  852. }
  853. return;
  854. }
  855. frames = self->frames;
  856. framesCount = self->framesCount;
  857. if (time >= frames[framesCount - IKCONSTRAINT_ENTRIES]) { /* Time is after last frame. */
  858. if (setupPose) {
  859. constraint->mix = constraint->data->mix + (frames[framesCount + IKCONSTRAINT_PREV_MIX] - constraint->data->mix) * alpha;
  860. constraint->bendDirection = mixingOut ? constraint->data->bendDirection
  861. : (int)frames[framesCount + IKCONSTRAINT_PREV_BEND_DIRECTION];
  862. } else {
  863. constraint->mix += (frames[framesCount + IKCONSTRAINT_PREV_MIX] - constraint->mix) * alpha;
  864. if (!mixingOut) constraint->bendDirection = (int)frames[framesCount + IKCONSTRAINT_PREV_BEND_DIRECTION];
  865. }
  866. return;
  867. }
  868. /* Interpolate between the previous frame and the current frame. */
  869. frame = binarySearch(self->frames, self->framesCount, time, IKCONSTRAINT_ENTRIES);
  870. mix = self->frames[frame + IKCONSTRAINT_PREV_MIX];
  871. frameTime = self->frames[frame];
  872. percent = spCurveTimeline_getCurvePercent(SUPER(self), frame / IKCONSTRAINT_ENTRIES - 1, 1 - (time - frameTime) / (self->frames[frame + IKCONSTRAINT_PREV_TIME] - frameTime));
  873. if (setupPose) {
  874. constraint->mix = constraint->data->mix + (mix + (frames[frame + IKCONSTRAINT_MIX] - mix) * percent - constraint->data->mix) * alpha;
  875. constraint->bendDirection = mixingOut ? constraint->data->bendDirection : (int)frames[frame + IKCONSTRAINT_PREV_BEND_DIRECTION];
  876. } else {
  877. constraint->mix += (mix + (frames[frame + IKCONSTRAINT_MIX] - mix) * percent - constraint->mix) * alpha;
  878. if (!mixingOut) constraint->bendDirection = (int)frames[frame + IKCONSTRAINT_PREV_BEND_DIRECTION];
  879. }
  880. UNUSED(lastTime);
  881. UNUSED(firedEvents);
  882. UNUSED(eventsCount);
  883. }
  884. int _spIkConstraintTimeline_getPropertyId (const spTimeline* timeline) {
  885. return (SP_TIMELINE_IKCONSTRAINT << 24) + SUB_CAST(spIkConstraintTimeline, timeline)->ikConstraintIndex;
  886. }
  887. spIkConstraintTimeline* spIkConstraintTimeline_create (int framesCount) {
  888. return (spIkConstraintTimeline*)_spBaseTimeline_create(framesCount, SP_TIMELINE_IKCONSTRAINT, IKCONSTRAINT_ENTRIES, _spIkConstraintTimeline_apply, _spIkConstraintTimeline_getPropertyId);
  889. }
  890. void spIkConstraintTimeline_setFrame (spIkConstraintTimeline* self, int frameIndex, float time, float mix, int bendDirection) {
  891. frameIndex *= IKCONSTRAINT_ENTRIES;
  892. self->frames[frameIndex] = time;
  893. self->frames[frameIndex + IKCONSTRAINT_MIX] = mix;
  894. self->frames[frameIndex + IKCONSTRAINT_BEND_DIRECTION] = (float)bendDirection;
  895. }
  896. /**/
  897. static const int TRANSFORMCONSTRAINT_PREV_TIME = -5;
  898. static const int TRANSFORMCONSTRAINT_PREV_ROTATE = -4;
  899. static const int TRANSFORMCONSTRAINT_PREV_TRANSLATE = -3;
  900. static const int TRANSFORMCONSTRAINT_PREV_SCALE = -2;
  901. static const int TRANSFORMCONSTRAINT_PREV_SHEAR = -1;
  902. static const int TRANSFORMCONSTRAINT_ROTATE = 1;
  903. static const int TRANSFORMCONSTRAINT_TRANSLATE = 2;
  904. static const int TRANSFORMCONSTRAINT_SCALE = 3;
  905. static const int TRANSFORMCONSTRAINT_SHEAR = 4;
  906. void _spTransformConstraintTimeline_apply (const spTimeline* timeline, spSkeleton* skeleton, float lastTime, float time,
  907. spEvent** firedEvents, int* eventsCount, float alpha, int setupPose, int mixingOut) {
  908. int frame;
  909. float frameTime, percent, rotate, translate, scale, shear;
  910. spTransformConstraint* constraint;
  911. spTransformConstraintTimeline* self = (spTransformConstraintTimeline*)timeline;
  912. float *frames;
  913. int framesCount;
  914. constraint = skeleton->transformConstraints[self->transformConstraintIndex];
  915. if (time < self->frames[0]) {
  916. if (setupPose) {
  917. spTransformConstraintData* data = constraint->data;
  918. constraint->rotateMix = data->rotateMix;
  919. constraint->translateMix = data->translateMix;
  920. constraint->scaleMix = data->scaleMix;
  921. constraint->shearMix = data->shearMix;
  922. }
  923. return;
  924. }
  925. frames = self->frames;
  926. framesCount = self->framesCount;
  927. if (time >= frames[framesCount - TRANSFORMCONSTRAINT_ENTRIES]) { /* Time is after last frame. */
  928. int i = framesCount;
  929. rotate = frames[i + TRANSFORMCONSTRAINT_PREV_ROTATE];
  930. translate = frames[i + TRANSFORMCONSTRAINT_PREV_TRANSLATE];
  931. scale = frames[i + TRANSFORMCONSTRAINT_PREV_SCALE];
  932. shear = frames[i + TRANSFORMCONSTRAINT_PREV_SHEAR];
  933. } else {
  934. /* Interpolate between the previous frame and the current frame. */
  935. frame = binarySearch(frames, framesCount, time, TRANSFORMCONSTRAINT_ENTRIES);
  936. rotate = frames[frame + TRANSFORMCONSTRAINT_PREV_ROTATE];
  937. translate = frames[frame + TRANSFORMCONSTRAINT_PREV_TRANSLATE];
  938. scale = frames[frame + TRANSFORMCONSTRAINT_PREV_SCALE];
  939. shear = frames[frame + TRANSFORMCONSTRAINT_PREV_SHEAR];
  940. frameTime = frames[frame];
  941. percent = spCurveTimeline_getCurvePercent(SUPER(self), frame / TRANSFORMCONSTRAINT_ENTRIES - 1,
  942. 1 - (time - frameTime) / (frames[frame + TRANSFORMCONSTRAINT_PREV_TIME] - frameTime));
  943. rotate += (frames[frame + TRANSFORMCONSTRAINT_ROTATE] - rotate) * percent;
  944. translate += (frames[frame + TRANSFORMCONSTRAINT_TRANSLATE] - translate) * percent;
  945. scale += (frames[frame + TRANSFORMCONSTRAINT_SCALE] - scale) * percent;
  946. shear += (frames[frame + TRANSFORMCONSTRAINT_SHEAR] - shear) * percent;
  947. }
  948. if (setupPose) {
  949. spTransformConstraintData* data = constraint->data;
  950. constraint->rotateMix = data->rotateMix + (rotate - data->rotateMix) * alpha;
  951. constraint->translateMix = data->translateMix + (translate - data->translateMix) * alpha;
  952. constraint->scaleMix = data->scaleMix + (scale - data->scaleMix) * alpha;
  953. constraint->shearMix = data->shearMix + (shear - data->shearMix) * alpha;
  954. } else {
  955. constraint->rotateMix += (rotate - constraint->rotateMix) * alpha;
  956. constraint->translateMix += (translate - constraint->translateMix) * alpha;
  957. constraint->scaleMix += (scale - constraint->scaleMix) * alpha;
  958. constraint->shearMix += (shear - constraint->shearMix) * alpha;
  959. }
  960. UNUSED(lastTime);
  961. UNUSED(firedEvents);
  962. UNUSED(eventsCount);
  963. }
  964. int _spTransformConstraintTimeline_getPropertyId (const spTimeline* timeline) {
  965. return (SP_TIMELINE_TRANSFORMCONSTRAINT << 24) + SUB_CAST(spTransformConstraintTimeline, timeline)->transformConstraintIndex;
  966. }
  967. spTransformConstraintTimeline* spTransformConstraintTimeline_create (int framesCount) {
  968. return (spTransformConstraintTimeline*)_spBaseTimeline_create(framesCount, SP_TIMELINE_TRANSFORMCONSTRAINT, TRANSFORMCONSTRAINT_ENTRIES, _spTransformConstraintTimeline_apply, _spTransformConstraintTimeline_getPropertyId);
  969. }
  970. void spTransformConstraintTimeline_setFrame (spTransformConstraintTimeline* self, int frameIndex, float time, float rotateMix, float translateMix, float scaleMix, float shearMix) {
  971. frameIndex *= TRANSFORMCONSTRAINT_ENTRIES;
  972. self->frames[frameIndex] = time;
  973. self->frames[frameIndex + TRANSFORMCONSTRAINT_ROTATE] = rotateMix;
  974. self->frames[frameIndex + TRANSFORMCONSTRAINT_TRANSLATE] = translateMix;
  975. self->frames[frameIndex + TRANSFORMCONSTRAINT_SCALE] = scaleMix;
  976. self->frames[frameIndex + TRANSFORMCONSTRAINT_SHEAR] = shearMix;
  977. }
  978. /**/
  979. static const int PATHCONSTRAINTPOSITION_PREV_TIME = -2;
  980. static const int PATHCONSTRAINTPOSITION_PREV_VALUE = -1;
  981. static const int PATHCONSTRAINTPOSITION_VALUE = 1;
  982. void _spPathConstraintPositionTimeline_apply(const spTimeline* timeline, spSkeleton* skeleton, float lastTime, float time,
  983. spEvent** firedEvents, int* eventsCount, float alpha, int setupPose, int mixingOut) {
  984. int frame;
  985. float frameTime, percent, position;
  986. spPathConstraint* constraint;
  987. spPathConstraintPositionTimeline* self = (spPathConstraintPositionTimeline*)timeline;
  988. float* frames;
  989. int framesCount;
  990. constraint = skeleton->pathConstraints[self->pathConstraintIndex];
  991. if (time < self->frames[0]) {
  992. if (setupPose) {
  993. constraint->position = constraint->data->position;
  994. }
  995. return;
  996. }
  997. frames = self->frames;
  998. framesCount = self->framesCount;
  999. if (time >= frames[framesCount - PATHCONSTRAINTPOSITION_ENTRIES]) /* Time is after last frame. */
  1000. position = frames[framesCount + PATHCONSTRAINTPOSITION_PREV_VALUE];
  1001. else {
  1002. /* Interpolate between the previous frame and the current frame. */
  1003. frame = binarySearch(frames, framesCount, time, PATHCONSTRAINTPOSITION_ENTRIES);
  1004. position = frames[frame + PATHCONSTRAINTPOSITION_PREV_VALUE];
  1005. frameTime = frames[frame];
  1006. percent = spCurveTimeline_getCurvePercent(SUPER(self), frame / PATHCONSTRAINTPOSITION_ENTRIES - 1,
  1007. 1 - (time - frameTime) / (frames[frame + PATHCONSTRAINTPOSITION_PREV_TIME] - frameTime));
  1008. position += (frames[frame + PATHCONSTRAINTPOSITION_VALUE] - position) * percent;
  1009. }
  1010. if (setupPose)
  1011. constraint->position = constraint->data->position + (position - constraint->data->position) * alpha;
  1012. else
  1013. constraint->position += (position - constraint->position) * alpha;
  1014. UNUSED(lastTime);
  1015. UNUSED(firedEvents);
  1016. UNUSED(eventsCount);
  1017. }
  1018. int _spPathConstraintPositionTimeline_getPropertyId (const spTimeline* timeline) {
  1019. return (SP_TIMELINE_PATHCONSTRAINTPOSITION << 24) + SUB_CAST(spPathConstraintPositionTimeline, timeline)->pathConstraintIndex;
  1020. }
  1021. spPathConstraintPositionTimeline* spPathConstraintPositionTimeline_create (int framesCount) {
  1022. return (spPathConstraintPositionTimeline*)_spBaseTimeline_create(framesCount, SP_TIMELINE_PATHCONSTRAINTPOSITION, PATHCONSTRAINTPOSITION_ENTRIES, _spPathConstraintPositionTimeline_apply, _spPathConstraintPositionTimeline_getPropertyId);
  1023. }
  1024. void spPathConstraintPositionTimeline_setFrame (spPathConstraintPositionTimeline* self, int frameIndex, float time, float value) {
  1025. frameIndex *= PATHCONSTRAINTPOSITION_ENTRIES;
  1026. self->frames[frameIndex] = time;
  1027. self->frames[frameIndex + PATHCONSTRAINTPOSITION_VALUE] = value;
  1028. }
  1029. /**/
  1030. static const int PATHCONSTRAINTSPACING_PREV_TIME = -2;
  1031. static const int PATHCONSTRAINTSPACING_PREV_VALUE = -1;
  1032. static const int PATHCONSTRAINTSPACING_VALUE = 1;
  1033. void _spPathConstraintSpacingTimeline_apply(const spTimeline* timeline, spSkeleton* skeleton, float lastTime, float time,
  1034. spEvent** firedEvents, int* eventsCount, float alpha, int setupPose, int mixingOut) {
  1035. int frame;
  1036. float frameTime, percent, spacing;
  1037. spPathConstraint* constraint;
  1038. spPathConstraintSpacingTimeline* self = (spPathConstraintSpacingTimeline*)timeline;
  1039. float* frames;
  1040. int framesCount;
  1041. constraint = skeleton->pathConstraints[self->pathConstraintIndex];
  1042. if (time < self->frames[0]) {
  1043. if (setupPose) {
  1044. constraint->spacing = constraint->data->spacing;
  1045. }
  1046. return;
  1047. }
  1048. frames = self->frames;
  1049. framesCount = self->framesCount;
  1050. if (time >= frames[framesCount - PATHCONSTRAINTSPACING_ENTRIES]) /* Time is after last frame. */
  1051. spacing = frames[framesCount + PATHCONSTRAINTSPACING_PREV_VALUE];
  1052. else {
  1053. /* Interpolate between the previous frame and the current frame. */
  1054. frame = binarySearch(frames, framesCount, time, PATHCONSTRAINTSPACING_ENTRIES);
  1055. spacing = frames[frame + PATHCONSTRAINTSPACING_PREV_VALUE];
  1056. frameTime = frames[frame];
  1057. percent = spCurveTimeline_getCurvePercent(SUPER(self), frame / PATHCONSTRAINTSPACING_ENTRIES - 1,
  1058. 1 - (time - frameTime) / (frames[frame + PATHCONSTRAINTSPACING_PREV_TIME] - frameTime));
  1059. spacing += (frames[frame + PATHCONSTRAINTSPACING_VALUE] - spacing) * percent;
  1060. }
  1061. if (setupPose)
  1062. constraint->spacing = constraint->data->spacing + (spacing - constraint->data->spacing) * alpha;
  1063. else
  1064. constraint->spacing += (spacing - constraint->spacing) * alpha;
  1065. UNUSED(lastTime);
  1066. UNUSED(firedEvents);
  1067. UNUSED(eventsCount);
  1068. }
  1069. int _spPathConstraintSpacingTimeline_getPropertyId (const spTimeline* timeline) {
  1070. return (SP_TIMELINE_PATHCONSTRAINTSPACING << 24) + SUB_CAST(spPathConstraintSpacingTimeline, timeline)->pathConstraintIndex;
  1071. }
  1072. spPathConstraintSpacingTimeline* spPathConstraintSpacingTimeline_create (int framesCount) {
  1073. return (spPathConstraintSpacingTimeline*)_spBaseTimeline_create(framesCount, SP_TIMELINE_PATHCONSTRAINTSPACING, PATHCONSTRAINTSPACING_ENTRIES, _spPathConstraintSpacingTimeline_apply, _spPathConstraintSpacingTimeline_getPropertyId);
  1074. }
  1075. void spPathConstraintSpacingTimeline_setFrame (spPathConstraintSpacingTimeline* self, int frameIndex, float time, float value) {
  1076. frameIndex *= PATHCONSTRAINTSPACING_ENTRIES;
  1077. self->frames[frameIndex] = time;
  1078. self->frames[frameIndex + PATHCONSTRAINTSPACING_VALUE] = value;
  1079. }
  1080. /**/
  1081. static const int PATHCONSTRAINTMIX_PREV_TIME = -3;
  1082. static const int PATHCONSTRAINTMIX_PREV_ROTATE = -2;
  1083. static const int PATHCONSTRAINTMIX_PREV_TRANSLATE = -1;
  1084. static const int PATHCONSTRAINTMIX_ROTATE = 1;
  1085. static const int PATHCONSTRAINTMIX_TRANSLATE = 2;
  1086. void _spPathConstraintMixTimeline_apply(const spTimeline* timeline, spSkeleton* skeleton, float lastTime, float time,
  1087. spEvent** firedEvents, int* eventsCount, float alpha, int setupPose, int mixingOut) {
  1088. int frame;
  1089. float frameTime, percent, rotate, translate;
  1090. spPathConstraint* constraint;
  1091. spPathConstraintMixTimeline* self = (spPathConstraintMixTimeline*)timeline;
  1092. float* frames;
  1093. int framesCount;
  1094. constraint = skeleton->pathConstraints[self->pathConstraintIndex];
  1095. if (time < self->frames[0]) {
  1096. if (setupPose) {
  1097. constraint->rotateMix = constraint->data->rotateMix;
  1098. constraint->translateMix = constraint->data->translateMix;
  1099. }
  1100. return;
  1101. }
  1102. frames = self->frames;
  1103. framesCount = self->framesCount;
  1104. if (time >= frames[framesCount - PATHCONSTRAINTMIX_ENTRIES]) { /* Time is after last frame. */
  1105. rotate = frames[framesCount + PATHCONSTRAINTMIX_PREV_ROTATE];
  1106. translate = frames[framesCount + PATHCONSTRAINTMIX_PREV_TRANSLATE];
  1107. } else {
  1108. /* Interpolate between the previous frame and the current frame. */
  1109. frame = binarySearch(frames, framesCount, time, PATHCONSTRAINTMIX_ENTRIES);
  1110. rotate = frames[frame + PATHCONSTRAINTMIX_PREV_ROTATE];
  1111. translate = frames[frame + PATHCONSTRAINTMIX_PREV_TRANSLATE];
  1112. frameTime = frames[frame];
  1113. percent = spCurveTimeline_getCurvePercent(SUPER(self), frame / PATHCONSTRAINTMIX_ENTRIES - 1,
  1114. 1 - (time - frameTime) / (frames[frame + PATHCONSTRAINTMIX_PREV_TIME] - frameTime));
  1115. rotate += (frames[frame + PATHCONSTRAINTMIX_ROTATE] - rotate) * percent;
  1116. translate += (frames[frame + PATHCONSTRAINTMIX_TRANSLATE] - translate) * percent;
  1117. }
  1118. if (setupPose) {
  1119. constraint->rotateMix = constraint->data->rotateMix + (rotate - constraint->data->rotateMix) * alpha;
  1120. constraint->translateMix = constraint->data->translateMix + (translate - constraint->data->translateMix) * alpha;
  1121. } else {
  1122. constraint->rotateMix += (rotate - constraint->rotateMix) * alpha;
  1123. constraint->translateMix += (translate - constraint->translateMix) * alpha;
  1124. }
  1125. UNUSED(lastTime);
  1126. UNUSED(firedEvents);
  1127. UNUSED(eventsCount);
  1128. }
  1129. int _spPathConstraintMixTimeline_getPropertyId (const spTimeline* timeline) {
  1130. return (SP_TIMELINE_PATHCONSTRAINTMIX << 24) + SUB_CAST(spPathConstraintMixTimeline, timeline)->pathConstraintIndex;
  1131. }
  1132. spPathConstraintMixTimeline* spPathConstraintMixTimeline_create (int framesCount) {
  1133. return (spPathConstraintMixTimeline*)_spBaseTimeline_create(framesCount, SP_TIMELINE_PATHCONSTRAINTMIX, PATHCONSTRAINTMIX_ENTRIES, _spPathConstraintMixTimeline_apply, _spPathConstraintMixTimeline_getPropertyId);
  1134. }
  1135. void spPathConstraintMixTimeline_setFrame (spPathConstraintMixTimeline* self, int frameIndex, float time, float rotateMix, float translateMix) {
  1136. frameIndex *= PATHCONSTRAINTMIX_ENTRIES;
  1137. self->frames[frameIndex] = time;
  1138. self->frames[frameIndex + PATHCONSTRAINTMIX_ROTATE] = rotateMix;
  1139. self->frames[frameIndex + PATHCONSTRAINTMIX_TRANSLATE] = translateMix;
  1140. }