30#include <spine/spine.h>
31#include <spine/PhysicsConstraintData.h>
32#include <spine/SkeletonRenderer.h>
36#ifndef ES_SPINE_VERSION
37#error "ES_SPINE_VERSION must name the vendored spine-cpp release (43)."
40namespace sp = ::spine;
46sp::SpineExtension* spine::getDefaultExtension() {
47 return new sp::DefaultSpineExtension();
50namespace es::ES_SPINE_NS {
59class UnloadedTextures :
public sp::TextureLoader {
61 void load(sp::AtlasPage& page,
const sp::String& path)
override {
65 void unload(
void* texture)
override { (void)texture; }
71sp::SkeletonRenderer g_renderer;
73int blendModeOf(sp::BlendMode mode) {
75 case sp::BlendMode_Additive:
return 1;
76 case sp::BlendMode_Multiply:
return 2;
77 case sp::BlendMode_Screen:
return 3;
82const char* cstr(
const sp::String& text) {
95 sp::SkeletonData*
data =
nullptr;
115 if (t == texture)
return true;
124 sp::AnimationState*
state =
nullptr;
136void destroy(Skeleton* skeleton) {
delete skeleton; }
137void destroy(Instance* instance) {
delete instance; }
139Skeleton*
loadSkeleton(
const void* skeletonData,
int length,
140 const char* atlasText,
int atlasLength,
141 bool binary, std::string& error) {
142 auto skeleton = std::make_unique<Skeleton>();
144 skeleton->atlas =
new sp::Atlas(atlasText, atlasLength,
"", &skeleton->loader);
145 if (skeleton->atlas->getPages().size() == 0) {
146 error =
"Failed to create atlas (invalid atlas text or no pages)";
151 sp::SkeletonBinary reader(*skeleton->atlas);
152 reader.setScale(1.0f);
153 skeleton->data = reader.readSkeletonData(
154 static_cast<const unsigned char*
>(skeletonData), length);
155 if (!skeleton->data) error = cstr(reader.getError());
157 sp::SkeletonJson reader(*skeleton->atlas);
158 reader.setScale(1.0f);
159 skeleton->data = reader.readSkeletonData(
static_cast<const char*
>(skeletonData));
160 if (!skeleton->data) error = cstr(reader.getError());
163 if (!skeleton->data)
return nullptr;
165 skeleton->stateData =
new sp::AnimationStateData(*skeleton->data);
166 skeleton->stateData->setDefaultMix(0.2f);
167 return skeleton.release();
171 if (!skeleton)
return 0;
172 return static_cast<int>(skeleton->atlas->getPages().size());
175const char*
atlasPageName(
const Skeleton* skeleton,
int page) {
176 if (!skeleton || page < 0 || page >=
atlasPageCount(skeleton))
return nullptr;
177 return cstr(skeleton->atlas->getPages()[page]->name);
180void setAtlasPageTexture(Skeleton* skeleton,
int page, uint32_t texture,
int width,
int height) {
181 if (!skeleton || page < 0 || page >=
atlasPageCount(skeleton))
return;
183 sp::AtlasPage* target = skeleton->atlas->getPages()[page];
184 void* handle =
reinterpret_cast<void*
>(
static_cast<uintptr_t
>(texture));
185 target->texture = handle;
186 target->width = width;
187 target->height = height;
191 sp::Array<sp::AtlasRegion*>& regions = skeleton->atlas->getRegions();
192 for (
size_t i = 0; i < regions.size(); ++i) {
193 if (regions[i]->getPage() == target) regions[i]->setRendererObject(handle);
196 if (target->pma && !skeleton->isPremultiplied(handle)) {
197 skeleton->premultiplied.push_back(handle);
202 if (skeleton) skeleton->stateData->setDefaultMix(seconds);
205void setMixDuration(Skeleton* skeleton,
const char* from,
const char* to,
float seconds) {
206 if (!skeleton)
return;
207 sp::Animation* a = skeleton->data->findAnimation(from);
208 sp::Animation* b = skeleton->data->findAnimation(to);
209 if (a && b) skeleton->stateData->setMix(*a, *b, seconds);
217 if (!skeleton)
return nullptr;
219 auto instance = std::make_unique<Instance>();
220 instance->owner = skeleton;
221 instance->skeleton =
new sp::Skeleton(*skeleton->data);
222 instance->state =
new sp::AnimationState(*skeleton->stateData);
224 instance->skeleton->setupPose();
225 instance->skeleton->updateWorldTransform(sp::Physics_Update);
226 return instance.release();
230 if (!instance)
return;
231 instance->state->update(dt);
232 instance->state->apply(*instance->skeleton);
236 if (!instance)
return;
237 instance->skeleton->update(dt);
238 instance->skeleton->updateWorldTransform(sp::Physics_Update);
241void update(Instance* instance,
float dt) {
247 if (!skeleton || !skeleton->data)
return -1.0f;
248 sp::Animation* found = skeleton->data->findAnimation(animation);
249 return found ? found->getDuration() : -1.0f;
253 if (!skeleton || !skeleton->data)
return false;
256 sp::Array<sp::ConstraintData*>& constraints = skeleton->data->getConstraints();
257 for (
size_t i = 0; i < constraints.size(); ++i) {
258 if (constraints[i]->getRTTI().instanceOf(sp::PhysicsConstraintData::rtti))
return true;
264 if (!instance)
return;
265 instance->state->clearTracks();
267 instance->skeleton->setupPose();
270bool playAnimation(Instance* instance,
const char* animation,
bool loop,
int track) {
271 if (!instance)
return false;
273 sp::Animation* found = instance->skeleton->getData().findAnimation(animation);
274 if (!found)
return false;
275 instance->state->setAnimation(
static_cast<size_t>(track), *found, loop);
279bool addAnimation(Instance* instance,
const char* animation,
bool loop,
float delay,
int track) {
280 if (!instance)
return false;
281 sp::Animation* found = instance->skeleton->getData().findAnimation(animation);
282 if (!found)
return false;
283 instance->state->addAnimation(
static_cast<size_t>(track), *found, loop, delay);
287void setSkin(Instance* instance,
const char* skin) {
288 if (!instance)
return;
289 if (!skin || skin[0] ==
'\0') {
290 instance->skeleton->setSkin(
static_cast<sp::Skin*
>(
nullptr));
292 sp::Skin* found = instance->skeleton->getData().findSkin(skin);
294 instance->skeleton->setSkin(found);
296 instance->skeleton->setupPoseSlots();
299void setTrackAlpha(Instance* instance,
int track,
float alpha) {
300 if (!instance)
return;
301 sp::TrackEntry* entry = instance->state->getTrack(
static_cast<size_t>(track));
302 if (entry) entry->setAlpha(alpha);
314 case sp::EventType_Start:
return EventKind::Start;
315 case sp::EventType_Interrupt:
return EventKind::Interrupt;
316 case sp::EventType_End:
return EventKind::End;
317 case sp::EventType_Complete:
return EventKind::Complete;
318 case sp::EventType_Dispose:
return EventKind::Dispose;
319 default:
return EventKind::Event;
323void eventListener(sp::AnimationState* state, sp::EventType type,
324 sp::TrackEntry* entry, sp::Event* event,
void* userData) {
327 if (!g_eventSink)
return;
331 out.kind = kindOf(type);
332 out.track = entry ?
static_cast<int>(entry->getTrackIndex()) : 0;
333 if (entry) out.animation = cstr(entry->getAnimation().getName());
334 if (type == sp::EventType_Event && event) {
335 out.floatValue =
event->getFloat();
336 out.intValue =
event->getInt();
337 out.name = cstr(event->getData().getName());
338 out.stringValue = cstr(event->getString());
346 return instance ? instance->state :
nullptr;
350 if (instance) instance->state->setListener(eventListener);
358 if (!instance)
return 0;
359 return static_cast<int>(instance->skeleton->getData().getAnimations().size());
362const char*
animationName(
const Instance* instance,
int index) {
363 if (!instance || index < 0 || index >=
animationCount(instance))
return nullptr;
364 return cstr(instance->skeleton->getData().getAnimations()[index]->getName());
368 if (!instance)
return 0;
369 return static_cast<int>(instance->skeleton->getData().getSkins().size());
372const char*
skinName(
const Instance* instance,
int index) {
373 if (!instance || index < 0 || index >=
skinCount(instance))
return nullptr;
374 return cstr(instance->skeleton->getData().getSkins()[index]->getName());
377bool bonePosition(
const Instance* instance,
const char* bone,
float* x,
float* y) {
378 if (!instance)
return false;
379 sp::Bone* found = instance->skeleton->findBone(bone);
380 if (!found)
return false;
382 *x = found->getAppliedPose().getWorldX();
383 *y = found->getAppliedPose().getWorldY();
387bool boneRotation(
const Instance* instance,
const char* bone,
float* degrees) {
388 if (!instance)
return false;
389 sp::Bone* found = instance->skeleton->findBone(bone);
390 if (!found)
return false;
391 *degrees = found->getAppliedPose().getWorldRotationX();
396 return instance ?
static_cast<int>(instance->skeleton->getBones().size()) : 0;
399const char*
boneName(
const Instance* instance,
int index) {
400 if (!instance || index < 0 || index >=
boneCount(instance))
return nullptr;
401 return cstr(instance->skeleton->getBones()[index]->getData().getName());
404int boneParent(
const Instance* instance,
int index) {
405 if (!instance || index < 0 || index >=
boneCount(instance))
return -1;
406 sp::Bone* parent = instance->skeleton->getBones()[index]->getParent();
407 return parent ? parent->getData().getIndex() : -1;
410bool bonePose(
const Instance* instance,
int index,
float* x,
float* y,
float* degrees,
float* length) {
411 if (!instance || index < 0 || index >=
boneCount(instance))
return false;
412 sp::Bone* b = instance->skeleton->getBones()[index];
413 *x = b->getAppliedPose().getWorldX();
414 *y = b->getAppliedPose().getWorldY();
415 *degrees = b->getAppliedPose().getWorldRotationX();
416 *length = b->getData().getLength();
420void bounds(
const Instance* instance,
float* x,
float* y,
float* width,
float* height) {
421 *x = *y = *width = *height = 0.0f;
422 if (!instance)
return;
423 instance->skeleton->getBounds(*x, *y, *width, *height);
428bool isKind(sp::Constraint* constraint, ConstraintKind kind) {
430 case ConstraintKind::Ik:
return constraint->getRTTI().isExactly(sp::IkConstraint::rtti);
431 case ConstraintKind::Transform:
432 return constraint->getRTTI().isExactly(sp::TransformConstraint::rtti);
433 case ConstraintKind::Path:
return constraint->getRTTI().isExactly(sp::PathConstraint::rtti);
441 if (!instance)
return 0;
442 sp::Array<sp::Constraint*>& constraints = instance->skeleton->getConstraints();
444 for (
size_t i = 0; i < constraints.size(); ++i) {
445 if (isKind(constraints[i], kind)) ++count;
450const char*
constraintName(
const Instance* instance, ConstraintKind kind,
int index) {
451 if (!instance || index < 0)
return nullptr;
452 sp::Array<sp::Constraint*>& constraints = instance->skeleton->getConstraints();
454 for (
size_t i = 0; i < constraints.size(); ++i) {
455 if (!isKind(constraints[i], kind))
continue;
456 if (seen == index)
return cstr(constraints[i]->getData().getName());
462bool transformMix(
const Instance* instance,
const char* name, TransformMix* out) {
463 if (!instance)
return false;
464 auto* c = instance->skeleton->findConstraint<sp::TransformConstraint>(name);
465 if (!c)
return false;
466 out->rotate = c->getPose().getMixRotate();
467 out->x = c->getPose().getMixX();
468 out->y = c->getPose().getMixY();
469 out->scaleX = c->getPose().getMixScaleX();
470 out->scaleY = c->getPose().getMixScaleY();
471 out->shearY = c->getPose().getMixShearY();
475bool setTransformMix(Instance* instance,
const char* name,
const TransformMix& mix) {
476 if (!instance)
return false;
477 auto* c = instance->skeleton->findConstraint<sp::TransformConstraint>(name);
478 if (!c)
return false;
479 c->getPose().setMixRotate(mix.rotate);
480 c->getPose().setMixX(mix.x);
481 c->getPose().setMixY(mix.y);
482 c->getPose().setMixScaleX(mix.scaleX);
483 c->getPose().setMixScaleY(mix.scaleY);
484 c->getPose().setMixShearY(mix.shearY);
488bool pathMix(
const Instance* instance,
const char* name, PathMix* out) {
489 if (!instance)
return false;
490 auto* c = instance->skeleton->findConstraint<sp::PathConstraint>(name);
491 if (!c)
return false;
492 out->position = c->getPose().getPosition();
493 out->spacing = c->getPose().getSpacing();
494 out->rotate = c->getPose().getMixRotate();
495 out->x = c->getPose().getMixX();
496 out->y = c->getPose().getMixY();
500bool setPathMix(Instance* instance,
const char* name,
const PathMix& mix) {
501 if (!instance)
return false;
502 auto* c = instance->skeleton->findConstraint<sp::PathConstraint>(name);
503 if (!c)
return false;
504 c->getPose().setPosition(mix.position);
505 c->getPose().setSpacing(mix.spacing);
506 c->getPose().setMixRotate(mix.rotate);
507 c->getPose().setMixX(mix.x);
508 c->getPose().setMixY(mix.y);
516bool setAttachment(Instance* instance,
const char* slot,
const char* attachment) {
517 if (!instance)
return false;
518 sp::Slot* found = instance->skeleton->findSlot(slot);
519 if (!found)
return false;
521 if (!attachment || attachment[0] ==
'\0') {
522 found->getPose().setAttachment(
nullptr);
526 sp::Attachment* wanted = instance->skeleton->getAttachment(found->getData().getIndex(), attachment);
527 if (!wanted)
return false;
528 found->getPose().setAttachment(wanted);
532bool setIkTarget(Instance* instance,
const char* constraint,
float x,
float y,
float mix) {
533 if (!instance)
return false;
534 auto* c = instance->skeleton->findConstraint<sp::IkConstraint>(constraint);
535 if (!c)
return false;
536 c->getTarget().getPose().setX(x);
537 c->getTarget().getPose().setY(y);
538 c->getPose().setMix(mix);
542bool setSlotColor(Instance* instance,
const char* slot,
float r,
float g,
float b,
float a) {
543 if (!instance)
return false;
544 sp::Slot* found = instance->skeleton->findSlot(slot);
545 if (!found)
return false;
546 found->getPose().getColor().set(r, g, b, a);
550bool setSkeletonColor(Instance* instance,
float r,
float g,
float b,
float a) {
551 if (!instance)
return false;
552 instance->skeleton->getColor().set(r, g, b, a);
560void render(Instance* instance, TriangleSink& sink,
bool clipping) {
561 if (!instance)
return;
567 for (sp::RenderCommand* command = g_renderer.render(*instance->skeleton);
568 command !=
nullptr; command = command->next) {
569 if (command->numVertices <= 0 || command->numIndices <= 0)
continue;
573 const uint32_t packed = command->colors[0];
575 static_cast<float>((packed >> 16) & 0xFF) / 255.0f,
576 static_cast<float>((packed >> 8) & 0xFF) / 255.0f,
577 static_cast<float>(packed & 0xFF) / 255.0f,
578 static_cast<float>((packed >> 24) & 0xFF) / 255.0f,
584 const bool pma = instance->owner->isPremultiplied(command->texture);
585 sink.setPremultipliedColor(pma);
588 sink.emit(command->positions, command->uvs, command->numVertices,
589 command->indices, command->numIndices,
590 static_cast<uint32_t
>(
reinterpret_cast<uintptr_t
>(command->texture)),
591 blendForPage(blendModeOf(command->blendMode), pma), rgba);
598bool renderStage(Instance*, TriangleSink&,
bool,
int, ProbeCounts*) {
602bool poseStage(Instance*,
float,
int, PoseCounts*) {
610bool renderCounted(Instance*, TriangleSink&,
bool, ProbeCounts*) {
618bool skeletonBounds(
const Skeleton*,
float*,
float*,
float*,
float*) {
623 return ES_SPINE_VERSION;
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)
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)
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).
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)
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)
auto sink(SignalType &signal)
Create a sink for a signal.
Instance & operator=(const Instance &)=delete
Instance(const Instance &)=delete
bool isPremultiplied(void *texture) const
Skeleton & operator=(const Skeleton &)=delete
spAnimationStateData * stateData
Skeleton(const Skeleton &)=delete
std::vector< void * > premultiplied