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
refactor: encapsulate Physics/Spine global state into context structs
esenginea76a53da8c16
diff --git a/src/esengine/bindings/PhysicsModuleEntry.cpp b/src/esengine/bindings/PhysicsModuleEntry.cppindex 416807c7c..87df11f3f 100644--- a/src/esengine/bindings/PhysicsModuleEntry.cpp+++ b/src/esengine/bindings/PhysicsModuleEntry.cpp@@ -19,33 +19,52 @@#include <cstring>// =============================================================================-// Global State+// Physics Context// =============================================================================-static b2WorldId g_worldId = b2_nullWorldId;-static float g_fixedTimestep = 1.0f / 60.0f;-static int g_subStepCount = 4;-static float g_accumulator = 0.0f;static constexpr int MAX_PHYSICS_STEPS_PER_FRAME = 8;-static std::unordered_map<uint32_t, b2BodyId> g_entityToBody;-static std::unordered_map<uint32_t, b2ShapeId> g_entityToShape;-static std::unordered_map<uint32_t, b2JointId> g_entityToJoint;-static std::vector<uint32_t> g_dynamicBodyEntities;--// [entityId_bits, x, y, angle, ...]-static std::vector<float> g_dynamicTransformBuffer;--// Collision event flat buffers: [entityA0_bits, entityB0_bits, nx0, ny0, cx0, cy0, ...]-static std::vector<float> g_collisionEnterBuffer;-// [entityA0_bits, entityB0_bits, ...]-static std::vector<float> g_collisionExitBuffer;-// [sensor0_bits, visitor0_bits, ...]-static std::vector<float> g_sensorEnterBuffer;-static std::vector<float> g_sensorExitBuffer;+struct PhysicsContext {+ b2WorldId worldId = b2_nullWorldId;+ float fixedTimestep = 1.0f / 60.0f;+ int subStepCount = 4;+ float accumulator = 0.0f;++ std::unordered_map<uint32_t, b2BodyId> entityToBody;+ std::unordered_map<uint32_t, b2ShapeId> entityToShape;+ std::unordered_map<uint32_t, b2JointId> entityToJoint;+ std::vector<uint32_t> dynamicBodyEntities;++ std::vector<float> dynamicTransformBuffer;++ std::vector<float> collisionEnterBuffer;+ std::vector<float> collisionExitBuffer;+ std::vector<float> sensorEnterBuffer;+ std::vector<float> sensorExitBuffer;++ float velocityBuffer[2] = {};+ float gravityBuffer[2] = {};++ void reset() {+ entityToBody.clear();+ entityToShape.clear();+ entityToJoint.clear();+ dynamicBodyEntities.clear();+ dynamicTransformBuffer.clear();+ collisionEnterBuffer.clear();+ collisionExitBuffer.clear();+ sensorEnterBuffer.clear();+ sensorExitBuffer.clear();+ accumulator = 0.0f;++ if (b2World_IsValid(worldId)) {+ b2DestroyWorld(worldId);+ }+ worldId = b2_nullWorldId;+ }+};-static float g_velocityBuffer[2];-static float g_gravityBuffer[2];+static PhysicsContext g_ctx;// =============================================================================// Helper: Entity ID from body user data@@ -79,37 +98,23 @@ extern "C" {EMSCRIPTEN_KEEPALIVEvoid physics_init(float gx, float gy, float timestep, int substeps,float contactHertz, float contactDampingRatio, float contactSpeed) {- if (b2World_IsValid(g_worldId)) return;+ if (b2World_IsValid(g_ctx.worldId)) return;b2WorldDef worldDef = b2DefaultWorldDef();worldDef.gravity = {gx, gy};worldDef.contactHertz = contactHertz;worldDef.contactDampingRatio = contactDampingRatio;worldDef.contactSpeed = contactSpeed;- g_worldId = b2CreateWorld(&worldDef);+ g_ctx.worldId = b2CreateWorld(&worldDef);- g_fixedTimestep = timestep;- g_subStepCount = substeps;- g_accumulator = 0.0f;+ g_ctx.fixedTimestep = timestep;+ g_ctx.subStepCount = substeps;+ g_ctx.accumulator = 0.0f;}EMSCRIPTEN_KEEPALIVEvoid physics_shutdown() {- if (!b2World_IsValid(g_worldId)) return;-- g_entityToBody.clear();- g_entityToShape.clear();- g_entityToJoint.clear();- g_dynamicBodyEntities.clear();- g_dynamicTransformBuffer.clear();- g_collisionEnterBuffer.clear();- g_collisionExitBuffer.clear();- g_sensorEnterBuffer.clear();- g_sensorExitBuffer.clear();- g_accumulator = 0.0f;-- b2DestroyWorld(g_worldId);- g_worldId = b2_nullWorldId;+ g_ctx.reset();}// Body Management@@ -118,9 +123,9 @@ EMSCRIPTEN_KEEPALIVEvoid physics_createBody(uint32_t entityId, int bodyType, float x, float y, float angle,float gravityScale, float linearDamping, float angularDamping,int fixedRotation, int bullet) {- if (!b2World_IsValid(g_worldId)) return;+ if (!b2World_IsValid(g_ctx.worldId)) return;if (entityId == 0xFFFFFFFF) return;- if (g_entityToBody.contains(entityId)) return;+ if (g_ctx.entityToBody.contains(entityId)) return;b2BodyDef bodyDef = b2DefaultBodyDef();@@ -138,42 +143,42 @@ void physics_createBody(uint32_t entityId, int bodyType, float x, float y, floatbodyDef.isBullet = bullet != 0;bodyDef.motionLocks.angularZ = fixedRotation != 0;- b2BodyId bodyId = b2CreateBody(g_worldId, &bodyDef);+ b2BodyId bodyId = b2CreateBody(g_ctx.worldId, &bodyDef);b2Body_SetUserData(bodyId, reinterpret_cast<void*>(static_cast<uintptr_t>(entityId)));- g_entityToBody[entityId] = bodyId;+ g_ctx.entityToBody[entityId] = bodyId;if (bodyDef.type == b2_dynamicBody) {- g_dynamicBodyEntities.push_back(entityId);+ g_ctx.dynamicBodyEntities.push_back(entityId);}}EMSCRIPTEN_KEEPALIVEvoid physics_destroyBody(uint32_t entityId) {- auto it = g_entityToBody.find(entityId);- if (it == g_entityToBody.end()) return;+ auto it = g_ctx.entityToBody.find(entityId);+ if (it == g_ctx.entityToBody.end()) return;- auto jit = g_entityToJoint.find(entityId);- if (jit != g_entityToJoint.end()) {+ auto jit = g_ctx.entityToJoint.find(entityId);+ if (jit != g_ctx.entityToJoint.end()) {if (b2Joint_IsValid(jit->second)) {b2DestroyJoint(jit->second, false);}- g_entityToJoint.erase(jit);+ g_ctx.entityToJoint.erase(jit);}if (b2Body_IsValid(it->second)) {b2DestroyBody(it->second);}- g_entityToBody.erase(it);- g_entityToShape.erase(entityId);- auto dit = std::find(g_dynamicBodyEntities.begin(), g_dynamicBodyEntities.end(), entityId);- if (dit != g_dynamicBodyEntities.end()) {- *dit = g_dynamicBodyEntities.back();- g_dynamicBodyEntities.pop_back();+ g_ctx.entityToBody.erase(it);+ g_ctx.entityToShape.erase(entityId);+ auto dit = std::find(g_ctx.dynamicBodyEntities.begin(), g_ctx.dynamicBodyEntities.end(), entityId);+ if (dit != g_ctx.dynamicBodyEntities.end()) {+ *dit = g_ctx.dynamicBodyEntities.back();+ g_ctx.dynamicBodyEntities.pop_back();}}EMSCRIPTEN_KEEPALIVEint physics_hasBody(uint32_t entityId) {- return g_entityToBody.contains(entityId) ? 1 : 0;+ return g_ctx.entityToBody.contains(entityId) ? 1 : 0;}// Shape Management@@ -183,8 +188,8 @@ void physics_addBoxShape(uint32_t entityId, float halfW, float halfH,float offX, float offY, float radius,float density, float friction, float restitution, int isSensor,uint32_t categoryBits, uint32_t maskBits) {- auto it = g_entityToBody.find(entityId);- if (it == g_entityToBody.end()) return;+ auto it = g_ctx.entityToBody.find(entityId);+ if (it == g_ctx.entityToBody.end()) return;b2ShapeDef shapeDef = b2DefaultShapeDef();shapeDef.density = density;@@ -205,7 +210,7 @@ void physics_addBoxShape(uint32_t entityId, float halfW, float halfH,polygon = b2MakeOffsetBox(halfW, halfH, {offX, offY}, b2MakeRot(0.0f));}b2ShapeId shapeId = b2CreatePolygonShape(it->second, &shapeDef, &polygon);- g_entityToShape[entityId] = shapeId;+ g_ctx.entityToShape[entityId] = shapeId;}EMSCRIPTEN_KEEPALIVE@@ -213,8 +218,8 @@ void physics_addCircleShape(uint32_t entityId, float radius,float offX, float offY,float density, float friction, float restitution, int isSensor,uint32_t categoryBits, uint32_t maskBits) {- auto it = g_entityToBody.find(entityId);- if (it == g_entityToBody.end()) return;+ auto it = g_ctx.entityToBody.find(entityId);+ if (it == g_ctx.entityToBody.end()) return;b2ShapeDef shapeDef = b2DefaultShapeDef();shapeDef.density = density;@@ -231,7 +236,7 @@ void physics_addCircleShape(uint32_t entityId, float radius,circle.radius = radius;b2ShapeId shapeId = b2CreateCircleShape(it->second, &shapeDef, &circle);- g_entityToShape[entityId] = shapeId;+ g_ctx.entityToShape[entityId] = shapeId;}EMSCRIPTEN_KEEPALIVE@@ -239,8 +244,8 @@ void physics_addCapsuleShape(uint32_t entityId, float radius, float halfHeight,float offX, float offY,float density, float friction, float restitution, int isSensor,uint32_t categoryBits, uint32_t maskBits) {- auto it = g_entityToBody.find(entityId);- if (it == g_entityToBody.end()) return;+ auto it = g_ctx.entityToBody.find(entityId);+ if (it == g_ctx.entityToBody.end()) return;b2ShapeDef shapeDef = b2DefaultShapeDef();shapeDef.density = density;@@ -258,15 +263,15 @@ void physics_addCapsuleShape(uint32_t entityId, float radius, float halfHeight,capsule.radius = radius;b2ShapeId shapeId = b2CreateCapsuleShape(it->second, &shapeDef, &capsule);- g_entityToShape[entityId] = shapeId;+ g_ctx.entityToShape[entityId] = shapeId;}EMSCRIPTEN_KEEPALIVEvoid physics_addSegmentShape(uint32_t entityId, float x1, float y1, float x2, float y2,float density, float friction, float restitution, int isSensor,uint32_t categoryBits, uint32_t maskBits) {- auto it = g_entityToBody.find(entityId);- if (it == g_entityToBody.end()) return;+ auto it = g_ctx.entityToBody.find(entityId);+ if (it == g_ctx.entityToBody.end()) return;b2ShapeDef shapeDef = b2DefaultShapeDef();shapeDef.density = density;@@ -283,15 +288,15 @@ void physics_addSegmentShape(uint32_t entityId, float x1, float y1, float x2, flsegment.point2 = {x2, y2};b2ShapeId shapeId = b2CreateSegmentShape(it->second, &shapeDef, &segment);- g_entityToShape[entityId] = shapeId;+ g_ctx.entityToShape[entityId] = shapeId;}EMSCRIPTEN_KEEPALIVEvoid physics_addPolygonShape(uint32_t entityId, uintptr_t verticesPtr, int vertexCount, float radius,float density, float friction, float restitution, int isSensor,uint32_t categoryBits, uint32_t maskBits) {- auto it = g_entityToBody.find(entityId);- if (it == g_entityToBody.end()) return;+ auto it = g_ctx.entityToBody.find(entityId);+ if (it == g_ctx.entityToBody.end()) return;if (vertexCount < 3 || vertexCount > B2_MAX_POLYGON_VERTICES) return;b2ShapeDef shapeDef = b2DefaultShapeDef();@@ -317,15 +322,15 @@ void physics_addPolygonShape(uint32_t entityId, uintptr_t verticesPtr, int verte? b2MakePolygon(&hull, radius): b2MakePolygon(&hull, 0.0f);b2ShapeId shapeId = b2CreatePolygonShape(it->second, &shapeDef, &polygon);- g_entityToShape[entityId] = shapeId;+ g_ctx.entityToShape[entityId] = shapeId;}EMSCRIPTEN_KEEPALIVEvoid physics_addChainShape(uint32_t entityId, uintptr_t pointsPtr, int pointCount, int isLoop,float friction, float restitution,uint32_t categoryBits, uint32_t maskBits) {- auto it = g_entityToBody.find(entityId);- if (it == g_entityToBody.end()) return;+ auto it = g_ctx.entityToBody.find(entityId);+ if (it == g_ctx.entityToBody.end()) return;if (pointCount < 4) return;auto* floats = reinterpret_cast<float*>(pointsPtr);@@ -354,24 +359,24 @@ void physics_addChainShape(uint32_t entityId, uintptr_t pointsPtr, int pointCounEMSCRIPTEN_KEEPALIVEvoid physics_step(float dt) {- if (!b2World_IsValid(g_worldId)) return;+ if (!b2World_IsValid(g_ctx.worldId)) return;- g_collisionEnterBuffer.clear();- g_collisionExitBuffer.clear();- g_sensorEnterBuffer.clear();- g_sensorExitBuffer.clear();+ g_ctx.collisionEnterBuffer.clear();+ g_ctx.collisionExitBuffer.clear();+ g_ctx.sensorEnterBuffer.clear();+ g_ctx.sensorExitBuffer.clear();- g_accumulator += dt;+ g_ctx.accumulator += dt;int steps = 0;- while (g_accumulator >= g_fixedTimestep && steps < MAX_PHYSICS_STEPS_PER_FRAME) {- b2World_Step(g_worldId, g_fixedTimestep, g_subStepCount);- g_accumulator -= g_fixedTimestep;+ while (g_ctx.accumulator >= g_ctx.fixedTimestep && steps < MAX_PHYSICS_STEPS_PER_FRAME) {+ b2World_Step(g_ctx.worldId, g_ctx.fixedTimestep, g_ctx.subStepCount);+ g_ctx.accumulator -= g_ctx.fixedTimestep;++steps;}- if (g_accumulator > g_fixedTimestep) {- g_accumulator = g_fixedTimestep;+ if (g_ctx.accumulator > g_ctx.fixedTimestep) {+ g_ctx.accumulator = g_ctx.fixedTimestep;}}@@ -379,8 +384,8 @@ void physics_step(float dt) {EMSCRIPTEN_KEEPALIVEvoid physics_setBodyTransform(uint32_t entityId, float x, float y, float angle) {- auto it = g_entityToBody.find(entityId);- if (it == g_entityToBody.end()) return;+ auto it = g_ctx.entityToBody.find(entityId);+ if (it == g_ctx.entityToBody.end()) return;if (!b2Body_IsValid(it->second)) return;b2Body_SetTransform(it->second, {x, y}, b2MakeRot(angle));@@ -388,36 +393,36 @@ void physics_setBodyTransform(uint32_t entityId, float x, float y, float angle)EMSCRIPTEN_KEEPALIVEint physics_getDynamicBodyCount() {- return static_cast<int>(g_dynamicBodyEntities.size());+ return static_cast<int>(g_ctx.dynamicBodyEntities.size());}EMSCRIPTEN_KEEPALIVEuintptr_t physics_getDynamicBodyTransforms() {- g_dynamicTransformBuffer.clear();+ g_ctx.dynamicTransformBuffer.clear();- for (uint32_t entityId : g_dynamicBodyEntities) {- auto it = g_entityToBody.find(entityId);- if (it == g_entityToBody.end() || !b2Body_IsValid(it->second)) continue;+ for (uint32_t entityId : g_ctx.dynamicBodyEntities) {+ auto it = g_ctx.entityToBody.find(entityId);+ if (it == g_ctx.entityToBody.end() || !b2Body_IsValid(it->second)) continue;b2Vec2 pos = b2Body_GetPosition(it->second);float angle = b2Rot_GetAngle(b2Body_GetRotation(it->second));- pushEntityBits(g_dynamicTransformBuffer, entityId);- g_dynamicTransformBuffer.push_back(pos.x);- g_dynamicTransformBuffer.push_back(pos.y);- g_dynamicTransformBuffer.push_back(angle);+ pushEntityBits(g_ctx.dynamicTransformBuffer, entityId);+ g_ctx.dynamicTransformBuffer.push_back(pos.x);+ g_ctx.dynamicTransformBuffer.push_back(pos.y);+ g_ctx.dynamicTransformBuffer.push_back(angle);}- return reinterpret_cast<uintptr_t>(g_dynamicTransformBuffer.data());+ return reinterpret_cast<uintptr_t>(g_ctx.dynamicTransformBuffer.data());}// Collision Events (flat buffer)EMSCRIPTEN_KEEPALIVEvoid physics_collectEvents() {- if (!b2World_IsValid(g_worldId)) return;+ if (!b2World_IsValid(g_ctx.worldId)) return;- b2ContactEvents contactEvents = b2World_GetContactEvents(g_worldId);+ b2ContactEvents contactEvents = b2World_GetContactEvents(g_ctx.worldId);for (int i = 0; i < contactEvents.beginCount; ++i) {auto& evt = contactEvents.beginEvents[i];@@ -425,8 +430,8 @@ void physics_collectEvents() {uint32_t entityB = entityFromShape(evt.shapeIdB);if (entityA == 0xFFFFFFFF || entityB == 0xFFFFFFFF) continue;- pushEntityBits(g_collisionEnterBuffer, entityA);- pushEntityBits(g_collisionEnterBuffer, entityB);+ pushEntityBits(g_ctx.collisionEnterBuffer, entityA);+ pushEntityBits(g_ctx.collisionEnterBuffer, entityB);float nx = 0, ny = 0, cx = 0, cy = 0;if (b2Contact_IsValid(evt.contactId)) {@@ -438,10 +443,10 @@ void physics_collectEvents() {cy = cd.manifold.points[0].point.y;}}- g_collisionEnterBuffer.push_back(nx);- g_collisionEnterBuffer.push_back(ny);- g_collisionEnterBuffer.push_back(cx);- g_collisionEnterBuffer.push_back(cy);+ g_ctx.collisionEnterBuffer.push_back(nx);+ g_ctx.collisionEnterBuffer.push_back(ny);+ g_ctx.collisionEnterBuffer.push_back(cx);+ g_ctx.collisionEnterBuffer.push_back(cy);}for (int i = 0; i < contactEvents.endCount; ++i) {@@ -452,11 +457,11 @@ void physics_collectEvents() {uint32_t entityB = entityFromShape(evt.shapeIdB);if (entityA == 0xFFFFFFFF || entityB == 0xFFFFFFFF) continue;- pushEntityBits(g_collisionExitBuffer, entityA);- pushEntityBits(g_collisionExitBuffer, entityB);+ pushEntityBits(g_ctx.collisionExitBuffer, entityA);+ pushEntityBits(g_ctx.collisionExitBuffer, entityB);}- b2SensorEvents sensorEvents = b2World_GetSensorEvents(g_worldId);+ b2SensorEvents sensorEvents = b2World_GetSensorEvents(g_ctx.worldId);for (int i = 0; i < sensorEvents.beginCount; ++i) {auto& evt = sensorEvents.beginEvents[i];@@ -464,8 +469,8 @@ void physics_collectEvents() {uint32_t visitor = entityFromShape(evt.visitorShapeId);if (sensor == 0xFFFFFFFF || visitor == 0xFFFFFFFF) continue;- pushEntityBits(g_sensorEnterBuffer, sensor);- pushEntityBits(g_sensorEnterBuffer, visitor);+ pushEntityBits(g_ctx.sensorEnterBuffer, sensor);+ pushEntityBits(g_ctx.sensorEnterBuffer, visitor);}for (int i = 0; i < sensorEvents.endCount; ++i) {@@ -476,57 +481,57 @@ void physics_collectEvents() {uint32_t visitor = entityFromShape(evt.visitorShapeId);if (sensor == 0xFFFFFFFF || visitor == 0xFFFFFFFF) continue;- pushEntityBits(g_sensorExitBuffer, sensor);- pushEntityBits(g_sensorExitBuffer, visitor);+ pushEntityBits(g_ctx.sensorExitBuffer, sensor);+ pushEntityBits(g_ctx.sensorExitBuffer, visitor);}}EMSCRIPTEN_KEEPALIVEint physics_getCollisionEnterCount() {- return static_cast<int>(g_collisionEnterBuffer.size() / 6);+ return static_cast<int>(g_ctx.collisionEnterBuffer.size() / 6);}EMSCRIPTEN_KEEPALIVEuintptr_t physics_getCollisionEnterBuffer() {- return reinterpret_cast<uintptr_t>(g_collisionEnterBuffer.data());+ return reinterpret_cast<uintptr_t>(g_ctx.collisionEnterBuffer.data());}EMSCRIPTEN_KEEPALIVEint physics_getCollisionExitCount() {- return static_cast<int>(g_collisionExitBuffer.size() / 2);+ return static_cast<int>(g_ctx.collisionExitBuffer.size() / 2);}EMSCRIPTEN_KEEPALIVEuintptr_t physics_getCollisionExitBuffer() {- return reinterpret_cast<uintptr_t>(g_collisionExitBuffer.data());+ return reinterpret_cast<uintptr_t>(g_ctx.collisionExitBuffer.data());}EMSCRIPTEN_KEEPALIVEint physics_getSensorEnterCount() {- return static_cast<int>(g_sensorEnterBuffer.size() / 2);+ return static_cast<int>(g_ctx.sensorEnterBuffer.size() / 2);}EMSCRIPTEN_KEEPALIVEuintptr_t physics_getSensorEnterBuffer() {- return reinterpret_cast<uintptr_t>(g_sensorEnterBuffer.data());+ return reinterpret_cast<uintptr_t>(g_ctx.sensorEnterBuffer.data());}EMSCRIPTEN_KEEPALIVEint physics_getSensorExitCount() {- return static_cast<int>(g_sensorExitBuffer.size() / 2);+ return static_cast<int>(g_ctx.sensorExitBuffer.size() / 2);}EMSCRIPTEN_KEEPALIVEuintptr_t physics_getSensorExitBuffer() {- return reinterpret_cast<uintptr_t>(g_sensorExitBuffer.data());+ return reinterpret_cast<uintptr_t>(g_ctx.sensorExitBuffer.data());}// Force / Impulse / VelocityEMSCRIPTEN_KEEPALIVEvoid physics_applyForce(uint32_t entityId, float forceX, float forceY) {- auto it = g_entityToBody.find(entityId);- if (it == g_entityToBody.end()) return;+ auto it = g_ctx.entityToBody.find(entityId);+ if (it == g_ctx.entityToBody.end()) return;if (!b2Body_IsValid(it->second)) return;b2Vec2 center = b2Body_GetPosition(it->second);@@ -535,8 +540,8 @@ void physics_applyForce(uint32_t entityId, float forceX, float forceY) {EMSCRIPTEN_KEEPALIVEvoid physics_applyImpulse(uint32_t entityId, float impulseX, float impulseY) {- auto it = g_entityToBody.find(entityId);- if (it == g_entityToBody.end()) return;+ auto it = g_ctx.entityToBody.find(entityId);+ if (it == g_ctx.entityToBody.end()) return;if (!b2Body_IsValid(it->second)) return;b2Vec2 center = b2Body_GetPosition(it->second);@@ -545,8 +550,8 @@ void physics_applyImpulse(uint32_t entityId, float impulseX, float impulseY) {EMSCRIPTEN_KEEPALIVEvoid physics_setLinearVelocity(uint32_t entityId, float vx, float vy) {- auto it = g_entityToBody.find(entityId);- if (it == g_entityToBody.end()) return;+ auto it = g_ctx.entityToBody.find(entityId);+ if (it == g_ctx.entityToBody.end()) return;if (!b2Body_IsValid(it->second)) return;b2Body_SetLinearVelocity(it->second, {vx, vy});@@ -554,75 +559,75 @@ void physics_setLinearVelocity(uint32_t entityId, float vx, float vy) {EMSCRIPTEN_KEEPALIVEuintptr_t physics_getLinearVelocity(uint32_t entityId) {- auto it = g_entityToBody.find(entityId);- if (it == g_entityToBody.end()) {- g_velocityBuffer[0] = 0;- g_velocityBuffer[1] = 0;- return reinterpret_cast<uintptr_t>(g_velocityBuffer);+ auto it = g_ctx.entityToBody.find(entityId);+ if (it == g_ctx.entityToBody.end()) {+ g_ctx.velocityBuffer[0] = 0;+ g_ctx.velocityBuffer[1] = 0;+ return reinterpret_cast<uintptr_t>(g_ctx.velocityBuffer);}if (!b2Body_IsValid(it->second)) {- g_velocityBuffer[0] = 0;- g_velocityBuffer[1] = 0;- return reinterpret_cast<uintptr_t>(g_velocityBuffer);+ g_ctx.velocityBuffer[0] = 0;+ g_ctx.velocityBuffer[1] = 0;+ return reinterpret_cast<uintptr_t>(g_ctx.velocityBuffer);}b2Vec2 v = b2Body_GetLinearVelocity(it->second);- g_velocityBuffer[0] = v.x;- g_velocityBuffer[1] = v.y;- return reinterpret_cast<uintptr_t>(g_velocityBuffer);+ g_ctx.velocityBuffer[0] = v.x;+ g_ctx.velocityBuffer[1] = v.y;+ return reinterpret_cast<uintptr_t>(g_ctx.velocityBuffer);}// GravityEMSCRIPTEN_KEEPALIVEvoid physics_setGravity(float gx, float gy) {- if (!b2World_IsValid(g_worldId)) return;- b2World_SetGravity(g_worldId, {gx, gy});+ if (!b2World_IsValid(g_ctx.worldId)) return;+ b2World_SetGravity(g_ctx.worldId, {gx, gy});}EMSCRIPTEN_KEEPALIVEuintptr_t physics_getGravity() {- if (!b2World_IsValid(g_worldId)) {- g_gravityBuffer[0] = 0;- g_gravityBuffer[1] = 0;- return reinterpret_cast<uintptr_t>(g_gravityBuffer);+ if (!b2World_IsValid(g_ctx.worldId)) {+ g_ctx.gravityBuffer[0] = 0;+ g_ctx.gravityBuffer[1] = 0;+ return reinterpret_cast<uintptr_t>(g_ctx.gravityBuffer);}- b2Vec2 g = b2World_GetGravity(g_worldId);- g_gravityBuffer[0] = g.x;- g_gravityBuffer[1] = g.y;- return reinterpret_cast<uintptr_t>(g_gravityBuffer);+ b2Vec2 g = b2World_GetGravity(g_ctx.worldId);+ g_ctx.gravityBuffer[0] = g.x;+ g_ctx.gravityBuffer[1] = g.y;+ return reinterpret_cast<uintptr_t>(g_ctx.gravityBuffer);}// Angular Velocity / TorqueEMSCRIPTEN_KEEPALIVEvoid physics_setAngularVelocity(uint32_t entityId, float omega) {- auto it = g_entityToBody.find(entityId);- if (it == g_entityToBody.end()) return;+ auto it = g_ctx.entityToBody.find(entityId);+ if (it == g_ctx.entityToBody.end()) return;if (!b2Body_IsValid(it->second)) return;b2Body_SetAngularVelocity(it->second, omega);}EMSCRIPTEN_KEEPALIVEfloat physics_getAngularVelocity(uint32_t entityId) {- auto it = g_entityToBody.find(entityId);- if (it == g_entityToBody.end()) return 0;+ auto it = g_ctx.entityToBody.find(entityId);+ if (it == g_ctx.entityToBody.end()) return 0;if (!b2Body_IsValid(it->second)) return 0;return b2Body_GetAngularVelocity(it->second);}EMSCRIPTEN_KEEPALIVEvoid physics_applyTorque(uint32_t entityId, float torque) {- auto it = g_entityToBody.find(entityId);- if (it == g_entityToBody.end()) return;+ auto it = g_ctx.entityToBody.find(entityId);+ if (it == g_ctx.entityToBody.end()) return;if (!b2Body_IsValid(it->second)) return;b2Body_ApplyTorque(it->second, torque, true);}EMSCRIPTEN_KEEPALIVEvoid physics_applyAngularImpulse(uint32_t entityId, float impulse) {- auto it = g_entityToBody.find(entityId);- if (it == g_entityToBody.end()) return;+ auto it = g_ctx.entityToBody.find(entityId);+ if (it == g_ctx.entityToBody.end()) return;if (!b2Body_IsValid(it->second)) return;b2Body_ApplyAngularImpulse(it->second, impulse, true);}@@ -633,8 +638,8 @@ EMSCRIPTEN_KEEPALIVEvoid physics_updateBodyProperties(uint32_t entityId, int bodyType,float gravityScale, float linearDamping, float angularDamping,int fixedRotation, int bullet) {- auto it = g_entityToBody.find(entityId);- if (it == g_entityToBody.end()) return;+ auto it = g_ctx.entityToBody.find(entityId);+ if (it == g_ctx.entityToBody.end()) return;if (!b2Body_IsValid(it->second)) return;b2BodyType type;@@ -646,12 +651,12 @@ void physics_updateBodyProperties(uint32_t entityId, int bodyType,b2BodyType oldType = b2Body_GetType(it->second);b2Body_SetType(it->second, type);if (oldType != type) {- auto dit = std::find(g_dynamicBodyEntities.begin(), g_dynamicBodyEntities.end(), entityId);- if (type == b2_dynamicBody && dit == g_dynamicBodyEntities.end()) {- g_dynamicBodyEntities.push_back(entityId);- } else if (type != b2_dynamicBody && dit != g_dynamicBodyEntities.end()) {- *dit = g_dynamicBodyEntities.back();- g_dynamicBodyEntities.pop_back();+ auto dit = std::find(g_ctx.dynamicBodyEntities.begin(), g_ctx.dynamicBodyEntities.end(), entityId);+ if (type == b2_dynamicBody && dit == g_ctx.dynamicBodyEntities.end()) {+ g_ctx.dynamicBodyEntities.push_back(entityId);+ } else if (type != b2_dynamicBody && dit != g_ctx.dynamicBodyEntities.end()) {+ *dit = g_ctx.dynamicBodyEntities.back();+ g_ctx.dynamicBodyEntities.pop_back();}}b2Body_SetGravityScale(it->second, gravityScale);@@ -673,11 +678,11 @@ int physics_createRevoluteJoint(uint32_t entityIdA, uint32_t entityIdB,int enableMotor, float motorSpeed, float maxMotorTorque,int enableLimit, float lowerAngle, float upperAngle,int collideConnected) {- if (!b2World_IsValid(g_worldId)) return 0;+ if (!b2World_IsValid(g_ctx.worldId)) return 0;- auto itA = g_entityToBody.find(entityIdA);- auto itB = g_entityToBody.find(entityIdB);- if (itA == g_entityToBody.end() || itB == g_entityToBody.end()) return 0;+ auto itA = g_ctx.entityToBody.find(entityIdA);+ auto itB = g_ctx.entityToBody.find(entityIdB);+ if (itA == g_ctx.entityToBody.end() || itB == g_ctx.entityToBody.end()) return 0;if (!b2Body_IsValid(itA->second) || !b2Body_IsValid(itB->second)) return 0;b2RevoluteJointDef jointDef = b2DefaultRevoluteJointDef();@@ -693,81 +698,81 @@ int physics_createRevoluteJoint(uint32_t entityIdA, uint32_t entityIdB,jointDef.upperAngle = upperAngle;jointDef.base.collideConnected = collideConnected != 0;- b2JointId jointId = b2CreateRevoluteJoint(g_worldId, &jointDef);- g_entityToJoint[entityIdB] = jointId;+ b2JointId jointId = b2CreateRevoluteJoint(g_ctx.worldId, &jointDef);+ g_ctx.entityToJoint[entityIdB] = jointId;return 1;}EMSCRIPTEN_KEEPALIVEvoid physics_destroyJoint(uint32_t entityId) {- auto it = g_entityToJoint.find(entityId);- if (it == g_entityToJoint.end()) return;+ auto it = g_ctx.entityToJoint.find(entityId);+ if (it == g_ctx.entityToJoint.end()) return;if (b2Joint_IsValid(it->second)) {b2DestroyJoint(it->second, true);}- g_entityToJoint.erase(it);+ g_ctx.entityToJoint.erase(it);}EMSCRIPTEN_KEEPALIVEvoid physics_setRevoluteMotorSpeed(uint32_t entityId, float speed) {- auto it = g_entityToJoint.find(entityId);- if (it == g_entityToJoint.end()) return;+ auto it = g_ctx.entityToJoint.find(entityId);+ if (it == g_ctx.entityToJoint.end()) return;if (!b2Joint_IsValid(it->second)) return;b2RevoluteJoint_SetMotorSpeed(it->second, speed);}EMSCRIPTEN_KEEPALIVEvoid physics_setRevoluteMaxMotorTorque(uint32_t entityId, float torque) {- auto it = g_entityToJoint.find(entityId);- if (it == g_entityToJoint.end()) return;+ auto it = g_ctx.entityToJoint.find(entityId);+ if (it == g_ctx.entityToJoint.end()) return;if (!b2Joint_IsValid(it->second)) return;b2RevoluteJoint_SetMaxMotorTorque(it->second, torque);}EMSCRIPTEN_KEEPALIVEvoid physics_enableRevoluteMotor(uint32_t entityId, int enable) {- auto it = g_entityToJoint.find(entityId);- if (it == g_entityToJoint.end()) return;+ auto it = g_ctx.entityToJoint.find(entityId);+ if (it == g_ctx.entityToJoint.end()) return;if (!b2Joint_IsValid(it->second)) return;b2RevoluteJoint_EnableMotor(it->second, enable != 0);}EMSCRIPTEN_KEEPALIVEvoid physics_enableRevoluteLimit(uint32_t entityId, int enable) {- auto it = g_entityToJoint.find(entityId);- if (it == g_entityToJoint.end()) return;+ auto it = g_ctx.entityToJoint.find(entityId);+ if (it == g_ctx.entityToJoint.end()) return;if (!b2Joint_IsValid(it->second)) return;b2RevoluteJoint_EnableLimit(it->second, enable != 0);}EMSCRIPTEN_KEEPALIVEvoid physics_setRevoluteLimits(uint32_t entityId, float lower, float upper) {- auto it = g_entityToJoint.find(entityId);- if (it == g_entityToJoint.end()) return;+ auto it = g_ctx.entityToJoint.find(entityId);+ if (it == g_ctx.entityToJoint.end()) return;if (!b2Joint_IsValid(it->second)) return;b2RevoluteJoint_SetLimits(it->second, lower, upper);}EMSCRIPTEN_KEEPALIVEfloat physics_getRevoluteAngle(uint32_t entityId) {- auto it = g_entityToJoint.find(entityId);- if (it == g_entityToJoint.end()) return 0;+ auto it = g_ctx.entityToJoint.find(entityId);+ if (it == g_ctx.entityToJoint.end()) return 0;if (!b2Joint_IsValid(it->second)) return 0;return b2RevoluteJoint_GetAngle(it->second);}EMSCRIPTEN_KEEPALIVEfloat physics_getRevoluteMotorTorque(uint32_t entityId) {- auto it = g_entityToJoint.find(entityId);- if (it == g_entityToJoint.end()) return 0;+ auto it = g_ctx.entityToJoint.find(entityId);+ if (it == g_ctx.entityToJoint.end()) return 0;if (!b2Joint_IsValid(it->second)) return 0;return b2RevoluteJoint_GetMotorTorque(it->second);}EMSCRIPTEN_KEEPALIVEint physics_hasJoint(uint32_t entityId) {- return g_entityToJoint.contains(entityId) ? 1 : 0;+ return g_ctx.entityToJoint.contains(entityId) ? 1 : 0;}} // extern "C"diff --git a/src/esengine/bindings/SpineModuleEntry.cpp b/src/esengine/bindings/SpineModuleEntry.cppindex b1228e315..d84172acf 100644--- a/src/esengine/bindings/SpineModuleEntry.cpp+++ b/src/esengine/bindings/SpineModuleEntry.cpp@@ -94,16 +94,46 @@ struct MeshBatch {int blendMode = 0;};-static std::unordered_map<int, SkeletonHandle> g_skeletons;-static std::unordered_map<int, SpineInstance> g_instances;-static int g_nextSkeletonId = 1;-static int g_nextInstanceId = 1;+// =============================================================================+// Spine Context+// =============================================================================++struct SpineContext {+ std::unordered_map<int, SkeletonHandle> skeletons;+ std::unordered_map<int, SpineInstance> instances;+ int nextSkeletonId = 1;+ int nextInstanceId = 1;++ std::vector<MeshBatch> meshBatches;+ std::vector<float> worldVertices;++ std::string stringBuffer;+ std::string lastError;-static std::vector<MeshBatch> g_meshBatches;-static std::vector<float> g_worldVertices;+ void reset() {+ for (auto& [id, inst] : instances) {+ if (inst.state) spAnimationState_dispose(inst.state);+ if (inst.skeleton) spSkeleton_dispose(inst.skeleton);+ }+ instances.clear();++ for (auto& [id, h] : skeletons) {+ if (h.stateData) spAnimationStateData_dispose(h.stateData);+ if (h.skeletonData) spSkeletonData_dispose(h.skeletonData);+ if (h.atlas) spAtlas_dispose(h.atlas);+ }+ skeletons.clear();++ nextSkeletonId = 1;+ nextInstanceId = 1;+ meshBatches.clear();+ worldVertices.clear();+ stringBuffer.clear();+ lastError.clear();+ }+};-static std::string g_stringBuffer;-static std::string g_lastError;+static SpineContext g_ctx;static void destroyInstance(SpineInstance& inst) {if (inst.state) spAnimationState_dispose(inst.state);@@ -125,15 +155,15 @@ extern "C" {EMSCRIPTEN_KEEPALIVEint spine_loadSkeleton(uintptr_t skelDataPtr, int skelDataLen,const char* atlasText, int atlasLen, int isBinary) {- g_lastError.clear();+ g_ctx.lastError.clear();- int id = g_nextSkeletonId;- auto& handle = g_skeletons[id];+ 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_lastError = "Failed to create atlas (invalid atlas text or no pages)";- g_skeletons.erase(id);+ g_ctx.lastError = "Failed to create atlas (invalid atlas text or no pages)";+ g_ctx.skeletons.erase(id);return -1;}@@ -143,7 +173,7 @@ int spine_loadSkeleton(uintptr_t skelDataPtr, int skelDataLen,handle.skeletonData = spSkeletonBinary_readSkeletonData(binary, reinterpret_cast<const unsigned char*>(skelDataPtr), skelDataLen);if (!handle.skeletonData && binary->error) {- g_lastError = binary->error;+ g_ctx.lastError = binary->error;}spSkeletonBinary_dispose(binary);} else {@@ -152,53 +182,53 @@ int spine_loadSkeleton(uintptr_t skelDataPtr, int skelDataLen,handle.skeletonData = spSkeletonJson_readSkeletonData(json, reinterpret_cast<const char*>(skelDataPtr));if (!handle.skeletonData && json->error) {- g_lastError = json->error;+ g_ctx.lastError = json->error;}spSkeletonJson_dispose(json);}if (!handle.skeletonData) {destroySkeleton(handle);- g_skeletons.erase(id);+ g_ctx.skeletons.erase(id);return -1;}handle.stateData = spAnimationStateData_create(handle.skeletonData);handle.stateData->defaultMix = 0.2f;- g_nextSkeletonId++;+ g_ctx.nextSkeletonId++;return id;}EMSCRIPTEN_KEEPALIVEconst char* spine_getLastError() {- return g_lastError.c_str();+ return g_ctx.lastError.c_str();}EMSCRIPTEN_KEEPALIVEvoid spine_unloadSkeleton(int handle) {- auto it = g_skeletons.find(handle);- if (it == g_skeletons.end()) return;+ auto it = g_ctx.skeletons.find(handle);+ if (it == g_ctx.skeletons.end()) return;std::vector<int> toRemove;- for (auto& [id, inst] : g_instances) {+ for (auto& [id, inst] : g_ctx.instances) {if (inst.skeletonHandle == handle) {toRemove.push_back(id);}}for (int id : toRemove) {- destroyInstance(g_instances[id]);- g_instances.erase(id);+ destroyInstance(g_ctx.instances[id]);+ g_ctx.instances.erase(id);}destroySkeleton(it->second);- g_skeletons.erase(it);+ g_ctx.skeletons.erase(it);}EMSCRIPTEN_KEEPALIVEint spine_getAtlasPageCount(int handle) {- auto it = g_skeletons.find(handle);- if (it == g_skeletons.end()) return 0;+ 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) {@@ -210,22 +240,22 @@ int spine_getAtlasPageCount(int handle) {EMSCRIPTEN_KEEPALIVEconst char* spine_getAtlasPageTextureName(int handle, int pageIndex) {- auto it = g_skeletons.find(handle);- if (it == g_skeletons.end()) return "";+ 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_stringBuffer = page->name;- return g_stringBuffer.c_str();+ g_ctx.stringBuffer = page->name;+ return g_ctx.stringBuffer.c_str();}EMSCRIPTEN_KEEPALIVEvoid spine_setAtlasPageTexture(int handle, int pageIndex,uint32_t textureId, int width, int height) {- auto it = g_skeletons.find(handle);- if (it == g_skeletons.end()) return;+ 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;@@ -254,11 +284,11 @@ void spine_setAtlasPageTexture(int handle, int pageIndex,EMSCRIPTEN_KEEPALIVEint spine_createInstance(int skeletonHandle) {- auto it = g_skeletons.find(skeletonHandle);- if (it == g_skeletons.end()) return -1;+ auto it = g_ctx.skeletons.find(skeletonHandle);+ if (it == g_ctx.skeletons.end()) return -1;- int id = g_nextInstanceId;- auto& inst = g_instances[id];+ 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);@@ -269,16 +299,16 @@ int spine_createInstance(int skeletonHandle) {spSkeleton_updateWorldTransform(inst.skeleton, SP_PHYSICS_UPDATE);#endif- g_nextInstanceId++;+ g_ctx.nextInstanceId++;return id;}EMSCRIPTEN_KEEPALIVEvoid spine_destroyInstance(int instanceId) {- auto it = g_instances.find(instanceId);- if (it == g_instances.end()) return;+ auto it = g_ctx.instances.find(instanceId);+ if (it == g_ctx.instances.end()) return;destroyInstance(it->second);- g_instances.erase(it);+ g_ctx.instances.erase(it);}// =============================================================================@@ -287,8 +317,8 @@ void spine_destroyInstance(int instanceId) {EMSCRIPTEN_KEEPALIVEint spine_playAnimation(int instanceId, const char* name, int loop, int track) {- auto it = g_instances.find(instanceId);- if (it == g_instances.end()) return 0;+ 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;@@ -297,8 +327,8 @@ int spine_playAnimation(int instanceId, const char* name, int loop, int track) {EMSCRIPTEN_KEEPALIVEint spine_addAnimation(int instanceId, const char* name,int loop, float delay, int track) {- auto it = g_instances.find(instanceId);- if (it == g_instances.end()) return 0;+ 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;@@ -306,8 +336,8 @@ int spine_addAnimation(int instanceId, const char* name,EMSCRIPTEN_KEEPALIVEvoid spine_setSkin(int instanceId, const char* name) {- auto it = g_instances.find(instanceId);- if (it == g_instances.end()) return;+ 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);@@ -319,8 +349,8 @@ void spine_setSkin(int instanceId, const char* name) {EMSCRIPTEN_KEEPALIVEvoid spine_update(int instanceId, float dt) {- auto it = g_instances.find(instanceId);- if (it == g_instances.end()) return;+ auto it = g_ctx.instances.find(instanceId);+ if (it == g_ctx.instances.end()) return;spAnimationState_update(it->second.state, dt);spAnimationState_apply(it->second.state, it->second.skeleton);@@ -338,49 +368,49 @@ void spine_update(int instanceId, float dt) {EMSCRIPTEN_KEEPALIVEconst char* spine_getAnimations(int instanceId) {- auto it = g_instances.find(instanceId);- if (it == g_instances.end()) {- g_stringBuffer = "[]";- return g_stringBuffer.c_str();+ 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_stringBuffer = "[";+ g_ctx.stringBuffer = "[";for (int i = 0; i < data->animationsCount; ++i) {- if (i > 0) g_stringBuffer += ",";- g_stringBuffer += "\"";- g_stringBuffer += data->animations[i]->name;- g_stringBuffer += "\"";+ if (i > 0) g_ctx.stringBuffer += ",";+ g_ctx.stringBuffer += "\"";+ g_ctx.stringBuffer += data->animations[i]->name;+ g_ctx.stringBuffer += "\"";}- g_stringBuffer += "]";- return g_stringBuffer.c_str();+ g_ctx.stringBuffer += "]";+ return g_ctx.stringBuffer.c_str();}EMSCRIPTEN_KEEPALIVEconst char* spine_getSkins(int instanceId) {- auto it = g_instances.find(instanceId);- if (it == g_instances.end()) {- g_stringBuffer = "[]";- return g_stringBuffer.c_str();+ 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_stringBuffer = "[";+ g_ctx.stringBuffer = "[";for (int i = 0; i < data->skinsCount; ++i) {- if (i > 0) g_stringBuffer += ",";- g_stringBuffer += "\"";- g_stringBuffer += data->skins[i]->name;- g_stringBuffer += "\"";+ if (i > 0) g_ctx.stringBuffer += ",";+ g_ctx.stringBuffer += "\"";+ g_ctx.stringBuffer += data->skins[i]->name;+ g_ctx.stringBuffer += "\"";}- g_stringBuffer += "]";- return g_stringBuffer.c_str();+ g_ctx.stringBuffer += "]";+ return g_ctx.stringBuffer.c_str();}EMSCRIPTEN_KEEPALIVEint spine_getBonePosition(int instanceId, const char* bone,uintptr_t outXPtr, uintptr_t outYPtr) {- auto it = g_instances.find(instanceId);- if (it == g_instances.end()) return 0;+ 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;@@ -392,8 +422,8 @@ int spine_getBonePosition(int instanceId, const char* bone,EMSCRIPTEN_KEEPALIVEfloat spine_getBoneRotation(int instanceId, const char* bone) {- auto it = g_instances.find(instanceId);- if (it == g_instances.end()) return 0.0f;+ 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;@@ -409,8 +439,8 @@ void spine_getBounds(int instanceId, uintptr_t outXPtr, uintptr_t outYPtr,auto* outW = reinterpret_cast<float*>(outWPtr);auto* outH = reinterpret_cast<float*>(outHPtr);- auto it = g_instances.find(instanceId);- if (it == g_instances.end()) {+ auto it = g_ctx.instances.find(instanceId);+ if (it == g_ctx.instances.end()) {*outX = *outY = *outW = *outH = 0;return;}@@ -428,21 +458,21 @@ void spine_getBounds(int instanceId, uintptr_t outXPtr, uintptr_t outYPtr,if (slot->attachment->type == SP_ATTACHMENT_REGION) {auto* region = reinterpret_cast<spRegionAttachment*>(slot->attachment);- g_worldVertices.resize(8);+ g_ctx.worldVertices.resize(8);#ifdef ES_SPINE_38- spRegionAttachment_computeWorldVertices(region, slot->bone, g_worldVertices.data(), 0, 2);+ spRegionAttachment_computeWorldVertices(region, slot->bone, g_ctx.worldVertices.data(), 0, 2);#else- spRegionAttachment_computeWorldVertices(region, slot, g_worldVertices.data(), 0, 2);+ spRegionAttachment_computeWorldVertices(region, slot, g_ctx.worldVertices.data(), 0, 2);#endif- verts = g_worldVertices.data();+ 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_worldVertices.resize(SUPER(mesh)->worldVerticesLength);+ g_ctx.worldVertices.resize(SUPER(mesh)->worldVerticesLength);spVertexAttachment_computeWorldVertices(SUPER(mesh), slot, 0,- SUPER(mesh)->worldVerticesLength, g_worldVertices.data(), 0, 2);- verts = g_worldVertices.data();+ SUPER(mesh)->worldVerticesLength, g_ctx.worldVertices.data(), 0, 2);+ verts = g_ctx.worldVertices.data();}if (verts && vertCount > 0) {@@ -473,10 +503,10 @@ void spine_getBounds(int instanceId, uintptr_t outXPtr, uintptr_t outYPtr,// =============================================================================static void extractMeshBatches(int instanceId) {- g_meshBatches.clear();+ g_ctx.meshBatches.clear();- auto it = g_instances.find(instanceId);- if (it == g_instances.end()) return;+ auto it = g_ctx.instances.find(instanceId);+ if (it == g_ctx.instances.end()) return;spSkeleton* skeleton = it->second.skeleton;spColor& skelColor = skeleton->color;@@ -512,11 +542,11 @@ static void extractMeshBatches(int instanceId) {if (attachment->type == SP_ATTACHMENT_REGION) {auto* region = reinterpret_cast<spRegionAttachment*>(attachment);- g_worldVertices.resize(8);+ g_ctx.worldVertices.resize(8);#ifdef ES_SPINE_38- spRegionAttachment_computeWorldVertices(region, slot->bone, g_worldVertices.data(), 0, 2);+ spRegionAttachment_computeWorldVertices(region, slot->bone, g_ctx.worldVertices.data(), 0, 2);#else- spRegionAttachment_computeWorldVertices(region, slot, g_worldVertices.data(), 0, 2);+ spRegionAttachment_computeWorldVertices(region, slot, g_ctx.worldVertices.data(), 0, 2);#endifuint32_t texId = getRegionTextureId(region);@@ -539,8 +569,8 @@ static void extractMeshBatches(int instanceId) {}if (needNewBatch) {- g_meshBatches.emplace_back();- currentBatch = &g_meshBatches.back();+ g_ctx.meshBatches.emplace_back();+ currentBatch = &g_ctx.meshBatches.back();currentBatch->textureId = texId;currentBatch->blendMode = effectiveBlend;currentTexture = texId;@@ -565,8 +595,8 @@ static void extractMeshBatches(int instanceId) {currentBatch->vertices.size() / 8);for (int j = 0; j < 4; ++j) {- currentBatch->vertices.push_back(g_worldVertices[j * 2]);- currentBatch->vertices.push_back(g_worldVertices[j * 2 + 1]);+ currentBatch->vertices.push_back(g_ctx.worldVertices[j * 2]);+ currentBatch->vertices.push_back(g_ctx.worldVertices[j * 2 + 1]);currentBatch->vertices.push_back(uvs[j * 2]);currentBatch->vertices.push_back(uvs[j * 2 + 1]);currentBatch->vertices.push_back(r);@@ -588,9 +618,9 @@ static void extractMeshBatches(int instanceId) {int worldVerticesLength = SUPER(mesh)->worldVerticesLength;int vertexCount = worldVerticesLength / 2;- g_worldVertices.resize(worldVerticesLength);+ g_ctx.worldVertices.resize(worldVerticesLength);spVertexAttachment_computeWorldVertices(SUPER(mesh), slot, 0,- worldVerticesLength, g_worldVertices.data(), 0, 2);+ worldVerticesLength, g_ctx.worldVertices.data(), 0, 2);uint32_t texId = getMeshTextureId(mesh);if (!texId) continue;@@ -612,8 +642,8 @@ static void extractMeshBatches(int instanceId) {}if (needNewBatch) {- g_meshBatches.emplace_back();- currentBatch = &g_meshBatches.back();+ g_ctx.meshBatches.emplace_back();+ currentBatch = &g_ctx.meshBatches.back();currentBatch->textureId = texId;currentBatch->blendMode = effectiveBlend;currentTexture = texId;@@ -638,8 +668,8 @@ static void extractMeshBatches(int instanceId) {currentBatch->vertices.size() / 8);for (int j = 0; j < vertexCount; ++j) {- currentBatch->vertices.push_back(g_worldVertices[j * 2]);- currentBatch->vertices.push_back(g_worldVertices[j * 2 + 1]);+ currentBatch->vertices.push_back(g_ctx.worldVertices[j * 2]);+ currentBatch->vertices.push_back(g_ctx.worldVertices[j * 2 + 1]);currentBatch->vertices.push_back(uvs[j * 2]);currentBatch->vertices.push_back(uvs[j * 2 + 1]);currentBatch->vertices.push_back(r);@@ -659,21 +689,21 @@ static void extractMeshBatches(int instanceId) {EMSCRIPTEN_KEEPALIVEint spine_getMeshBatchCount(int instanceId) {extractMeshBatches(instanceId);- return static_cast<int>(g_meshBatches.size());+ return static_cast<int>(g_ctx.meshBatches.size());}EMSCRIPTEN_KEEPALIVEint spine_getMeshBatchVertexCount(int instanceId, int batchIndex) {(void)instanceId;- if (batchIndex < 0 || batchIndex >= static_cast<int>(g_meshBatches.size())) return 0;- return static_cast<int>(g_meshBatches[batchIndex].vertices.size() / 8);+ if (batchIndex < 0 || batchIndex >= static_cast<int>(g_ctx.meshBatches.size())) return 0;+ return static_cast<int>(g_ctx.meshBatches[batchIndex].vertices.size() / 8);}EMSCRIPTEN_KEEPALIVEint spine_getMeshBatchIndexCount(int instanceId, int batchIndex) {(void)instanceId;- if (batchIndex < 0 || batchIndex >= static_cast<int>(g_meshBatches.size())) return 0;- return static_cast<int>(g_meshBatches[batchIndex].indices.size());+ if (batchIndex < 0 || batchIndex >= static_cast<int>(g_ctx.meshBatches.size())) return 0;+ return static_cast<int>(g_ctx.meshBatches[batchIndex].indices.size());}EMSCRIPTEN_KEEPALIVE@@ -681,9 +711,9 @@ void spine_getMeshBatchData(int instanceId, int batchIndex,uintptr_t outVerticesPtr, uintptr_t outIndicesPtr,uintptr_t outTextureIdPtr, uintptr_t outBlendModePtr) {(void)instanceId;- if (batchIndex < 0 || batchIndex >= static_cast<int>(g_meshBatches.size())) return;+ if (batchIndex < 0 || batchIndex >= static_cast<int>(g_ctx.meshBatches.size())) return;- auto& batch = g_meshBatches[batchIndex];+ auto& batch = g_ctx.meshBatches[batchIndex];auto* outVertices = reinterpret_cast<float*>(outVerticesPtr);auto* outIndices = reinterpret_cast<uint16_t*>(outIndicesPtr);