28#include <spine/spine.h>
29#include <spine/extension.h>
34#if ES_SPINE_VERSION != 21
35#error "SpineRuntime21.cpp is the spine-c 2.1 backend; build it with ES_SPINE_VERSION=21."
60namespace es::ES_SPINE_NS {
64 spSkeletonData*
data =
nullptr;
73 if (
data) spSkeletonData_dispose(
data);
80 spAnimationState*
state =
nullptr;
97std::vector<float> g_worldVertices;
99std::vector<uint16_t> g_indices;
101EventSink g_eventSink =
nullptr;
103const uint16_t QUAD_INDICES[6] = {0, 1, 2, 2, 3, 0};
105spAtlasPage* pageAt(
const Skeleton* skeleton,
int index) {
106 if (!skeleton || index < 0)
return nullptr;
107 spAtlasPage* page = skeleton->atlas->pages;
108 for (
int i = 0; i < index && page; ++i) page = page->next;
113uint32_t textureOf(
const void* rendererObject) {
114 auto* region =
reinterpret_cast<const spAtlasRegion*
>(rendererObject);
115 if (!region || !region->page)
return 0;
116 return static_cast<uint32_t
>(
reinterpret_cast<uintptr_t
>(region->page->rendererObject));
119void widenIndices(
const int* triangles,
int count) {
120 g_indices.resize(
static_cast<size_t>(count));
121 for (
int i = 0; i < count; ++i) {
122 g_indices[
static_cast<size_t>(i)] =
static_cast<uint16_t
>(triangles[i]);
128 const float* positions =
nullptr;
129 const float* uvs =
nullptr;
130 const uint16_t* indices =
nullptr;
133 uint32_t texture = 0;
134 float color[4] = {1.0f, 1.0f, 1.0f, 1.0f};
142bool pose(spSlot* slot, Posed& out) {
143 spAttachment* attachment = slot->attachment;
144 if (!attachment)
return false;
146 switch (attachment->type) {
147 case SP_ATTACHMENT_REGION: {
148 auto* region =
reinterpret_cast<spRegionAttachment*
>(attachment);
149 out.texture = textureOf(region->rendererObject);
150 if (!out.texture)
return false;
152 g_worldVertices.resize(8);
153 spRegionAttachment_computeWorldVertices(region, slot->bone, g_worldVertices.data());
154 out.positions = g_worldVertices.data();
155 out.uvs = region->uvs;
156 out.indices = QUAD_INDICES;
159 out.color[0] = region->r;
160 out.color[1] = region->g;
161 out.color[2] = region->b;
162 out.color[3] = region->a;
165 case SP_ATTACHMENT_MESH: {
166 auto* mesh =
reinterpret_cast<spMeshAttachment*
>(attachment);
167 out.texture = textureOf(mesh->rendererObject);
168 if (!out.texture)
return false;
171 g_worldVertices.resize(
static_cast<size_t>(mesh->verticesCount));
172 spMeshAttachment_computeWorldVertices(mesh, slot, g_worldVertices.data());
173 widenIndices(mesh->triangles, mesh->trianglesCount);
174 out.positions = g_worldVertices.data();
176 out.indices = g_indices.data();
177 out.vertexCount = mesh->verticesCount / 2;
178 out.indexCount = mesh->trianglesCount;
179 out.color[0] = mesh->r;
180 out.color[1] = mesh->g;
181 out.color[2] = mesh->b;
182 out.color[3] = mesh->a;
185 case SP_ATTACHMENT_SKINNED_MESH: {
188 auto* mesh =
reinterpret_cast<spSkinnedMeshAttachment*
>(attachment);
189 out.texture = textureOf(mesh->rendererObject);
190 if (!out.texture)
return false;
192 g_worldVertices.resize(
static_cast<size_t>(mesh->uvsCount));
193 spSkinnedMeshAttachment_computeWorldVertices(mesh, slot, g_worldVertices.data());
194 widenIndices(mesh->triangles, mesh->trianglesCount);
195 out.positions = g_worldVertices.data();
197 out.indices = g_indices.data();
198 out.vertexCount = mesh->uvsCount / 2;
199 out.indexCount = mesh->trianglesCount;
200 out.color[0] = mesh->r;
201 out.color[1] = mesh->g;
202 out.color[2] = mesh->b;
203 out.color[3] = mesh->a;
213 case SP_ANIMATION_START:
return EventKind::Start;
214 case SP_ANIMATION_END:
return EventKind::End;
215 case SP_ANIMATION_COMPLETE:
return EventKind::Complete;
216 default:
return EventKind::Event;
223void eventListener(spAnimationState* state,
int trackIndex, spEventType type,
224 spEvent* event,
int loopCount) {
226 if (!g_eventSink)
return;
230 out.kind = kindOf(type);
231 out.track = trackIndex;
233 spTrackEntry* entry = spAnimationState_getCurrent(state, trackIndex);
234 if (entry && entry->animation) out.animation = entry->animation->name;
236 if (type == SP_ANIMATION_EVENT && event) {
237 out.floatValue =
event->floatValue;
238 out.intValue =
event->intValue;
239 out.name =
event->data->name;
240 out.stringValue =
event->stringValue;
252 const char* atlasText,
int atlasLength,
253 bool binary, std::string& error) {
257 error =
"Spine 2.1 binary skeletons are not supported: re-export from Spine "
258 "with the skeleton data format set to JSON.";
262 auto skeleton = std::make_unique<Skeleton>();
264 skeleton->atlas = spAtlas_create(atlasText, atlasLength,
"",
nullptr);
265 if (!skeleton->atlas || !skeleton->atlas->pages) {
266 error =
"Failed to create atlas (invalid atlas text or no pages)";
270 spSkeletonJson* reader = spSkeletonJson_create(skeleton->atlas);
272 error =
"Failed to create skeleton json reader";
275 reader->scale = 1.0f;
276 skeleton->data = spSkeletonJson_readSkeletonData(
277 reader,
static_cast<const char*
>(skeletonData));
278 if (!skeleton->data && reader->error) error = reader->error;
279 spSkeletonJson_dispose(reader);
282 if (!skeleton->data)
return nullptr;
284 skeleton->stateData = spAnimationStateData_create(skeleton->data);
285 skeleton->stateData->defaultMix = 0.2f;
286 return skeleton.release();
290 if (!skeleton)
return 0;
292 for (spAtlasPage* page = skeleton->
atlas->pages; page; page = page->next) ++count;
297 spAtlasPage* p = pageAt(skeleton, page);
298 return p ? p->name :
nullptr;
302 spAtlasPage* p = pageAt(skeleton, page);
307 p->rendererObject =
reinterpret_cast<void*
>(
static_cast<uintptr_t
>(texture));
317 if (!skeleton || !skeleton->
stateData)
return;
318 spAnimation* a = spSkeletonData_findAnimation(skeleton->
data, from);
319 spAnimation* b = spSkeletonData_findAnimation(skeleton->
data, to);
320 if (a && b) spAnimationStateData_setMix(skeleton->
stateData, a, b, seconds);
328 if (!skeleton)
return nullptr;
330 auto instance = std::make_unique<Instance>();
331 instance->skeleton = spSkeleton_create(skeleton->
data);
332 instance->state = spAnimationState_create(skeleton->
stateData);
333 if (!instance->skeleton || !instance->state)
return nullptr;
335 spSkeleton_setToSetupPose(instance->skeleton);
336 spSkeleton_updateWorldTransform(instance->skeleton);
337 return instance.release();
341 if (!instance)
return;
342 spAnimationState_update(instance->
state, dt);
347 if (!instance)
return;
349 spSkeleton_updateWorldTransform(instance->
skeleton);
358 if (!skeleton || !skeleton->
data)
return -1.0f;
359 spAnimation* found = spSkeletonData_findAnimation(skeleton->
data, animation);
360 return found ? found->duration : -1.0f;
369 if (!instance)
return;
370 spAnimationState_clearTracks(instance->
state);
371 spSkeleton_setToSetupPose(instance->
skeleton);
375 if (!instance)
return false;
376 if (!spSkeletonData_findAnimation(instance->
skeleton->data, animation))
return false;
377 return spAnimationState_setAnimationByName(instance->
state, track, animation, loop) !=
nullptr;
381 if (!instance)
return false;
382 if (!spSkeletonData_findAnimation(instance->
skeleton->data, animation))
return false;
383 return spAnimationState_addAnimationByName(instance->
state, track, animation, loop, delay) !=
nullptr;
387 if (!instance)
return;
388 if (!skin || skin[0] ==
'\0') {
389 spSkeleton_setSkin(instance->
skeleton,
nullptr);
391 spSkeleton_setSkinByName(instance->
skeleton, skin);
393 spSkeleton_setSlotsToSetupPose(instance->
skeleton);
397 if (!instance)
return;
398 spTrackEntry* entry = spAnimationState_getCurrent(instance->
state, track);
401 if (entry) entry->mix = alpha;
409 return instance ? instance->
state :
nullptr;
413 if (instance) instance->
state->listener = eventListener;
421 return instance ? instance->
skeleton->data->animationsCount : 0;
425 if (!instance)
return nullptr;
426 spSkeletonData* data = instance->
skeleton->data;
427 if (index < 0 || index >= data->animationsCount)
return nullptr;
428 return data->animations[index]->name;
432 return instance ? instance->
skeleton->data->skinsCount : 0;
436 if (!instance)
return nullptr;
437 spSkeletonData* data = instance->
skeleton->data;
438 if (index < 0 || index >= data->skinsCount)
return nullptr;
439 return data->skins[index]->name;
443 if (!instance)
return false;
444 spBone* b = spSkeleton_findBone(instance->
skeleton, bone);
445 if (!b)
return false;
452 if (!instance)
return false;
453 spBone* b = spSkeleton_findBone(instance->
skeleton, bone);
454 if (!b)
return false;
456 *degrees = b->worldRotation;
461 return instance ? instance->
skeleton->bonesCount : 0;
465 if (!instance || index < 0 || index >= instance->
skeleton->bonesCount)
return nullptr;
466 return instance->
skeleton->bones[index]->data->name;
470 if (!instance || index < 0 || index >= instance->
skeleton->bonesCount)
return -1;
471 const spBone* parent = instance->
skeleton->bones[index]->parent;
472 if (!parent)
return -1;
474 for (
int i = 0; i < index; ++i) {
475 if (instance->
skeleton->bones[i] == parent)
return i;
480bool bonePose(
const Instance* instance,
int index,
float* x,
float* y,
float* degrees,
float* length) {
481 if (!instance || index < 0 || index >= instance->
skeleton->bonesCount)
return false;
482 spBone* b = instance->
skeleton->bones[index];
485 *degrees = b->worldRotation;
486 *length = b->data->length;
490void bounds(
const Instance* instance,
float* x,
float* y,
float* width,
float* height) {
491 *x = *y = *width = *height = 0.0f;
492 if (!instance)
return;
494 spSkeleton* skeleton = instance->
skeleton;
495 float minX = 1e30f, minY = 1e30f, maxX = -1e30f, maxY = -1e30f;
498 for (
int i = 0; i < skeleton->slotsCount; ++i) {
500 if (!pose(skeleton->drawOrder[i], posed))
continue;
501 for (
int v = 0; v < posed.vertexCount; ++v) {
502 const float vx = posed.positions[v * 2];
503 const float vy = posed.positions[v * 2 + 1];
504 if (vx < minX) minX = vx;
505 if (vx > maxX) maxX = vx;
506 if (vy < minY) minY = vy;
507 if (vy > maxY) maxY = vy;
515 *width = maxX - minX;
516 *height = maxY - minY;
522 return instance->
skeleton->ikConstraintsCount;
527 if (index < 0 || index >= instance->
skeleton->ikConstraintsCount)
return nullptr;
528 return instance->
skeleton->ikConstraints[index]->data->name;
541 if (!instance)
return false;
542 const char* name = (attachment && attachment[0] !=
'\0') ? attachment :
nullptr;
543 return spSkeleton_setAttachment(instance->
skeleton, slot, name) != 0;
547 if (!instance)
return false;
548 spIkConstraint* c = spSkeleton_findIkConstraint(instance->
skeleton, constraint);
549 if (!c)
return false;
557 if (!instance)
return false;
558 spSlot* s = spSkeleton_findSlot(instance->
skeleton, slot);
559 if (!s)
return false;
568 if (!instance)
return false;
584 if (!instance)
return;
586 spSkeleton* skeleton = instance->
skeleton;
588 for (
int i = 0; i < skeleton->slotsCount; ++i) {
589 spSlot* slot = skeleton->drawOrder[i];
593 if (!pose(slot, posed))
continue;
597 const int blendMode = slot->data->additiveBlending ? 1 : 0;
599 const float rgba[4] = {
600 skeleton->r * slot->r * posed.color[0],
601 skeleton->g * slot->g * posed.color[1],
602 skeleton->b * slot->b * posed.color[2],
603 skeleton->a * slot->a * posed.color[3],
606 sink.emit(posed.positions, posed.uvs, posed.vertexCount,
607 posed.indices, posed.indexCount, posed.texture, blendMode, rgba);
639 return ES_SPINE_VERSION;
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)
bool clipStorage(ClipStorage *out)
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)
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 destroy(Skeleton *skeleton)
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)
void render(Instance *instance, TriangleSink &sink, bool clipping)
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)
Instance & operator=(const Instance &)=delete
Instance(const Instance &)=delete
Skeleton & operator=(const Skeleton &)=delete
spAnimationStateData * stateData
Skeleton(const Skeleton &)=delete