Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

refactor(spine): the module's ABI is one file, the runtime behind it is swappable

yhh4fe0668ff9ce
diff --git a/CMakeLists.txt b/CMakeLists.txt
index c81a34c3f..80cd3d3f2 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -303,64 +303,15 @@ if(ES_BUILD_WEB OR ES_BUILD_WXGAME)
)
endif()
- # Spine standalone WASM module (spine-c)
+ # One standalone WASM module per vendored Spine release: same exported ABI
+ # (SpineBindings.hpp), a different runtime backend behind it. Everything that
+ # varies is the vendored tree's own shape, so the modules are one function
+ # rather than one copied block each.
if(ES_ENABLE_SPINE)
- file(GLOB_RECURSE SPINE_MODULE_SPINE_SOURCES
- "${CMAKE_CURRENT_SOURCE_DIR}/third_party/spine-runtimes-4.2/spine-c/spine-c/src/spine/*.c"
- )
- add_executable(spine_module
- src/esengine/bindings/SpineModuleEntry.cpp
- ${SPINE_MODULE_SPINE_SOURCES}
- )
- target_include_directories(spine_module PRIVATE
- ${CMAKE_CURRENT_SOURCE_DIR}/third_party/spine-runtimes-4.2/spine-c/spine-c/include
- )
- es_apply_spine_module_settings(spine_module)
-
- set_target_properties(spine_module PROPERTIES
- OUTPUT_NAME "spine42"
- RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/sdk"
- )
- endif()
-
- # Spine 4.1 standalone module (spine-c)
- if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/third_party/spine-runtimes-4.1")
- file(GLOB_RECURSE SPINE41_MODULE_SOURCES
- "${CMAKE_CURRENT_SOURCE_DIR}/third_party/spine-runtimes-4.1/spine-c/spine-c/src/spine/*.c"
- )
- add_executable(spine_module_41
- src/esengine/bindings/SpineModuleEntry.cpp
- ${SPINE41_MODULE_SOURCES}
- )
- target_include_directories(spine_module_41 PRIVATE
- ${CMAKE_CURRENT_SOURCE_DIR}/third_party/spine-runtimes-4.1/spine-c/spine-c/include
- )
- target_compile_definitions(spine_module_41 PRIVATE ES_SPINE_41)
- es_apply_spine_module_settings(spine_module_41)
- set_target_properties(spine_module_41 PROPERTIES
- OUTPUT_NAME "spine41"
- RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/sdk"
- )
- endif()
-
- # Spine 3.8 standalone module (spine-c)
- if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/third_party/spine-runtimes-3.8")
- file(GLOB_RECURSE SPINE38_MODULE_SOURCES
- "${CMAKE_CURRENT_SOURCE_DIR}/third_party/spine-runtimes-3.8/spine-c/spine-c/src/spine/*.c"
- )
- add_executable(spine_module_38
- src/esengine/bindings/SpineModuleEntry.cpp
- ${SPINE38_MODULE_SOURCES}
- )
- target_include_directories(spine_module_38 PRIVATE
- ${CMAKE_CURRENT_SOURCE_DIR}/third_party/spine-runtimes-3.8/spine-c/spine-c/include
- )
- target_compile_definitions(spine_module_38 PRIVATE ES_SPINE_38)
- es_apply_spine_module_settings(spine_module_38)
- set_target_properties(spine_module_38 PROPERTIES
- OUTPUT_NAME "spine38"
- RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/sdk"
- )
+ es_add_spine_module(spine_module 4.2 spine42)
+ es_add_spine_module(spine_module_43 4.3 spine43)
+ es_add_spine_module(spine_module_41 4.1 spine41)
+ es_add_spine_module(spine_module_38 3.8 spine38)
endif()
# Basis Universal KTX2 transcoder module (compressed textures — RC6)
diff --git a/cmake/Emscripten.cmake b/cmake/Emscripten.cmake
index f87c013b4..d6d05c418 100644
--- a/cmake/Emscripten.cmake
+++ b/cmake/Emscripten.cmake
@@ -308,6 +308,55 @@ function(es_apply_spine_module_settings TARGET_NAME)
endif()
endfunction()
+# Declares the wasm module for one vendored Spine release. The entry TU and the
+# exported ABI are shared across releases; what differs is the runtime's own sources
+# and the backend that speaks to them — and the tree's shape says which:
+#
+# 3.8 / 4.1 / 4.2 spine-c/spine-c/{src,include} the pure-C runtime
+# 4.3+ spine-cpp/{src,include} 4.3 regenerated spine-c as a
+# wrapper over C++, so bind C++
+#
+# A runtime nobody checked out is skipped, not an error: a shallow clone still builds
+# the modules it has. A checked-out tree in a shape we don't know is an error, because
+# silently skipping it would ship a project's Spine version as "unsupported".
+function(es_add_spine_module TARGET_NAME VERSION OUTPUT_NAME)
+ set(_root "${CMAKE_CURRENT_SOURCE_DIR}/third_party/spine-runtimes-${VERSION}")
+ set(_bindings "${CMAKE_CURRENT_SOURCE_DIR}/src/esengine/bindings")
+
+ if(NOT EXISTS "${_root}")
+ return()
+ endif()
+
+ if(EXISTS "${_root}/spine-c/spine-c/include")
+ set(_backend "${_bindings}/SpineRuntimeC.cpp")
+ set(_include "${_root}/spine-c/spine-c/include")
+ file(GLOB_RECURSE _runtime "${_root}/spine-c/spine-c/src/spine/*.c")
+ elseif(EXISTS "${_root}/spine-cpp/include")
+ set(_backend "${_bindings}/SpineRuntimeCpp.cpp")
+ set(_include "${_root}/spine-cpp/include")
+ file(GLOB_RECURSE _runtime "${_root}/spine-cpp/src/spine/*.cpp")
+ else()
+ message(FATAL_ERROR
+ "Spine ${VERSION} is checked out at ${_root} but neither spine-c nor "
+ "spine-cpp is where this build expects it.")
+ endif()
+
+ string(REPLACE "." "" _tag ${VERSION})
+
+ add_executable(${TARGET_NAME}
+ "${_bindings}/SpineModuleEntry.cpp"
+ "${_backend}"
+ ${_runtime}
+ )
+ target_include_directories(${TARGET_NAME} PRIVATE "${_include}")
+ target_compile_definitions(${TARGET_NAME} PRIVATE ES_SPINE_VERSION=${_tag})
+ es_apply_spine_module_settings(${TARGET_NAME})
+ set_target_properties(${TARGET_NAME} PROPERTIES
+ OUTPUT_NAME "${OUTPUT_NAME}"
+ RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/sdk"
+ )
+endfunction()
+
# =============================================================================
# Basis Universal KTX2 Transcoder Module (standalone WASM, no GL)
# =============================================================================
diff --git a/native/CMakeLists.txt b/native/CMakeLists.txt
index 2b6e3d601..46cede43b 100644
--- a/native/CMakeLists.txt
+++ b/native/CMakeLists.txt
@@ -71,30 +71,41 @@ set(BOX2D_UNIT_TESTS OFF CACHE BOOL "" FORCE)
set(BOX2D_VALIDATE OFF CACHE BOOL "" FORCE) # release: no heavy internal assertions
add_subdirectory("${TP}/box2d" "${CMAKE_BINARY_DIR}/box2d" EXCLUDE_FROM_ALL)
-# The Spine runtime: spine-c plus the module's entry points, compiled into the host.
-# The three vendored runtimes (3.8 / 4.1 / 4.2) export the same symbol names, so a
-# binary carries exactly ONE — which is why the web ships a module per version and a
-# native app picks at build time. `spine_runtimeVersion()` reports the choice, and the
-# SDK's acquirer answers only that version's id.
-set(ESTELLA_SPINE_VERSION "4.2" CACHE STRING "Spine runtime to link (4.2 / 4.1 / 3.8)")
-set(SPINE_DIR "${TP}/spine-runtimes-${ESTELLA_SPINE_VERSION}/spine-c/spine-c")
-if(NOT EXISTS "${SPINE_DIR}/include")
- message(FATAL_ERROR "Spine runtime ${ESTELLA_SPINE_VERSION} not vendored at ${SPINE_DIR}. "
- "Pass -DESTELLA_SPINE_VERSION=<4.2|4.1|3.8>.")
+# The Spine runtime: the vendored release plus the module's entry points, compiled
+# into the host. Every runtime backend exports the same symbol names, so a binary
+# carries exactly ONE — which is why the web ships a module per version and a native
+# app picks at build time. `spine_runtimeVersion()` reports the choice, and the SDK's
+# acquirer answers only that version's id.
+#
+# Which backend goes with which release is the tree's shape, exactly as the web build
+# reads it (es_add_spine_module in cmake/Emscripten.cmake): 3.8 - 4.2 vendor the pure-C
+# runtime, 4.3 replaced it with a generated wrapper so we bind spine-cpp instead.
+set(ESTELLA_SPINE_VERSION "4.2" CACHE STRING "Spine runtime to link (4.3 / 4.2 / 4.1 / 3.8)")
+set(SPINE_ROOT "${TP}/spine-runtimes-${ESTELLA_SPINE_VERSION}")
+if(EXISTS "${SPINE_ROOT}/spine-c/spine-c/include")
+ set(SPINE_BACKEND "${SRC}/esengine/bindings/SpineRuntimeC.cpp")
+ set(SPINE_INCLUDE "${SPINE_ROOT}/spine-c/spine-c/include")
+ file(GLOB SPINE_SOURCES "${SPINE_ROOT}/spine-c/spine-c/src/spine/*.c")
+elseif(EXISTS "${SPINE_ROOT}/spine-cpp/include")
+ set(SPINE_BACKEND "${SRC}/esengine/bindings/SpineRuntimeCpp.cpp")
+ set(SPINE_INCLUDE "${SPINE_ROOT}/spine-cpp/include")
+ file(GLOB SPINE_SOURCES "${SPINE_ROOT}/spine-cpp/src/spine/*.cpp")
+else()
+ message(FATAL_ERROR "Spine runtime ${ESTELLA_SPINE_VERSION} not vendored at ${SPINE_ROOT}. "
+ "Pass -DESTELLA_SPINE_VERSION=<4.3|4.2|4.1|3.8>.")
endif()
-file(GLOB SPINE_SOURCES "${SPINE_DIR}/src/spine/*.c")
-add_library(spine_c STATIC ${SPINE_SOURCES})
-target_include_directories(spine_c PUBLIC "${SPINE_DIR}/include")
-target_compile_options(spine_c PRIVATE -w) # vendored C, not ours to lint
-add_library(estella_spine STATIC "${SRC}/esengine/bindings/SpineModuleEntry.cpp")
+add_library(spine_runtime STATIC ${SPINE_SOURCES})
+target_include_directories(spine_runtime PUBLIC "${SPINE_INCLUDE}")
+target_compile_options(spine_runtime PRIVATE -w) # vendored, not ours to lint
+
+string(REPLACE "." "" SPINE_TAG ${ESTELLA_SPINE_VERSION})
+add_library(estella_spine STATIC
+ "${SRC}/esengine/bindings/SpineModuleEntry.cpp"
+ "${SPINE_BACKEND}")
target_include_directories(estella_spine PUBLIC "${SRC}")
-target_link_libraries(estella_spine PUBLIC spine_c)
-if(ESTELLA_SPINE_VERSION STREQUAL "4.1")
- target_compile_definitions(estella_spine PRIVATE ES_SPINE_41)
-elseif(ESTELLA_SPINE_VERSION STREQUAL "3.8")
- target_compile_definitions(estella_spine PRIVATE ES_SPINE_38)
-endif()
+target_link_libraries(estella_spine PUBLIC spine_runtime)
+target_compile_definitions(estella_spine PRIVATE ES_SPINE_VERSION=${SPINE_TAG})
# The engine's own MPEG-1 decoder (pl_mpeg), the same TU the web links as the
# "videodec" side module. Header-only dependency, so nothing to fetch.
@@ -236,7 +247,7 @@ if(ESTELLA_QUICKJS_DIR)
$<TARGET_FILE:estella_js_host> $<TARGET_FILE:esengine>
$<TARGET_FILE:estella_physics> $<TARGET_FILE:box2d>
$<TARGET_FILE:estella_video>
- $<TARGET_FILE:estella_spine> $<TARGET_FILE:spine_c>
+ $<TARGET_FILE:estella_spine> $<TARGET_FILE:spine_runtime>
$<TARGET_FILE:qjs> $<TARGET_FILE:yogacore> "${ESTELLA_DAWN_LIB}"
COMMENT "Merging the iOS host, engine and Dawn into libestella_ios.a")
else()
diff --git a/src/esengine/bindings/SpineModuleEntry.cpp b/src/esengine/bindings/SpineModuleEntry.cpp
index 3598338f9..c76c1b209 100644
--- a/src/esengine/bindings/SpineModuleEntry.cpp
+++ b/src/esengine/bindings/SpineModuleEntry.cpp
@@ -2,17 +2,26 @@
// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
/**
* @file SpineModuleEntry.cpp
- * @brief Standalone Spine WASM module entry point
+ * @brief The Spine module's exported ABI — everything that does not depend on
+ * which Spine release is vendored.
*
- * Pure computation module (no GL dependencies, no filesystem).
- * Handles: skeleton loading, animation update, mesh extraction.
- * Core WASM handles all rendering via renderer_submitTriangles.
+ * @details Pure computation: skeleton loading, animation update, mesh extraction.
+ * No GL, no filesystem. The engine core submits the batches this hands back.
*
- * Uses spine-c (pure C runtime) for minimal WASM size.
+ * Handle tables, batch packing and the readback buffers live here and are
+ * written once. The runtime itself sits behind SpineRuntime.hpp, implemented
+ * per vendored release, so adding a Spine version costs a backend rather
+ * than another branch through this file.
*
- * The same TU compiles into the native host binary, where there is no side-module
- * story: the entry points are declared in SpineBindings.hpp and reached through the
- * QuickJS wrappers EHT generates from them.
+ * The same TU compiles into the native host binary, where there is no side
+ * module story: the entry points are declared in SpineBindings.hpp and
+ * reached through the QuickJS wrappers EHT generates from them.
+ *
+ * @author ESEngine Team
+ * @date 2026
+ *
+ * @copyright Copyright (c) 2026 ESEngine Team
+ * Licensed under the Apache License, Version 2.0.
*/
// KEEPALIVE keeps these exported through the emscripten link; natively the attribute
@@ -24,180 +33,191 @@
#endif
#include "SpineBindings.hpp"
+#include "SpineRuntime.hpp"
-#include <spine/spine.h>
-#include <spine/extension.h>
-
+#include <cstdio>
+#include <cstring>
+#include <iterator>
+#include <string>
#include <unordered_map>
#include <vector>
-#include <string>
-#include <cstring>
-#include <cstdint>
-#include <utility>
-// =============================================================================
-// Spine-C Required Callbacks
-// =============================================================================
+namespace {
-void _spAtlasPage_createTexture(spAtlasPage* self, const char* path) {
- (void)self;
- (void)path;
-}
+using es::spine::ConstraintKind;
+using es::spine::Event;
+using es::spine::InstancePtr;
+using es::spine::PathMix;
+using es::spine::SkeletonPtr;
+using es::spine::TransformMix;
-void _spAtlasPage_disposeTexture(spAtlasPage* self) {
- (void)self;
-}
+/// One draw's worth of geometry: interleaved x,y,u,v,r,g,b,a plus its indices.
+struct MeshBatch {
+ std::vector<float> vertices;
+ std::vector<uint16_t> indices;
+ uint32_t texture = 0;
+ int blendMode = 0;
+};
-char* _spUtil_readFile(const char* path, int* length) {
- (void)path;
- *length = 0;
- return nullptr;
-}
+constexpr int VERTEX_FLOATS = 8;
+/// 16-bit indices, so a batch stops short of the point where one would wrap.
+constexpr size_t MAX_BATCH_VERTICES = 65535;
-// =============================================================================
-// Texture ID Helpers
-// =============================================================================
+constexpr int MAX_EVENTS_PER_UPDATE = 64;
-#ifdef ES_SPINE_38
-static uint32_t getRegionTextureId(spRegionAttachment* attachment) {
- auto* region = reinterpret_cast<spAtlasRegion*>(attachment->rendererObject);
- if (!region || !region->page) return 0;
- return static_cast<uint32_t>(
- reinterpret_cast<uintptr_t>(region->page->rendererObject));
-}
+struct LiveInstance {
+ InstancePtr instance;
+ int skeletonHandle = -1;
+};
-static uint32_t getMeshTextureId(spMeshAttachment* attachment) {
- auto* region = reinterpret_cast<spAtlasRegion*>(attachment->rendererObject);
- if (!region || !region->page) return 0;
- return static_cast<uint32_t>(
- reinterpret_cast<uintptr_t>(region->page->rendererObject));
-}
-#else
-static uint32_t getRegionTextureId(spRegionAttachment* attachment) {
- if (!attachment->region) return 0;
- return static_cast<uint32_t>(
- reinterpret_cast<uintptr_t>(attachment->region->rendererObject));
-}
+struct EventRecord {
+ const char* animationName = nullptr;
+ const char* eventName = nullptr;
+ const char* stringValue = nullptr;
+};
-static uint32_t getMeshTextureId(spMeshAttachment* attachment) {
- if (!attachment->region) return 0;
- return static_cast<uint32_t>(
- reinterpret_cast<uintptr_t>(attachment->region->rendererObject));
-}
-#endif
+struct Context {
+ std::unordered_map<int, SkeletonPtr> skeletons;
+ std::unordered_map<int, LiveInstance> instances;
+ int nextSkeletonId = 1;
+ int nextInstanceId = 1;
-// =============================================================================
-// Data Structures
-// =============================================================================
+ std::vector<MeshBatch> batches;
+
+ /// Clip-region processing, on by default: a debug and perf knob, so a scene
+ /// with no clip regions can skip the machinery and a clipped one can be
+ /// compared against its unclipped self.
+ bool clippingEnabled = true;
+
+ std::string stringBuffer;
+ std::string lastError;
+
+ std::vector<float> eventBuffer;
+ std::vector<EventRecord> eventRecords;
+ int eventCount = 0;
-// Move-only RAII over spine-c's C objects: the destructor disposes, so the
-// owning maps clean up on erase()/clear() and every error path that bails after
-// a partial construct frees automatically — no manual dispose, no leak path.
-struct SkeletonHandle {
- spAtlas* atlas = nullptr;
- spSkeletonData* skeletonData = nullptr;
- spAnimationStateData* stateData = nullptr;
-
- SkeletonHandle() = default;
- SkeletonHandle(const SkeletonHandle&) = delete;
- SkeletonHandle& operator=(const SkeletonHandle&) = delete;
- SkeletonHandle(SkeletonHandle&& o) noexcept { swap(o); }
- SkeletonHandle& operator=(SkeletonHandle&& o) noexcept { dispose(); swap(o); return *this; }
- ~SkeletonHandle() { dispose(); }
-
- void swap(SkeletonHandle& o) noexcept {
- std::swap(atlas, o.atlas);
- std::swap(skeletonData, o.skeletonData);
- std::swap(stateData, o.stateData);
+ es::spine::Instance* instanceOf(int id) {
+ auto it = instances.find(id);
+ return it == instances.end() ? nullptr : it->second.instance.get();
}
- void dispose() {
- // Reverse of construction order: stateData/skeletonData reference the atlas.
- if (stateData) { spAnimationStateData_dispose(stateData); stateData = nullptr; }
- if (skeletonData) { spSkeletonData_dispose(skeletonData); skeletonData = nullptr; }
- if (atlas) { spAtlas_dispose(atlas); atlas = nullptr; }
+
+ es::spine::Skeleton* skeletonOf(int handle) {
+ auto it = skeletons.find(handle);
+ return it == skeletons.end() ? nullptr : it->second.get();
}
};
-struct SpineInstance {
- spSkeleton* skeleton = nullptr;
- spAnimationState* state = nullptr;
- int skeletonHandle = -1;
+Context g_ctx;
- SpineInstance() = default;
- SpineInstance(const SpineInstance&) = delete;
- SpineInstance& operator=(const SpineInstance&) = delete;
- SpineInstance(SpineInstance&& o) noexcept { swap(o); }
- SpineInstance& operator=(SpineInstance&& o) noexcept { dispose(); swap(o); return *this; }
- ~SpineInstance() { dispose(); }
-
- void swap(SpineInstance& o) noexcept {
- std::swap(skeleton, o.skeleton);
- std::swap(state, o.state);
- std::swap(skeletonHandle, o.skeletonHandle);
+/**
+ * Collects a backend's triangles into batches. A texture or blend-mode change
+ * starts a new batch, as does filling one up; within a batch the incoming indices
+ * are rebased onto the vertices already written.
+ */
+class BatchCollector final : public es::spine::TriangleSink {
+public:
+ void emit(const float* positions, const float* uvs, int vertexCount,
+ const uint16_t* indices, int indexCount,
+ uint32_t texture, int blendMode, const float rgba[4]) override {
+ if (vertexCount <= 0 || indexCount <= 0) return;
+
+ MeshBatch& batch = batchFor(texture, blendMode, vertexCount);
+ const auto base = static_cast<uint16_t>(batch.vertices.size() / VERTEX_FLOATS);
+
+ for (int i = 0; i < vertexCount; ++i) {
+ batch.vertices.push_back(positions[i * 2]);
+ batch.vertices.push_back(positions[i * 2 + 1]);
+ batch.vertices.push_back(uvs[i * 2]);
+ batch.vertices.push_back(uvs[i * 2 + 1]);
+ batch.vertices.push_back(rgba[0]);
+ batch.vertices.push_back(rgba[1]);
+ batch.vertices.push_back(rgba[2]);
+ batch.vertices.push_back(rgba[3]);
+ }
+ for (int i = 0; i < indexCount; ++i) {
+ batch.indices.push_back(static_cast<uint16_t>(base + indices[i]));
+ }
}
- void dispose() {
- if (state) { spAnimationState_dispose(state); state = nullptr; }
- if (skeleton) { spSkeleton_dispose(skeleton); skeleton = nullptr; }
+
+private:
+ // Indexed rather than pointed at: the batch list reallocates as it grows, and
+ // an index survives that.
+ MeshBatch& batchFor(uint32_t texture, int blendMode, int incomingVertices) {
+ if (current_ < g_ctx.batches.size()) {
+ MeshBatch& open = g_ctx.batches[current_];
+ const bool sameState = texture == open.texture && blendMode == open.blendMode;
+ const bool fits =
+ open.vertices.size() / VERTEX_FLOATS + static_cast<size_t>(incomingVertices)
+ <= MAX_BATCH_VERTICES;
+ if (sameState && fits) return open;
+ }
+ current_ = g_ctx.batches.size();
+ g_ctx.batches.emplace_back();
+ MeshBatch& fresh = g_ctx.batches.back();
+ fresh.texture = texture;
+ fresh.blendMode = blendMode;
+ return fresh;
}
-};
-struct MeshBatch {
- std::vector<float> vertices;
- std::vector<uint16_t> indices;
- uint32_t textureId = 0;
- int blendMode = 0;
+ size_t current_ = static_cast<size_t>(-1);
};
-// =============================================================================
-// Spine Context
-// =============================================================================
+/// Packs one event into the float buffer the SDK reads back, plus its strings.
+void collectEvent(const Event& event) {
+ if (g_ctx.eventCount >= MAX_EVENTS_PER_UPDATE) return;
-struct SpineContext {
- std::unordered_map<int, SkeletonHandle> skeletons;
- std::unordered_map<int, SpineInstance> instances;
- int nextSkeletonId = 1;
- int nextInstanceId = 1;
+ // Two ints ride in the float buffer as their bit patterns — the SDK reads them
+ // back through the same reinterpretation.
+ const auto pushInt = [](int value) {
+ float bits;
+ std::memcpy(&bits, &value, sizeof(bits));
+ g_ctx.eventBuffer.push_back(bits);
+ };
- std::vector<MeshBatch> meshBatches;
- std::vector<float> worldVertices;
+ pushInt(static_cast<int>(event.kind));
+ pushInt(event.track);
+ g_ctx.eventBuffer.push_back(event.floatValue);
+ pushInt(event.intValue);
- // Spine's own clip region machinery (spSkeletonClipping), shared across
- // frames and lazily created. clippingEnabled is a global toggle (debug /
- // perf knob, default on) so a clip-on vs clip-off mesh can be compared.
- spSkeletonClipping* clipper = nullptr;
- bool clippingEnabled = true;
+ g_ctx.eventRecords.push_back(EventRecord{
+ event.animation,
+ event.name,
+ event.stringValue,
+ });
+ ++g_ctx.eventCount;
+}
- std::string stringBuffer;
- std::string lastError;
+/// Re-poses `instanceId` and refills the batch list the getters below read.
+void extractBatches(int instanceId) {
+ g_ctx.batches.clear();
+ auto* instance = g_ctx.instanceOf(instanceId);
+ if (!instance) return;
- struct EventRecord {
- const char* animationName = nullptr;
- const char* eventName = nullptr;
- const char* stringValue = nullptr;
- };
+ BatchCollector collector;
+ es::spine::render(instance, collector, g_ctx.clippingEnabled);
+}
- std::vector<float> eventBuffer;
- std::vector<EventRecord> eventRecords;
- int eventCount = 0;
+const char* publish(const char* text) {
+ g_ctx.stringBuffer = text ? text : "";
+ return g_ctx.stringBuffer.c_str();
+}
- void reset() {
- instances.clear(); // RAII dtors dispose — instances before skeletons
- skeletons.clear();
-
- nextSkeletonId = 1;
- nextInstanceId = 1;
- meshBatches.clear();
- worldVertices.clear();
- if (clipper) { spSkeletonClipping_dispose(clipper); clipper = nullptr; }
- stringBuffer.clear();
- lastError.clear();
- eventBuffer.clear();
- eventRecords.clear();
- eventCount = 0;
- }
-};
+/// Builds `["a","b"]` from an indexed name getter — how animations and skins cross.
+const char* publishNameArray(int count, const char* (*nameAt)(const es::spine::Instance*, int),
+ const es::spine::Instance* instance) {
+ g_ctx.stringBuffer = "[";
+ for (int i = 0; i < count; ++i) {
+ if (i > 0) g_ctx.stringBuffer += ',';
+ const char* name = nameAt(instance, i);
+ g_ctx.stringBuffer += '"';
+ g_ctx.stringBuffer += name ? name : "";
+ g_ctx.stringBuffer += '"';
+ }
+ g_ctx.stringBuffer += ']';
+ return g_ctx.stringBuffer.c_str();
+}
-static SpineContext g_ctx;
+} // namespace
// =============================================================================
// Resource Management
@@ -210,55 +230,13 @@ int spine_loadSkeleton(uintptr_t skelDataPtr, int skelDataLen,
const char* atlasText, int atlasLen, int isBinary) {
g_ctx.lastError.clear();
- int id = g_ctx.nextSkeletonId;
- auto& handle = g_ctx.skeletons[id];
-
- handle.atlas = spAtlas_create(atlasText, atlasLen, "", nullptr);
- if (!handle.atlas || !handle.atlas->pages) {
- g_ctx.lastError = "Failed to create atlas (invalid atlas text or no pages)";
- g_ctx.skeletons.erase(id);
- return -1;
- }
-
- if (isBinary) {
- spSkeletonBinary* binary = spSkeletonBinary_create(handle.atlas);
- if (!binary) {
- g_ctx.lastError = "Failed to create skeleton binary reader";
- g_ctx.skeletons.erase(id);
- return -1;
- }
- binary->scale = 1.0f;
- handle.skeletonData = spSkeletonBinary_readSkeletonData(
- binary, reinterpret_cast<const unsigned char*>(skelDataPtr), skelDataLen);
- if (!handle.skeletonData && binary->error) {
- g_ctx.lastError = binary->error;
- }
- spSkeletonBinary_dispose(binary);
- } else {
- spSkeletonJson* json = spSkeletonJson_create(handle.atlas);
- if (!json) {
- g_ctx.lastError = "Failed to create skeleton json reader";
- g_ctx.skeletons.erase(id);
- return -1;
- }
- json->scale = 1.0f;
- handle.skeletonData = spSkeletonJson_readSkeletonData(
- json, reinterpret_cast<const char*>(skelDataPtr));
- if (!handle.skeletonData && json->error) {
- g_ctx.lastError = json->error;
- }
- spSkeletonJson_dispose(json);
- }
-
- if (!handle.skeletonData) {
- g_ctx.skeletons.erase(id);
- return -1;
- }
-
- handle.stateData = spAnimationStateData_create(handle.skeletonData);
- handle.stateData->defaultMix = 0.2f;
+ es::spine::Skeleton* loaded = es::spine::loadSkeleton(
+ reinterpret_cast<const void*>(skelDataPtr), skelDataLen,
+ atlasText, atlasLen, isBinary != 0, g_ctx.lastError);
+ if (!loaded) return -1;
- g_ctx.nextSkeletonId++;
+ const int id = g_ctx.nextSkeletonId++;
+ g_ctx.skeletons.emplace(id, SkeletonPtr(loaded));
return id;
}
@@ -272,70 +250,29 @@ void spine_unloadSkeleton(int handle) {
auto it = g_ctx.skeletons.find(handle);
if (it == g_ctx.skeletons.end()) return;
- std::vector<int> toRemove;
- for (auto& [id, inst] : g_ctx.instances) {
- if (inst.skeletonHandle == handle) {
- toRemove.push_back(id);
- }
+ // Instances hold the skeleton's data; they go first.
+ for (auto instance = g_ctx.instances.begin(); instance != g_ctx.instances.end();) {
+ instance = instance->second.skeletonHandle == handle
+ ? g_ctx.instances.erase(instance)
+ : std::next(instance);
}
- for (int id : toRemove) {
- g_ctx.instances.erase(id);
- }
-
g_ctx.skeletons.erase(it);
}
EMSCRIPTEN_KEEPALIVE
int spine_getAtlasPageCount(int handle) {
- auto it = g_ctx.skeletons.find(handle);
- if (it == g_ctx.skeletons.end()) return 0;
- int count = 0;
- spAtlasPage* page = it->second.atlas->pages;
- while (page) {
- count++;
- page = page->next;
- }
- return count;
+ return es::spine::atlasPageCount(g_ctx.skeletonOf(handle));
}
EMSCRIPTEN_KEEPALIVE
const char* spine_getAtlasPageTextureName(int handle, int pageIndex) {
- auto it = g_ctx.skeletons.find(handle);
- if (it == g_ctx.skeletons.end()) return "";
- spAtlasPage* page = it->second.atlas->pages;
- for (int i = 0; i < pageIndex && page; i++) {
- page = page->next;
- }
- if (!page) return "";
- g_ctx.stringBuffer = page->name;
- return g_ctx.stringBuffer.c_str();
+ return publish(es::spine::atlasPageName(g_ctx.skeletonOf(handle), pageIndex));
}
EMSCRIPTEN_KEEPALIVE
void spine_setAtlasPageTexture(int handle, int pageIndex,
- uint32_t textureId, int width, int height) {
- auto it = g_ctx.skeletons.find(handle);
- if (it == g_ctx.skeletons.end()) return;
- spAtlasPage* page = it->second.atlas->pages;
- for (int i = 0; i < pageIndex && page; i++) {
- page = page->next;
- }
- if (!page) return;
-
- void* texPtr = reinterpret_cast<void*>(static_cast<uintptr_t>(textureId));
- page->rendererObject = texPtr;
- page->width = width;
- page->height = height;
-
-#ifndef ES_SPINE_38
- spAtlasRegion* region = it->second.atlas->regions;
- while (region) {
- if (region->page == page) {
- region->super.rendererObject = texPtr;
- }
- region = region->next;
- }
-#endif
+ uint32_t textureId, int width, int height) {
+ es::spine::setAtlasPageTexture(g_ctx.skeletonOf(handle), pageIndex, textureId, width, height);
}
// =============================================================================
@@ -344,34 +281,20 @@ void spine_setAtlasPageTexture(int handle, int pageIndex,
EMSCRIPTEN_KEEPALIVE
int spine_createInstance(int skeletonHandle) {
- auto it = g_ctx.skeletons.find(skeletonHandle);
- if (it == g_ctx.skeletons.end()) return -1;
-
- int id = g_ctx.nextInstanceId;
- auto& inst = g_ctx.instances[id];
- inst.skeletonHandle = skeletonHandle;
- inst.skeleton = spSkeleton_create(it->second.skeletonData);
- inst.state = spAnimationState_create(it->second.stateData);
- if (!inst.skeleton || !inst.state) {
- g_ctx.instances.erase(id); // RAII frees whichever was created
- return -1;
- }
- spSkeleton_setToSetupPose(inst.skeleton);
-#if defined(ES_SPINE_38) || defined(ES_SPINE_41)
- spSkeleton_updateWorldTransform(inst.skeleton);
-#else
- spSkeleton_updateWorldTransform(inst.skeleton, SP_PHYSICS_UPDATE);
-#endif
+ auto* skeleton = g_ctx.skeletonOf(skeletonHandle);
+ if (!skeleton) return -1;
+
+ es::spine::Instance* created = es::spine::createInstance(skeleton);
+ if (!created) return -1;
- g_ctx.nextInstanceId++;
+ const int id = g_ctx.nextInstanceId++;
+ g_ctx.instances.emplace(id, LiveInstance{InstancePtr(created), skeletonHandle});
return id;
}
EMSCRIPTEN_KEEPALIVE
void spine_destroyInstance(int instanceId) {
- auto it = g_ctx.instances.find(instanceId);
- if (it == g_ctx.instances.end()) return;
- g_ctx.instances.erase(it);
+ g_ctx.instances.erase(instanceId);
}
// =============================================================================
@@ -380,52 +303,29 @@ void spine_destroyInstance(int instanceId) {
EMSCRIPTEN_KEEPALIVE
int spine_playAnimation(int instanceId, const char* name, int loop, int track) {
- auto it = g_ctx.instances.find(instanceId);
- if (it == g_ctx.instances.end()) return 0;
- spTrackEntry* entry = spAnimationState_setAnimationByName(
- it->second.state, track, name, loop);
- return entry ? 1 : 0;
+ return es::spine::playAnimation(g_ctx.instanceOf(instanceId), name, loop != 0, track) ? 1 : 0;
}
EMSCRIPTEN_KEEPALIVE
-int spine_addAnimation(int instanceId, const char* name,
- int loop, float delay, int track) {
- auto it = g_ctx.instances.find(instanceId);
- if (it == g_ctx.instances.end()) return 0;
- spTrackEntry* entry = spAnimationState_addAnimationByName(
- it->second.state, track, name, loop, delay);
- return entry ? 1 : 0;
+int spine_addAnimation(int instanceId, const char* name, int loop, float delay, int track) {
+ return es::spine::addAnimation(g_ctx.instanceOf(instanceId), name, loop != 0, delay, track) ? 1 : 0;
}
EMSCRIPTEN_KEEPALIVE
void spine_setSkin(int instanceId, const char* name) {
- auto it = g_ctx.instances.find(instanceId);
- if (it == g_ctx.instances.end()) return;
-
- if (!name || name[0] == '\0') {
- spSkeleton_setSkin(it->second.skeleton, nullptr);
- } else {
- spSkeleton_setSkinByName(it->second.skeleton, name);
- }
- spSkeleton_setSlotsToSetupPose(it->second.skeleton);
+ es::spine::setSkin(g_ctx.instanceOf(instanceId), name);
}
EMSCRIPTEN_KEEPALIVE
void spine_update(int instanceId, float dt) {
- auto it = g_ctx.instances.find(instanceId);
- if (it == g_ctx.instances.end()) return;
+ auto* instance = g_ctx.instanceOf(instanceId);
+ if (!instance) return;
g_ctx.eventBuffer.clear();
+ g_ctx.eventRecords.clear();
g_ctx.eventCount = 0;
- spAnimationState_update(it->second.state, dt);
- spAnimationState_apply(it->second.state, it->second.skeleton);
-#if defined(ES_SPINE_38) || defined(ES_SPINE_41)
- spSkeleton_updateWorldTransform(it->second.skeleton);
-#else
- spSkeleton_update(it->second.skeleton, dt);
- spSkeleton_updateWorldTransform(it->second.skeleton, SP_PHYSICS_UPDATE);
-#endif
+ es::spine::update(instance, dt);
}
// =============================================================================
@@ -434,370 +334,83 @@ void spine_update(int instanceId, float dt) {
EMSCRIPTEN_KEEPALIVE
const char* spine_getAnimations(int instanceId) {
- auto it = g_ctx.instances.find(instanceId);
- if (it == g_ctx.instances.end()) {
- g_ctx.stringBuffer = "[]";
- return g_ctx.stringBuffer.c_str();
- }
-
- spSkeletonData* data = it->second.skeleton->data;
- g_ctx.stringBuffer = "[";
- for (int i = 0; i < data->animationsCount; ++i) {
- if (i > 0) g_ctx.stringBuffer += ",";
- g_ctx.stringBuffer += "\"";
- g_ctx.stringBuffer += data->animations[i]->name;
- g_ctx.stringBuffer += "\"";
- }
- g_ctx.stringBuffer += "]";
- return g_ctx.stringBuffer.c_str();
+ auto* instance = g_ctx.instanceOf(instanceId);
+ if (!instance) return publish("[]");
+ return publishNameArray(es::spine::animationCount(instance), es::spine::animationName, instance);
}
EMSCRIPTEN_KEEPALIVE
const char* spine_getSkins(int instanceId) {
- auto it = g_ctx.instances.find(instanceId);
- if (it == g_ctx.instances.end()) {
- g_ctx.stringBuffer = "[]";
- return g_ctx.stringBuffer.c_str();
- }
-
- spSkeletonData* data = it->second.skeleton->data;
- g_ctx.stringBuffer = "[";
- for (int i = 0; i < data->skinsCount; ++i) {
- if (i > 0) g_ctx.stringBuffer += ",";
- g_ctx.stringBuffer += "\"";
- g_ctx.stringBuffer += data->skins[i]->name;
- g_ctx.stringBuffer += "\"";
- }
- g_ctx.stringBuffer += "]";
- return g_ctx.stringBuffer.c_str();
+ auto* instance = g_ctx.instanceOf(instanceId);
+ if (!instance) return publish("[]");
+ return publishNameArray(es::spine::skinCount(instance), es::spine::skinName, instance);
}
EMSCRIPTEN_KEEPALIVE
int spine_getBonePosition(int instanceId, const char* bone,
uintptr_t outXPtr, uintptr_t outYPtr) {
- auto it = g_ctx.instances.find(instanceId);
- if (it == g_ctx.instances.end()) return 0;
-
- spBone* b = spSkeleton_findBone(it->second.skeleton, bone);
- if (!b) return 0;
-
- *reinterpret_cast<float*>(outXPtr) = b->worldX;
- *reinterpret_cast<float*>(outYPtr) = b->worldY;
- return 1;
+ return es::spine::bonePosition(g_ctx.instanceOf(instanceId), bone,
+ reinterpret_cast<float*>(outXPtr),
+ reinterpret_cast<float*>(outYPtr))
+ ? 1
+ : 0;
}
EMSCRIPTEN_KEEPALIVE
float spine_getBoneRotation(int instanceId, const char* bone) {
- auto it = g_ctx.instances.find(instanceId);
- if (it == g_ctx.instances.end()) return 0.0f;
-
- spBone* b = spSkeleton_findBone(it->second.skeleton, bone);
- if (!b) return 0.0f;
-
- return spBone_getWorldRotationX(b);
+ float degrees = 0.0f;
+ es::spine::boneRotation(g_ctx.instanceOf(instanceId), bone, &degrees);
+ return degrees;
}
EMSCRIPTEN_KEEPALIVE
void spine_getBounds(int instanceId, uintptr_t outXPtr, uintptr_t outYPtr,
- uintptr_t outWPtr, uintptr_t outHPtr) {
- auto* outX = reinterpret_cast<float*>(outXPtr);
- auto* outY = reinterpret_cast<float*>(outYPtr);
- auto* outW = reinterpret_cast<float*>(outWPtr);
- auto* outH = reinterpret_cast<float*>(outHPtr);
-
- auto it = g_ctx.instances.find(instanceId);
- if (it == g_ctx.instances.end()) {
- *outX = *outY = *outW = *outH = 0;
- return;
- }
-
- spSkeleton* skeleton = it->second.skeleton;
- float minX = 1e30f, minY = 1e30f, maxX = -1e30f, maxY = -1e30f;
- bool hasVerts = false;
-
- for (int i = 0; i < skeleton->slotsCount; i++) {
- spSlot* slot = skeleton->drawOrder[i];
- if (!slot->attachment) continue;
-
- float* verts = nullptr;
- int vertCount = 0;
-
- if (slot->attachment->type == SP_ATTACHMENT_REGION) {
- auto* region = reinterpret_cast<spRegionAttachment*>(slot->attachment);
- g_ctx.worldVertices.resize(8);
-#ifdef ES_SPINE_38
- spRegionAttachment_computeWorldVertices(region, slot->bone, g_ctx.worldVertices.data(), 0, 2);
-#else
- spRegionAttachment_computeWorldVertices(region, slot, g_ctx.worldVertices.data(), 0, 2);
-#endif
- verts = g_ctx.worldVertices.data();
- vertCount = 4;
- } else if (slot->attachment->type == SP_ATTACHMENT_MESH) {
- auto* mesh = reinterpret_cast<spMeshAttachment*>(slot->attachment);
- vertCount = SUPER(mesh)->worldVerticesLength / 2;
- g_ctx.worldVertices.resize(SUPER(mesh)->worldVerticesLength);
- spVertexAttachment_computeWorldVertices(SUPER(mesh), slot, 0,
- SUPER(mesh)->worldVerticesLength, g_ctx.worldVertices.data(), 0, 2);
- verts = g_ctx.worldVertices.data();
- }
-
- if (verts && vertCount > 0) {
- hasVerts = true;
- for (int j = 0; j < vertCount; j++) {
- float x = verts[j * 2];
- float y = verts[j * 2 + 1];
- if (x < minX) minX = x;
- if (x > maxX) maxX = x;
- if (y < minY) minY = y;
- if (y > maxY) maxY = y;
- }
- }
- }
-
- if (hasVerts) {
- *outX = minX;
- *outY = minY;
- *outW = maxX - minX;
- *outH = maxY - minY;
- } else {
- *outX = *outY = *outW = *outH = 0;
- }
+ uintptr_t outWPtr, uintptr_t outHPtr) {
+ es::spine::bounds(g_ctx.instanceOf(instanceId),
+ reinterpret_cast<float*>(outXPtr), reinterpret_cast<float*>(outYPtr),
+ reinterpret_cast<float*>(outWPtr), reinterpret_cast<float*>(outHPtr));
}
// =============================================================================
// Mesh Extraction
// =============================================================================
-// Emit one attachment's triangles into the current batch, applying the active
-// clip region when clipping is on. Uses spine's own spSkeletonClipping — the
-// same algorithm the native path used — so 4.2 (which routes here once a
-// provider is configured) finally clips instead of dropping the clip silently.
-// `verts`/`uvs` are x,y / u,v interleaved; `tris` index into `verts`. Clipping
-// replaces the triangle set with the polygon intersection, never expanding it.
-static void emitClippedTriangles(
- MeshBatch*& currentBatch, uint32_t& currentTexture, int& currentBlend,
- float* verts, int vertCount, float* uvs,
- unsigned short* tris, int triCount,
- uint32_t texId, int effectiveBlend,
- float r, float g, float b, float a
-) {
- float* outVerts = verts;
- float* outUVs = uvs;
- unsigned short* outTris = tris;
- int outVertCount = vertCount;
- int outTriCount = triCount;
-
- if (g_ctx.clippingEnabled && spSkeletonClipping_isClipping(g_ctx.clipper)) {
- spSkeletonClipping_clipTriangles(
- g_ctx.clipper, verts, vertCount * 2, tris, triCount, uvs, 2);
- outVerts = g_ctx.clipper->clippedVertices->items;
- outVertCount = g_ctx.clipper->clippedVertices->size / 2;
- outUVs = g_ctx.clipper->clippedUVs->items;
- outTris = g_ctx.clipper->clippedTriangles->items;
- outTriCount = g_ctx.clipper->clippedTriangles->size;
- }
-
- if (outVertCount == 0 || outTriCount == 0) return;
-
- bool needNewBatch = !currentBatch || texId != currentTexture || effectiveBlend != currentBlend;
- if (!needNewBatch && currentBatch->vertices.size() / 8 + outVertCount > 65535) {
- needNewBatch = true;
- }
- if (needNewBatch) {
- g_ctx.meshBatches.emplace_back();
- currentBatch = &g_ctx.meshBatches.back();
- currentBatch->textureId = texId;
- currentBatch->blendMode = effectiveBlend;
- currentTexture = texId;
- currentBlend = effectiveBlend;
- }
-
- auto baseIndex = static_cast<uint16_t>(currentBatch->vertices.size() / 8);
- for (int j = 0; j < outVertCount; ++j) {
- currentBatch->vertices.push_back(outVerts[j * 2]);
- currentBatch->vertices.push_back(outVerts[j * 2 + 1]);
- currentBatch->vertices.push_back(outUVs[j * 2]);
- currentBatch->vertices.push_back(outUVs[j * 2 + 1]);
- currentBatch->vertices.push_back(r);
- currentBatch->vertices.push_back(g);
- currentBatch->vertices.push_back(b);
- currentBatch->vertices.push_back(a);
- }
- for (int j = 0; j < outTriCount; ++j) {
- currentBatch->indices.push_back(static_cast<uint16_t>(baseIndex + outTris[j]));
- }
-}
-
-static void extractMeshBatches(int instanceId) {
- g_ctx.meshBatches.clear();
-
- auto it = g_ctx.instances.find(instanceId);
- if (it == g_ctx.instances.end()) return;
-
- if (!g_ctx.clipper) g_ctx.clipper = spSkeletonClipping_create();
-
- spSkeleton* skeleton = it->second.skeleton;
- spColor& skelColor = skeleton->color;
-
- MeshBatch* currentBatch = nullptr;
- uint32_t currentTexture = 0;
- int currentBlend = 0;
-
- for (int i = 0; i < skeleton->slotsCount; ++i) {
- spSlot* slot = skeleton->drawOrder[i];
- if (!slot) continue;
-
- spAttachment* attachment = slot->attachment;
-
- // A clipping attachment opens a clip region (until its endSlot); it emits
- // no geometry itself.
- if (attachment && attachment->type == SP_ATTACHMENT_CLIPPING) {
- if (g_ctx.clippingEnabled) {
- spSkeletonClipping_clipStart(g_ctx.clipper, slot,
- reinterpret_cast<spClippingAttachment*>(attachment));
- }
- continue;
- }
-
- bool visible = attachment != nullptr;
-#if !defined(ES_SPINE_38) && !defined(ES_SPINE_41)
- if (visible && !slot->data->visible) visible = false;
-#endif
-
- if (visible) {
- spColor& slotColor = slot->color;
-
- int blendMode = 0;
- switch (slot->data->blendMode) {
- case SP_BLEND_MODE_NORMAL: blendMode = 0; break;
- case SP_BLEND_MODE_ADDITIVE: blendMode = 1; break;
- case SP_BLEND_MODE_MULTIPLY: blendMode = 2; break;
- case SP_BLEND_MODE_SCREEN: blendMode = 3; break;
- }
-
- if (attachment->type == SP_ATTACHMENT_REGION) {
- auto* region = reinterpret_cast<spRegionAttachment*>(attachment);
- uint32_t texId = getRegionTextureId(region);
- if (texId) {
- g_ctx.worldVertices.resize(8);
-#ifdef ES_SPINE_38
- spRegionAttachment_computeWorldVertices(region, slot->bone, g_ctx.worldVertices.data(), 0, 2);
-#else
- spRegionAttachment_computeWorldVertices(region, slot, g_ctx.worldVertices.data(), 0, 2);
-#endif
- int effectiveBlend = blendMode;
-#ifndef ES_SPINE_38
- if (region->region) {
- auto* atlasReg = reinterpret_cast<spAtlasRegion*>(region->region);
- if (atlasReg->page && atlasReg->page->pma) {
- if (effectiveBlend == 0) effectiveBlend = 4;
- else if (effectiveBlend == 1) effectiveBlend = 5;
- }
- }
-#endif
- spColor& attachColor = region->color;
- float a = skelColor.a * slotColor.a * attachColor.a;
- float r = skelColor.r * slotColor.r * attachColor.r;
- float g = skelColor.g * slotColor.g * attachColor.g;
- float b = skelColor.b * slotColor.b * attachColor.b;
- if (effectiveBlend >= 4) { r *= a; g *= a; b *= a; }
-
- static unsigned short QUAD_TRIS[6] = {0, 1, 2, 2, 3, 0};
- emitClippedTriangles(currentBatch, currentTexture, currentBlend,
- g_ctx.worldVertices.data(), 4, region->uvs, QUAD_TRIS, 6,
- texId, effectiveBlend, r, g, b, a);
- }
- } else if (attachment->type == SP_ATTACHMENT_MESH) {
- auto* mesh = reinterpret_cast<spMeshAttachment*>(attachment);
- uint32_t texId = getMeshTextureId(mesh);
- if (texId) {
- int worldVerticesLength = SUPER(mesh)->worldVerticesLength;
- int vertexCount = worldVerticesLength / 2;
- g_ctx.worldVertices.resize(worldVerticesLength);
- spVertexAttachment_computeWorldVertices(SUPER(mesh), slot, 0,
- worldVerticesLength, g_ctx.worldVertices.data(), 0, 2);
-
- int effectiveBlend = blendMode;
-#ifndef ES_SPINE_38
- if (mesh->region) {
- auto* atlasReg = reinterpret_cast<spAtlasRegion*>(mesh->region);
- if (atlasReg->page && atlasReg->page->pma) {
- if (effectiveBlend == 0) effectiveBlend = 4;
- else if (effectiveBlend == 1) effectiveBlend = 5;
- }
- }
-#endif
- spColor& attachColor = mesh->color;
- float a = skelColor.a * slotColor.a * attachColor.a;
- float r = skelColor.r * slotColor.r * attachColor.r;
- float g = skelColor.g * slotColor.g * attachColor.g;
- float b = skelColor.b * slotColor.b * attachColor.b;
- if (effectiveBlend >= 4) { r *= a; g *= a; b *= a; }
-
- emitClippedTriangles(currentBatch, currentTexture, currentBlend,
- g_ctx.worldVertices.data(), vertexCount, mesh->uvs,
- mesh->triangles, mesh->trianglesCount,
- texId, effectiveBlend, r, g, b, a);
- }
- }
- }
-
- // clipEnd closes the region when this slot is the clip's endSlot
- // (cheap no-op otherwise). Called for every non-clip slot, per spine.
- if (g_ctx.clippingEnabled) {
- spSkeletonClipping_clipEnd(g_ctx.clipper, slot);
- }
- }
-
- if (g_ctx.clippingEnabled) {
- spSkeletonClipping_clipEnd2(g_ctx.clipper);
- }
-}
-
EMSCRIPTEN_KEEPALIVE
int spine_getMeshBatchCount(int instanceId) {
- extractMeshBatches(instanceId);
- return static_cast<int>(g_ctx.meshBatches.size());
+ extractBatches(instanceId);
+ return static_cast<int>(g_ctx.batches.size());
}
EMSCRIPTEN_KEEPALIVE
int spine_getMeshBatchVertexCount(int instanceId, int batchIndex) {
(void)instanceId;
- if (batchIndex < 0 || batchIndex >= static_cast<int>(g_ctx.meshBatches.size())) return 0;
- return static_cast<int>(g_ctx.meshBatches[batchIndex].vertices.size() / 8);
+ if (batchIndex < 0 || batchIndex >= static_cast<int>(g_ctx.batches.size())) return 0;
+ return static_cast<int>(g_ctx.batches[batchIndex].vertices.size() / VERTEX_FLOATS);
}
EMSCRIPTEN_KEEPALIVE
int spine_getMeshBatchIndexCount(int instanceId, int batchIndex) {
(void)instanceId;
- if (batchIndex < 0 || batchIndex >= static_cast<int>(g_ctx.meshBatches.size())) return 0;
- return static_cast<int>(g_ctx.meshBatches[batchIndex].indices.size());
+ if (batchIndex < 0 || batchIndex >= static_cast<int>(g_ctx.batches.size())) return 0;
+ return static_cast<int>(g_ctx.batches[batchIndex].indices.size());
}
EMSCRIPTEN_KEEPALIVE
void spine_getMeshBatchData(int instanceId, int batchIndex,
- uintptr_t outVerticesPtr, uintptr_t outIndicesPtr,
- uintptr_t outTextureIdPtr, uintptr_t outBlendModePtr) {
+ uintptr_t outVerticesPtr, uintptr_t outIndicesPtr,
+ uintptr_t outTextureIdPtr, uintptr_t outBlendModePtr) {
(void)instanceId;
- if (batchIndex < 0 || batchIndex >= static_cast<int>(g_ctx.meshBatches.size())) return;
-
- auto& batch = g_ctx.meshBatches[batchIndex];
+ if (batchIndex < 0 || batchIndex >= static_cast<int>(g_ctx.batches.size())) return;
- auto* outVertices = reinterpret_cast<float*>(outVerticesPtr);
- auto* outIndices = reinterpret_cast<uint16_t*>(outIndicesPtr);
- auto* outTextureId = reinterpret_cast<uint32_t*>(outTextureIdPtr);
- auto* outBlendMode = reinterpret_cast<int*>(outBlendModePtr);
-
- std::memcpy(outVertices, batch.vertices.data(),
+ const MeshBatch& batch = g_ctx.batches[batchIndex];
+ std::memcpy(reinterpret_cast<float*>(outVerticesPtr), batch.vertices.data(),
batch.vertices.size() * sizeof(float));
- std::memcpy(outIndices, batch.indices.data(),
+ std::memcpy(reinterpret_cast<uint16_t*>(outIndicesPtr), batch.indices.data(),
batch.indices.size() * sizeof(uint16_t));
- *outTextureId = batch.textureId;
- *outBlendMode = batch.blendMode;
+ *reinterpret_cast<uint32_t*>(outTextureIdPtr) = batch.texture;
+ *reinterpret_cast<int*>(outBlendModePtr) = batch.blendMode;
}
-// Toggle clip-region processing (default on). Lets a caller compare clip-on vs
-// clip-off output; also a perf escape hatch for scenes with no clip regions.
EMSCRIPTEN_KEEPALIVE
void spine_setClippingEnabled(int enabled) {
g_ctx.clippingEnabled = enabled != 0;
@@ -809,84 +422,30 @@ void spine_setClippingEnabled(int enabled) {
EMSCRIPTEN_KEEPALIVE
void spine_setDefaultMix(int skeletonHandle, float duration) {
- auto it = g_ctx.skeletons.find(skeletonHandle);
- if (it == g_ctx.skeletons.end() || !it->second.stateData) return;
- it->second.stateData->defaultMix = duration;
+ es::spine::setDefaultMix(g_ctx.skeletonOf(skeletonHandle), duration);
}
EMSCRIPTEN_KEEPALIVE
void spine_setMixDuration(int skeletonHandle, const char* fromAnim,
- const char* toAnim, float duration) {
- auto it = g_ctx.skeletons.find(skeletonHandle);
- if (it == g_ctx.skeletons.end() || !it->second.stateData) return;
-
- spAnimation* from = spSkeletonData_findAnimation(it->second.skeletonData, fromAnim);
- spAnimation* to = spSkeletonData_findAnimation(it->second.skeletonData, toAnim);
- if (!from || !to) return;
-
- spAnimationStateData_setMix(it->second.stateData, from, to, duration);
+ const char* toAnim, float duration) {
+ es::spine::setMixDuration(g_ctx.skeletonOf(skeletonHandle), fromAnim, toAnim, duration);
}
EMSCRIPTEN_KEEPALIVE
void spine_setTrackAlpha(int instanceId, int track, float alpha) {
- auto it = g_ctx.instances.find(instanceId);
- if (it == g_ctx.instances.end()) return;
-
- spTrackEntry* entry = spAnimationState_getCurrent(it->second.state, track);
- if (entry) {
- entry->alpha = alpha;
- }
+ es::spine::setTrackAlpha(g_ctx.instanceOf(instanceId), track, alpha);
}
// =============================================================================
// Event Collection
// =============================================================================
-static constexpr int EVENT_STRIDE = 4;
-static constexpr int MAX_EVENTS_PER_UPDATE = 64;
-
-static void spineEventListener(spAnimationState* state, spEventType type,
- spTrackEntry* entry, spEvent* event) {
- (void)state;
- if (g_ctx.eventCount >= MAX_EVENTS_PER_UPDATE) return;
-
- float typeF;
- int typeInt = static_cast<int>(type);
- std::memcpy(&typeF, &typeInt, sizeof(float));
- g_ctx.eventBuffer.push_back(typeF);
-
- float trackF;
- int trackInt = entry ? entry->trackIndex : 0;
- std::memcpy(&trackF, &trackInt, sizeof(float));
- g_ctx.eventBuffer.push_back(trackF);
-
- if (type == SP_ANIMATION_EVENT && event) {
- g_ctx.eventBuffer.push_back(event->floatValue);
- float intValF;
- int intVal = event->intValue;
- std::memcpy(&intValF, &intVal, sizeof(float));
- g_ctx.eventBuffer.push_back(intValF);
- } else {
- g_ctx.eventBuffer.push_back(0.0f);
- g_ctx.eventBuffer.push_back(0.0f);
- }
-
- SpineContext::EventRecord record{};
- record.animationName = (entry && entry->animation) ? entry->animation->name : nullptr;
- if (type == SP_ANIMATION_EVENT && event) {
- record.eventName = event->data->name;
- record.stringValue = event->stringValue;
- }
- g_ctx.eventRecords.push_back(record);
-
- g_ctx.eventCount++;
-}
-
EMSCRIPTEN_KEEPALIVE
void spine_enableEvents(int instanceId) {
- auto it = g_ctx.instances.find(instanceId);
- if (it == g_ctx.instances.end()) return;
- it->second.state->listener = spineEventListener;
+ auto* instance = g_ctx.instanceOf(instanceId);
+ if (!instance) return;
+ es::spine::setEventSink(collectEvent);
+ es::spine::enableEvents(instance);
}
EMSCRIPTEN_KEEPALIVE
@@ -910,244 +469,135 @@ void spine_clearEvents() {
EMSCRIPTEN_KEEPALIVE
const char* spine_getEventAnimationName(int index) {
if (index < 0 || index >= g_ctx.eventCount) return "";
- auto name = g_ctx.eventRecords[index].animationName;
+ const char* name = g_ctx.eventRecords[index].animationName;
return name ? name : "";
}
EMSCRIPTEN_KEEPALIVE
const char* spine_getEventName(int index) {
if (index < 0 || index >= g_ctx.eventCount) return "";
- auto name = g_ctx.eventRecords[index].eventName;
+ const char* name = g_ctx.eventRecords[index].eventName;
return name ? name : "";
}
EMSCRIPTEN_KEEPALIVE
const char* spine_getEventStringValue(int index) {
if (index < 0 || index >= g_ctx.eventCount) return "";
- auto val = g_ctx.eventRecords[index].stringValue;
- return val ? val : "";
+ const char* value = g_ctx.eventRecords[index].stringValue;
+ return value ? value : "";
}
// =============================================================================
-// Attachment Manipulation
+// Attachment / Constraint / Colour Control
// =============================================================================
EMSCRIPTEN_KEEPALIVE
-int spine_setAttachment(int instanceId, const char* slotName,
- const char* attachmentName) {
- auto it = g_ctx.instances.find(instanceId);
- if (it == g_ctx.instances.end()) return 0;
-
- int result = spSkeleton_setAttachment(
- it->second.skeleton, slotName,
- (attachmentName && attachmentName[0] != '\0') ? attachmentName : nullptr);
- return result;
+int spine_setAttachment(int instanceId, const char* slotName, const char* attachmentName) {
+ return es::spine::setAttachment(g_ctx.instanceOf(instanceId), slotName, attachmentName) ? 1 : 0;
}
-// =============================================================================
-// IK Constraint Control
-// =============================================================================
-
EMSCRIPTEN_KEEPALIVE
int spine_setIKTarget(int instanceId, const char* constraintName,
- float targetX, float targetY, float mix) {
- auto it = g_ctx.instances.find(instanceId);
- if (it == g_ctx.instances.end()) return 0;
-
- spIkConstraint* constraint = spSkeleton_findIkConstraint(
- it->second.skeleton, constraintName);
- if (!constraint) return 0;
-
- constraint->target->x = targetX;
- constraint->target->y = targetY;
- constraint->mix = mix;
- return 1;
+ float targetX, float targetY, float mix) {
+ return es::spine::setIkTarget(g_ctx.instanceOf(instanceId), constraintName,
+ targetX, targetY, mix)
+ ? 1
+ : 0;
}
-// =============================================================================
-// Transform Constraint Control
-// =============================================================================
-
EMSCRIPTEN_KEEPALIVE
const char* spine_listConstraints(int instanceId) {
- static std::string jsonBuf;
- auto it = g_ctx.instances.find(instanceId);
- if (it == g_ctx.instances.end()) return "{}";
-
- auto* skeleton = it->second.skeleton;
- jsonBuf = "{\"ik\":[";
- for (int i = 0; i < skeleton->ikConstraintsCount; ++i) {
- if (i > 0) jsonBuf += ',';
- jsonBuf += '"';
- jsonBuf += skeleton->ikConstraints[i]->data->name;
- jsonBuf += '"';
- }
- jsonBuf += "],\"transform\":[";
- for (int i = 0; i < skeleton->transformConstraintsCount; ++i) {
- if (i > 0) jsonBuf += ',';
- jsonBuf += '"';
- jsonBuf += skeleton->transformConstraints[i]->data->name;
- jsonBuf += '"';
- }
- jsonBuf += "],\"path\":[";
- for (int i = 0; i < skeleton->pathConstraintsCount; ++i) {
- if (i > 0) jsonBuf += ',';
- jsonBuf += '"';
- jsonBuf += skeleton->pathConstraints[i]->data->name;
- jsonBuf += '"';
+ auto* instance = g_ctx.instanceOf(instanceId);
+ if (!instance) return publish("{}");
+
+ static constexpr struct {
+ const char* key;
+ ConstraintKind kind;
+ } kinds[] = {
+ {"ik", ConstraintKind::Ik},
+ {"transform", ConstraintKind::Transform},
+ {"path", ConstraintKind::Path},
+ };
+
+ g_ctx.stringBuffer = "{";
+ for (size_t k = 0; k < sizeof(kinds) / sizeof(kinds[0]); ++k) {
+ if (k > 0) g_ctx.stringBuffer += ',';
+ g_ctx.stringBuffer += '"';
+ g_ctx.stringBuffer += kinds[k].key;
+ g_ctx.stringBuffer += "\":[";
+ const int count = es::spine::constraintCount(instance, kinds[k].kind);
+ for (int i = 0; i < count; ++i) {
+ if (i > 0) g_ctx.stringBuffer += ',';
+ const char* name = es::spine::constraintName(instance, kinds[k].kind, i);
+ g_ctx.stringBuffer += '"';
+ g_ctx.stringBuffer += name ? name : "";
+ g_ctx.stringBuffer += '"';
+ }
+ g_ctx.stringBuffer += ']';
}
- jsonBuf += "]}";
- return jsonBuf.c_str();
+ g_ctx.stringBuffer += '}';
+ return g_ctx.stringBuffer.c_str();
}
EMSCRIPTEN_KEEPALIVE
const char* spine_getTransformConstraintMix(int instanceId, const char* name) {
- static std::string jsonBuf;
- auto it = g_ctx.instances.find(instanceId);
- if (it == g_ctx.instances.end()) return "";
-
- auto* constraint = spSkeleton_findTransformConstraint(it->second.skeleton, name);
- if (!constraint) return "";
-
- char buf[256];
-#ifdef ES_SPINE_38
- snprintf(buf, sizeof(buf),
- "{\"mixRotate\":%.6g,\"mixX\":%.6g,\"mixY\":%.6g,\"mixScaleX\":%.6g,\"mixScaleY\":%.6g,\"mixShearY\":%.6g}",
- constraint->rotateMix, constraint->translateMix, constraint->translateMix,
- constraint->scaleMix, constraint->scaleMix, constraint->shearMix);
-#else
- snprintf(buf, sizeof(buf),
- "{\"mixRotate\":%.6g,\"mixX\":%.6g,\"mixY\":%.6g,\"mixScaleX\":%.6g,\"mixScaleY\":%.6g,\"mixShearY\":%.6g}",
- constraint->mixRotate, constraint->mixX, constraint->mixY,
- constraint->mixScaleX, constraint->mixScaleY, constraint->mixShearY);
-#endif
- jsonBuf = buf;
- return jsonBuf.c_str();
+ TransformMix mix;
+ if (!es::spine::transformMix(g_ctx.instanceOf(instanceId), name, &mix)) return publish("");
+
+ char buffer[256];
+ std::snprintf(buffer, sizeof(buffer),
+ "{\"mixRotate\":%.6g,\"mixX\":%.6g,\"mixY\":%.6g,"
+ "\"mixScaleX\":%.6g,\"mixScaleY\":%.6g,\"mixShearY\":%.6g}",
+ mix.rotate, mix.x, mix.y, mix.scaleX, mix.scaleY, mix.shearY);
+ return publish(buffer);
}
EMSCRIPTEN_KEEPALIVE
-int spine_setTransformConstraintMix(int instanceId, const char* name,
- float rotate, float x, float y, float scaleX, float scaleY, float shearY) {
- auto it = g_ctx.instances.find(instanceId);
- if (it == g_ctx.instances.end()) return 0;
-
- auto* constraint = spSkeleton_findTransformConstraint(it->second.skeleton, name);
- if (!constraint) return 0;
-
-#ifdef ES_SPINE_38
- constraint->rotateMix = rotate;
- constraint->translateMix = (x + y) * 0.5f;
- constraint->scaleMix = (scaleX + scaleY) * 0.5f;
- constraint->shearMix = shearY;
-#else
- constraint->mixRotate = rotate;
- constraint->mixX = x;
- constraint->mixY = y;
- constraint->mixScaleX = scaleX;
- constraint->mixScaleY = scaleY;
- constraint->mixShearY = shearY;
-#endif
- return 1;
+int spine_setTransformConstraintMix(int instanceId, const char* name, float rotate,
+ float x, float y, float scaleX, float scaleY, float shearY) {
+ const TransformMix mix{rotate, x, y, scaleX, scaleY, shearY};
+ return es::spine::setTransformMix(g_ctx.instanceOf(instanceId), name, mix) ? 1 : 0;
}
-// =============================================================================
-// Path Constraint Control
-// =============================================================================
-
EMSCRIPTEN_KEEPALIVE
const char* spine_getPathConstraintMix(int instanceId, const char* name) {
- static std::string jsonBuf;
- auto it = g_ctx.instances.find(instanceId);
- if (it == g_ctx.instances.end()) return "";
-
- auto* constraint = spSkeleton_findPathConstraint(it->second.skeleton, name);
- if (!constraint) return "";
-
- char buf[256];
-#ifdef ES_SPINE_38
- snprintf(buf, sizeof(buf),
- "{\"position\":%.6g,\"spacing\":%.6g,\"mixRotate\":%.6g,\"mixX\":%.6g,\"mixY\":%.6g}",
- constraint->position, constraint->spacing,
- constraint->rotateMix, constraint->translateMix, constraint->translateMix);
-#else
- snprintf(buf, sizeof(buf),
- "{\"position\":%.6g,\"spacing\":%.6g,\"mixRotate\":%.6g,\"mixX\":%.6g,\"mixY\":%.6g}",
- constraint->position, constraint->spacing,
- constraint->mixRotate, constraint->mixX, constraint->mixY);
-#endif
- jsonBuf = buf;
- return jsonBuf.c_str();
+ PathMix mix;
+ if (!es::spine::pathMix(g_ctx.instanceOf(instanceId), name, &mix)) return publish("");
+
+ char buffer[256];
+ std::snprintf(buffer, sizeof(buffer),
+ "{\"position\":%.6g,\"spacing\":%.6g,"
+ "\"mixRotate\":%.6g,\"mixX\":%.6g,\"mixY\":%.6g}",
+ mix.position, mix.spacing, mix.rotate, mix.x, mix.y);
+ return publish(buffer);
}
EMSCRIPTEN_KEEPALIVE
-int spine_setPathConstraintMix(int instanceId, const char* name,
- float position, float spacing, float rotate, float x, float y) {
- auto it = g_ctx.instances.find(instanceId);
- if (it == g_ctx.instances.end()) return 0;
-
- auto* constraint = spSkeleton_findPathConstraint(it->second.skeleton, name);
- if (!constraint) return 0;
-
- constraint->position = position;
- constraint->spacing = spacing;
-#ifdef ES_SPINE_38
- constraint->rotateMix = rotate;
- constraint->translateMix = (x + y) * 0.5f;
-#else
- constraint->mixRotate = rotate;
- constraint->mixX = x;
- constraint->mixY = y;
-#endif
- return 1;
+int spine_setPathConstraintMix(int instanceId, const char* name, float position,
+ float spacing, float rotate, float x, float y) {
+ const PathMix mix{position, spacing, rotate, x, y};
+ return es::spine::setPathMix(g_ctx.instanceOf(instanceId), name, mix) ? 1 : 0;
}
-// =============================================================================
-// Slot Color Control
-// =============================================================================
-
EMSCRIPTEN_KEEPALIVE
int spine_setSlotColor(int instanceId, const char* slotName,
- float r, float g, float b, float a) {
- auto it = g_ctx.instances.find(instanceId);
- if (it == g_ctx.instances.end()) return 0;
-
- spSlot* slot = spSkeleton_findSlot(it->second.skeleton, slotName);
- if (!slot) return 0;
-
- slot->color.r = r;
- slot->color.g = g;
- slot->color.b = b;
- slot->color.a = a;
- return 1;
+ float r, float g, float b, float a) {
+ return es::spine::setSlotColor(g_ctx.instanceOf(instanceId), slotName, r, g, b, a) ? 1 : 0;
}
-// Whole-skeleton tint: extraction already multiplies skeleton->color into every
-// vertex (skelColor * slotColor * attachColor), so this is all the wiring a
-// SpineAnimation.color needs — the skeleton-level analogue of setSlotColor.
+// Whole-skeleton tint: extraction already multiplies the skeleton colour into every
+// vertex, so this is all the wiring a SpineAnimation.color needs.
EMSCRIPTEN_KEEPALIVE
int spine_setSkeletonColor(int instanceId, float r, float g, float b, float a) {
- auto it = g_ctx.instances.find(instanceId);
- if (it == g_ctx.instances.end()) return 0;
-
- it->second.skeleton->color.r = r;
- it->second.skeleton->color.g = g;
- it->second.skeleton->color.b = b;
- it->second.skeleton->color.a = a;
- return 1;
+ return es::spine::setSkeletonColor(g_ctx.instanceOf(instanceId), r, g, b, a) ? 1 : 0;
}
-// Which spine-c runtime this build linked. The three vendored runtimes export the
-// same symbols, so a binary carries exactly one and the caller has to be able to ask
-// (the web names the artifact instead — spine38.wasm / spine41 / spine42).
+// Which Spine runtime this build linked. Every backend exports the same symbols, so a
+// binary carries exactly one and the caller has to be able to ask (the web names the
+// artifact instead — spine38.wasm / spine41 / spine42 / spine43).
EMSCRIPTEN_KEEPALIVE
int spine_runtimeVersion() {
-#if defined(ES_SPINE_38)
- return 38;
-#elif defined(ES_SPINE_41)
- return 41;
-#else
- return 42;
-#endif
+ return es::spine::version();
}
// The live extent of the event buffer, for the readback getter above: it is this
@@ -1158,7 +608,7 @@ size_t spine_eventBufferBytes() {
return g_ctx.eventBuffer.size() * sizeof(float);
}
-} // extern "C"
+} // extern "C"
#ifdef __EMSCRIPTEN__
// The module is linked as an executable, so it needs an entry point; a native build
diff --git a/src/esengine/bindings/SpineRuntime.hpp b/src/esengine/bindings/SpineRuntime.hpp
new file mode 100644
index 000000000..1591f7587
--- /dev/null
+++ b/src/esengine/bindings/SpineRuntime.hpp
@@ -0,0 +1,184 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/**
+ * @file SpineRuntime.hpp
+ * @brief What a vendored Spine runtime owes the module, and nothing more.
+ *
+ * @details SpineModuleEntry.cpp implements the exported ABI (SpineBindings.hpp):
+ * handle tables, batch packing, the string and event buffers the caller
+ * reads back. None of that changes with the Spine release. Everything
+ * that does — the runtime's own types, its spelling of "update the world
+ * transform", whether slot colour is a struct or four floats — lives
+ * behind this header, implemented once per vendored runtime.
+ *
+ * Exactly one implementation links into a binary: the web ships a module
+ * per version (spine38 / spine41 / spine42 / spine43) and a native host
+ * compiles the one its project needs. Selection is therefore a link-time
+ * fact, not a runtime one — no vtable, no dispatch, and a backend that
+ * forgets a function fails to link rather than failing on a device.
+ *
+ * Skeleton and Instance stay incomplete here on purpose: the entry layer
+ * only ever holds them by pointer, so it cannot reach into a runtime's
+ * structs and quietly grow a dependency on one release's layout.
+ *
+ * @author ESEngine Team
+ * @date 2026
+ *
+ * @copyright Copyright (c) 2026 ESEngine Team
+ * Licensed under the Apache License, Version 2.0.
+ */
+#pragma once
+
+#include <cstdint>
+#include <memory>
+#include <string>
+
+namespace es::spine {
+
+/** A loaded skeleton: its atlas, its data, and the mix table instances share. */
+struct Skeleton;
+
+/** One posable copy of a Skeleton, with its own animation state. */
+struct Instance;
+
+void destroy(Skeleton* skeleton);
+void destroy(Instance* instance);
+
+/// Owning handles, so every error path that bails mid-construct still frees.
+struct SkeletonDeleter {
+ void operator()(Skeleton* p) const { destroy(p); }
+};
+struct InstanceDeleter {
+ void operator()(Instance* p) const { destroy(p); }
+};
+using SkeletonPtr = std::unique_ptr<Skeleton, SkeletonDeleter>;
+using InstancePtr = std::unique_ptr<Instance, InstanceDeleter>;
+
+enum class ConstraintKind { Ik, Transform, Path };
+
+/**
+ * Constraint mix weights, always per-axis. 4.0 split the old single rotate /
+ * translate / scale / shear mixes into these; a runtime that predates the split
+ * folds them back on the way in and mirrors them on the way out, so the SDK sees
+ * one shape whatever it loaded.
+ */
+struct TransformMix {
+ float rotate = 0.0f, x = 0.0f, y = 0.0f;
+ float scaleX = 0.0f, scaleY = 0.0f, shearY = 0.0f;
+};
+
+struct PathMix {
+ float position = 0.0f, spacing = 0.0f;
+ float rotate = 0.0f, x = 0.0f, y = 0.0f;
+};
+
+/**
+ * Animation-state events, numbered as the SDK decodes them (SPINE_TYPE_MAP in
+ * SpinePlugin.ts). The numbering is 4.x's; runtimes that number their own enum
+ * differently translate onto this rather than making the SDK guess which
+ * dialect it is talking to.
+ */
+enum class EventKind {
+ Start = 0,
+ Interrupt = 1,
+ End = 2,
+ Complete = 3,
+ Dispose = 4,
+ Event = 5,
+};
+
+struct Event {
+ EventKind kind = EventKind::Event;
+ int track = 0;
+ float floatValue = 0.0f;
+ int intValue = 0;
+ /// Borrowed from the runtime's own data — valid until the skeleton unloads.
+ const char* animation = nullptr;
+ const char* name = nullptr;
+ const char* stringValue = nullptr;
+};
+
+/// The module is a singleton, so its collector is process-wide rather than per instance.
+using EventSink = void (*)(const Event&);
+
+/**
+ * Where a backend pours the triangles it posed. The sink batches: it decides when
+ * a texture or blend change starts a new batch and when one has filled up, so that
+ * policy is written once instead of once per runtime.
+ *
+ * `positions` and `uvs` are x,y / u,v interleaved; `indices` index into them. One
+ * colour covers the emission — Spine's colour is per attachment (skeleton × slot ×
+ * attachment), so a per-vertex array would carry the same value repeated.
+ *
+ * `blendMode` is 0 normal, 1 additive, 2 multiply, 3 screen, and 4 / 5 for the
+ * premultiplied twins of the first two, which is how the engine's renderer reads it.
+ */
+struct TriangleSink {
+ virtual ~TriangleSink() = default;
+ virtual void emit(const float* positions, const float* uvs, int vertexCount,
+ const uint16_t* indices, int indexCount,
+ uint32_t texture, int blendMode, const float rgba[4]) = 0;
+};
+
+// --- resources -------------------------------------------------------------
+
+/** Returns null and fills `error` when the atlas or the skeleton data is unreadable. */
+Skeleton* loadSkeleton(const void* skeletonData, int length,
+ const char* atlasText, int atlasLength,
+ bool binary, std::string& error);
+
+int atlasPageCount(const Skeleton* skeleton);
+const char* atlasPageName(const Skeleton* skeleton, int page);
+void setAtlasPageTexture(Skeleton* skeleton, int page, uint32_t texture, int width, int height);
+
+void setDefaultMix(Skeleton* skeleton, float seconds);
+void setMixDuration(Skeleton* skeleton, const char* from, const char* to, float seconds);
+
+// --- instances -------------------------------------------------------------
+
+Instance* createInstance(Skeleton* skeleton);
+void update(Instance* instance, float dt);
+
+bool playAnimation(Instance* instance, const char* animation, bool loop, int track);
+bool addAnimation(Instance* instance, const char* animation, bool loop, float delay, int track);
+/** Null or empty restores the default skin. */
+void setSkin(Instance* instance, const char* skin);
+void setTrackAlpha(Instance* instance, int track, float alpha);
+
+void setEventSink(EventSink sink);
+void enableEvents(Instance* instance);
+
+// --- queries ---------------------------------------------------------------
+
+int animationCount(const Instance* instance);
+const char* animationName(const Instance* instance, int index);
+int skinCount(const Instance* instance);
+const char* skinName(const Instance* instance, int index);
+
+bool bonePosition(const Instance* instance, const char* bone, float* x, float* y);
+bool boneRotation(const Instance* instance, const char* bone, float* degrees);
+void bounds(const Instance* instance, float* x, float* y, float* width, float* height);
+
+int constraintCount(const Instance* instance, ConstraintKind kind);
+const char* constraintName(const Instance* instance, ConstraintKind kind, int index);
+bool transformMix(const Instance* instance, const char* name, TransformMix* out);
+bool setTransformMix(Instance* instance, const char* name, const TransformMix& mix);
+bool pathMix(const Instance* instance, const char* name, PathMix* out);
+bool setPathMix(Instance* instance, const char* name, const PathMix& mix);
+
+// --- posing ----------------------------------------------------------------
+
+bool setAttachment(Instance* instance, const char* slot, const char* attachment);
+bool setIkTarget(Instance* instance, const char* constraint, float x, float y, float mix);
+bool setSlotColor(Instance* instance, const char* slot, float r, float g, float b, float a);
+bool setSkeletonColor(Instance* instance, float r, float g, float b, float a);
+
+// --- geometry --------------------------------------------------------------
+
+/** Poses the skeleton's attachments into `sink`, applying clip regions when asked. */
+void render(Instance* instance, TriangleSink& sink, bool clipping);
+
+/** 21 / 38 / 41 / 42 / 43 — whatever `spine_runtimeVersion()` should report. */
+int version();
+
+} // namespace es::spine
diff --git a/src/esengine/bindings/SpineRuntimeC.cpp b/src/esengine/bindings/SpineRuntimeC.cpp
new file mode 100644
index 000000000..a7c3080b3
--- /dev/null
+++ b/src/esengine/bindings/SpineRuntimeC.cpp
@@ -0,0 +1,703 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/**
+ * @file SpineRuntimeC.cpp
+ * @brief The SpineRuntime backend for spine-c, the pure-C runtime (3.8 - 4.2).
+ *
+ * @details One vendored release links at a time; ES_SPINE_VERSION says which, as an
+ * integer so the differences read as the version ranges they are ("region
+ * attachments carried their own atlas pointer before 4.0") instead of a
+ * chain of per-release macros.
+ *
+ * 4.3 retired this runtime — its C API was regenerated on top of spine-cpp
+ * and shares nothing with these types — so that release has its own backend
+ * rather than another branch in here.
+ *
+ * @author ESEngine Team
+ * @date 2026
+ *
+ * @copyright Copyright (c) 2026 ESEngine Team
+ * Licensed under the Apache License, Version 2.0.
+ */
+
+#include "SpineRuntime.hpp"
+
+#include <spine/spine.h>
+#include <spine/extension.h>
+
+#include <cstdio>
+#include <cstring>
+#include <vector>
+
+#ifndef ES_SPINE_VERSION
+#error "ES_SPINE_VERSION must name the vendored spine-c release (38 / 41 / 42)."
+#endif
+
+// =============================================================================
+// spine-c Required Callbacks
+// =============================================================================
+//
+// The module never reads a file or creates a texture: the SDK loads atlas pages
+// and hands their ids back through setAtlasPageTexture. spine-c still insists the
+// hooks exist, so they exist and do nothing.
+
+void _spAtlasPage_createTexture(spAtlasPage* self, const char* path) {
+ (void)self;
+ (void)path;
+}
+
+void _spAtlasPage_disposeTexture(spAtlasPage* self) {
+ (void)self;
+}
+
+char* _spUtil_readFile(const char* path, int* length) {
+ (void)path;
+ *length = 0;
+ return nullptr;
+}
+
+namespace es::spine {
+
+// =============================================================================
+// Resources
+// =============================================================================
+
+struct Skeleton {
+ spAtlas* atlas = nullptr;
+ spSkeletonData* data = nullptr;
+ spAnimationStateData* stateData = nullptr;
+
+ Skeleton() = default;
+ Skeleton(const Skeleton&) = delete;
+ Skeleton& operator=(const Skeleton&) = delete;
+
+ // Reverse of construction order: stateData/data reference the atlas.
+ ~Skeleton() {
+ if (stateData) spAnimationStateData_dispose(stateData);
+ if (data) spSkeletonData_dispose(data);
+ if (atlas) spAtlas_dispose(atlas);
+ }
+};
+
+struct Instance {
+ spSkeleton* skeleton = nullptr;
+ spAnimationState* state = nullptr;
+
+ Instance() = default;
+ Instance(const Instance&) = delete;
+ Instance& operator=(const Instance&) = delete;
+
+ ~Instance() {
+ if (state) spAnimationState_dispose(state);
+ if (skeleton) spSkeleton_dispose(skeleton);
+ }
+};
+
+void destroy(Skeleton* skeleton) { delete skeleton; }
+void destroy(Instance* instance) { delete instance; }
+
+namespace {
+
+/// Scratch for one attachment's world vertices — reused across attachments and frames.
+std::vector<float> g_worldVertices;
+
+/// spine's own clip machinery, created on first use and kept for the module's life.
+spSkeletonClipping* g_clipper = nullptr;
+
+EventSink g_eventSink = nullptr;
+
+spAtlasPage* pageAt(const Skeleton* skeleton, int index) {
+ if (!skeleton || index < 0) return nullptr;
+ spAtlasPage* page = skeleton->atlas->pages;
+ for (int i = 0; i < index && page; ++i) page = page->next;
+ return page;
+}
+
+#if ES_SPINE_VERSION < 40
+// Before 4.0 an attachment pointed straight at its atlas region; the texture id
+// the SDK registered lives on that region's page.
+uint32_t regionTexture(spRegionAttachment* attachment) {
+ auto* region = reinterpret_cast<spAtlasRegion*>(attachment->rendererObject);
+ if (!region || !region->page) return 0;
+ return static_cast<uint32_t>(reinterpret_cast<uintptr_t>(region->page->rendererObject));
+}
+
+uint32_t meshTexture(spMeshAttachment* attachment) {
+ auto* region = reinterpret_cast<spAtlasRegion*>(attachment->rendererObject);
+ if (!region || !region->page) return 0;
+ return static_cast<uint32_t>(reinterpret_cast<uintptr_t>(region->page->rendererObject));
+}
+#else
+uint32_t regionTexture(spRegionAttachment* attachment) {
+ if (!attachment->region) return 0;
+ return static_cast<uint32_t>(reinterpret_cast<uintptr_t>(attachment->region->rendererObject));
+}
+
+uint32_t meshTexture(spMeshAttachment* attachment) {
+ if (!attachment->region) return 0;
+ return static_cast<uint32_t>(reinterpret_cast<uintptr_t>(attachment->region->rendererObject));
+}
+
+/// Straight-alpha blend modes get their premultiplied twin when the page is PMA.
+int premultiply(int blendMode, spAtlasRegion* region) {
+ if (!region || !region->page || !region->page->pma) return blendMode;
+ if (blendMode == 0) return 4;
+ if (blendMode == 1) return 5;
+ return blendMode;
+}
+#endif
+
+int blendModeOf(spSlot* slot) {
+ switch (slot->data->blendMode) {
+ case SP_BLEND_MODE_ADDITIVE: return 1;
+ case SP_BLEND_MODE_MULTIPLY: return 2;
+ case SP_BLEND_MODE_SCREEN: return 3;
+ default: return 0;
+ }
+}
+
+void eventListener(spAnimationState* state, spEventType type, spTrackEntry* entry, spEvent* event) {
+ (void)state;
+ if (!g_eventSink) return;
+
+ Event out;
+ out.kind = static_cast<EventKind>(static_cast<int>(type));
+ out.track = entry ? entry->trackIndex : 0;
+ out.animation = (entry && entry->animation) ? entry->animation->name : nullptr;
+ if (type == SP_ANIMATION_EVENT && event) {
+ out.floatValue = event->floatValue;
+ out.intValue = event->intValue;
+ out.name = event->data->name;
+ out.stringValue = event->stringValue;
+ }
+ g_eventSink(out);
+}
+
+} // namespace
+
+Skeleton* loadSkeleton(const void* skeletonData, int length,
+ const char* atlasText, int atlasLength,
+ bool binary, std::string& error) {
+ auto skeleton = std::make_unique<Skeleton>();
+
+ skeleton->atlas = spAtlas_create(atlasText, atlasLength, "", nullptr);
+ if (!skeleton->atlas || !skeleton->atlas->pages) {
+ error = "Failed to create atlas (invalid atlas text or no pages)";
+ return nullptr;
+ }
+
+ if (binary) {
+ spSkeletonBinary* reader = spSkeletonBinary_create(skeleton->atlas);
+ if (!reader) {
+ error = "Failed to create skeleton binary reader";
+ return nullptr;
+ }
+ reader->scale = 1.0f;
+ skeleton->data = spSkeletonBinary_readSkeletonData(
+ reader, static_cast<const unsigned char*>(skeletonData), length);
+ if (!skeleton->data && reader->error) error = reader->error;
+ spSkeletonBinary_dispose(reader);
+ } else {
+ spSkeletonJson* reader = spSkeletonJson_create(skeleton->atlas);
+ if (!reader) {
+ error = "Failed to create skeleton json reader";
+ return nullptr;
+ }
+ reader->scale = 1.0f;
+ skeleton->data = spSkeletonJson_readSkeletonData(
+ reader, static_cast<const char*>(skeletonData));
+ if (!skeleton->data && reader->error) error = reader->error;
+ spSkeletonJson_dispose(reader);
+ }
+
+ if (!skeleton->data) return nullptr;
+
+ skeleton->stateData = spAnimationStateData_create(skeleton->data);
+ skeleton->stateData->defaultMix = 0.2f;
+ return skeleton.release();
+}
+
+int atlasPageCount(const Skeleton* skeleton) {
+ if (!skeleton) return 0;
+ int count = 0;
+ for (spAtlasPage* page = skeleton->atlas->pages; page; page = page->next) ++count;
+ return count;
+}
+
+const char* atlasPageName(const Skeleton* skeleton, int page) {
+ spAtlasPage* p = pageAt(skeleton, page);
+ return p ? p->name : nullptr;
+}
+
+void setAtlasPageTexture(Skeleton* skeleton, int page, uint32_t texture, int width, int height) {
+ spAtlasPage* p = pageAt(skeleton, page);
+ if (!p) return;
+
+ void* handle = reinterpret_cast<void*>(static_cast<uintptr_t>(texture));
+ p->rendererObject = handle;
+ p->width = width;
+ p->height = height;
+
+#if ES_SPINE_VERSION >= 40
+ // 4.0 moved the renderer object onto the region; attachments read it there.
+ for (spAtlasRegion* region = skeleton->atlas->regions; region; region = region->next) {
+ if (region->page == p) region->super.rendererObject = handle;
+ }
+#endif
+}
+
+void setDefaultMix(Skeleton* skeleton, float seconds) {
+ if (skeleton && skeleton->stateData) skeleton->stateData->defaultMix = seconds;
+}
+
+void setMixDuration(Skeleton* skeleton, const char* from, const char* to, float seconds) {
+ if (!skeleton || !skeleton->stateData) return;
+ spAnimation* a = spSkeletonData_findAnimation(skeleton->data, from);
+ spAnimation* b = spSkeletonData_findAnimation(skeleton->data, to);
+ if (a && b) spAnimationStateData_setMix(skeleton->stateData, a, b, seconds);
+}
+
+// =============================================================================
+// Instances
+// =============================================================================
+
+Instance* createInstance(Skeleton* skeleton) {
+ if (!skeleton) return nullptr;
+
+ auto instance = std::make_unique<Instance>();
+ instance->skeleton = spSkeleton_create(skeleton->data);
+ instance->state = spAnimationState_create(skeleton->stateData);
+ if (!instance->skeleton || !instance->state) return nullptr;
+
+ spSkeleton_setToSetupPose(instance->skeleton);
+#if ES_SPINE_VERSION >= 42
+ spSkeleton_updateWorldTransform(instance->skeleton, SP_PHYSICS_UPDATE);
+#else
+ spSkeleton_updateWorldTransform(instance->skeleton);
+#endif
+ return instance.release();
+}
+
+void update(Instance* instance, float dt) {
+ if (!instance) return;
+ spAnimationState_update(instance->state, dt);
+ spAnimationState_apply(instance->state, instance->skeleton);
+#if ES_SPINE_VERSION >= 42
+ // 4.2 skeletons carry physics constraints, which advance with the clock.
+ spSkeleton_update(instance->skeleton, dt);
+ spSkeleton_updateWorldTransform(instance->skeleton, SP_PHYSICS_UPDATE);
+#else
+ spSkeleton_updateWorldTransform(instance->skeleton);
+#endif
+}
+
+bool playAnimation(Instance* instance, const char* animation, bool loop, int track) {
+ if (!instance) return false;
+ return spAnimationState_setAnimationByName(instance->state, track, animation, loop) != nullptr;
+}
+
+bool addAnimation(Instance* instance, const char* animation, bool loop, float delay, int track) {
+ if (!instance) return false;
+ return spAnimationState_addAnimationByName(instance->state, track, animation, loop, delay) != nullptr;
+}
+
+void setSkin(Instance* instance, const char* skin) {
+ if (!instance) return;
+ if (!skin || skin[0] == '\0') {
+ spSkeleton_setSkin(instance->skeleton, nullptr);
+ } else {
+ spSkeleton_setSkinByName(instance->skeleton, skin);
+ }
+ spSkeleton_setSlotsToSetupPose(instance->skeleton);
+}
+
+void setTrackAlpha(Instance* instance, int track, float alpha) {
+ if (!instance) return;
+ spTrackEntry* entry = spAnimationState_getCurrent(instance->state, track);
+ if (entry) entry->alpha = alpha;
+}
+
+void setEventSink(EventSink sink) {
+ g_eventSink = sink;
+}
+
+void enableEvents(Instance* instance) {
+ if (instance) instance->state->listener = eventListener;
+}
+
+// =============================================================================
+// Queries
+// =============================================================================
+
+int animationCount(const Instance* instance) {
+ return instance ? instance->skeleton->data->animationsCount : 0;
+}
+
+const char* animationName(const Instance* instance, int index) {
+ if (!instance) return nullptr;
+ spSkeletonData* data = instance->skeleton->data;
+ if (index < 0 || index >= data->animationsCount) return nullptr;
+ return data->animations[index]->name;
+}
+
+int skinCount(const Instance* instance) {
+ return instance ? instance->skeleton->data->skinsCount : 0;
+}
+
+const char* skinName(const Instance* instance, int index) {
+ if (!instance) return nullptr;
+ spSkeletonData* data = instance->skeleton->data;
+ if (index < 0 || index >= data->skinsCount) return nullptr;
+ return data->skins[index]->name;
+}
+
+bool bonePosition(const Instance* instance, const char* bone, float* x, float* y) {
+ if (!instance) return false;
+ spBone* b = spSkeleton_findBone(instance->skeleton, bone);
+ if (!b) return false;
+ *x = b->worldX;
+ *y = b->worldY;
+ return true;
+}
+
+bool boneRotation(const Instance* instance, const char* bone, float* degrees) {
+ if (!instance) return false;
+ spBone* b = spSkeleton_findBone(instance->skeleton, bone);
+ if (!b) return false;
+ *degrees = spBone_getWorldRotationX(b);
+ return true;
+}
+
+void bounds(const Instance* instance, float* x, float* y, float* width, float* height) {
+ *x = *y = *width = *height = 0.0f;
+ if (!instance) return;
+
+ spSkeleton* skeleton = instance->skeleton;
+ float minX = 1e30f, minY = 1e30f, maxX = -1e30f, maxY = -1e30f;
+ bool any = false;
+
+ for (int i = 0; i < skeleton->slotsCount; ++i) {
+ spSlot* slot = skeleton->drawOrder[i];
+ if (!slot->attachment) continue;
+
+ int vertexCount = 0;
+ if (slot->attachment->type == SP_ATTACHMENT_REGION) {
+ auto* region = reinterpret_cast<spRegionAttachment*>(slot->attachment);
+ g_worldVertices.resize(8);
+#if ES_SPINE_VERSION < 40
+ spRegionAttachment_computeWorldVertices(region, slot->bone, g_worldVertices.data(), 0, 2);
+#else
+ spRegionAttachment_computeWorldVertices(region, slot, g_worldVertices.data(), 0, 2);
+#endif
+ vertexCount = 4;
+ } else if (slot->attachment->type == SP_ATTACHMENT_MESH) {
+ auto* mesh = reinterpret_cast<spMeshAttachment*>(slot->attachment);
+ const int length = SUPER(mesh)->worldVerticesLength;
+ g_worldVertices.resize(length);
+ spVertexAttachment_computeWorldVertices(SUPER(mesh), slot, 0, length,
+ g_worldVertices.data(), 0, 2);
+ vertexCount = length / 2;
+ }
+
+ for (int j = 0; j < vertexCount; ++j) {
+ const float vx = g_worldVertices[j * 2];
+ const float vy = g_worldVertices[j * 2 + 1];
+ if (vx < minX) minX = vx;
+ if (vx > maxX) maxX = vx;
+ if (vy < minY) minY = vy;
+ if (vy > maxY) maxY = vy;
+ any = true;
+ }
+ }
+
+ if (!any) return;
+ *x = minX;
+ *y = minY;
+ *width = maxX - minX;
+ *height = maxY - minY;
+}
+
+int constraintCount(const Instance* instance, ConstraintKind kind) {
+ if (!instance) return 0;
+ switch (kind) {
+ case ConstraintKind::Ik: return instance->skeleton->ikConstraintsCount;
+ case ConstraintKind::Transform: return instance->skeleton->transformConstraintsCount;
+ case ConstraintKind::Path: return instance->skeleton->pathConstraintsCount;
+ }
+ return 0;
+}
+
+const char* constraintName(const Instance* instance, ConstraintKind kind, int index) {
+ if (!instance || index < 0 || index >= constraintCount(instance, kind)) return nullptr;
+ switch (kind) {
+ case ConstraintKind::Ik: return instance->skeleton->ikConstraints[index]->data->name;
+ case ConstraintKind::Transform: return instance->skeleton->transformConstraints[index]->data->name;
+ case ConstraintKind::Path: return instance->skeleton->pathConstraints[index]->data->name;
+ }
+ return nullptr;
+}
+
+bool transformMix(const Instance* instance, const char* name, TransformMix* out) {
+ if (!instance) return false;
+ auto* c = spSkeleton_findTransformConstraint(instance->skeleton, name);
+ if (!c) return false;
+#if ES_SPINE_VERSION < 40
+ // One mix per channel before 4.0; report it on both axes so the SDK sees one shape.
+ out->rotate = c->rotateMix;
+ out->x = out->y = c->translateMix;
+ out->scaleX = out->scaleY = c->scaleMix;
+ out->shearY = c->shearMix;
+#else
+ out->rotate = c->mixRotate;
+ out->x = c->mixX;
+ out->y = c->mixY;
+ out->scaleX = c->mixScaleX;
+ out->scaleY = c->mixScaleY;
+ out->shearY = c->mixShearY;
+#endif
+ return true;
+}
+
+bool setTransformMix(Instance* instance, const char* name, const TransformMix& mix) {
+ if (!instance) return false;
+ auto* c = spSkeleton_findTransformConstraint(instance->skeleton, name);
+ if (!c) return false;
+#if ES_SPINE_VERSION < 40
+ c->rotateMix = mix.rotate;
+ c->translateMix = (mix.x + mix.y) * 0.5f;
+ c->scaleMix = (mix.scaleX + mix.scaleY) * 0.5f;
+ c->shearMix = mix.shearY;
+#else
+ c->mixRotate = mix.rotate;
+ c->mixX = mix.x;
+ c->mixY = mix.y;
+ c->mixScaleX = mix.scaleX;
+ c->mixScaleY = mix.scaleY;
+ c->mixShearY = mix.shearY;
+#endif
+ return true;
+}
+
+bool pathMix(const Instance* instance, const char* name, PathMix* out) {
+ if (!instance) return false;
+ auto* c = spSkeleton_findPathConstraint(instance->skeleton, name);
+ if (!c) return false;
+ out->position = c->position;
+ out->spacing = c->spacing;
+#if ES_SPINE_VERSION < 40
+ out->rotate = c->rotateMix;
+ out->x = out->y = c->translateMix;
+#else
+ out->rotate = c->mixRotate;
+ out->x = c->mixX;
+ out->y = c->mixY;
+#endif
+ return true;
+}
+
+bool setPathMix(Instance* instance, const char* name, const PathMix& mix) {
+ if (!instance) return false;
+ auto* c = spSkeleton_findPathConstraint(instance->skeleton, name);
+ if (!c) return false;
+ c->position = mix.position;
+ c->spacing = mix.spacing;
+#if ES_SPINE_VERSION < 40
+ c->rotateMix = mix.rotate;
+ c->translateMix = (mix.x + mix.y) * 0.5f;
+#else
+ c->mixRotate = mix.rotate;
+ c->mixX = mix.x;
+ c->mixY = mix.y;
+#endif
+ return true;
+}
+
+// =============================================================================
+// Posing
+// =============================================================================
+
+bool setAttachment(Instance* instance, const char* slot, const char* attachment) {
+ if (!instance) return false;
+ const char* name = (attachment && attachment[0] != '\0') ? attachment : nullptr;
+ return spSkeleton_setAttachment(instance->skeleton, slot, name) != 0;
+}
+
+bool setIkTarget(Instance* instance, const char* constraint, float x, float y, float mix) {
+ if (!instance) return false;
+ spIkConstraint* c = spSkeleton_findIkConstraint(instance->skeleton, constraint);
+ if (!c) return false;
+ c->target->x = x;
+ c->target->y = y;
+ c->mix = mix;
+ return true;
+}
+
+bool setSlotColor(Instance* instance, const char* slot, float r, float g, float b, float a) {
+ if (!instance) return false;
+ spSlot* s = spSkeleton_findSlot(instance->skeleton, slot);
+ if (!s) return false;
+ s->color.r = r;
+ s->color.g = g;
+ s->color.b = b;
+ s->color.a = a;
+ return true;
+}
+
+bool setSkeletonColor(Instance* instance, float r, float g, float b, float a) {
+ if (!instance) return false;
+ instance->skeleton->color.r = r;
+ instance->skeleton->color.g = g;
+ instance->skeleton->color.b = b;
+ instance->skeleton->color.a = a;
+ return true;
+}
+
+// =============================================================================
+// Geometry
+// =============================================================================
+
+namespace {
+
+/**
+ * Hand one attachment's triangles to the sink, clipped against the open clip
+ * region when there is one. Clipping replaces the triangle set with the polygon
+ * intersection, so it can only shrink the geometry, never grow it.
+ */
+void emit(TriangleSink& sink, bool clipping,
+ float* positions, int vertexCount, float* uvs,
+ unsigned short* triangles, int triangleCount,
+ uint32_t texture, int blendMode, const float rgba[4]) {
+ float* outPositions = positions;
+ float* outUVs = uvs;
+ unsigned short* outTriangles = triangles;
+ int outVertices = vertexCount;
+ int outTriangleCount = triangleCount;
+
+ if (clipping && spSkeletonClipping_isClipping(g_clipper)) {
+ spSkeletonClipping_clipTriangles(g_clipper, positions, vertexCount * 2,
+ triangles, triangleCount, uvs, 2);
+ outPositions = g_clipper->clippedVertices->items;
+ outVertices = g_clipper->clippedVertices->size / 2;
+ outUVs = g_clipper->clippedUVs->items;
+ outTriangles = g_clipper->clippedTriangles->items;
+ outTriangleCount = g_clipper->clippedTriangles->size;
+ }
+
+ if (outVertices == 0 || outTriangleCount == 0) return;
+ sink.emit(outPositions, outUVs, outVertices, outTriangles, outTriangleCount,
+ texture, blendMode, rgba);
+}
+
+} // namespace
+
+void render(Instance* instance, TriangleSink& sink, bool clipping) {
+ if (!instance) return;
+ if (!g_clipper) g_clipper = spSkeletonClipping_create();
+
+ spSkeleton* skeleton = instance->skeleton;
+ const spColor& skeletonColor = skeleton->color;
+
+ for (int i = 0; i < skeleton->slotsCount; ++i) {
+ spSlot* slot = skeleton->drawOrder[i];
+ if (!slot) continue;
+
+ spAttachment* attachment = slot->attachment;
+
+ // A clipping attachment opens a region that runs until its end slot; it
+ // draws nothing itself.
+ if (attachment && attachment->type == SP_ATTACHMENT_CLIPPING) {
+ if (clipping) {
+ spSkeletonClipping_clipStart(g_clipper, slot,
+ reinterpret_cast<spClippingAttachment*>(attachment));
+ }
+ continue;
+ }
+
+ bool visible = attachment != nullptr;
+#if ES_SPINE_VERSION >= 42
+ if (visible && !slot->data->visible) visible = false;
+#endif
+
+ if (visible) {
+ const spColor& slotColor = slot->color;
+ const int blendMode = blendModeOf(slot);
+
+ if (attachment->type == SP_ATTACHMENT_REGION) {
+ auto* region = reinterpret_cast<spRegionAttachment*>(attachment);
+ const uint32_t texture = regionTexture(region);
+ if (texture) {
+ g_worldVertices.resize(8);
+#if ES_SPINE_VERSION < 40
+ spRegionAttachment_computeWorldVertices(region, slot->bone,
+ g_worldVertices.data(), 0, 2);
+ const int effectiveBlend = blendMode;
+#else
+ spRegionAttachment_computeWorldVertices(region, slot,
+ g_worldVertices.data(), 0, 2);
+ const int effectiveBlend = premultiply(
+ blendMode, reinterpret_cast<spAtlasRegion*>(region->region));
+#endif
+ const spColor& attachmentColor = region->color;
+ float rgba[4] = {
+ skeletonColor.r * slotColor.r * attachmentColor.r,
+ skeletonColor.g * slotColor.g * attachmentColor.g,
+ skeletonColor.b * slotColor.b * attachmentColor.b,
+ skeletonColor.a * slotColor.a * attachmentColor.a,
+ };
+ if (effectiveBlend >= 4) {
+ rgba[0] *= rgba[3];
+ rgba[1] *= rgba[3];
+ rgba[2] *= rgba[3];
+ }
+
+ static unsigned short quad[6] = {0, 1, 2, 2, 3, 0};
+ emit(sink, clipping, g_worldVertices.data(), 4, region->uvs, quad, 6,
+ texture, effectiveBlend, rgba);
+ }
+ } else if (attachment->type == SP_ATTACHMENT_MESH) {
+ auto* mesh = reinterpret_cast<spMeshAttachment*>(attachment);
+ const uint32_t texture = meshTexture(mesh);
+ if (texture) {
+ const int length = SUPER(mesh)->worldVerticesLength;
+ g_worldVertices.resize(length);
+ spVertexAttachment_computeWorldVertices(SUPER(mesh), slot, 0, length,
+ g_worldVertices.data(), 0, 2);
+#if ES_SPINE_VERSION < 40
+ const int effectiveBlend = blendMode;
+#else
+ const int effectiveBlend = premultiply(
+ blendMode, reinterpret_cast<spAtlasRegion*>(mesh->region));
+#endif
+ const spColor& attachmentColor = mesh->color;
+ float rgba[4] = {
+ skeletonColor.r * slotColor.r * attachmentColor.r,
+ skeletonColor.g * slotColor.g * attachmentColor.g,
+ skeletonColor.b * slotColor.b * attachmentColor.b,
+ skeletonColor.a * slotColor.a * attachmentColor.a,
+ };
+ if (effectiveBlend >= 4) {
+ rgba[0] *= rgba[3];
+ rgba[1] *= rgba[3];
+ rgba[2] *= rgba[3];
+ }
+
+ emit(sink, clipping, g_worldVertices.data(), length / 2, mesh->uvs,
+ mesh->triangles, mesh->trianglesCount,
+ texture, effectiveBlend, rgba);
+ }
+ }
+ }
+
+ // Closes the region when this slot is the clip's end slot; a cheap no-op
+ // otherwise, which is why every non-clip slot passes through here.
+ if (clipping) spSkeletonClipping_clipEnd(g_clipper, slot);
+ }
+
+ if (clipping) spSkeletonClipping_clipEnd2(g_clipper);
+}
+
+int version() {
+ return ES_SPINE_VERSION;
+}
+
+} // namespace es::spine