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.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
feat(bindings): the host types DragonBones needs, for a module with no scene graph
esengineb6badb4d9279
diff --git a/CMakeLists.txt b/CMakeLists.txtindex 1ff4c2927..616c791e5 100644--- a/CMakeLists.txt+++ b/CMakeLists.txt@@ -318,6 +318,21 @@ if(ES_BUILD_WEB OR ES_BUILD_WXGAME)es_add_spine_module(spine_module_21 2.1 spine21)endif()+ # DragonBones: one module, not one per version. Its authoring tool is no+ # longer developed, so the format cannot move and there is nothing to track.+ if(ES_ENABLE_DRAGONBONES)+ add_library(dragonbones_adapter STATIC+ src/esengine/bindings/DragonBonesAdapter.cpp+ )+ target_link_libraries(dragonbones_adapter PUBLIC dragonbones)+ target_include_directories(dragonbones_adapter PUBLIC+ ${CMAKE_CURRENT_SOURCE_DIR}/src/esengine/bindings+ )+ if(ES_BUILD_WEB OR ES_BUILD_WXGAME)+ target_compile_options(dragonbones_adapter PRIVATE ${ES_EMSCRIPTEN_COMPILE_FLAGS})+ endif()+ endif()+# Basis Universal KTX2 transcoder module (compressed textures — RC6)if(ES_ENABLE_BASIS)add_executable(basis_modulediff --git a/src/esengine/bindings/DragonBonesAdapter.cpp b/src/esengine/bindings/DragonBonesAdapter.cppnew file mode 100644index 000000000..62f289328--- /dev/null+++ b/src/esengine/bindings/DragonBonesAdapter.cpp@@ -0,0 +1,233 @@+// SPDX-License-Identifier: Apache-2.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+/**+ * @file DragonBonesAdapter.cpp+ * @brief Geometry for the host types in DragonBonesAdapter.hpp.+ */+#include "DragonBonesAdapter.hpp"++#include <cmath>+#include <cstdlib>++DRAGONBONES_NAMESPACE_BEGIN++namespace {++/// Atlas pixels → 0..1, which is what the renderer samples with.+inline void normalize(float& u, float& v, const TextureAtlasData* page) {+ const float w = page && page->width > 0 ? static_cast<float>(page->width) : 1.0f;+ const float h = page && page->height > 0 ? static_cast<float>(page->height) : 1.0f;+ u /= w;+ v /= h;+}++} // namespace++void EsSlot::_updateFrame() {+ positions.clear();+ uvs.clear();+ indices.clear();+ textureId = 0;++ const auto* vertices =+ (_deformVertices != nullptr && _display == _meshDisplay) ? _deformVertices->verticesData : nullptr;+ auto* texture = static_cast<EsTextureData*>(_textureData);+ if (_displayIndex < 0 || _display == nullptr || texture == nullptr || texture->textureId == 0) return;++ textureId = texture->textureId;+ const auto& region = texture->region;+ const auto* page = texture->parent;++ if (vertices != nullptr) {+ const auto* data = vertices->data;+ const auto* intArray = data->intArray;+ const auto* floatArray = data->floatArray;+ const unsigned vertexCount = intArray[vertices->offset + static_cast<unsigned>(BinaryOffset::MeshVertexCount)];+ const unsigned triangleCount =+ intArray[vertices->offset + static_cast<unsigned>(BinaryOffset::MeshTriangleCount)];+ int vertexOffset = intArray[vertices->offset + static_cast<unsigned>(BinaryOffset::MeshFloatOffset)];+ // The parser packs this offset as a signed 16-bit value; a mesh far enough+ // into the float array wraps negative and has to be brought back.+ if (vertexOffset < 0) vertexOffset += 65536;+ const unsigned uvOffset = static_cast<unsigned>(vertexOffset) + vertexCount * 2;++ positions.resize(vertexCount * 2);+ uvs.resize(vertexCount * 2);+ for (unsigned i = 0; i < vertexCount * 2; i += 2) {+ positions[i] = floatArray[vertexOffset + i];+ positions[i + 1] = floatArray[vertexOffset + i + 1];++ const float u = floatArray[uvOffset + i];+ const float v = floatArray[uvOffset + i + 1];+ // A rotated atlas entry is stored turned a quarter clockwise, so the+ // mesh's u walks the region's height and its v walks the width.+ float su = texture->rotated ? region.x + (1.0f - v) * region.width : region.x + u * region.width;+ float sv = texture->rotated ? region.y + u * region.height : region.y + v * region.height;+ normalize(su, sv, page);+ uvs[i] = su;+ uvs[i + 1] = sv;+ }++ indices.resize(triangleCount * 3);+ for (unsigned i = 0; i < triangleCount * 3; ++i) {+ indices[i] = static_cast<std::uint16_t>(+ intArray[vertices->offset + static_cast<unsigned>(BinaryOffset::MeshVertexIndices) + i]);+ }++ // A weighted mesh is posed in ARMATURE space by _updateMesh, so its+ // vertices must not be run through the slot transform as well.+ if (vertices->weight != nullptr) _identityTransform();+ return;+ }++ // A plain image: one quad, sized in the same scaled units the pivot is in+ // (Slot::_updateDisplayData multiplies the pivot by width * scale).+ const float scale = texture->parent->scale * _armature->_armatureData->scale;+ const float w = std::abs(region.width) * scale;+ const float h = std::abs(region.height) * scale;++ positions = {0.0f, 0.0f, 0.0f, h, w, h, w, 0.0f};+ float u0 = region.x, v0 = region.y;+ float u1 = region.x + region.width, v1 = region.y + region.height;+ normalize(u0, v0, page);+ normalize(u1, v1, page);+ uvs = {u0, v0, u0, v1, u1, v1, u1, v0};+ indices = {0, 1, 2, 0, 2, 3};+}++void EsSlot::_updateMesh() {+ if (_deformVertices == nullptr) return;+ const auto* vertices = _deformVertices->verticesData;+ if (vertices == nullptr) return;++ const float scale = _armature->_armatureData->scale;+ const auto& deform = _deformVertices->vertices;+ const auto& bones = _deformVertices->bones;+ const auto* weight = vertices->weight;+ const bool hasDeform = !deform.empty();++ const auto* data = vertices->data;+ const auto* intArray = data->intArray;+ const auto* floatArray = data->floatArray;+ const auto vertexCount =+ static_cast<std::size_t>(intArray[vertices->offset + static_cast<unsigned>(BinaryOffset::MeshVertexCount)]);+ if (positions.size() < vertexCount * 2) positions.resize(vertexCount * 2);++ if (weight != nullptr) {+ int weightFloatOffset = intArray[weight->offset + static_cast<unsigned>(BinaryOffset::WeigthFloatOffset)];+ if (weightFloatOffset < 0) weightFloatOffset += 65536;++ // Each vertex names its own bone count, then that many (boneIndex, weight)+ // pairs — so the three cursors advance together and none can be derived.+ std::size_t iB = weight->offset + static_cast<unsigned>(BinaryOffset::WeigthBoneIndices) + bones.size();+ std::size_t iV = static_cast<std::size_t>(weightFloatOffset);+ std::size_t iF = 0;++ for (std::size_t i = 0; i < vertexCount; ++i) {+ const auto boneCount = static_cast<std::size_t>(intArray[iB++]);+ float xG = 0.0f, yG = 0.0f;+ for (std::size_t j = 0; j < boneCount; ++j) {+ const auto boneIndex = static_cast<unsigned>(intArray[iB++]);+ const auto* bone = bones[boneIndex];+ if (bone == nullptr) continue;++ const auto& matrix = bone->globalTransformMatrix;+ const float w = floatArray[iV++];+ float xL = floatArray[iV++] * scale;+ float yL = floatArray[iV++] * scale;+ if (hasDeform) {+ xL += deform[iF++];+ yL += deform[iF++];+ }+ xG += (matrix.a * xL + matrix.c * yL + matrix.tx) * w;+ yG += (matrix.b * xL + matrix.d * yL + matrix.ty) * w;+ }+ positions[i * 2] = xG;+ positions[i * 2 + 1] = yG;+ }+ return;+ }++ if (!hasDeform) return;++ int vertexOffset = intArray[vertices->offset + static_cast<unsigned>(BinaryOffset::MeshFloatOffset)];+ if (vertexOffset < 0) vertexOffset += 65536;+ for (std::size_t i = 0; i < vertexCount; ++i) {+ positions[i * 2] = floatArray[vertexOffset + i * 2] * scale + deform[i * 2];+ positions[i * 2 + 1] = floatArray[vertexOffset + i * 2 + 1] * scale + deform[i * 2 + 1];+ }+}++void EsSlot::emit(es::skeletal::TriangleSink& sink, float alpha) const {+ if (indices.empty() || textureId == 0 || !_visible) return;++ const std::size_t vertexCount = positions.size() / 2;+ std::vector<float> world(positions.size());++ if (armatureSpace) {+ world = positions;+ } else {+ const auto& m = globalTransformMatrix;+ for (std::size_t i = 0; i < vertexCount; ++i) {+ // The pivot moves the display's origin, and it lives in the same scaled+ // units as the vertices — so it is subtracted before the matrix, not after.+ const float x = positions[i * 2] - _pivotX;+ const float y = positions[i * 2 + 1] - _pivotY;+ world[i * 2] = m.a * x + m.c * y + m.tx;+ world[i * 2 + 1] = m.b * x + m.d * y + m.ty;+ }+ }++ const float rgba[4] = {+ _colorTransform.redMultiplier,+ _colorTransform.greenMultiplier,+ _colorTransform.blueMultiplier,+ _colorTransform.alphaMultiplier * alpha,+ };+ sink.emit(world.data(), uvs.data(), static_cast<int>(vertexCount), indices.data(),+ static_cast<int>(indices.size()), textureId, static_cast<int>(_blendMode), rgba);+}++void EsArmatureProxy::dispose(bool) {+ if (_armature != nullptr) {+ _armature->dispose();+ _armature = nullptr;+ }+}++// — Factory ------------------------------------------------------------------++EsFactory& EsFactory::instance() {+ static EsFactory factory;+ return factory;+}++TextureAtlasData* EsFactory::_buildTextureAtlasData(TextureAtlasData* textureAtlasData, void* textureAtlas) const {+ if (textureAtlasData != nullptr) {+ static_cast<EsTextureAtlasData*>(textureAtlasData)+ ->setTexture(static_cast<std::uint32_t>(reinterpret_cast<std::uintptr_t>(textureAtlas)));+ return textureAtlasData;+ }+ return BaseObject::borrowObject<EsTextureAtlasData>();+}++Armature* EsFactory::_buildArmature(const BuildArmaturePackage& dataPackage) const {+ auto* armature = BaseObject::borrowObject<Armature>();+ // The proxy is an interface, not a pooled BaseObject; the armature owns it for+ // as long as it lives and drops it in dispose().+ auto* proxy = new EsArmatureProxy();+ armature->init(dataPackage.armature, proxy, proxy, _dragonBones);+ return armature;+}++Slot* EsFactory::_buildSlot(const BuildArmaturePackage&, const SlotData* slotData, Armature*) const {+ auto* slot = BaseObject::borrowObject<EsSlot>();+ slot->init(slotData, nullptr, &slot->rawDisplay, &slot->meshDisplay);+ return slot;+}++Armature* EsFactory::buildArmature(const std::string& armatureName, const std::string& dragonBonesName) {+ return BaseFactory::buildArmature(armatureName, dragonBonesName, "", "");+}++DRAGONBONES_NAMESPACE_ENDdiff --git a/src/esengine/bindings/DragonBonesAdapter.hpp b/src/esengine/bindings/DragonBonesAdapter.hppnew file mode 100644index 000000000..3f0a1b706--- /dev/null+++ b/src/esengine/bindings/DragonBonesAdapter.hpp@@ -0,0 +1,168 @@+// SPDX-License-Identifier: Apache-2.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+/**+ * @file DragonBonesAdapter.hpp+ * @brief The host types DragonBones requires, implemented for a module that+ * renders nothing itself and only hands triangles across a boundary.+ *+ * @details DragonBones is written against an engine that owns a scene graph: a+ * Slot is expected to add, remove and reorder a display object, and to+ * push a transform onto it. This module has no scene graph — it poses,+ * reads the geometry back, and forgets. So most of the fourteen methods a+ * Slot must implement are legitimately empty here, and the two that are+ * not (`_updateFrame`, `_updateMesh`) build the vertices the render walk+ * later emits.+ *+ * THE ONE SUBTLETY IS WHOSE SPACE THE VERTICES ARE IN. A plain image or+ * an unweighted mesh is posed in slot-local space and needs the slot's+ * global matrix applied. A WEIGHTED mesh is already in armature space,+ * because the bones were applied while computing it — DragonBones signals+ * exactly this by calling `_identityTransform` instead of+ * `_updateTransform`, and getting it backwards detaches every weighted+ * mesh from its armature.+ *+ * @author ESEngine Team+ * @date 2026+ *+ * @copyright Copyright (c) 2026 ESEngine Team+ * Licensed under the Apache License, Version 2.0.+ */+#pragma once++#include <cstdint>+#include <vector>++#include "dragonBones/DragonBonesHeaders.h"++#include "SkeletalModule.hpp"++DRAGONBONES_NAMESPACE_BEGIN++/// One atlas page: the texture the host bound to it, and its pixel size.+class EsTextureData : public TextureData {+ BIND_CLASS_TYPE_B(EsTextureData);++public:+ std::uint32_t textureId = 0;++ EsTextureData() = default;++ void _onClear() override {+ textureId = 0;+ TextureData::_onClear();+ }+};++class EsTextureAtlasData : public TextureAtlasData {+ BIND_CLASS_TYPE_B(EsTextureAtlasData);++public:+ /// Bound after parsing, once the host has uploaded the page (see setTexture).+ std::uint32_t textureId = 0;++ EsTextureAtlasData() = default;++ void _onClear() override {+ textureId = 0;+ TextureAtlasData::_onClear();+ }++ TextureData* createTexture() const override { return BaseObject::borrowObject<EsTextureData>(); }++ /// Point every page of this atlas at an uploaded texture.+ void setTexture(std::uint32_t id) {+ textureId = id;+ for (const auto& pair : textures) {+ static_cast<EsTextureData*>(pair.second)->textureId = id;+ }+ }+};++/**+ * A slot that keeps its posed geometry instead of drawing it.+ *+ * Positions and UVs are parallel arrays; `armatureSpace` records which space the+ * positions are in, per the header note.+ */+class EsSlot : public Slot {+ BIND_CLASS_TYPE_A(EsSlot);++public:+ std::vector<float> positions; ///< x,y per vertex+ std::vector<float> uvs; ///< u,v per vertex+ std::vector<std::uint16_t> indices;+ std::uint32_t textureId = 0;+ bool armatureSpace = false; ///< positions already have the bones applied++ /// Slot::init takes the two displays as opaque pointers and never dereferences+ /// them; `_display == _meshDisplay` is the only question ever asked of them.+ char rawDisplay = 0;+ char meshDisplay = 0;++ void _onClear() override {+ positions.clear();+ uvs.clear();+ indices.clear();+ textureId = 0;+ armatureSpace = false;+ Slot::_onClear();+ }++ /// Pour this slot's triangles into `sink`, applying its transform if it owes one.+ void emit(es::skeletal::TriangleSink& sink, float alpha) const;++protected:+ // — Scene-graph duties this module does not have —+ void _initDisplay(void*, bool) override {}+ void _disposeDisplay(void*, bool) override {}+ void _onUpdateDisplay() override {}+ void _addDisplay() override {}+ void _replaceDisplay(void*, bool) override {}+ void _removeDisplay() override {}+ void _updateZOrder() override {}+ void _updateVisible() override {}+ void _updateBlendMode() override {}+ void _updateColor() override {}++ // — The ones that carry geometry —+ void _updateFrame() override;+ void _updateMesh() override;+ void _updateTransform() override { armatureSpace = false; }+ void _identityTransform() override { armatureSpace = true; }+};++/// The armature's "display": nothing, because there is nothing to display into.+class EsArmatureProxy : public IArmatureProxy {+public:+ ~EsArmatureProxy() override = default;++ void dbInit(Armature* armatureValue) override { _armature = armatureValue; }+ void dbClear() override { _armature = nullptr; }+ void dbUpdate() override {}+ void dispose(bool disposeProxy) override;+ Armature* getArmature() const override { return _armature; }+ Animation* getAnimation() const override { return _armature ? _armature->getAnimation() : nullptr; }++ 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 {}++private:+ Armature* _armature = nullptr;+};++class EsFactory : public BaseFactory {+public:+ static EsFactory& instance();++ /// Build an armature by name; null when this data holds no such armature.+ Armature* buildArmature(const std::string& armatureName, const std::string& dragonBonesName);++protected:+ 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;+};++DRAGONBONES_NAMESPACE_END