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.

fix(dragonbones): the armature stands up, and what it retires is recycled

yhh468ee6292412
diff --git a/sdk/tests/dragonbones-load.integration.test.ts b/sdk/tests/dragonbones-load.integration.test.ts
index afaaeddcf..7da20b932 100644
--- a/sdk/tests/dragonbones-load.integration.test.ts
+++ b/sdk/tests/dragonbones-load.integration.test.ts
@@ -144,6 +144,82 @@ describe.skipIf(!HAS_ASSETS)('DragonBones side module', () => {
call('db_unloadSkeleton', null, ['number'], [handle]);
});
+ it('survives an animation being retired — a crossfade, then the state going away', () => {
+ // The runtime hands a finished animation state back through
+ // `_armature->_dragonBones->bufferObject(state)`, and BaseFactory leaves
+ // that pointer null unless an integration sets one. Nothing here failed
+ // when it was null: on wasm the null dereference lands in readable low
+ // memory, so this test passed against the broken build too — it took an
+ // arm64 SIGSEGV to see it. Kept because the wasm build is where the code
+ // is edited, and a leak (the buffer never drains) is the quiet half of the
+ // same bug.
+ const handle = load();
+ call('db_setAtlasTexture', null, ['number', 'number'], [handle, 7]);
+ const instance = call<number>('db_createInstance', 'number', ['number', 'string'], [handle, 'Dragon']);
+ call('db_playAnimation', 'number', ['number', 'string', 'number'], [instance, 'stand', 0]);
+
+ // Cross-fade repeatedly and run past each fade, so states really do retire
+ // rather than just being queued behind one another.
+ for (const name of ['walk', 'jump', 'fall', 'stand', 'walk']) {
+ call('db_fadeInAnimation', 'number', ['number', 'string', 'number', 'number'], [instance, name, 0.2, 0]);
+ for (let i = 0; i < 30; i++) call('db_update', null, ['number', 'number'], [instance, 1 / 60]);
+ }
+
+ // Still posing afterwards: a recycled state that was handed back twice, or
+ // one still in use, shows up here as no geometry rather than as a throw.
+ expect(call<number>('db_getMeshBatchCount', 'number', ['number'], [instance])).toBeGreaterThan(0);
+
+ call('db_destroyInstance', null, ['number'], [instance]);
+ call('db_unloadSkeleton', null, ['number'], [handle]);
+ });
+
+ it('stands the character up, rather than posing it in the authoring tool\'s axes', () => {
+ // DragonBones poses y-DOWN and the engine's world is y-up, so the adapter
+ // flips. Nothing else catches it: every other assertion here is about
+ // magnitude or finiteness, and an upside-down armature satisfies all of
+ // them — it took a screenshot to notice, which is exactly why this exists.
+ //
+ // The test is DragonBoy's head against his feet. The head slot sits at the
+ // TOP of a standing character, so its vertices must have the greater y.
+ const handle = load();
+ call('db_setAtlasTexture', null, ['number', 'number'], [handle, 7]);
+ const instance = call<number>('db_createInstance', 'number', ['number', 'string'], [handle, 'Dragon']);
+ call('db_playAnimation', 'number', ['number', 'string', 'number'], [instance, 'stand', 0]);
+ call('db_update', null, ['number', 'number'], [instance, 1 / 60]);
+
+ // Every posed vertex, across every batch — the split into batches is a
+ // texture/blend detail and says nothing about where a part is.
+ let minY = Infinity, maxY = -Infinity;
+ const batches = call<number>('db_getMeshBatchCount', 'number', ['number'], [instance]);
+ expect(batches).toBeGreaterThan(0);
+ for (let b = 0; b < batches; b++) {
+ const vertexCount = call<number>('db_getMeshBatchVertexCount', 'number', ['number', 'number'], [instance, b]);
+ const indexCount = call<number>('db_getMeshBatchIndexCount', 'number', ['number', 'number'], [instance, b]);
+ const vp = M._malloc(vertexCount * 8 * 4);
+ const ip = M._malloc(indexCount * 2);
+ const tp = M._malloc(4);
+ const bp = M._malloc(4);
+ call('db_getMeshBatchData', null, ['number', 'number', 'number', 'number', 'number', 'number'], [
+ instance, b, vp, ip, tp, bp,
+ ]);
+ const v = new Float32Array(M.HEAPF32.buffer, vp, vertexCount * 8);
+ for (let i = 0; i < vertexCount; i++) {
+ const y = v[i * 8 + 1];
+ if (y < minY) minY = y;
+ if (y > maxY) maxY = y;
+ }
+ M._free(vp); M._free(ip); M._free(tp); M._free(bp);
+ }
+
+ // The armature's origin is at the feet, so a right-way-up DragonBoy
+ // reaches much further above it than below.
+ expect(maxY).toBeGreaterThan(0);
+ expect(maxY).toBeGreaterThan(Math.abs(minY));
+
+ call('db_destroyInstance', null, ['number'], [instance]);
+ call('db_unloadSkeleton', null, ['number'], [handle]);
+ });
+
it('moves the character as it walks, and keeps it a character-sized thing', () => {
const handle = load();
call('db_setAtlasTexture', null, ['number', 'number'], [handle, 7]);
diff --git a/src/esengine/bindings/DragonBonesAdapter.cpp b/src/esengine/bindings/DragonBonesAdapter.cpp
index 95e44b7b8..eaa4cc339 100644
--- a/src/esengine/bindings/DragonBonesAdapter.cpp
+++ b/src/esengine/bindings/DragonBonesAdapter.cpp
@@ -178,6 +178,17 @@ void EsSlot::emit(es::skeletal::TriangleSink& sink, float alpha) const {
}
}
+ // DragonBones poses in a y-DOWN space (its authoring tool inherits Flash's
+ // screen axes); the engine's world is y-up. Flipping here, at the one place
+ // geometry leaves the runtime, is what keeps every consumer consistent —
+ // db_getBounds reads these same vertices, so the box follows the character
+ // instead of describing a mirror of it. Winding is left alone: the 2D
+ // pipeline does not cull, and reversing it would only hide the flip if it
+ // ever started to.
+ for (std::size_t i = 0; i < vertexCount; ++i) {
+ world[i * 2 + 1] = -world[i * 2 + 1];
+ }
+
const float rgba[4] = {
_colorTransform.redMultiplier,
_colorTransform.greenMultiplier,
@@ -197,6 +208,28 @@ void EsArmatureProxy::dispose(bool) {
// — Factory ------------------------------------------------------------------
+/**
+ * BaseFactory leaves `_dragonBones` null and never assigns it — every integration
+ * is expected to bring its own. Skipping that step does not fail to link and does
+ * not fail to draw: it fails the first time an armature retires an animation
+ * state, because `Animation::advanceTime` calls `_armature->_dragonBones->
+ * bufferObject(state)` unconditionally. On wasm a null dereference lands in
+ * readable low memory and passes unnoticed; on a device it is a SIGSEGV. So the
+ * bug looked platform-specific and was not.
+ */
+EsFactory::EsFactory() {
+ _dragonBones = new DragonBones(&_eventDispatcher);
+}
+
+EsFactory::~EsFactory() {
+ delete _dragonBones;
+ _dragonBones = nullptr;
+}
+
+void EsFactory::advanceTime(float passedTime) {
+ if (_dragonBones != nullptr) _dragonBones->advanceTime(passedTime);
+}
+
EsFactory& EsFactory::instance() {
static EsFactory factory;
return factory;
diff --git a/src/esengine/bindings/DragonBonesAdapter.hpp b/src/esengine/bindings/DragonBonesAdapter.hpp
index 3f0a1b706..76a377a58 100644
--- a/src/esengine/bindings/DragonBonesAdapter.hpp
+++ b/src/esengine/bindings/DragonBonesAdapter.hpp
@@ -152,14 +152,42 @@ private:
Armature* _armature = nullptr;
};
+/// Nothing listens: events leave through the module's own ABI, not this one.
+class EsEventDispatcher : public IEventDispatcher {
+public:
+ ~EsEventDispatcher() override = default;
+ bool hasDBEventListener(const std::string&) const override { return false; }
+ void addDBEventListener(const std::string&, const std::function<void(EventObject*)>&) override {}
+ void removeDBEventListener(const std::string&, const std::function<void(EventObject*)>&) override {}
+ void dispatchDBEvent(const std::string&, EventObject*) override {}
+};
+
class EsFactory : public BaseFactory {
public:
+ EsFactory();
+ ~EsFactory() override;
+
static EsFactory& instance();
+ /**
+ * Recycle what the last frame retired, and drain the event queue.
+ *
+ * Not optional. An armature hands a finished animation state back by calling
+ * `_dragonBones->bufferObject(…)`, and the buffer is only emptied here — so
+ * without this the pool grows for the life of the app, and every queued event
+ * with it. The module calls it once per frame, before advancing armatures.
+ */
+ void advanceTime(float passedTime);
+
/// Build an armature by name; null when this data holds no such armature.
Armature* buildArmature(const std::string& armatureName, const std::string& dragonBonesName);
protected:
+ /// The runtime object every armature recycles and buffers events through.
+ /// BaseFactory declares `_dragonBones` and leaves it null; nothing in the
+ /// vendored tree assigns it, so each integration owns one.
+ EsEventDispatcher _eventDispatcher;
+
TextureAtlasData* _buildTextureAtlasData(TextureAtlasData* textureAtlasData, void* textureAtlas) const override;
Armature* _buildArmature(const BuildArmaturePackage& dataPackage) const override;
Slot* _buildSlot(const BuildArmaturePackage& dataPackage, const SlotData* slotData, Armature* armature) const override;
diff --git a/src/esengine/bindings/DragonBonesModuleEntry.cpp b/src/esengine/bindings/DragonBonesModuleEntry.cpp
index 66993dd08..0a84b94ed 100644
--- a/src/esengine/bindings/DragonBonesModuleEntry.cpp
+++ b/src/esengine/bindings/DragonBonesModuleEntry.cpp
@@ -267,6 +267,11 @@ void db_update(int instanceId, float dt) {
auto* armature = g_ctx.armatureOf(instanceId);
if (!armature) return;
g_ctx.events.clear();
+ // Return what the LAST advance retired before this one retires more. The
+ // runtime object is the only thing that empties that buffer, and an armature
+ // fills it whenever an animation state finishes or is faded out — so nothing
+ // else here can be what frees them.
+ EsFactory::instance().advanceTime(dt);
armature->advanceTime(dt);
}