35#include <emscripten.h>
37#define EMSCRIPTEN_KEEPALIVE
51using dragonBones::Armature;
52using dragonBones::DragonBonesData;
53using dragonBones::EsArmatureProxy;
54using dragonBones::EsFactory;
55using dragonBones::EsSlot;
56using dragonBones::EsTextureAtlasData;
59struct LoadedSkeleton {
60 DragonBonesData* data =
nullptr;
66 Armature* armature =
nullptr;
67 int skeletonHandle = -1;
70 float tint[4] = {1.0f, 1.0f, 1.0f, 1.0f};
81 std::string lastError;
83 Armature* armatureOf(
int id) {
84 auto* live = instances.
find(
id);
85 return live ? live->armature :
nullptr;
92std::string skeletonName(
int handle) {
return "es_db_" + std::to_string(handle); }
100void extractBatches(
int instanceId) {
101 g_ctx.batches.reset();
102 auto* live =
g_ctx.instances.find(instanceId);
103 if (!live || !live->armature)
return;
106 for (
auto* slot : live->armature->getSlots()) {
107 static_cast<EsSlot*
>(slot)->emit(collector, live->tint);
120int db_loadSkeleton(uintptr_t skeletonPtr,
int skeletonLen, uintptr_t atlasPtr,
int atlasLen) {
121 g_ctx.lastError.clear();
122 if (skeletonPtr == 0 || skeletonLen <= 0 || atlasPtr == 0 || atlasLen <= 0) {
123 g_ctx.lastError =
"empty skeleton or atlas data";
129 const std::string skeletonText(
reinterpret_cast<const char*
>(skeletonPtr),
130 static_cast<std::size_t
>(skeletonLen));
131 const std::string atlasText(
reinterpret_cast<const char*
>(atlasPtr),
static_cast<std::size_t
>(atlasLen));
133 const int handle =
g_ctx.skeletons.add(LoadedSkeleton{});
134 auto* held =
g_ctx.skeletons.find(handle);
135 held->name = skeletonName(handle);
138 held->data = factory.parseDragonBonesData(skeletonText.c_str(), held->name, 1.0f);
139 if (held->data ==
nullptr) {
140 g_ctx.lastError =
"skeleton data could not be parsed";
141 g_ctx.skeletons.erase(handle);
146 factory.parseTextureAtlasData(atlasText.c_str(),
nullptr, held->name, 1.0f));
147 if (held->atlas ==
nullptr) {
148 g_ctx.lastError =
"texture atlas data could not be parsed";
149 factory.removeDragonBonesData(held->name);
150 g_ctx.skeletons.erase(handle);
161 auto* held =
g_ctx.skeletons.find(handle);
165 g_ctx.instances.eraseIf([handle](LiveInstance& live) {
166 if (live.skeletonHandle != handle)
return false;
167 if (live.armature) live.armature->dispose();
172 factory.removeTextureAtlasData(held->name);
173 factory.removeDragonBonesData(held->name);
174 g_ctx.skeletons.erase(handle);
180 auto* held =
g_ctx.skeletons.find(handle);
181 if (!held || !held->data)
return g_ctx.strings.publish(
"[]");
182 const auto& names = held->data->armatureNames;
183 return g_ctx.strings.publishArray(
static_cast<int>(names.size()),
184 [&names](
int i) { return names[static_cast<std::size_t>(i)].c_str(); });
190 auto* held =
g_ctx.skeletons.find(handle);
191 return g_ctx.strings.publish(held && held->atlas ? held->atlas->imagePath.c_str() :
"");
196 auto* held =
g_ctx.skeletons.find(handle);
197 if (!held || !held->atlas)
return;
198 held->atlas->setTexture(textureId);
204 g_ctx.instances.eraseIf([handle](LiveInstance& live) {
205 if (live.skeletonHandle == handle && live.armature) live.armature->invalidUpdate(
"",
true);
216 auto* held =
g_ctx.skeletons.find(skeletonHandle);
218 g_ctx.lastError =
"no such skeleton";
222 if (armature ==
nullptr) {
223 g_ctx.lastError =
"no such armature in this skeleton";
226 return g_ctx.instances.add(LiveInstance{armature, skeletonHandle});
231 auto* live =
g_ctx.instances.find(instanceId);
233 if (live->armature) live->armature->dispose();
234 g_ctx.instances.erase(instanceId);
244 auto* armature =
g_ctx.armatureOf(instanceId);
245 if (!armature)
return 0;
246 return armature->getAnimation()->play(name ? name :
"", playTimes) !=
nullptr ? 1 : 0;
252 auto* armature =
g_ctx.armatureOf(instanceId);
253 if (!armature)
return 0;
254 return armature->getAnimation()->fadeIn(name ? name :
"", fadeSeconds, playTimes) !=
nullptr ? 1 : 0;
259 auto* armature =
g_ctx.armatureOf(instanceId);
260 if (armature) armature->getAnimation()->stop(name ? name :
"");
265 auto* armature =
g_ctx.armatureOf(instanceId);
266 if (armature) armature->getAnimation()->timeScale = scale;
272void db_setColor(
int instanceId,
float r,
float g,
float b,
float a) {
273 auto* live =
g_ctx.instances.find(instanceId);
283 auto* armature =
g_ctx.armatureOf(instanceId);
284 if (!armature)
return;
285 g_ctx.events.clear();
291 armature->advanceTime(dt);
296 auto* armature =
g_ctx.armatureOf(instanceId);
297 if (!armature)
return g_ctx.strings.publish(
"[]");
298 const auto& names = armature->getAnimation()->getAnimationNames();
299 return g_ctx.strings.publishArray(
static_cast<int>(names.size()),
300 [&names](
int i) { return names[static_cast<std::size_t>(i)].c_str(); });
306 auto* armature =
g_ctx.armatureOf(instanceId);
307 if (!armature)
return g_ctx.strings.publish(
"[]");
308 const auto& bones = armature->getBones();
309 return g_ctx.strings.publishArray(
static_cast<int>(bones.size()),
310 [&bones](
int i) { return bones[static_cast<std::size_t>(i)]->getName().c_str(); });
321 auto* armature =
g_ctx.armatureOf(instanceId);
322 if (!armature)
return 0;
323 const auto& bones = armature->getBones();
324 float* out =
reinterpret_cast<float*
>(outPtr);
325 const int n = std::min(
static_cast<int>(bones.size()), capacity);
326 for (
int i = 0; i < n; ++i) {
327 dragonBones::Bone* bone = bones[
static_cast<std::size_t
>(i)];
328 bone->updateGlobalTransform();
330 if (
const dragonBones::Bone* p = bone->getParent()) {
331 for (
int j = 0; j < i; ++j) {
332 if (bones[
static_cast<std::size_t
>(j)] == p) { parent = j;
break; }
335 constexpr float kRadToDeg = 57.29577951308232f;
336 float* at = out + i * 5;
337 at[0] =
static_cast<float>(parent);
338 at[1] = bone->global.x;
339 at[2] = -bone->global.y;
340 at[3] = -bone->global.rotation * kRadToDeg;
341 at[4] = bone->getBoneData()->length;
356 extractBatches(instanceId);
357 return static_cast<int>(
g_ctx.batches.size());
363 if (batchIndex < 0 || batchIndex >=
static_cast<int>(
g_ctx.batches.size()))
return 0;
370 if (batchIndex < 0 || batchIndex >=
static_cast<int>(
g_ctx.batches.size()))
return 0;
371 return static_cast<int>(
g_ctx.batches[batchIndex].indices.size());
375void db_getMeshBatchData(
int instanceId,
int batchIndex, uintptr_t outVerticesPtr, uintptr_t outIndicesPtr,
376 uintptr_t outTextureIdPtr, uintptr_t outBlendModePtr) {
378 if (batchIndex < 0 || batchIndex >=
static_cast<int>(
g_ctx.batches.size()))
return;
381 std::memcpy(
reinterpret_cast<float*
>(outVerticesPtr), batch.
vertices.
data(),
383 std::memcpy(
reinterpret_cast<uint16_t*
>(outIndicesPtr), batch.
indices.
data(),
385 *
reinterpret_cast<uint32_t*
>(outTextureIdPtr) = batch.
texture;
386 *
reinterpret_cast<int32_t*
>(outBlendModePtr) = batch.
blendMode;
390void db_getBounds(
int instanceId, uintptr_t outXPtr, uintptr_t outYPtr, uintptr_t outWPtr, uintptr_t outHPtr) {
393 extractBatches(instanceId);
394 float minX = 0.0f, minY = 0.0f, maxX = 0.0f, maxY = 0.0f;
396 for (
const auto& batch :
g_ctx.batches) {
398 const float x = batch.vertices[i];
399 const float y = batch.vertices[i + 1];
406 minX = x < minX ? x : minX;
407 maxX = x > maxX ? x : maxX;
408 minY = y < minY ? y : minY;
409 maxY = y > maxY ? y : maxY;
412 *
reinterpret_cast<float*
>(outXPtr) = minX;
413 *
reinterpret_cast<float*
>(outYPtr) = minY;
414 *
reinterpret_cast<float*
>(outWPtr) = maxX - minX;
415 *
reinterpret_cast<float*
>(outHPtr) = maxY - minY;
The host types DragonBones requires, implemented for a module that renders nothing itself and only ha...
The DragonBones module's exported ABI, declared apart from its bodies.
void db_stopAnimation(int instanceId, const char *name)
int db_getBonePoses(int instanceId, uintptr_t outPtr, int capacity)
void db_update(int instanceId, float dt)
void db_setColor(int instanceId, float r, float g, float b, float a)
int db_runtimeVersion()
5 — the DragonBones data format this runtime reads, for the SDK's version gate.
const char * db_getArmatures(int handle)
["armatureA","armatureB"] — a DragonBones file names more than one.
void db_setTimeScale(int instanceId, float scale)
int db_getMeshBatchCount(int instanceId)
void db_unloadSkeleton(int handle)
int db_getMeshBatchVertexCount(int instanceId, int batchIndex)
const char * db_getAtlasImageName(int handle)
The image this file's atlas expects, so the caller knows what to upload.
int db_loadSkeleton(uintptr_t skeletonPtr, int skeletonLen, uintptr_t atlasPtr, int atlasLen)
const char * db_getBones(int instanceId)
int db_getMeshBatchIndexCount(int instanceId, int batchIndex)
int db_createInstance(int skeletonHandle, const char *armatureName)
#define EMSCRIPTEN_KEEPALIVE
const char * db_getAnimations(int instanceId)
void db_getMeshBatchData(int instanceId, int batchIndex, uintptr_t outVerticesPtr, uintptr_t outIndicesPtr, uintptr_t outTextureIdPtr, uintptr_t outBlendModePtr)
int db_playAnimation(int instanceId, const char *name, int playTimes)
playTimes: -1 keeps what the data says, 0 loops, n plays n times.
void db_destroyInstance(int instanceId)
void db_setAtlasTexture(int handle, uint32_t textureId)
void db_getBounds(int instanceId, uintptr_t outXPtr, uintptr_t outYPtr, uintptr_t outWPtr, uintptr_t outHPtr)
int db_fadeInAnimation(int instanceId, const char *name, float fadeSeconds, int playTimes)
Where Spine would set a mix duration: DragonBones fades at the moment of play.
const char * db_getLastError()
What every 2D skeletal-animation side module needs, none of which is about any particular one.
static EsFactory & instance()
Armature * buildArmature(const std::string &armatureName, const std::string &dragonBonesName)
Build an armature by name; null when this data holds no such armature.
void advanceTime(float passedTime)
constexpr int VERTEX_FLOATS
Interleaved x,y,u,v,r,g,b,a per vertex — the layout the SDK reads back.
One draw's worth of geometry.
Buffer< std::uint16_t > indices