Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
SpineRuntimeC.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
22
23#include "./SpineRuntime.hpp"
24
25#include <spine/spine.h>
26#include <spine/extension.h>
27
28#include <cstdio>
29#include <cstring>
30#include <vector>
31
32#ifndef ES_SPINE_VERSION
33#error "ES_SPINE_VERSION must name the vendored spine-c release (38 / 41 / 42)."
34#endif
35
36// =============================================================================
37// spine-c Required Callbacks
38// =============================================================================
39//
40// The module never reads a file or creates a texture: the SDK loads atlas pages
41// and hands their ids back through setAtlasPageTexture. spine-c still insists the
42// hooks exist, so they exist and do nothing.
43
44void _spAtlasPage_createTexture(spAtlasPage* self, const char* path) {
45 (void)self;
46 (void)path;
47}
48
49void _spAtlasPage_disposeTexture(spAtlasPage* self) {
50 (void)self;
51}
52
53char* _spUtil_readFile(const char* path, int* length) {
54 (void)path;
55 *length = 0;
56 return nullptr;
57}
58
59namespace es::ES_SPINE_NS {
60
61// =============================================================================
62// Resources
63// =============================================================================
64
65struct Skeleton {
66 spAtlas* atlas = nullptr;
67 spSkeletonData* data = nullptr;
68 spAnimationStateData* stateData = nullptr;
69
70 Skeleton() = default;
71 Skeleton(const Skeleton&) = delete;
72 Skeleton& operator=(const Skeleton&) = delete;
73
74 // Reverse of construction order: stateData/data reference the atlas.
76 if (stateData) spAnimationStateData_dispose(stateData);
77 if (data) spSkeletonData_dispose(data);
78 if (atlas) spAtlas_dispose(atlas);
79 }
80};
81
82struct Instance {
83 spSkeleton* skeleton = nullptr;
84 spAnimationState* state = nullptr;
85
86 Instance() = default;
87 Instance(const Instance&) = delete;
88 Instance& operator=(const Instance&) = delete;
89
91 if (state) spAnimationState_dispose(state);
92 if (skeleton) spSkeleton_dispose(skeleton);
93 }
94};
95
96void destroy(Skeleton* skeleton) { delete skeleton; }
97void destroy(Instance* instance) { delete instance; }
98
99namespace {
100
102std::vector<float> g_worldVertices;
103
105spSkeletonClipping* g_clipper = nullptr;
106
107EventSink g_eventSink = nullptr;
108
109spAtlasPage* pageAt(const Skeleton* skeleton, int index) {
110 if (!skeleton || index < 0) return nullptr;
111 spAtlasPage* page = skeleton->atlas->pages;
112 for (int i = 0; i < index && page; ++i) page = page->next;
113 return page;
114}
115
116#if ES_SPINE_VERSION < 40
117// Before 4.0 an attachment pointed straight at its atlas region; the texture id
118// the SDK registered lives on that region's page.
119uint32_t regionTexture(spRegionAttachment* attachment) {
120 auto* region = reinterpret_cast<spAtlasRegion*>(attachment->rendererObject);
121 if (!region || !region->page) return 0;
122 return static_cast<uint32_t>(reinterpret_cast<uintptr_t>(region->page->rendererObject));
123}
124
125uint32_t meshTexture(spMeshAttachment* attachment) {
126 auto* region = reinterpret_cast<spAtlasRegion*>(attachment->rendererObject);
127 if (!region || !region->page) return 0;
128 return static_cast<uint32_t>(reinterpret_cast<uintptr_t>(region->page->rendererObject));
129}
130#else
131uint32_t regionTexture(spRegionAttachment* attachment) {
132 if (!attachment->region) return 0;
133 return static_cast<uint32_t>(reinterpret_cast<uintptr_t>(attachment->region->rendererObject));
134}
135
136uint32_t meshTexture(spMeshAttachment* attachment) {
137 if (!attachment->region) return 0;
138 return static_cast<uint32_t>(reinterpret_cast<uintptr_t>(attachment->region->rendererObject));
139}
140
144bool pagePremultiplied(spAtlasRegion* region) {
145 return region && region->page && region->page->pma;
146}
147#endif
148
149int blendModeOf(spSlot* slot) {
150 switch (slot->data->blendMode) {
151 case SP_BLEND_MODE_ADDITIVE: return 1;
152 case SP_BLEND_MODE_MULTIPLY: return 2;
153 case SP_BLEND_MODE_SCREEN: return 3;
154 default: return 0;
155 }
156}
157
158void eventListener(spAnimationState* state, spEventType type, spTrackEntry* entry, spEvent* event) {
159 (void)state;
160 if (!g_eventSink) return;
161
162 Event out;
163 out.source = state;
164 out.kind = static_cast<EventKind>(static_cast<int>(type));
165 out.track = entry ? entry->trackIndex : 0;
166 out.animation = (entry && entry->animation) ? entry->animation->name : nullptr;
167 if (type == SP_ANIMATION_EVENT && event) {
168 out.floatValue = event->floatValue;
169 out.intValue = event->intValue;
170 out.name = event->data->name;
171 out.stringValue = event->stringValue;
172 }
173 g_eventSink(out);
174}
175
178std::uint32_t g_probeEvents = 0;
179void countingListener(spAnimationState*, spEventType, spTrackEntry*, spEvent*) {
180 ++g_probeEvents;
181}
182
183} // namespace
184
185Skeleton* loadSkeleton(const void* skeletonData, int length,
186 const char* atlasText, int atlasLength,
187 bool binary, std::string& error) {
188 auto skeleton = std::make_unique<Skeleton>();
189
190 skeleton->atlas = spAtlas_create(atlasText, atlasLength, "", nullptr);
191 if (!skeleton->atlas || !skeleton->atlas->pages) {
192 error = "Failed to create atlas (invalid atlas text or no pages)";
193 return nullptr;
194 }
195
196 if (binary) {
197 spSkeletonBinary* reader = spSkeletonBinary_create(skeleton->atlas);
198 if (!reader) {
199 error = "Failed to create skeleton binary reader";
200 return nullptr;
201 }
202 reader->scale = 1.0f;
203 skeleton->data = spSkeletonBinary_readSkeletonData(
204 reader, static_cast<const unsigned char*>(skeletonData), length);
205 if (!skeleton->data && reader->error) error = reader->error;
206 spSkeletonBinary_dispose(reader);
207 } else {
208 spSkeletonJson* reader = spSkeletonJson_create(skeleton->atlas);
209 if (!reader) {
210 error = "Failed to create skeleton json reader";
211 return nullptr;
212 }
213 reader->scale = 1.0f;
214 skeleton->data = spSkeletonJson_readSkeletonData(
215 reader, static_cast<const char*>(skeletonData));
216 if (!skeleton->data && reader->error) error = reader->error;
217 spSkeletonJson_dispose(reader);
218 }
219
220 if (!skeleton->data) return nullptr;
221
222 skeleton->stateData = spAnimationStateData_create(skeleton->data);
223 skeleton->stateData->defaultMix = 0.2f;
224 return skeleton.release();
225}
226
227int atlasPageCount(const Skeleton* skeleton) {
228 if (!skeleton) return 0;
229 int count = 0;
230 for (spAtlasPage* page = skeleton->atlas->pages; page; page = page->next) ++count;
231 return count;
232}
233
234const char* atlasPageName(const Skeleton* skeleton, int page) {
235 spAtlasPage* p = pageAt(skeleton, page);
236 return p ? p->name : nullptr;
237}
238
239void setAtlasPageTexture(Skeleton* skeleton, int page, uint32_t texture, int width, int height) {
240 spAtlasPage* p = pageAt(skeleton, page);
241 if (!p) return;
242
243 void* handle = reinterpret_cast<void*>(static_cast<uintptr_t>(texture));
244 p->rendererObject = handle;
245 p->width = width;
246 p->height = height;
247
248#if ES_SPINE_VERSION >= 40
249 // 4.0 moved the renderer object onto the region; attachments read it there.
250 for (spAtlasRegion* region = skeleton->atlas->regions; region; region = region->next) {
251 if (region->page == p) region->super.rendererObject = handle;
252 }
253#endif
254}
255
256void setDefaultMix(Skeleton* skeleton, float seconds) {
257 if (skeleton && skeleton->stateData) skeleton->stateData->defaultMix = seconds;
258}
259
260void setMixDuration(Skeleton* skeleton, const char* from, const char* to, float seconds) {
261 if (!skeleton || !skeleton->stateData) return;
262 spAnimation* a = spSkeletonData_findAnimation(skeleton->data, from);
263 spAnimation* b = spSkeletonData_findAnimation(skeleton->data, to);
264 if (a && b) spAnimationStateData_setMix(skeleton->stateData, a, b, seconds);
265}
266
267// =============================================================================
268// Instances
269// =============================================================================
270
271Instance* createInstance(Skeleton* skeleton) {
272 if (!skeleton) return nullptr;
273
274 auto instance = std::make_unique<Instance>();
275 instance->skeleton = spSkeleton_create(skeleton->data);
276 instance->state = spAnimationState_create(skeleton->stateData);
277 if (!instance->skeleton || !instance->state) return nullptr;
278
279 spSkeleton_setToSetupPose(instance->skeleton);
280#if ES_SPINE_VERSION >= 42
281 spSkeleton_updateWorldTransform(instance->skeleton, SP_PHYSICS_UPDATE);
282#else
283 spSkeleton_updateWorldTransform(instance->skeleton);
284#endif
285 return instance.release();
286}
287
288void advanceAndApply(Instance* instance, float dt) {
289 if (!instance) return;
290 spAnimationState_update(instance->state, dt);
291 spAnimationState_apply(instance->state, instance->skeleton);
292}
293
294void materializeWorldPose(Instance* instance, float dt) {
295 if (!instance) return;
296#if ES_SPINE_VERSION >= 42
297 // 4.2 skeletons carry physics constraints, which advance with the clock —
298 // which is exactly why a skeleton that has any may not defer this.
299 spSkeleton_update(instance->skeleton, dt);
300 spSkeleton_updateWorldTransform(instance->skeleton, SP_PHYSICS_UPDATE);
301#else
302 (void)dt;
303 spSkeleton_updateWorldTransform(instance->skeleton);
304#endif
305}
306
307void update(Instance* instance, float dt) {
308 advanceAndApply(instance, dt);
309 materializeWorldPose(instance, dt);
310}
311
312float animationDuration(const Skeleton* skeleton, const char* animation) {
313 if (!skeleton || !skeleton->data) return -1.0f;
314 spAnimation* found = spSkeletonData_findAnimation(skeleton->data, animation);
315 return found ? found->duration : -1.0f;
316}
317
318bool requiresContinuousWorldPose(const Skeleton* skeleton) {
319 if (!skeleton || !skeleton->data) return false;
320#if ES_SPINE_VERSION >= 42
321 return skeleton->data->physicsConstraintsCount > 0;
322#else
323 return false;
324#endif
325}
326
327namespace {
328
331void countTrack(spTrackEntry* entry, PoseCounts& counts) {
332 for (; entry; entry = entry->mixingFrom) {
333 ++counts.entries;
334 if (!entry->animation) continue;
335#if ES_SPINE_VERSION >= 41
336 // 4.1 moved the timeline list into an array type; 3.8 counts them itself.
337 counts.timelines += static_cast<std::uint32_t>(entry->animation->timelines->size);
338#else
339 counts.timelines += static_cast<std::uint32_t>(entry->animation->timelinesCount);
340#endif
341 }
342}
343
344} // namespace
345
346bool poseStage(Instance* instance, float dt, int stage, PoseCounts* counts) {
347 if (!instance) return false;
348
349 if (counts) {
350 *counts = PoseCounts{};
351 for (int i = 0; i < instance->state->tracksCount; ++i) {
352 spTrackEntry* entry = instance->state->tracks[i];
353 if (!entry) continue;
354 ++counts->tracks;
355 countTrack(entry, *counts);
356 }
357 const spSkeleton* skeleton = instance->skeleton;
358 counts->bones = static_cast<std::uint32_t>(skeleton->bonesCount);
359 counts->ikConstraints = static_cast<std::uint32_t>(skeleton->ikConstraintsCount);
360 counts->transformConstraints = static_cast<std::uint32_t>(skeleton->transformConstraintsCount);
361 counts->pathConstraints = static_cast<std::uint32_t>(skeleton->pathConstraintsCount);
362#if ES_SPINE_VERSION >= 42
363 counts->physicsConstraints = static_cast<std::uint32_t>(skeleton->physicsConstraintsCount);
364#endif
365 }
366
367 if (stage >= POSE_ADVANCE) spAnimationState_update(instance->state, dt);
368 if (stage >= POSE_APPLY) {
369 spAnimationStateListener installed = instance->state->listener;
370 instance->state->listener = countingListener;
371 g_probeEvents = 0;
372 spAnimationState_apply(instance->state, instance->skeleton);
373 instance->state->listener = installed;
374 if (counts) counts->events = g_probeEvents;
375 }
376 if (stage >= POSE_WORLD) {
377#if ES_SPINE_VERSION >= 42
378 spSkeleton_update(instance->skeleton, dt);
379 spSkeleton_updateWorldTransform(instance->skeleton, SP_PHYSICS_UPDATE);
380#else
381 spSkeleton_updateWorldTransform(instance->skeleton);
382#endif
383 }
384 return true;
385}
386
387void setToSetupPose(Instance* instance) {
388 if (!instance) return;
389 spAnimationState_clearTracks(instance->state);
390 spSkeleton_setToSetupPose(instance->skeleton);
391}
392
393bool playAnimation(Instance* instance, const char* animation, bool loop, int track) {
394 if (!instance) return false;
395 // Resolved HERE: spine-c builds a track entry around a null animation and
396 // hands it back, so a non-null entry says nothing about the name — and
397 // posing that entry reads address zero, which on wasm neither crashes nor draws.
398 if (!spSkeletonData_findAnimation(instance->skeleton->data, animation)) return false;
399 return spAnimationState_setAnimationByName(instance->state, track, animation, loop) != nullptr;
400}
401
402bool addAnimation(Instance* instance, const char* animation, bool loop, float delay, int track) {
403 if (!instance) return false;
404 if (!spSkeletonData_findAnimation(instance->skeleton->data, animation)) return false;
405 return spAnimationState_addAnimationByName(instance->state, track, animation, loop, delay) != nullptr;
406}
407
408void setSkin(Instance* instance, const char* skin) {
409 if (!instance) return;
410 if (!skin || skin[0] == '\0') {
411 spSkeleton_setSkin(instance->skeleton, nullptr);
412 } else {
413 spSkeleton_setSkinByName(instance->skeleton, skin);
414 }
415 spSkeleton_setSlotsToSetupPose(instance->skeleton);
416}
417
418void setTrackAlpha(Instance* instance, int track, float alpha) {
419 if (!instance) return;
420 spTrackEntry* entry = spAnimationState_getCurrent(instance->state, track);
421 if (entry) entry->alpha = alpha;
422}
423
424void setEventSink(EventSink sink) {
425 g_eventSink = sink;
426}
427
428const void* eventSource(Instance* instance) {
429 return instance ? instance->state : nullptr;
430}
431
432void enableEvents(Instance* instance) {
433 if (instance) instance->state->listener = eventListener;
434}
435
436// =============================================================================
437// Queries
438// =============================================================================
439
440int animationCount(const Instance* instance) {
441 return instance ? instance->skeleton->data->animationsCount : 0;
442}
443
444const char* animationName(const Instance* instance, int index) {
445 if (!instance) return nullptr;
446 spSkeletonData* data = instance->skeleton->data;
447 if (index < 0 || index >= data->animationsCount) return nullptr;
448 return data->animations[index]->name;
449}
450
451int skinCount(const Instance* instance) {
452 return instance ? instance->skeleton->data->skinsCount : 0;
453}
454
455const char* skinName(const Instance* instance, int index) {
456 if (!instance) return nullptr;
457 spSkeletonData* data = instance->skeleton->data;
458 if (index < 0 || index >= data->skinsCount) return nullptr;
459 return data->skins[index]->name;
460}
461
462bool bonePosition(const Instance* instance, const char* bone, float* x, float* y) {
463 if (!instance) return false;
464 spBone* b = spSkeleton_findBone(instance->skeleton, bone);
465 if (!b) return false;
466 *x = b->worldX;
467 *y = b->worldY;
468 return true;
469}
470
471bool boneRotation(const Instance* instance, const char* bone, float* degrees) {
472 if (!instance) return false;
473 spBone* b = spSkeleton_findBone(instance->skeleton, bone);
474 if (!b) return false;
475 *degrees = spBone_getWorldRotationX(b);
476 return true;
477}
478
479int boneCount(const Instance* instance) {
480 return instance ? instance->skeleton->bonesCount : 0;
481}
482
483const char* boneName(const Instance* instance, int index) {
484 if (!instance || index < 0 || index >= instance->skeleton->bonesCount) return nullptr;
485 return instance->skeleton->bones[index]->data->name;
486}
487
488int boneParent(const Instance* instance, int index) {
489 if (!instance || index < 0 || index >= instance->skeleton->bonesCount) return -1;
490 const spBone* parent = instance->skeleton->bones[index]->parent;
491 return parent ? parent->data->index : -1;
492}
493
494bool bonePose(const Instance* instance, int index, float* x, float* y, float* degrees, float* length) {
495 if (!instance || index < 0 || index >= instance->skeleton->bonesCount) return false;
496 spBone* b = instance->skeleton->bones[index];
497 *x = b->worldX;
498 *y = b->worldY;
499 *degrees = spBone_getWorldRotationX(b);
500 *length = b->data->length;
501 return true;
502}
503
504bool skeletonBounds(const Skeleton* skeleton, float* x, float* y, float* width, float* height) {
505 if (!skeleton || !skeleton->data) return false;
506 *x = skeleton->data->x;
507 *y = skeleton->data->y;
508 *width = skeleton->data->width;
509 *height = skeleton->data->height;
510 return true;
511}
512
513void bounds(const Instance* instance, float* x, float* y, float* width, float* height) {
514 *x = *y = *width = *height = 0.0f;
515 if (!instance) return;
516
517 spSkeleton* skeleton = instance->skeleton;
518 float minX = 1e30f, minY = 1e30f, maxX = -1e30f, maxY = -1e30f;
519 bool any = false;
520
521 for (int i = 0; i < skeleton->slotsCount; ++i) {
522 spSlot* slot = skeleton->drawOrder[i];
523 if (!slot->attachment) continue;
524
525 int vertexCount = 0;
526 if (slot->attachment->type == SP_ATTACHMENT_REGION) {
527 auto* region = reinterpret_cast<spRegionAttachment*>(slot->attachment);
528 g_worldVertices.resize(8);
529#if ES_SPINE_VERSION < 40
530 spRegionAttachment_computeWorldVertices(region, slot->bone, g_worldVertices.data(), 0, 2);
531#else
532 spRegionAttachment_computeWorldVertices(region, slot, g_worldVertices.data(), 0, 2);
533#endif
534 vertexCount = 4;
535 } else if (slot->attachment->type == SP_ATTACHMENT_MESH) {
536 auto* mesh = reinterpret_cast<spMeshAttachment*>(slot->attachment);
537 const int length = SUPER(mesh)->worldVerticesLength;
538 g_worldVertices.resize(length);
539 spVertexAttachment_computeWorldVertices(SUPER(mesh), slot, 0, length,
540 g_worldVertices.data(), 0, 2);
541 vertexCount = length / 2;
542 }
543
544 for (int j = 0; j < vertexCount; ++j) {
545 const float vx = g_worldVertices[j * 2];
546 const float vy = g_worldVertices[j * 2 + 1];
547 if (vx < minX) minX = vx;
548 if (vx > maxX) maxX = vx;
549 if (vy < minY) minY = vy;
550 if (vy > maxY) maxY = vy;
551 any = true;
552 }
553 }
554
555 if (!any) return;
556 *x = minX;
557 *y = minY;
558 *width = maxX - minX;
559 *height = maxY - minY;
560}
561
562int constraintCount(const Instance* instance, ConstraintKind kind) {
563 if (!instance) return 0;
564 switch (kind) {
565 case ConstraintKind::Ik: return instance->skeleton->ikConstraintsCount;
566 case ConstraintKind::Transform: return instance->skeleton->transformConstraintsCount;
567 case ConstraintKind::Path: return instance->skeleton->pathConstraintsCount;
568 }
569 return 0;
570}
571
572const char* constraintName(const Instance* instance, ConstraintKind kind, int index) {
573 if (!instance || index < 0 || index >= constraintCount(instance, kind)) return nullptr;
574 switch (kind) {
575 case ConstraintKind::Ik: return instance->skeleton->ikConstraints[index]->data->name;
576 case ConstraintKind::Transform: return instance->skeleton->transformConstraints[index]->data->name;
577 case ConstraintKind::Path: return instance->skeleton->pathConstraints[index]->data->name;
578 }
579 return nullptr;
580}
581
582bool transformMix(const Instance* instance, const char* name, TransformMix* out) {
583 if (!instance) return false;
584 auto* c = spSkeleton_findTransformConstraint(instance->skeleton, name);
585 if (!c) return false;
586#if ES_SPINE_VERSION < 40
587 // One mix per channel before 4.0; report it on both axes so the SDK sees one shape.
588 out->rotate = c->rotateMix;
589 out->x = out->y = c->translateMix;
590 out->scaleX = out->scaleY = c->scaleMix;
591 out->shearY = c->shearMix;
592#else
593 out->rotate = c->mixRotate;
594 out->x = c->mixX;
595 out->y = c->mixY;
596 out->scaleX = c->mixScaleX;
597 out->scaleY = c->mixScaleY;
598 out->shearY = c->mixShearY;
599#endif
600 return true;
601}
602
603bool setTransformMix(Instance* instance, const char* name, const TransformMix& mix) {
604 if (!instance) return false;
605 auto* c = spSkeleton_findTransformConstraint(instance->skeleton, name);
606 if (!c) return false;
607#if ES_SPINE_VERSION < 40
608 c->rotateMix = mix.rotate;
609 c->translateMix = (mix.x + mix.y) * 0.5f;
610 c->scaleMix = (mix.scaleX + mix.scaleY) * 0.5f;
611 c->shearMix = mix.shearY;
612#else
613 c->mixRotate = mix.rotate;
614 c->mixX = mix.x;
615 c->mixY = mix.y;
616 c->mixScaleX = mix.scaleX;
617 c->mixScaleY = mix.scaleY;
618 c->mixShearY = mix.shearY;
619#endif
620 return true;
621}
622
623bool pathMix(const Instance* instance, const char* name, PathMix* out) {
624 if (!instance) return false;
625 auto* c = spSkeleton_findPathConstraint(instance->skeleton, name);
626 if (!c) return false;
627 out->position = c->position;
628 out->spacing = c->spacing;
629#if ES_SPINE_VERSION < 40
630 out->rotate = c->rotateMix;
631 out->x = out->y = c->translateMix;
632#else
633 out->rotate = c->mixRotate;
634 out->x = c->mixX;
635 out->y = c->mixY;
636#endif
637 return true;
638}
639
640bool setPathMix(Instance* instance, const char* name, const PathMix& mix) {
641 if (!instance) return false;
642 auto* c = spSkeleton_findPathConstraint(instance->skeleton, name);
643 if (!c) return false;
644 c->position = mix.position;
645 c->spacing = mix.spacing;
646#if ES_SPINE_VERSION < 40
647 c->rotateMix = mix.rotate;
648 c->translateMix = (mix.x + mix.y) * 0.5f;
649#else
650 c->mixRotate = mix.rotate;
651 c->mixX = mix.x;
652 c->mixY = mix.y;
653#endif
654 return true;
655}
656
657// =============================================================================
658// Posing
659// =============================================================================
660
661bool setAttachment(Instance* instance, const char* slot, const char* attachment) {
662 if (!instance) return false;
663 const char* name = (attachment && attachment[0] != '\0') ? attachment : nullptr;
664 return spSkeleton_setAttachment(instance->skeleton, slot, name) != 0;
665}
666
667bool setIkTarget(Instance* instance, const char* constraint, float x, float y, float mix) {
668 if (!instance) return false;
669 spIkConstraint* c = spSkeleton_findIkConstraint(instance->skeleton, constraint);
670 if (!c) return false;
671 c->target->x = x;
672 c->target->y = y;
673 c->mix = mix;
674 return true;
675}
676
677bool setSlotColor(Instance* instance, const char* slot, float r, float g, float b, float a) {
678 if (!instance) return false;
679 spSlot* s = spSkeleton_findSlot(instance->skeleton, slot);
680 if (!s) return false;
681 s->color.r = r;
682 s->color.g = g;
683 s->color.b = b;
684 s->color.a = a;
685 return true;
686}
687
688bool setSkeletonColor(Instance* instance, float r, float g, float b, float a) {
689 if (!instance) return false;
690 instance->skeleton->color.r = r;
691 instance->skeleton->color.g = g;
692 instance->skeleton->color.b = b;
693 instance->skeleton->color.a = a;
694 return true;
695}
696
697// =============================================================================
698// Geometry
699// =============================================================================
700
701namespace {
702
709struct ClipRegion {
710 float minX, minY, maxX, maxY;
712 std::uint32_t edges;
716 bool convex;
717};
718ClipRegion g_clipRegion{};
719
720void captureClipRegion() {
721 const spFloatArray* polygon = g_clipper->clippingPolygon;
722 g_clipRegion = ClipRegion{};
723 if (!polygon || polygon->size < 2) return;
724 g_clipRegion.minX = g_clipRegion.maxX = polygon->items[0];
725 g_clipRegion.minY = g_clipRegion.maxY = polygon->items[1];
726 for (int i = 2; i < polygon->size; i += 2) {
727 const float x = polygon->items[i];
728 const float y = polygon->items[i + 1];
729 g_clipRegion.minX = x < g_clipRegion.minX ? x : g_clipRegion.minX;
730 g_clipRegion.maxX = x > g_clipRegion.maxX ? x : g_clipRegion.maxX;
731 g_clipRegion.minY = y < g_clipRegion.minY ? y : g_clipRegion.minY;
732 g_clipRegion.maxY = y > g_clipRegion.maxY ? y : g_clipRegion.maxY;
733 }
734 g_clipRegion.convex = g_clipper->clippingPolygons
735 && g_clipper->clippingPolygons->size == 1;
736 if (g_clipper->clippingPolygons) {
737 for (int p = 0; p < g_clipper->clippingPolygons->size; ++p) {
738 g_clipRegion.edges +=
739 static_cast<std::uint32_t>(g_clipper->clippingPolygons->items[p]->size / 2);
740 }
741 }
742}
743
745bool outsideClipBounds(const float* positions, int vertexCount) {
746 float minX = positions[0], maxX = positions[0];
747 float minY = positions[1], maxY = positions[1];
748 for (int i = 2; i < vertexCount * 2; i += 2) {
749 const float x = positions[i];
750 const float y = positions[i + 1];
751 minX = x < minX ? x : minX;
752 maxX = x > maxX ? x : maxX;
753 minY = y < minY ? y : minY;
754 maxY = y > maxY ? y : maxY;
755 }
756 return maxX < g_clipRegion.minX || minX > g_clipRegion.maxX
757 || maxY < g_clipRegion.minY || minY > g_clipRegion.maxY;
758}
759
766bool insideConvexClip(const float* positions, int vertexCount) {
767 if (!g_clipRegion.convex) return false;
768 const spFloatArray* polygon = g_clipper->clippingPolygons->items[0];
769 const float* edges = polygon->items;
770 for (int e = 0; e <= polygon->size - 4; e += 2) {
771 const float deltaX = edges[e] - edges[e + 2];
772 const float deltaY = edges[e + 1] - edges[e + 3];
773 const float edgeX2 = edges[e + 2], edgeY2 = edges[e + 3];
774 for (int i = 0; i < vertexCount * 2; i += 2) {
775 if (deltaX * (positions[i + 1] - edgeY2) - deltaY * (positions[i] - edgeX2) <= 0) {
776 return false;
777 }
778 }
779 }
780 return true;
781}
782
783namespace {
784
790void makeClockwise(spFloatArray* polygon) {
791 float* vertices = polygon->items;
792 const int length = polygon->size;
793 float area = vertices[length - 2] * vertices[1] - vertices[0] * vertices[length - 1];
794 for (int i = 0, n = length - 3; i < n; i += 2) {
795 const float p1x = vertices[i], p1y = vertices[i + 1];
796 const float p2x = vertices[i + 2], p2y = vertices[i + 3];
797 area += p1x * p2y - p2x * p1y;
798 }
799 if (area < 0) return;
800 for (int i = 0, lastX = length - 2, n = length >> 1; i < n; i += 2) {
801 const float x = vertices[i], y = vertices[i + 1];
802 const int other = lastX - i;
803 vertices[i] = vertices[other];
804 vertices[i + 1] = vertices[other + 1];
805 vertices[other] = x;
806 vertices[other + 1] = y;
807 }
808}
809
811spSlot* firstClipSlot(spSkeleton* skeleton, spClippingAttachment** clip) {
812 for (int i = 0; i < skeleton->slotsCount; ++i) {
813 spSlot* slot = skeleton->drawOrder[i];
814 if (!slot || !slot->attachment) continue;
815 if (slot->attachment->type != SP_ATTACHMENT_CLIPPING) continue;
816 *clip = reinterpret_cast<spClippingAttachment*>(slot->attachment);
817 return slot;
818 }
819 return nullptr;
820}
821
823spArrayFloatArray* g_convexPolygons = nullptr;
824
831bool isDefinitelyConvex(const spFloatArray* polygon) {
832 const int count = polygon->size / 2;
833 if (count < 3) return false;
834 const float* v = polygon->items;
835
836 float minX = v[0], maxX = v[0], minY = v[1], maxY = v[1];
837 for (int i = 2; i < polygon->size; i += 2) {
838 minX = v[i] < minX ? v[i] : minX;
839 maxX = v[i] > maxX ? v[i] : maxX;
840 minY = v[i + 1] < minY ? v[i + 1] : minY;
841 maxY = v[i + 1] > maxY ? v[i + 1] : maxY;
842 }
843 const float span = (maxX - minX) > (maxY - minY) ? (maxX - minX) : (maxY - minY);
844 if (!(span > 0.0f)) return false;
845 const float crossEpsilon = span * span * 1e-7f;
846
847 int sign = 0;
848 int xTurns = 0;
849 int yTurns = 0;
850 float previousDx = 0.0f;
851 float previousDy = 0.0f;
852 for (int i = 0; i < count; ++i) {
853 const int a = i * 2;
854 const int b = ((i + 1) % count) * 2;
855 const int c = ((i + 2) % count) * 2;
856 const float dx = v[b] - v[a], dy = v[b + 1] - v[a + 1];
857 const float ex = v[c] - v[b], ey = v[c + 1] - v[b + 1];
858 const float cross = dx * ey - dy * ex;
859 if (cross > -crossEpsilon && cross < crossEpsilon) return false;
860 const int turn = cross > 0.0f ? 1 : -1;
861 if (sign == 0) sign = turn;
862 else if (turn != sign) return false;
863
864 // A simple convex polygon's edge directions cross each axis exactly
865 // twice; a star keeps a consistent turn but crosses them four times.
866 if (i > 0) {
867 if ((dx < 0.0f) != (previousDx < 0.0f)) ++xTurns;
868 if ((dy < 0.0f) != (previousDy < 0.0f)) ++yTurns;
869 }
870 previousDx = dx;
871 previousDy = dy;
872 }
873 if ((v[0] - v[(count - 1) * 2] < 0.0f) != (previousDx < 0.0f)) ++xTurns;
874 if ((v[1] - v[(count - 1) * 2 + 1] < 0.0f) != (previousDy < 0.0f)) ++yTurns;
875 return xTurns <= 2 && yTurns <= 2;
876}
877
884spArrayFloatArray* asSingleConvexPiece(spFloatArray* polygon) {
885 if (!g_convexPolygons) {
886 g_convexPolygons = spArrayFloatArray_create(1);
887 spArrayFloatArray_add(g_convexPolygons, spFloatArray_create(16));
888 }
889 spFloatArray* piece = g_convexPolygons->items[0];
890 spFloatArray_clear(piece);
891 const int last = polygon->size - 2;
892 spFloatArray_addAllValues(piece, polygon->items, last, 2);
893 spFloatArray_addAllValues(piece, polygon->items, 0, last);
894 spFloatArray_add(piece, piece->items[0]);
895 spFloatArray_add(piece, piece->items[1]);
896 g_convexPolygons->size = 1;
897 return g_convexPolygons;
898}
899
905template <bool COUNT>
906int openClipRegion(spSlot* slot, spClippingAttachment* clip, ProbeCounts* counts) {
907 if (g_clipper->clipAttachment) return 0;
908 g_clipper->clipAttachment = clip;
909
910 const int length = clip->super.worldVerticesLength;
911 float* vertices = spFloatArray_setSize(g_clipper->clippingPolygon, length)->items;
912 spVertexAttachment_computeWorldVertices(&clip->super, slot, 0, length, vertices, 0, 2);
913 makeClockwise(g_clipper->clippingPolygon);
914
915 if (isDefinitelyConvex(g_clipper->clippingPolygon)) {
916 if constexpr (COUNT) counts->clipConvexBypasses++;
917 g_clipper->clippingPolygons = asSingleConvexPiece(g_clipper->clippingPolygon);
918 return 1;
919 }
920
921 if constexpr (COUNT) counts->clipDecompositions++;
922 g_clipper->clippingPolygons = spTriangulator_decompose(
923 g_clipper->triangulator, g_clipper->clippingPolygon,
924 spTriangulator_triangulate(g_clipper->triangulator, g_clipper->clippingPolygon));
925 for (int i = 0; i < g_clipper->clippingPolygons->size; ++i) {
926 spFloatArray* piece = g_clipper->clippingPolygons->items[i];
927 makeClockwise(piece);
928 spFloatArray_add(piece, piece->items[0]);
929 spFloatArray_add(piece, piece->items[1]);
930 }
931 return g_clipper->clippingPolygons->size;
932}
933
934} // namespace
935
942template <int STAGE, bool COUNT>
943void emit(TriangleSink& sink, bool clipping,
944 float* positions, int vertexCount, float* uvs,
945 unsigned short* triangles, int triangleCount,
946 uint32_t texture, int blendMode, const float rgba[4],
947 ProbeCounts* counts) {
948 float* outPositions = positions;
949 float* outUVs = uvs;
950 unsigned short* outTriangles = triangles;
951 int outVertices = vertexCount;
952 int outTriangleCount = triangleCount;
953
954 if (STAGE >= STAGE_CLIP && clipping && spSkeletonClipping_isClipping(g_clipper)) {
955 // Neither changes what is drawn. The first answers "nothing of this
956 // survives" without cutting; the second answers "all of it does", and
957 // keeps the attachment's own topology instead of a per-triangle rebuild.
958 if constexpr (COUNT) {
959 counts->clipCandidateTriangles += static_cast<std::uint32_t>(triangleCount / 3);
960 }
961 if (outsideClipBounds(positions, vertexCount)) {
962 if constexpr (COUNT) {
963 counts->clipBoundsRejects++;
964 counts->clipRejectedTriangles += static_cast<std::uint32_t>(triangleCount / 3);
965 }
966 return;
967 }
968 if (insideConvexClip(positions, vertexCount)) {
969 if constexpr (COUNT) {
970 counts->clipInsideAccepts++;
971 counts->clipBypassedTriangles += static_cast<std::uint32_t>(triangleCount / 3);
972 }
973 } else {
974 spSkeletonClipping_clipTriangles(g_clipper, positions, vertexCount * 2,
975 triangles, triangleCount, uvs, 2);
976 outPositions = g_clipper->clippedVertices->items;
977 outVertices = g_clipper->clippedVertices->size / 2;
978 outUVs = g_clipper->clippedUVs->items;
979 outTriangles = g_clipper->clippedTriangles->items;
980 outTriangleCount = g_clipper->clippedTriangles->size;
981 if constexpr (COUNT) {
982 counts->clippedEmits++;
983 counts->clipInputTriangles += static_cast<std::uint32_t>(triangleCount / 3);
984 counts->clipOutputTriangles += static_cast<std::uint32_t>(outTriangleCount / 3);
985 counts->clipInputVertices += static_cast<std::uint32_t>(vertexCount);
986 counts->clipOutputVertices += static_cast<std::uint32_t>(outVertices);
987 counts->clipEdgeWork +=
988 static_cast<std::uint32_t>(triangleCount / 3) * g_clipRegion.edges;
989 }
990 }
991 }
992
993 if (outVertices == 0 || outTriangleCount == 0) return;
994 if constexpr (COUNT) {
995 counts->emits++;
996 counts->verticesEmitted += static_cast<std::uint32_t>(outVertices);
997 counts->indicesEmitted += static_cast<std::uint32_t>(outTriangleCount);
998 }
999 if constexpr (STAGE < STAGE_EMIT) return;
1000 sink.emit(outPositions, outUVs, outVertices, outTriangles, outTriangleCount,
1001 texture, blendMode, rgba);
1002}
1003
1004} // namespace
1005
1011template <int STAGE, bool COUNT>
1012void renderImpl(Instance* instance, TriangleSink& sink, bool clipping, ProbeCounts* counts) {
1013 if (!instance) return;
1014 if (!g_clipper) g_clipper = spSkeletonClipping_create();
1015 if constexpr (STAGE < STAGE_TRAVERSE) return;
1016
1017 spSkeleton* skeleton = instance->skeleton;
1018 const spColor& skeletonColor = skeleton->color;
1019
1020 for (int i = 0; i < skeleton->slotsCount; ++i) {
1021 if constexpr (COUNT) counts->slots++;
1022 spSlot* slot = skeleton->drawOrder[i];
1023 if (!slot) continue;
1024
1025 spAttachment* attachment = slot->attachment;
1026
1027 // A clipping attachment opens a region that runs until its end slot; it
1028 // draws nothing itself.
1029 if (attachment && attachment->type == SP_ATTACHMENT_CLIPPING) {
1030 if constexpr (COUNT) counts->clipStarts++;
1031 if (clipping && STAGE >= STAGE_CLIP_START) {
1032 auto* clip = reinterpret_cast<spClippingAttachment*>(attachment);
1033 const int pieces = openClipRegion<COUNT>(slot, clip, counts);
1034 captureClipRegion();
1035 if constexpr (COUNT) {
1036 counts->clipPolygons += static_cast<std::uint32_t>(pieces);
1037 counts->clipPolygonVertices +=
1038 static_cast<std::uint32_t>(clip->super.worldVerticesLength / 2);
1039 for (int p = 0; p < g_clipper->clippingPolygons->size; ++p) {
1040 counts->clipPolygonEdges +=
1041 static_cast<std::uint32_t>(g_clipper->clippingPolygons->items[p]->size / 2);
1042 }
1043 }
1044 }
1045 continue;
1046 }
1047
1048 bool visible = attachment != nullptr;
1049#if ES_SPINE_VERSION >= 42
1050 if (visible && !slot->data->visible) visible = false;
1051#endif
1052
1053 if (visible) {
1054 const spColor& slotColor = slot->color;
1055 const int blendMode = blendModeOf(slot);
1056
1057 if (attachment->type == SP_ATTACHMENT_REGION) {
1058 if constexpr (COUNT) counts->regionAttachments++;
1059 // Nothing below this is what a traversal costs.
1060 if constexpr (STAGE < STAGE_VERTICES) continue;
1061 auto* region = reinterpret_cast<spRegionAttachment*>(attachment);
1062 g_worldVertices.resize(8);
1063#if ES_SPINE_VERSION < 40
1064 spRegionAttachment_computeWorldVertices(region, slot->bone,
1065 g_worldVertices.data(), 0, 2);
1066 const bool pma = false; // no page carries a pma flag before 4.0
1067#else
1068 // Applies the attachment's SEQUENCE, if it has one — which swaps the
1069 // region this attachment points at for the frame the slot is on, and
1070 // with it the page, the uvs and the renderer object. So everything
1071 // that identifies the artwork must be read AFTER this call: a
1072 // sequence whose frames sit on different atlas pages (an effect
1073 // flipbook usually does) otherwise draws the new frame's uvs against
1074 // the previous frame's texture, and the effect samples empty space.
1075 spRegionAttachment_computeWorldVertices(region, slot,
1076 g_worldVertices.data(), 0, 2);
1077 const bool pma = pagePremultiplied(reinterpret_cast<spAtlasRegion*>(region->region));
1078#endif
1079 const uint32_t texture = regionTexture(region);
1080 if (texture) {
1081 const spColor& attachmentColor = region->color;
1082 float rgba[4] = {
1083 skeletonColor.r * slotColor.r * attachmentColor.r,
1084 skeletonColor.g * slotColor.g * attachmentColor.g,
1085 skeletonColor.b * slotColor.b * attachmentColor.b,
1086 skeletonColor.a * slotColor.a * attachmentColor.a,
1087 };
1088 sink.setPremultipliedColor(pma);
1089 premultiplyTint(rgba, pma);
1090
1091 if constexpr (COUNT) counts->verticesGenerated += 4;
1092 static unsigned short quad[6] = {0, 1, 2, 2, 3, 0};
1093 emit<STAGE, COUNT>(sink, clipping, g_worldVertices.data(), 4, region->uvs,
1094 quad, 6, texture, blendForPage(blendMode, pma), rgba,
1095 counts);
1096 }
1097 } else if (attachment->type == SP_ATTACHMENT_MESH) {
1098 if constexpr (COUNT) counts->meshAttachments++;
1099 if constexpr (STAGE < STAGE_VERTICES) continue;
1100 auto* mesh = reinterpret_cast<spMeshAttachment*>(attachment);
1101 const int length = SUPER(mesh)->worldVerticesLength;
1102 g_worldVertices.resize(length);
1103 // Applies this attachment's SEQUENCE — see the region branch above:
1104 // the texture, the region and the uvs are only settled once this has
1105 // run, so none of them may be read before it.
1106 spVertexAttachment_computeWorldVertices(SUPER(mesh), slot, 0, length,
1107 g_worldVertices.data(), 0, 2);
1108#if ES_SPINE_VERSION < 40
1109 const bool pma = false; // no page carries a pma flag before 4.0
1110#else
1111 const bool pma = pagePremultiplied(reinterpret_cast<spAtlasRegion*>(mesh->region));
1112#endif
1113 const uint32_t texture = meshTexture(mesh);
1114 if (texture) {
1115 const spColor& attachmentColor = mesh->color;
1116 float rgba[4] = {
1117 skeletonColor.r * slotColor.r * attachmentColor.r,
1118 skeletonColor.g * slotColor.g * attachmentColor.g,
1119 skeletonColor.b * slotColor.b * attachmentColor.b,
1120 skeletonColor.a * slotColor.a * attachmentColor.a,
1121 };
1122 sink.setPremultipliedColor(pma);
1123 premultiplyTint(rgba, pma);
1124
1125 if constexpr (COUNT) counts->verticesGenerated += length / 2;
1126 emit<STAGE, COUNT>(sink, clipping, g_worldVertices.data(), length / 2,
1127 mesh->uvs, mesh->triangles, mesh->trianglesCount,
1128 texture, blendForPage(blendMode, pma), rgba, counts);
1129 }
1130 }
1131 }
1132
1133 // Closes the region when this slot is the clip's end slot; a cheap no-op
1134 // otherwise, which is why every non-clip slot passes through here.
1135 if (clipping && STAGE >= STAGE_CLIP_START) spSkeletonClipping_clipEnd(g_clipper, slot);
1136 }
1137
1138 if (clipping && STAGE >= STAGE_CLIP_START) spSkeletonClipping_clipEnd2(g_clipper);
1139}
1140
1141bool clipStorage(ClipStorage* out) {
1142 if (!out || !g_clipper) return false;
1143 out->polygon = static_cast<std::uint32_t>(g_clipper->clippingPolygon->capacity);
1144 out->output = static_cast<std::uint32_t>(g_clipper->clipOutput->capacity);
1145 out->vertices = static_cast<std::uint32_t>(g_clipper->clippedVertices->capacity);
1146 out->uvs = static_cast<std::uint32_t>(g_clipper->clippedUVs->capacity);
1147 out->triangles = static_cast<std::uint32_t>(g_clipper->clippedTriangles->capacity);
1148 out->scratch = static_cast<std::uint32_t>(g_clipper->scratch->capacity);
1149 return true;
1150}
1151
1152void render(Instance* instance, TriangleSink& sink, bool clipping) {
1153 renderImpl<STAGE_EMIT, false>(instance, sink, clipping, nullptr);
1154}
1155
1156
1157namespace {
1158
1159bool openToStage(Instance* instance, int stage, ClipStartCounts* counts) {
1160 spClippingAttachment* clip = nullptr;
1161 spSlot* slot = firstClipSlot(instance->skeleton, &clip);
1162 if (!slot) return false;
1163
1164 const int length = clip->super.worldVerticesLength;
1165 counts->rawVertices = static_cast<std::uint32_t>(length / 2);
1166 float* vertices = spFloatArray_setSize(g_clipper->clippingPolygon, length)->items;
1167 if (stage < CLIP_START_WORLD) return true;
1168
1169 g_clipper->clipAttachment = clip;
1170 spVertexAttachment_computeWorldVertices(&clip->super, slot, 0, length, vertices, 0, 2);
1171 if (stage < CLIP_START_WINDING) return true;
1172
1173 makeClockwise(g_clipper->clippingPolygon);
1174 if (stage < CLIP_START_TRIANGULATE) return true;
1175
1176 spShortArray* triangles =
1177 spTriangulator_triangulate(g_clipper->triangulator, g_clipper->clippingPolygon);
1178 counts->triangulationTriangles = static_cast<std::uint32_t>(triangles->size / 3);
1179 counts->triangulatorScratch = static_cast<std::uint32_t>(
1180 g_clipper->triangulator->indicesArray->capacity
1181 + g_clipper->triangulator->isConcaveArray->capacity
1182 + g_clipper->triangulator->triangles->capacity);
1183 if (stage < CLIP_START_DECOMPOSE) return true;
1184
1185 g_clipper->clippingPolygons =
1186 spTriangulator_decompose(g_clipper->triangulator, g_clipper->clippingPolygon, triangles);
1187 counts->pieces = static_cast<std::uint32_t>(g_clipper->clippingPolygons->size);
1188 if (stage < CLIP_START_PIECES) return true;
1189
1190 for (int i = 0; i < g_clipper->clippingPolygons->size; ++i) {
1191 spFloatArray* polygon = g_clipper->clippingPolygons->items[i];
1192 makeClockwise(polygon);
1193 spFloatArray_add(polygon, polygon->items[0]);
1194 spFloatArray_add(polygon, polygon->items[1]);
1195 }
1196 for (int i = 0; i < g_clipper->clippingPolygons->size; ++i) {
1197 counts->effectiveEdges +=
1198 static_cast<std::uint32_t>(g_clipper->clippingPolygons->items[i]->size / 2);
1199 }
1200 if (stage < CLIP_START_PUBLISH) return true;
1201
1202 captureClipRegion();
1203 counts->minX = g_clipRegion.minX;
1204 counts->minY = g_clipRegion.minY;
1205 counts->maxX = g_clipRegion.maxX;
1206 counts->maxY = g_clipRegion.maxY;
1207 return true;
1208}
1209
1210} // namespace
1211
1212
1213bool clipStartStage(Instance* instance, int stage, ClipStartCounts* counts) {
1214 if (!instance || !counts) return false;
1215 *counts = ClipStartCounts{};
1216 if (!g_clipper) g_clipper = spSkeletonClipping_create();
1217
1218 // Closed on both sides. A region left open makes the next real extraction's
1219 // clipStart return immediately, so the ladder would be measuring one thing
1220 // and poisoning another.
1221 spSkeletonClipping_clipEnd2(g_clipper);
1222 const bool opened = openToStage(instance, stage, counts);
1223 spSkeletonClipping_clipEnd2(g_clipper);
1224 return opened;
1225}
1226
1227bool renderCounted(Instance* instance, TriangleSink& sink, bool clipping, ProbeCounts* counts) {
1228 if (!instance || !counts) return false;
1229 *counts = ProbeCounts{};
1230 renderImpl<STAGE_EMIT, true>(instance, sink, clipping, counts);
1231 return true;
1232}
1233
1234bool renderStage(Instance* instance, TriangleSink& sink, bool clipping,
1235 int stage, ProbeCounts* counts) {
1236 switch (stage) {
1237 case STAGE_SETUP: renderImpl<STAGE_SETUP, true>(instance, sink, clipping, counts); break;
1238 case STAGE_TRAVERSE: renderImpl<STAGE_TRAVERSE, true>(instance, sink, clipping, counts); break;
1239 case STAGE_VERTICES: renderImpl<STAGE_VERTICES, true>(instance, sink, clipping, counts); break;
1240 case STAGE_CLIP_START: renderImpl<STAGE_CLIP_START, true>(instance, sink, clipping, counts); break;
1241 case STAGE_CLIP: renderImpl<STAGE_CLIP, true>(instance, sink, clipping, counts); break;
1242 case STAGE_EMIT: renderImpl<STAGE_EMIT, true>(instance, sink, clipping, counts); break;
1243 default: return false;
1244 }
1245 return true;
1246}
1247
1248int version() {
1249 return ES_SPINE_VERSION;
1250}
1251
1252} // namespace es::ES_SPINE_NS
char * _spUtil_readFile(const char *path, int *length)
void _spAtlasPage_createTexture(spAtlasPage *self, const char *path)
void _spAtlasPage_disposeTexture(spAtlasPage *self)
What a vendored Spine runtime owes the module, and nothing more.
bool renderCounted(Instance *instance, TriangleSink &sink, bool clipping, ProbeCounts *counts)
int atlasPageCount(const Skeleton *skeleton)
void(*)(const Event &) EventSink
The module is a singleton, so its collector is process-wide rather than per instance.
bool bonePose(const Instance *instance, int index, float *x, float *y, float *degrees, float *length)
void advanceAndApply(Instance *instance, float dt)
bool playAnimation(Instance *instance, const char *animation, bool loop, int track)
const void * eventSource(Instance *instance)
bool pathMix(const Instance *instance, const char *name, PathMix *out)
void premultiplyTint(float rgba[4], bool pagePremultiplied)
int boneParent(const Instance *instance, int index)
void update(Instance *instance, float dt)
const char * skinName(const Instance *instance, int index)
bool boneRotation(const Instance *instance, const char *bone, float *degrees)
void enableEvents(Instance *instance)
void setDefaultMix(Skeleton *skeleton, float seconds)
int boneCount(const Instance *instance)
bool transformMix(const Instance *instance, const char *name, TransformMix *out)
float animationDuration(const Skeleton *skeleton, const char *animation)
bool setSkeletonColor(Instance *instance, float r, float g, float b, float a)
Skeleton * loadSkeleton(const void *skeletonData, int length, const char *atlasText, int atlasLength, bool binary, std::string &error)
void setTrackAlpha(Instance *instance, int track, float alpha)
int blendForPage(int slotBlend, bool pagePremultiplied)
The blend code to emit for a slot's mode on a page of the given kind (see above).
es::skeletal::TriangleSink TriangleSink
const char * boneName(const Instance *instance, int index)
const char * constraintName(const Instance *instance, ConstraintKind kind, int index)
void setEventSink(EventSink sink)
const char * atlasPageName(const Skeleton *skeleton, int page)
bool skeletonBounds(const Skeleton *skeleton, float *x, float *y, float *width, float *height)
void setAtlasPageTexture(Skeleton *skeleton, int page, uint32_t texture, int width, int height)
bool poseStage(Instance *instance, float dt, int stage, PoseCounts *counts)
void bounds(const Instance *instance, float *x, float *y, float *width, float *height)
bool setPathMix(Instance *instance, const char *name, const PathMix &mix)
int skinCount(const Instance *instance)
const char * animationName(const Instance *instance, int index)
void renderImpl(Instance *instance, TriangleSink &sink, bool clipping, ProbeCounts *counts)
@ STAGE_EMIT
…and hand them to the sink. The full extraction.
@ STAGE_VERTICES
…and compute world vertices and tint.
@ STAGE_TRAVERSE
Walk the draw order, resolving what each slot holds.
@ STAGE_CLIP
…and cut the triangles against those pieces.
@ STAGE_SETUP
The instance and the clipper; the loop does not run.
bool addAnimation(Instance *instance, const char *animation, bool loop, float delay, int track)
bool clipStartStage(Instance *instance, int stage, ClipStartCounts *counts)
bool setAttachment(Instance *instance, const char *slot, const char *attachment)
bool bonePosition(const Instance *instance, const char *bone, float *x, float *y)
bool setSlotColor(Instance *instance, const char *slot, float r, float g, float b, float a)
bool setTransformMix(Instance *instance, const char *name, const TransformMix &mix)
void setSkin(Instance *instance, const char *skin)
int constraintCount(const Instance *instance, ConstraintKind kind)
Instance * createInstance(Skeleton *skeleton)
void setToSetupPose(Instance *instance)
void setMixDuration(Skeleton *skeleton, const char *from, const char *to, float seconds)
bool setIkTarget(Instance *instance, const char *constraint, float x, float y, float mix)
int animationCount(const Instance *instance)
void materializeWorldPose(Instance *instance, float dt)
bool renderStage(Instance *instance, TriangleSink &sink, bool clipping, int stage, ProbeCounts *counts)
bool requiresContinuousWorldPose(const Skeleton *skeleton)
auto sink(SignalType &signal)
Create a sink for a signal.
Definition Sink.hpp:175
Instance & operator=(const Instance &)=delete
spAnimationState * state
Instance(const Instance &)=delete
std::uint32_t regionAttachments
std::uint32_t verticesGenerated
std::uint32_t meshAttachments
std::uint32_t clipPolygonVertices
std::uint32_t clipPolygonEdges
Skeleton & operator=(const Skeleton &)=delete
spSkeletonData * data
spAnimationStateData * stateData
Skeleton(const Skeleton &)=delete