[cocos2dx] Merged SkeletonRenderer code for v3 and v4. Only the batchers remain in separate source files.

This commit is contained in:
badlogic 2019-12-10 15:00:32 +01:00
parent d8f94bc18a
commit 9ab8c3817b
5 changed files with 95 additions and 955 deletions

View File

@ -166,6 +166,7 @@
763105D820BC1B9700927A1E /* Updatable.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 763104A520BC1B5600927A1E /* Updatable.cpp */; };
763105D920BC1B9700927A1E /* VertexAttachment.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7631049220BC1B5500927A1E /* VertexAttachment.cpp */; };
763105DA20BC1B9700927A1E /* VertexEffect.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 763104AE20BC1B5800927A1E /* VertexEffect.cpp */; };
763A8A98239FDC0400FBEDDC /* SkeletonRenderer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 763A8A97239FDC0400FBEDDC /* SkeletonRenderer.cpp */; };
76798D1D22A95AB400F77964 /* ConstraintData.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 76798D1C22A95AB300F77964 /* ConstraintData.cpp */; };
76798D1E22A95AEF00F77964 /* ConstraintData.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 76798D1C22A95AB300F77964 /* ConstraintData.cpp */; };
767D80E322B29F22000BD703 /* MixAndMatchExample.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 767D80E022B29F22000BD703 /* MixAndMatchExample.cpp */; };
@ -202,14 +203,11 @@
76D1BFE02029E35200A0272D /* SkeletonRendererSeparatorExample.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 76D1BFDF2029E35200A0272D /* SkeletonRendererSeparatorExample.cpp */; };
76D1BFE12029E37700A0272D /* SkeletonRendererSeparatorExample.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 76D1BFDF2029E35200A0272D /* SkeletonRendererSeparatorExample.cpp */; };
76D28ACB239FAF0B00FB142D /* SkeletonTwoColorBatch.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 76D28AC5239FAF0A00FB142D /* SkeletonTwoColorBatch.cpp */; };
76D28ACC239FAF0B00FB142D /* SkeletonRenderer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 76D28AC9239FAF0B00FB142D /* SkeletonRenderer.cpp */; };
76D28ACD239FAF0B00FB142D /* SkeletonBatch.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 76D28ACA239FAF0B00FB142D /* SkeletonBatch.cpp */; };
76D28ACE239FAF2C00FB142D /* SkeletonBatch.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 76D28ACA239FAF0B00FB142D /* SkeletonBatch.cpp */; };
76D28ACF239FAF2C00FB142D /* SkeletonRenderer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 76D28AC9239FAF0B00FB142D /* SkeletonRenderer.cpp */; };
76D28AD0239FAF2C00FB142D /* SkeletonTwoColorBatch.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 76D28AC5239FAF0A00FB142D /* SkeletonTwoColorBatch.cpp */; };
76D520E61EB362DD00572471 /* CoinExample.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 76D520E41EB362DD00572471 /* CoinExample.cpp */; };
76D520E71EB3634600572471 /* CoinExample.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 76D520E41EB362DD00572471 /* CoinExample.cpp */; };
76F10839239FC8DA005B0CE9 /* SkeletonRenderer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 76F10836239FC8DA005B0CE9 /* SkeletonRenderer.cpp */; };
76F1083A239FC8DA005B0CE9 /* SkeletonTwoColorBatch.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 76F10837239FC8DA005B0CE9 /* SkeletonTwoColorBatch.cpp */; };
76F1083B239FC8DA005B0CE9 /* SkeletonBatch.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 76F10838239FC8DA005B0CE9 /* SkeletonBatch.cpp */; };
76F5BD551D2BD7D3005917E5 /* TankExample.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 76F5BD531D2BD7D3005917E5 /* TankExample.cpp */; };
@ -368,6 +366,7 @@
763104C020BC1B5E00927A1E /* SkeletonBounds.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = SkeletonBounds.cpp; path = "../../../spine-cpp/spine-cpp/src/spine/SkeletonBounds.cpp"; sourceTree = "<group>"; };
763104C120BC1B5E00927A1E /* ShearTimeline.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = ShearTimeline.cpp; path = "../../../spine-cpp/spine-cpp/src/spine/ShearTimeline.cpp"; sourceTree = "<group>"; };
763104C220BC1B5E00927A1E /* TranslateTimeline.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = TranslateTimeline.cpp; path = "../../../spine-cpp/spine-cpp/src/spine/TranslateTimeline.cpp"; sourceTree = "<group>"; };
763A8A97239FDC0400FBEDDC /* SkeletonRenderer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = SkeletonRenderer.cpp; path = ../../src/spine/SkeletonRenderer.cpp; sourceTree = "<group>"; };
76798D1C22A95AB300F77964 /* ConstraintData.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = ConstraintData.cpp; path = "../../../spine-cpp/spine-cpp/src/spine/ConstraintData.cpp"; sourceTree = "<group>"; };
767D80E022B29F22000BD703 /* MixAndMatchExample.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = MixAndMatchExample.cpp; sourceTree = "<group>"; };
767D80E222B29F22000BD703 /* MixAndMatchExample.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MixAndMatchExample.h; sourceTree = "<group>"; };
@ -396,13 +395,11 @@
76D28AC5239FAF0A00FB142D /* SkeletonTwoColorBatch.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = SkeletonTwoColorBatch.cpp; path = ../../src/spine/v3/SkeletonTwoColorBatch.cpp; sourceTree = "<group>"; };
76D28AC6239FAF0B00FB142D /* SkeletonTwoColorBatch.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SkeletonTwoColorBatch.h; path = ../../src/spine/v3/SkeletonTwoColorBatch.h; sourceTree = "<group>"; };
76D28AC8239FAF0B00FB142D /* SkeletonBatch.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SkeletonBatch.h; path = ../../src/spine/v3/SkeletonBatch.h; sourceTree = "<group>"; };
76D28AC9239FAF0B00FB142D /* SkeletonRenderer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = SkeletonRenderer.cpp; path = ../../src/spine/v3/SkeletonRenderer.cpp; sourceTree = "<group>"; };
76D28ACA239FAF0B00FB142D /* SkeletonBatch.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = SkeletonBatch.cpp; path = ../../src/spine/v3/SkeletonBatch.cpp; sourceTree = "<group>"; };
76D520E41EB362DD00572471 /* CoinExample.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CoinExample.cpp; sourceTree = "<group>"; };
76D520E51EB362DD00572471 /* CoinExample.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CoinExample.h; sourceTree = "<group>"; };
76F10834239FC8D9005B0CE9 /* SkeletonBatch.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SkeletonBatch.h; path = ../../src/spine/v4/SkeletonBatch.h; sourceTree = "<group>"; };
76F10835239FC8DA005B0CE9 /* SkeletonTwoColorBatch.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SkeletonTwoColorBatch.h; path = ../../src/spine/v4/SkeletonTwoColorBatch.h; sourceTree = "<group>"; };
76F10836239FC8DA005B0CE9 /* SkeletonRenderer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = SkeletonRenderer.cpp; path = ../../src/spine/v4/SkeletonRenderer.cpp; sourceTree = "<group>"; };
76F10837239FC8DA005B0CE9 /* SkeletonTwoColorBatch.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = SkeletonTwoColorBatch.cpp; path = ../../src/spine/v4/SkeletonTwoColorBatch.cpp; sourceTree = "<group>"; };
76F10838239FC8DA005B0CE9 /* SkeletonBatch.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = SkeletonBatch.cpp; path = ../../src/spine/v4/SkeletonBatch.cpp; sourceTree = "<group>"; };
76F1083C239FC8E5005B0CE9 /* SkeletonRenderer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SkeletonRenderer.h; path = ../../src/spine/SkeletonRenderer.h; sourceTree = "<group>"; };
@ -697,13 +694,14 @@
76AAA3FF1D18102C00C54FCB /* spine-cocos2dx */ = {
isa = PBXGroup;
children = (
76F1083C239FC8E5005B0CE9 /* SkeletonRenderer.h */,
76F10833239FC8CB005B0CE9 /* v4 */,
76D28AC2239FAED800FB142D /* v3 */,
76AAA4001D18106000C54FCB /* AttachmentVertices.cpp */,
76AAA4011D18106000C54FCB /* AttachmentVertices.h */,
76AAA4041D18106000C54FCB /* SkeletonAnimation.cpp */,
76AAA4051D18106000C54FCB /* SkeletonAnimation.h */,
763A8A97239FDC0400FBEDDC /* SkeletonRenderer.cpp */,
76F1083C239FC8E5005B0CE9 /* SkeletonRenderer.h */,
76AAA4001D18106000C54FCB /* AttachmentVertices.cpp */,
76AAA4011D18106000C54FCB /* AttachmentVertices.h */,
76AAA40A1D18106000C54FCB /* spine-cocos2dx.cpp */,
76AAA40B1D18106000C54FCB /* spine-cocos2dx.h */,
);
@ -715,7 +713,6 @@
children = (
76D28ACA239FAF0B00FB142D /* SkeletonBatch.cpp */,
76D28AC8239FAF0B00FB142D /* SkeletonBatch.h */,
76D28AC9239FAF0B00FB142D /* SkeletonRenderer.cpp */,
76D28AC5239FAF0A00FB142D /* SkeletonTwoColorBatch.cpp */,
76D28AC6239FAF0B00FB142D /* SkeletonTwoColorBatch.h */,
);
@ -727,7 +724,6 @@
children = (
76F10838239FC8DA005B0CE9 /* SkeletonBatch.cpp */,
76F10834239FC8D9005B0CE9 /* SkeletonBatch.h */,
76F10836239FC8DA005B0CE9 /* SkeletonRenderer.cpp */,
76F10837239FC8DA005B0CE9 /* SkeletonTwoColorBatch.cpp */,
76F10835239FC8DA005B0CE9 /* SkeletonTwoColorBatch.h */,
);
@ -949,14 +945,13 @@
763104EA20BC1B5E00927A1E /* IkConstraintData.cpp in Sources */,
763104F420BC1B5E00927A1E /* Attachment.cpp in Sources */,
763104E620BC1B5E00927A1E /* TransformConstraintData.cpp in Sources */,
76D28ACC239FAF0B00FB142D /* SkeletonRenderer.cpp in Sources */,
763104D720BC1B5E00927A1E /* SpineObject.cpp in Sources */,
763104CA20BC1B5E00927A1E /* PathConstraintSpacingTimeline.cpp in Sources */,
76D520E61EB362DD00572471 /* CoinExample.cpp in Sources */,
763104E220BC1B5E00927A1E /* Updatable.cpp in Sources */,
763A8A98239FDC0400FBEDDC /* SkeletonRenderer.cpp in Sources */,
763104D220BC1B5E00927A1E /* RotateTimeline.cpp in Sources */,
763104E320BC1B5E00927A1E /* RegionAttachment.cpp in Sources */,
76F10839239FC8DA005B0CE9 /* SkeletonRenderer.cpp in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@ -965,7 +960,6 @@
buildActionMask = 2147483647;
files = (
76D28ACE239FAF2C00FB142D /* SkeletonBatch.cpp in Sources */,
76D28ACF239FAF2C00FB142D /* SkeletonRenderer.cpp in Sources */,
76D28AD0239FAF2C00FB142D /* SkeletonTwoColorBatch.cpp in Sources */,
767D80E422B2A003000BD703 /* MixAndMatchExample.cpp in Sources */,
76798D1E22A95AEF00F77964 /* ConstraintData.cpp in Sources */,

View File

@ -28,8 +28,6 @@
*****************************************************************************/
#include <spine/spine-cocos2dx.h>
#if COCOS2D_VERSION < 0x00040000
#include <spine/Extension.h>
#include <spine/AttachmentVertices.h>
#include <algorithm>
@ -45,7 +43,7 @@ namespace spine {
int computeTotalCoordCount(Skeleton& skeleton, int startSlotIndex, int endSlotIndex);
cocos2d::Rect computeBoundingRect(const float* coords, int vertexCount);
void interleaveCoordinates(float* dst, const float* src, int vertexCount, int dstStride);
BlendFunc makeBlendFunc(int blendMode, bool premultipliedAlpha);
BlendFunc makeBlendFunc(BlendMode blendMode, bool premultipliedAlpha);
void transformWorldVertices(float* dstCoord, int coordCount, Skeleton& skeleton, int startSlotIndex, int endSlotIndex);
bool cullRectangle(Renderer* renderer, const Mat4& transform, const cocos2d::Rect& rect);
Color4B ColorToColor4B(const Color& color);
@ -95,7 +93,7 @@ namespace spine {
_blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
setOpacityModifyRGB(true);
setupGLProgramState(false);
setTwoColorTint(false);
_skeleton->setToSetupPose();
_skeleton->updateWorldTransform();
@ -103,7 +101,9 @@ namespace spine {
void SkeletonRenderer::setupGLProgramState (bool twoColorTintEnabled) {
if (twoColorTintEnabled) {
#if COCOS2D_VERSION < 0x00040000
setGLProgramState(SkeletonTwoColorBatch::getInstance()->getTwoColorTintProgramState());
#endif
return;
}
@ -127,7 +127,9 @@ namespace spine {
break;
}
}
#if COCOS2D_VERSION < 0x00040000
setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR_NO_MVP, texture));
#endif
}
void SkeletonRenderer::setSkeletonData (SkeletonData *skeletonData, bool ownsSkeletonData) {
@ -136,26 +138,26 @@ namespace spine {
}
SkeletonRenderer::SkeletonRenderer ()
: _atlas(nullptr), _attachmentLoader(nullptr), _debugSlots(false), _debugBones(false), _debugMeshes(false), _debugBoundingRect(false), _timeScale(1), _effect(nullptr), _startSlotIndex(0), _endSlotIndex(std::numeric_limits<int>::max()) {
: _atlas(nullptr), _attachmentLoader(nullptr), _timeScale(1), _debugSlots(false), _debugBones(false), _debugMeshes(false), _debugBoundingRect(false), _effect(nullptr), _startSlotIndex(0), _endSlotIndex(std::numeric_limits<int>::max()) {
}
SkeletonRenderer::SkeletonRenderer(Skeleton* skeleton, bool ownsSkeleton, bool ownsSkeletonData, bool ownsAtlas)
: _atlas(nullptr), _attachmentLoader(nullptr), _debugSlots(false), _debugBones(false), _debugMeshes(false), _debugBoundingRect(false), _timeScale(1), _effect(nullptr), _startSlotIndex(0), _endSlotIndex(std::numeric_limits<int>::max()) {
: _atlas(nullptr), _attachmentLoader(nullptr), _timeScale(1), _debugSlots(false), _debugBones(false), _debugMeshes(false), _debugBoundingRect(false), _effect(nullptr), _startSlotIndex(0), _endSlotIndex(std::numeric_limits<int>::max()) {
initWithSkeleton(skeleton, ownsSkeleton, ownsSkeletonData, ownsAtlas);
}
SkeletonRenderer::SkeletonRenderer (SkeletonData *skeletonData, bool ownsSkeletonData)
: _atlas(nullptr), _attachmentLoader(nullptr), _debugSlots(false), _debugBones(false), _debugMeshes(false), _debugBoundingRect(false), _timeScale(1), _effect(nullptr), _startSlotIndex(0), _endSlotIndex(std::numeric_limits<int>::max()) {
: _atlas(nullptr), _attachmentLoader(nullptr), _timeScale(1), _debugSlots(false), _debugBones(false), _debugMeshes(false), _debugBoundingRect(false), _effect(nullptr), _startSlotIndex(0), _endSlotIndex(std::numeric_limits<int>::max()) {
initWithData(skeletonData, ownsSkeletonData);
}
SkeletonRenderer::SkeletonRenderer (const std::string& skeletonDataFile, Atlas* atlas, float scale)
: _atlas(nullptr), _attachmentLoader(nullptr), _debugSlots(false), _debugBones(false), _debugMeshes(false), _debugBoundingRect(false), _timeScale(1), _effect(nullptr), _startSlotIndex(0), _endSlotIndex(std::numeric_limits<int>::max()) {
: _atlas(nullptr), _attachmentLoader(nullptr), _timeScale(1), _debugSlots(false), _debugBones(false), _debugMeshes(false), _debugBoundingRect(false), _effect(nullptr), _startSlotIndex(0), _endSlotIndex(std::numeric_limits<int>::max()) {
initWithJsonFile(skeletonDataFile, atlas, scale);
}
SkeletonRenderer::SkeletonRenderer (const std::string& skeletonDataFile, const std::string& atlasFile, float scale)
: _atlas(nullptr), _attachmentLoader(nullptr), _debugSlots(false), _debugBones(false), _debugMeshes(false), _debugBoundingRect(false), _timeScale(1), _effect(nullptr), _startSlotIndex(0), _endSlotIndex(std::numeric_limits<int>::max()) {
: _atlas(nullptr), _attachmentLoader(nullptr), _timeScale(1), _debugSlots(false), _debugBones(false), _debugMeshes(false), _debugBoundingRect(false), _effect(nullptr), _startSlotIndex(0), _endSlotIndex(std::numeric_limits<int>::max()) {
initWithJsonFile(skeletonDataFile, atlasFile, scale);
}
@ -449,7 +451,11 @@ namespace spine {
batch->allocateIndices(triangles.indexCount);
memcpy(triangles.indices, _clipper->getClippedTriangles().buffer(), sizeof(unsigned short) * _clipper->getClippedTriangles().size());
#if COCOS2D_VERSION < 0x00040000
cocos2d::TrianglesCommand* batchedTriangles = batch->addCommand(renderer, _globalZOrder, attachmentVertices->_texture, _glProgramState, blendFunc, triangles, transform, transformFlags);
#else
cocos2d::TrianglesCommand* batchedTriangles = batch->addCommand(renderer, _globalZOrder, attachmentVertices->_texture, blendFunc, triangles, transform, transformFlags);
#endif
const float* verts = _clipper->getClippedVertices().buffer();
const float* uvs = _clipper->getClippedUVs().buffer();
@ -477,8 +483,11 @@ namespace spine {
}
} else {
// Not clipping
#if COCOS2D_VERSION < 0x00040000
cocos2d::TrianglesCommand* batchedTriangles = batch->addCommand(renderer, _globalZOrder, attachmentVertices->_texture, _glProgramState, blendFunc, triangles, transform, transformFlags);
#else
cocos2d::TrianglesCommand* batchedTriangles = batch->addCommand(renderer, _globalZOrder, attachmentVertices->_texture, blendFunc, triangles, transform, transformFlags);
#endif
if (_effect) {
V3F_C4B_T2F* vertex = batchedTriangles->getTriangles().verts;
@ -513,7 +522,11 @@ namespace spine {
trianglesTwoColor.indices = twoColorBatch->allocateIndices(trianglesTwoColor.indexCount);
memcpy(trianglesTwoColor.indices, _clipper->getClippedTriangles().buffer(), sizeof(unsigned short) * _clipper->getClippedTriangles().size());
#if COCOS2D_VERSION < 0x00040000
TwoColorTrianglesCommand* batchedTriangles = lastTwoColorTrianglesCommand = twoColorBatch->addCommand(renderer, _globalZOrder, attachmentVertices->_texture->getName(), _glProgramState, blendFunc, trianglesTwoColor, transform, transformFlags);
#else
TwoColorTrianglesCommand* batchedTriangles = lastTwoColorTrianglesCommand = twoColorBatch->addCommand(renderer, _globalZOrder, attachmentVertices->_texture, blendFunc, trianglesTwoColor, transform, transformFlags);
#endif
const float* verts = _clipper->getClippedVertices().buffer();
const float* uvs = _clipper->getClippedUVs().buffer();
@ -543,7 +556,12 @@ namespace spine {
}
}
} else {
#if COCOS2D_VERSION < 0x00040000
TwoColorTrianglesCommand* batchedTriangles = lastTwoColorTrianglesCommand = twoColorBatch->addCommand(renderer, _globalZOrder, attachmentVertices->_texture->getName(), _glProgramState, blendFunc, trianglesTwoColor, transform, transformFlags);
#else
TwoColorTrianglesCommand* batchedTriangles = lastTwoColorTrianglesCommand = twoColorBatch->addCommand(renderer, _globalZOrder, attachmentVertices->_texture, blendFunc, trianglesTwoColor, transform, transformFlags);
#endif
if (_effect) {
V3F_C4B_C4B_T2F* vertex = batchedTriangles->getTriangles().verts;
@ -563,7 +581,7 @@ namespace spine {
}
}
}
_clipper->clipEnd(*slot);
_clipper->clipEnd(*slot);
}
_clipper->clipEnd();
@ -616,17 +634,21 @@ namespace spine {
void SkeletonRenderer::drawDebug (Renderer* renderer, const Mat4 &transform, uint32_t transformFlags) {
#if !defined(USE_MATRIX_STACK_PROJECTION_ONLY)
#if !defined(USE_MATRIX_STACK_PROJECTION_ONLY)
Director* director = Director::getInstance();
director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, transform);
#endif
#endif
DrawNode* drawNode = DrawNode::create();
// Draw bounding rectangle
if (_debugBoundingRect) {
#if COCOS2D_VERSION < 0x00040000
glLineWidth(2);
#else
drawNode->setLineWidth(2.0f);
#endif
const cocos2d::Rect brect = getBoundingBox();
const Vec2 points[4] =
{
@ -641,7 +663,11 @@ namespace spine {
if (_debugSlots) {
// Slots.
// DrawPrimitives::setDrawColor4B(0, 0, 255, 255);
glLineWidth(1);
#if COCOS2D_VERSION < 0x00040000
glLineWidth(2);
#else
drawNode->setLineWidth(2.0f);
#endif
V3F_C4B_T2F_Quad quad;
for (int i = 0, n = _skeleton->getSlots().size(); i < n; i++) {
Slot* slot = _skeleton->getDrawOrder()[i];
@ -669,7 +695,11 @@ namespace spine {
if (_debugBones) {
// Bone lengths.
#if COCOS2D_VERSION < 0x00040000
glLineWidth(2);
#else
drawNode->setLineWidth(2.0f);
#endif
for (int i = 0, n = _skeleton->getBones().size(); i < n; i++) {
Bone *bone = _skeleton->getBones()[i];
if (!bone->isActive()) continue;
@ -689,7 +719,11 @@ namespace spine {
if (_debugMeshes) {
// Meshes.
glLineWidth(1);
#if COCOS2D_VERSION < 0x00040000
glLineWidth(2);
#else
drawNode->setLineWidth(2.0f);
#endif
for (int i = 0, n = _skeleton->getSlots().size(); i < n; ++i) {
Slot* slot = _skeleton->getDrawOrder()[i];
if (!slot->getBone().isActive()) continue;
@ -715,9 +749,9 @@ namespace spine {
}
drawNode->draw(renderer, transform, transformFlags);
#if !defined(USE_MATRIX_STACK_PROJECTION_ONLY)
#if !defined(USE_MATRIX_STACK_PROJECTION_ONLY)
director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
#endif
#endif
}
cocos2d::Rect SkeletonRenderer::getBoundingBox () const {
@ -776,11 +810,18 @@ namespace spine {
}
void SkeletonRenderer::setTwoColorTint(bool enabled) {
#if COCOS2D_VERSION > 0x00040000
_twoColorTint = enabled;
#endif
setupGLProgramState(enabled);
}
bool SkeletonRenderer::isTwoColorTint() {
#if COCOS2D_VERSION < 0x00040000
return getGLProgramState() == SkeletonTwoColorBatch::getInstance()->getTwoColorTintProgramState();
#else
return _twoColorTint;
#endif
}
void SkeletonRenderer::setVertexEffect(VertexEffect *effect) {
@ -833,17 +874,17 @@ namespace spine {
}
void SkeletonRenderer::onEnter () {
#if CC_ENABLE_SCRIPT_BINDING
#if CC_ENABLE_SCRIPT_BINDING
if (_scriptType == kScriptTypeJavascript && ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnEnter)) return;
#endif
#endif
Node::onEnter();
scheduleUpdate();
}
void SkeletonRenderer::onExit () {
#if CC_ENABLE_SCRIPT_BINDING
#if CC_ENABLE_SCRIPT_BINDING
if (_scriptType == kScriptTypeJavascript && ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnExit)) return;
#endif
#endif
Node::onExit();
unscheduleUpdate();
}
@ -922,9 +963,9 @@ namespace spine {
void transformWorldVertices(float* dstCoord, int coordCount, Skeleton& skeleton, int startSlotIndex, int endSlotIndex) {
float* dstPtr = dstCoord;
#ifndef NDEBUG
#ifndef NDEBUG
float* const dstEnd = dstCoord + coordCount;
#endif
#endif
for (size_t i = 0; i < skeleton.getSlots().size(); ++i) {
/*const*/ Slot& slot = *skeleton.getDrawOrder()[i]; // match the draw order of SkeletonRenderer::Draw
Attachment* const attachment = slot.getAttachment();
@ -966,8 +1007,10 @@ namespace spine {
}
BlendFunc makeBlendFunc(int blendMode, bool premultipliedAlpha) {
BlendFunc makeBlendFunc(BlendMode blendMode, bool premultipliedAlpha) {
BlendFunc blendFunc;
#if COCOS2D_VERSION < 0x00040000
switch (blendMode) {
case BlendMode_Additive:
blendFunc.src = premultipliedAlpha ? GL_ONE : GL_SRC_ALPHA;
@ -986,6 +1029,25 @@ namespace spine {
blendFunc.dst = GL_ONE_MINUS_SRC_ALPHA;
break;
}
#else
switch (blendMode) {
case BlendMode_Additive:
blendFunc.src = premultipliedAlpha ? backend::BlendFactor::ONE : backend::BlendFactor::SRC_ALPHA;
blendFunc.dst = backend::BlendFactor::ONE;
break;
case BlendMode_Multiply:
blendFunc.src = backend::BlendFactor::DST_COLOR;
blendFunc.dst = backend::BlendFactor::ONE_MINUS_SRC_ALPHA;
break;
case BlendMode_Screen:
blendFunc.src = backend::BlendFactor::ONE;
blendFunc.dst = backend::BlendFactor::ONE_MINUS_SRC_COLOR;
break;
default:
blendFunc.src = premultipliedAlpha ? backend::BlendFactor::ONE : backend::BlendFactor::SRC_ALPHA;
blendFunc.dst = backend::BlendFactor::ONE_MINUS_SRC_ALPHA;
}
#endif
return blendFunc;
}
@ -1028,5 +1090,3 @@ namespace spine {
}
}
#endif

View File

@ -31,8 +31,6 @@
#define SPINE_SKELETONRENDERER_H_
#include "cocos2d.h"
#if COCOS2D_VERSION < 0x00040000
#include <spine/spine.h>
namespace spine {
@ -159,10 +157,9 @@ namespace spine {
int _startSlotIndex;
int _endSlotIndex;
bool _twoColorTint;
};
}
#endif
#endif /* SPINE_SKELETONRENDERER_H_ */

View File

@ -148,8 +148,7 @@ void SkeletonBatch::deallocateIndices(uint32_t numIndices) {
cocos2d::TrianglesCommand* SkeletonBatch::addCommand(cocos2d::Renderer* renderer, float globalOrder, cocos2d::Texture2D* texture, cocos2d::BlendFunc blendType, const cocos2d::TrianglesCommand::Triangles& triangles, const cocos2d::Mat4& mv, uint32_t flags) {
TrianglesCommand* command = nextFreeCommand();
const cocos2d::Mat4& projectionMat = Director::getInstance()->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
auto &pipelineDescriptor = command->getPipelineDescriptor();
const cocos2d::Mat4& projectionMat = Director::getInstance()->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
auto programState = command->getPipelineDescriptor().programState;
CCASSERT(programState, "programState should not be null");

View File

@ -1,910 +0,0 @@
/******************************************************************************
* Spine Runtimes License Agreement
* Last updated May 1, 2019. Replaces all prior versions.
*
* Copyright (c) 2013-2019, Esoteric Software LLC
*
* Integration of the Spine Runtimes into software or otherwise creating
* derivative works of the Spine Runtimes is permitted under the terms and
* conditions of Section 2 of the Spine Editor License Agreement:
* http://esotericsoftware.com/spine-editor-license
*
* Otherwise, it is permitted to integrate the Spine Runtimes into software
* or otherwise create derivative works of the Spine Runtimes (collectively,
* "Products"), provided that each user of the Products must obtain their own
* Spine Editor license and redistribution of the Products in any form must
* include this license and copyright notice.
*
* THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
* NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, BUSINESS
* INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/
#include <spine/spine-cocos2dx.h>
#if COCOS2D_VERSION >= 0x00040000
#include <spine/Extension.h>
#include <spine/AttachmentVertices.h>
#include <algorithm>
#include "renderer/backend/Types.h"
#define INITIAL_WORLD_VERTICES_LENGTH 1000
// Used for transforming attachments for bounding boxes & debug rendering
static float* worldVertices = nullptr;
static size_t worldVerticesLength = 0;
void ensureWorldVerticesCapacity(size_t capacity) {
if (worldVerticesLength < capacity) {
float* newWorldVertices = new float[capacity];
memcpy(newWorldVertices, worldVertices, capacity * sizeof(float));
delete[] worldVertices;
worldVertices = newWorldVertices;
worldVerticesLength = capacity;
}
}
USING_NS_CC;
using std::min;
using std::max;
namespace spine {
static Cocos2dTextureLoader textureLoader;
void SkeletonRenderer::destroyScratchBuffers() {
if (worldVertices) {
delete[] worldVertices;
worldVertices = nullptr;
worldVerticesLength = 0;
}
}
SkeletonRenderer* SkeletonRenderer::createWithSkeleton(Skeleton* skeleton, bool ownsSkeleton, bool ownsSkeletonData) {
SkeletonRenderer* node = new SkeletonRenderer(skeleton, ownsSkeleton, ownsSkeletonData);
node->autorelease();
return node;
}
SkeletonRenderer* SkeletonRenderer::createWithData (SkeletonData* skeletonData, bool ownsSkeletonData) {
SkeletonRenderer* node = new SkeletonRenderer(skeletonData, ownsSkeletonData);
node->autorelease();
return node;
}
SkeletonRenderer* SkeletonRenderer::createWithFile (const std::string& skeletonDataFile, Atlas* atlas, float scale) {
SkeletonRenderer* node = new SkeletonRenderer(skeletonDataFile, atlas, scale);
node->autorelease();
return node;
}
SkeletonRenderer* SkeletonRenderer::createWithFile (const std::string& skeletonDataFile, const std::string& atlasFile, float scale) {
SkeletonRenderer* node = new SkeletonRenderer(skeletonDataFile, atlasFile, scale);
node->autorelease();
return node;
}
void SkeletonRenderer::initialize () {
if (!worldVertices) {
worldVertices = new float[INITIAL_WORLD_VERTICES_LENGTH];
worldVerticesLength = INITIAL_WORLD_VERTICES_LENGTH;
}
_clipper = new (__FILE__, __LINE__) SkeletonClipping();
_blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
setOpacityModifyRGB(true);
setupGLProgramState(false);
_skeleton->setToSetupPose();
_skeleton->updateWorldTransform();
}
void SkeletonRenderer::setupGLProgramState (bool twoColorTintEnabled) {
_twoColorTintEnabled = twoColorTintEnabled;
if (twoColorTintEnabled) {
return;
}
Texture2D *texture = nullptr;
for (int i = 0, n = _skeleton->getSlots().size(); i < n; i++) {
Slot* slot = _skeleton->getDrawOrder()[i];
if (!slot->getAttachment()) continue;
if (slot->getAttachment()->getRTTI().isExactly(RegionAttachment::rtti)) {
RegionAttachment* attachment = (RegionAttachment*)slot->getAttachment();
texture = static_cast<AttachmentVertices*>(attachment->getRendererObject())->_texture;
} else if (slot->getAttachment()->getRTTI().isExactly(RegionAttachment::rtti)) {
MeshAttachment* attachment = (MeshAttachment*)slot->getAttachment();
texture = static_cast<AttachmentVertices*>(attachment->getRendererObject())->_texture;
} else {
continue;
}
if (texture != nullptr) {
break;
}
}
//setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR_NO_MVP, texture));
}
void SkeletonRenderer::setSkeletonData (SkeletonData *skeletonData, bool ownsSkeletonData) {
_skeleton = new (__FILE__, __LINE__) Skeleton(skeletonData);
_ownsSkeletonData = ownsSkeletonData;
}
SkeletonRenderer::SkeletonRenderer ()
: _atlas(nullptr), _attachmentLoader(nullptr), _debugSlots(false), _debugBones(false), _debugMeshes(false), _timeScale(1), _effect(nullptr), _startSlotIndex(-1), _endSlotIndex(-1) {
}
SkeletonRenderer::SkeletonRenderer(Skeleton* skeleton, bool ownsSkeleton, bool ownsSkeletonData, bool ownsAtlas)
: _atlas(nullptr), _attachmentLoader(nullptr), _debugSlots(false), _debugBones(false), _debugMeshes(false), _timeScale(1), _effect(nullptr), _startSlotIndex(-1), _endSlotIndex(-1) {
initWithSkeleton(skeleton, ownsSkeleton, ownsSkeletonData, ownsAtlas);
}
SkeletonRenderer::SkeletonRenderer (SkeletonData *skeletonData, bool ownsSkeletonData)
: _atlas(nullptr), _attachmentLoader(nullptr), _debugSlots(false), _debugBones(false), _debugMeshes(false), _timeScale(1), _effect(nullptr), _startSlotIndex(-1), _endSlotIndex(-1) {
initWithData(skeletonData, ownsSkeletonData);
}
SkeletonRenderer::SkeletonRenderer (const std::string& skeletonDataFile, Atlas* atlas, float scale)
: _atlas(nullptr), _attachmentLoader(nullptr), _debugSlots(false), _debugBones(false), _debugMeshes(false), _timeScale(1), _effect(nullptr), _startSlotIndex(-1), _endSlotIndex(-1) {
initWithJsonFile(skeletonDataFile, atlas, scale);
}
SkeletonRenderer::SkeletonRenderer (const std::string& skeletonDataFile, const std::string& atlasFile, float scale)
: _atlas(nullptr), _attachmentLoader(nullptr), _debugSlots(false), _debugBones(false), _debugMeshes(false), _timeScale(1), _effect(nullptr), _startSlotIndex(-1), _endSlotIndex(-1) {
initWithJsonFile(skeletonDataFile, atlasFile, scale);
}
SkeletonRenderer::~SkeletonRenderer () {
if (_ownsSkeletonData) delete _skeleton->getData();
if (_ownsSkeleton) delete _skeleton;
if (_ownsAtlas && _atlas) delete _atlas;
if (_attachmentLoader) delete _attachmentLoader;
delete _clipper;
}
void SkeletonRenderer::initWithSkeleton(Skeleton* skeleton, bool ownsSkeleton, bool ownsSkeletonData, bool ownsAtlas) {
_skeleton = skeleton;
_ownsSkeleton = ownsSkeleton;
_ownsSkeletonData = ownsSkeletonData;
_ownsAtlas = ownsAtlas;
initialize();
}
void SkeletonRenderer::initWithData (SkeletonData* skeletonData, bool ownsSkeletonData) {
_ownsSkeleton = true;
setSkeletonData(skeletonData, ownsSkeletonData);
initialize();
}
void SkeletonRenderer::initWithJsonFile (const std::string& skeletonDataFile, Atlas* atlas, float scale) {
_atlas = atlas;
_attachmentLoader = new (__FILE__, __LINE__) Cocos2dAtlasAttachmentLoader(_atlas);
SkeletonJson* json = new (__FILE__, __LINE__) SkeletonJson(_attachmentLoader);
json->setScale(scale);
SkeletonData* skeletonData = json->readSkeletonDataFile(skeletonDataFile.c_str());
CCASSERT(skeletonData, !json->getError().isEmpty() ? json->getError().buffer() : "Error reading skeleton data.");
delete json;
_ownsSkeleton = true;
setSkeletonData(skeletonData, true);
initialize();
}
void SkeletonRenderer::initWithJsonFile (const std::string& skeletonDataFile, const std::string& atlasFile, float scale) {
_atlas = new (__FILE__, __LINE__) Atlas(atlasFile.c_str(), &textureLoader);
CCASSERT(_atlas, "Error reading atlas file.");
_attachmentLoader = new (__FILE__, __LINE__) Cocos2dAtlasAttachmentLoader(_atlas);
SkeletonJson* json = new (__FILE__, __LINE__) SkeletonJson(_attachmentLoader);
json->setScale(scale);
SkeletonData* skeletonData = json->readSkeletonDataFile(skeletonDataFile.c_str());
CCASSERT(skeletonData, !json->getError().isEmpty() ? json->getError().buffer() : "Error reading skeleton data.");
delete json;
_ownsSkeleton = true;
_ownsAtlas = true;
setSkeletonData(skeletonData, true);
initialize();
}
void SkeletonRenderer::initWithBinaryFile (const std::string& skeletonDataFile, Atlas* atlas, float scale) {
_atlas = atlas;
_attachmentLoader = new (__FILE__, __LINE__) Cocos2dAtlasAttachmentLoader(_atlas);
SkeletonBinary* binary = new (__FILE__, __LINE__) SkeletonBinary(_attachmentLoader);
binary->setScale(scale);
SkeletonData* skeletonData = binary->readSkeletonDataFile(skeletonDataFile.c_str());
CCASSERT(skeletonData, !binary->getError().isEmpty() ? binary->getError().buffer() : "Error reading skeleton data.");
delete binary;
_ownsSkeleton = true;
setSkeletonData(skeletonData, true);
initialize();
}
void SkeletonRenderer::initWithBinaryFile (const std::string& skeletonDataFile, const std::string& atlasFile, float scale) {
_atlas = new (__FILE__, __LINE__) Atlas(atlasFile.c_str(), &textureLoader);
CCASSERT(_atlas, "Error reading atlas file.");
_attachmentLoader = new (__FILE__, __LINE__) Cocos2dAtlasAttachmentLoader(_atlas);
SkeletonBinary* binary = new (__FILE__, __LINE__) SkeletonBinary(_attachmentLoader);
binary->setScale(scale);
SkeletonData* skeletonData = binary->readSkeletonDataFile(skeletonDataFile.c_str());
CCASSERT(skeletonData, !binary->getError().isEmpty() ? binary->getError().buffer() : "Error reading skeleton data.");
delete binary;
_ownsSkeleton = true;
_ownsAtlas = true;
setSkeletonData(skeletonData, true);
initialize();
}
void SkeletonRenderer::update (float deltaTime) {
Node::update(deltaTime);
if (_ownsSkeleton) _skeleton->update(deltaTime * _timeScale);
}
void SkeletonRenderer::draw (Renderer* renderer, const Mat4& transform, uint32_t transformFlags) {
SkeletonBatch* batch = SkeletonBatch::getInstance();
SkeletonTwoColorBatch* twoColorBatch = SkeletonTwoColorBatch::getInstance();
bool isTwoColorTint = this->isTwoColorTint();
// Early exit if the skeleton is invisible
if (getDisplayedOpacity() == 0 || _skeleton->getColor().a == 0){
return;
}
if (_effect) _effect->begin(*_skeleton);
Color4F nodeColor;
nodeColor.r = getDisplayedColor().r / (float)255;
nodeColor.g = getDisplayedColor().g / (float)255;
nodeColor.b = getDisplayedColor().b / (float)255;
nodeColor.a = getDisplayedOpacity() / (float)255;
Color4F color;
Color4F darkColor;
float darkPremultipliedAlpha = _premultipliedAlpha ? 255 : 0;
AttachmentVertices* attachmentVertices = nullptr;
TwoColorTrianglesCommand* lastTwoColorTrianglesCommand = nullptr;
bool inRange = _startSlotIndex != -1 || _endSlotIndex != -1 ? false : true;
for (int i = 0, n = _skeleton->getSlots().size(); i < n; ++i) {
Slot* slot = _skeleton->getDrawOrder()[i];
if (_startSlotIndex >= 0 && _startSlotIndex == slot->getData().getIndex()) {
inRange = true;
}
if (!inRange) {
_clipper->clipEnd(*slot);
continue;
}
if (_endSlotIndex >= 0 && _endSlotIndex == slot->getData().getIndex()) {
inRange = false;
}
if (!slot->getAttachment()) {
_clipper->clipEnd(*slot);
continue;
}
// Early exit if slot is invisible
if (slot->getColor().a == 0) {
_clipper->clipEnd(*slot);
continue;
}
cocos2d::TrianglesCommand::Triangles triangles;
TwoColorTriangles trianglesTwoColor;
if (slot->getAttachment()->getRTTI().isExactly(RegionAttachment::rtti)) {
RegionAttachment* attachment = (RegionAttachment*)slot->getAttachment();
attachmentVertices = (AttachmentVertices*)attachment->getRendererObject();
// Early exit if attachment is invisible
if (attachment->getColor().a == 0) {
_clipper->clipEnd(*slot);
continue;
}
if (!isTwoColorTint) {
triangles.indices = attachmentVertices->_triangles->indices;
triangles.indexCount = attachmentVertices->_triangles->indexCount;
triangles.verts = batch->allocateVertices(attachmentVertices->_triangles->vertCount);
triangles.vertCount = attachmentVertices->_triangles->vertCount;
memcpy(triangles.verts, attachmentVertices->_triangles->verts, sizeof(cocos2d::V3F_C4B_T2F) * attachmentVertices->_triangles->vertCount);
attachment->computeWorldVertices(slot->getBone(), (float*)triangles.verts, 0, 6);
} else {
trianglesTwoColor.indices = attachmentVertices->_triangles->indices;
trianglesTwoColor.indexCount = attachmentVertices->_triangles->indexCount;
trianglesTwoColor.verts = twoColorBatch->allocateVertices(attachmentVertices->_triangles->vertCount);
trianglesTwoColor.vertCount = attachmentVertices->_triangles->vertCount;
for (int i = 0; i < trianglesTwoColor.vertCount; i++) {
trianglesTwoColor.verts[i].texCoords = attachmentVertices->_triangles->verts[i].texCoords;
}
attachment->computeWorldVertices(slot->getBone(), (float*)trianglesTwoColor.verts, 0, 7);
}
color.r = attachment->getColor().r;
color.g = attachment->getColor().g;
color.b = attachment->getColor().b;
color.a = attachment->getColor().a;
}
else if (slot->getAttachment()->getRTTI().isExactly(MeshAttachment::rtti)) {
MeshAttachment* attachment = (MeshAttachment*)slot->getAttachment();
attachmentVertices = (AttachmentVertices*)attachment->getRendererObject();
// Early exit if attachment is invisible
if (attachment->getColor().a == 0) {
_clipper->clipEnd(*slot);
continue;
}
if (!isTwoColorTint) {
triangles.indices = attachmentVertices->_triangles->indices;
triangles.indexCount = attachmentVertices->_triangles->indexCount;
triangles.verts = batch->allocateVertices(attachmentVertices->_triangles->vertCount);
triangles.vertCount = attachmentVertices->_triangles->vertCount;
memcpy(triangles.verts, attachmentVertices->_triangles->verts, sizeof(cocos2d::V3F_C4B_T2F) * attachmentVertices->_triangles->vertCount);
attachment->computeWorldVertices(*slot, 0, attachment->getWorldVerticesLength(), (float*)triangles.verts, 0, 6);
} else {
trianglesTwoColor.indices = attachmentVertices->_triangles->indices;
trianglesTwoColor.indexCount = attachmentVertices->_triangles->indexCount;
trianglesTwoColor.verts = twoColorBatch->allocateVertices(attachmentVertices->_triangles->vertCount);
trianglesTwoColor.vertCount = attachmentVertices->_triangles->vertCount;
for (int i = 0; i < trianglesTwoColor.vertCount; i++) {
trianglesTwoColor.verts[i].texCoords = attachmentVertices->_triangles->verts[i].texCoords;
}
attachment->computeWorldVertices(*slot, 0, attachment->getWorldVerticesLength(), (float*)trianglesTwoColor.verts, 0, 7);
}
color.r = attachment->getColor().r;
color.g = attachment->getColor().g;
color.b = attachment->getColor().b;
color.a = attachment->getColor().a;
}
else if (slot->getAttachment()->getRTTI().isExactly(ClippingAttachment::rtti)) {
ClippingAttachment* clip = (ClippingAttachment*)slot->getAttachment();
_clipper->clipStart(*slot, clip);
continue;
} else {
_clipper->clipEnd(*slot);
continue;
}
float alpha = nodeColor.a * _skeleton->getColor().a * slot->getColor().a * color.a * 255;
// skip rendering if the color of this attachment is 0
if (alpha == 0){
_clipper->clipEnd(*slot);
continue;
}
float multiplier = _premultipliedAlpha ? alpha : 255;
float red = nodeColor.r * _skeleton->getColor().r * color.r * multiplier;
float green = nodeColor.g * _skeleton->getColor().g * color.g * multiplier;
float blue = nodeColor.b * _skeleton->getColor().b * color.b * multiplier;
color.r = red * slot->getColor().r;
color.g = green * slot->getColor().g;
color.b = blue * slot->getColor().b;
color.a = alpha;
if (slot->hasDarkColor()) {
darkColor.r = red * slot->getDarkColor().r;
darkColor.g = green * slot->getDarkColor().g;
darkColor.b = blue * slot->getDarkColor().b;
} else {
darkColor.r = 0;
darkColor.g = 0;
darkColor.b = 0;
}
darkColor.a = darkPremultipliedAlpha;
BlendFunc blendFunc;
switch (slot->getData().getBlendMode()) {
case BlendMode_Additive:
blendFunc.src = _premultipliedAlpha ? backend::BlendFactor::ONE : backend::BlendFactor::SRC_ALPHA;
blendFunc.dst = backend::BlendFactor::ONE;
break;
case BlendMode_Multiply:
blendFunc.src = backend::BlendFactor::DST_COLOR;
blendFunc.dst = backend::BlendFactor::ONE_MINUS_SRC_ALPHA;
break;
case BlendMode_Screen:
blendFunc.src = backend::BlendFactor::ONE;
blendFunc.dst = backend::BlendFactor::ONE_MINUS_SRC_COLOR;
break;
default:
blendFunc.src = _premultipliedAlpha ? backend::BlendFactor::ONE : backend::BlendFactor::SRC_ALPHA;
blendFunc.dst = backend::BlendFactor::ONE_MINUS_SRC_ALPHA;
}
if (!isTwoColorTint) {
if (_clipper->isClipping()) {
_clipper->clipTriangles((float*)&triangles.verts[0].vertices, triangles.indices, triangles.indexCount, (float*)&triangles.verts[0].texCoords, sizeof(cocos2d::V3F_C4B_T2F) / 4);
batch->deallocateVertices(triangles.vertCount);
if (_clipper->getClippedTriangles().size() == 0){
_clipper->clipEnd(*slot);
continue;
}
triangles.vertCount = _clipper->getClippedVertices().size() >> 1;
triangles.verts = batch->allocateVertices(triangles.vertCount);
triangles.indexCount = _clipper->getClippedTriangles().size();
triangles.indices = batch->allocateIndices(triangles.indexCount);
memcpy(triangles.indices, _clipper->getClippedTriangles().buffer(), sizeof(unsigned short) * _clipper->getClippedTriangles().size());
cocos2d::TrianglesCommand* batchedTriangles = batch->addCommand(renderer, _globalZOrder, attachmentVertices->_texture, blendFunc, triangles, transform, transformFlags);
float* verts = _clipper->getClippedVertices().buffer();
float* uvs = _clipper->getClippedUVs().buffer();
if (_effect) {
Color light;
Color dark;
light.r = color.r / 255.0f;
light.g = color.g / 255.0f;
light.b = color.b / 255.0f;
light.a = color.a / 255.0f;
dark.r = dark.g = dark.b = dark.a = 0;
for (int v = 0, vn = batchedTriangles->getTriangles().vertCount, vv = 0; v < vn; ++v, vv+=2) {
V3F_C4B_T2F* vertex = batchedTriangles->getTriangles().verts + v;
Color lightCopy = light;
Color darkCopy = dark;
vertex->vertices.x = verts[vv];
vertex->vertices.y = verts[vv + 1];
vertex->texCoords.u = uvs[vv];
vertex->texCoords.v = uvs[vv + 1];
_effect->transform(vertex->vertices.x, vertex->vertices.y, vertex->texCoords.u, vertex->texCoords.v, lightCopy, darkCopy);
vertex->colors.r = (uint8_t)(lightCopy.r * 255);
vertex->colors.g = (uint8_t)(lightCopy.g * 255);
vertex->colors.b = (uint8_t)(lightCopy.b * 255);
vertex->colors.a = (uint8_t)(lightCopy.a * 255);
}
} else {
for (int v = 0, vn = batchedTriangles->getTriangles().vertCount, vv = 0; v < vn; ++v, vv+=2) {
V3F_C4B_T2F* vertex = batchedTriangles->getTriangles().verts + v;
vertex->vertices.x = verts[vv];
vertex->vertices.y = verts[vv + 1];
vertex->texCoords.u = uvs[vv];
vertex->texCoords.v = uvs[vv + 1];
vertex->colors.r = (uint8_t)color.r;
vertex->colors.g = (uint8_t)color.g;
vertex->colors.b = (uint8_t)color.b;
vertex->colors.a = (uint8_t)color.a;
}
}
} else {
cocos2d::TrianglesCommand* batchedTriangles = batch->addCommand(renderer, _globalZOrder, attachmentVertices->_texture, blendFunc, triangles, transform, transformFlags);
if (_effect) {
Color light;
Color dark;
light.r = color.r / 255.0f;
light.g = color.g / 255.0f;
light.b = color.b / 255.0f;
light.a = color.a / 255.0f;
dark.r = dark.g = dark.b = dark.a = 0;
for (int v = 0, vn = batchedTriangles->getTriangles().vertCount; v < vn; ++v) {
V3F_C4B_T2F* vertex = batchedTriangles->getTriangles().verts + v;
Color lightCopy = light;
Color darkCopy = dark;
_effect->transform(vertex->vertices.x, vertex->vertices.y, vertex->texCoords.u, vertex->texCoords.v, lightCopy, darkCopy);
vertex->colors.r = (uint8_t)(lightCopy.r * 255);
vertex->colors.g = (uint8_t)(lightCopy.g * 255);
vertex->colors.b = (uint8_t)(lightCopy.b * 255);
vertex->colors.a = (uint8_t)(lightCopy.a * 255);
}
} else {
for (int v = 0, vn = batchedTriangles->getTriangles().vertCount; v < vn; ++v) {
V3F_C4B_T2F* vertex = batchedTriangles->getTriangles().verts + v;
vertex->colors.r = (uint8_t)color.r;
vertex->colors.g = (uint8_t)color.g;
vertex->colors.b = (uint8_t)color.b;
vertex->colors.a = (uint8_t)color.a;
}
}
}
} else {
if (_clipper->isClipping()) {
_clipper->clipTriangles((float*)&trianglesTwoColor.verts[0].position, trianglesTwoColor.indices, trianglesTwoColor.indexCount, (float*)&trianglesTwoColor.verts[0].texCoords, sizeof(V3F_C4B_C4B_T2F) / 4);
twoColorBatch->deallocateVertices(trianglesTwoColor.vertCount);
if (_clipper->getClippedTriangles().size() == 0){
_clipper->clipEnd(*slot);
continue;
}
trianglesTwoColor.vertCount = _clipper->getClippedVertices().size() >> 1;
trianglesTwoColor.verts = twoColorBatch->allocateVertices(trianglesTwoColor.vertCount);
trianglesTwoColor.indexCount = _clipper->getClippedTriangles().size();
trianglesTwoColor.indices = twoColorBatch->allocateIndices(trianglesTwoColor.indexCount);
memcpy(trianglesTwoColor.indices, _clipper->getClippedTriangles().buffer(), sizeof(unsigned short) * _clipper->getClippedTriangles().size());
TwoColorTrianglesCommand* batchedTriangles = lastTwoColorTrianglesCommand = twoColorBatch->addCommand(renderer, _globalZOrder, attachmentVertices->_texture, blendFunc, trianglesTwoColor, transform, transformFlags);
float* verts = _clipper->getClippedVertices().buffer();
float* uvs = _clipper->getClippedUVs().buffer();
if (_effect) {
Color light;
Color dark;
light.r = color.r / 255.0f;
light.g = color.g / 255.0f;
light.b = color.b / 255.0f;
light.a = color.a / 255.0f;
dark.r = darkColor.r / 255.0f;
dark.g = darkColor.g / 255.0f;
dark.b = darkColor.b / 255.0f;
dark.a = darkColor.a / 255.0f;
for (int v = 0, vn = batchedTriangles->getTriangles().vertCount, vv = 0; v < vn; ++v, vv += 2) {
V3F_C4B_C4B_T2F* vertex = batchedTriangles->getTriangles().verts + v;
Color lightCopy = light;
Color darkCopy = dark;
vertex->position.x = verts[vv];
vertex->position.y = verts[vv + 1];
vertex->texCoords.u = uvs[vv];
vertex->texCoords.v = uvs[vv + 1];
_effect->transform(vertex->position.x, vertex->position.y, vertex->texCoords.u, vertex->texCoords.v, lightCopy, darkCopy);
vertex->color.r = (uint8_t)(lightCopy.r * 255);
vertex->color.g = (uint8_t)(lightCopy.g * 255);
vertex->color.b = (uint8_t)(lightCopy.b * 255);
vertex->color.a = (uint8_t)(lightCopy.a * 255);
vertex->color2.r = (uint8_t)(darkCopy.r * 255);
vertex->color2.g = (uint8_t)(darkCopy.g * 255);
vertex->color2.b = (uint8_t)(darkCopy.b * 255);
vertex->color2.a = (uint8_t)darkColor.a;
}
} else {
for (int v = 0, vn = batchedTriangles->getTriangles().vertCount, vv = 0; v < vn; ++v, vv += 2) {
V3F_C4B_C4B_T2F* vertex = batchedTriangles->getTriangles().verts + v;
vertex->position.x = verts[vv];
vertex->position.y = verts[vv + 1];
vertex->texCoords.u = uvs[vv];
vertex->texCoords.v = uvs[vv + 1];
vertex->color.r = (uint8_t)color.r;
vertex->color.g = (uint8_t)color.g;
vertex->color.b = (uint8_t)color.b;
vertex->color.a = (uint8_t)color.a;
vertex->color2.r = (uint8_t)darkColor.r;
vertex->color2.g = (uint8_t)darkColor.g;
vertex->color2.b = (uint8_t)darkColor.b;
vertex->color2.a = (uint8_t)darkColor.a;
}
}
} else {
TwoColorTrianglesCommand* batchedTriangles = lastTwoColorTrianglesCommand = twoColorBatch->addCommand(renderer, _globalZOrder, attachmentVertices->_texture, blendFunc, trianglesTwoColor, transform, transformFlags);
if (_effect) {
Color light;
Color dark;
light.r = color.r / 255.0f;
light.g = color.g / 255.0f;
light.b = color.b / 255.0f;
light.a = color.a / 255.0f;
dark.r = darkColor.r / 255.0f;
dark.g = darkColor.g / 255.0f;
dark.b = darkColor.b / 255.0f;
dark.a = darkColor.a / 255.0f;
for (int v = 0, vn = batchedTriangles->getTriangles().vertCount; v < vn; ++v) {
V3F_C4B_C4B_T2F* vertex = batchedTriangles->getTriangles().verts + v;
Color lightCopy = light;
Color darkCopy = dark;
_effect->transform(vertex->position.x, vertex->position.y, vertex->texCoords.u, vertex->texCoords.v, lightCopy, darkCopy);
vertex->color.r = (uint8_t)(lightCopy.r * 255);
vertex->color.g = (uint8_t)(lightCopy.g * 255);
vertex->color.b = (uint8_t)(lightCopy.b * 255);
vertex->color.a = (uint8_t)(lightCopy.a * 255);
vertex->color2.r = (uint8_t)(darkCopy.r * 255);
vertex->color2.g = (uint8_t)(darkCopy.g * 255);
vertex->color2.b = (uint8_t)(darkCopy.b * 255);
vertex->color2.a = (uint8_t)darkColor.a;
}
} else {
for (int v = 0, vn = batchedTriangles->getTriangles().vertCount; v < vn; ++v) {
V3F_C4B_C4B_T2F* vertex = batchedTriangles->getTriangles().verts + v;
vertex->color.r = (uint8_t)color.r;
vertex->color.g = (uint8_t)color.g;
vertex->color.b = (uint8_t)color.b;
vertex->color.a = (uint8_t)color.a;
vertex->color2.r = (uint8_t)darkColor.r;
vertex->color2.g = (uint8_t)darkColor.g;
vertex->color2.b = (uint8_t)darkColor.b;
vertex->color2.a = (uint8_t)darkColor.a;
}
}
}
}
_clipper->clipEnd(*slot);
}
_clipper->clipEnd();
if (lastTwoColorTrianglesCommand) {
Node* parent = this->getParent();
// We need to decide if we can postpone flushing the current
// batch. We can postpone if the next sibling node is a
// two color tinted skeleton with the same global-z.
// The parent->getChildrenCount() > 100 check is a hack
// as checking for a sibling is an O(n) operation, and if
// all children of this nodes parent are skeletons, we
// are in O(n2) territory.
if (!parent || parent->getChildrenCount() > 100 || getChildrenCount() != 0) {
lastTwoColorTrianglesCommand->setForceFlush(true);
} else {
cocos2d::Vector<Node*>& children = parent->getChildren();
Node* sibling = nullptr;
for (ssize_t i = 0; i < children.size(); i++) {
if (children.at(i) == this) {
if (i < children.size() - 1) {
sibling = children.at(i+1);
break;
}
}
}
if (!sibling) {
lastTwoColorTrianglesCommand->setForceFlush(true);
} else {
SkeletonRenderer* siblingSkeleton = dynamic_cast<SkeletonRenderer*>(sibling);
if (!siblingSkeleton || // flush is next sibling isn't a SkeletonRenderer
!siblingSkeleton->isTwoColorTint() || // flush if next sibling isn't two color tinted
!siblingSkeleton->isVisible() || // flush if next sibling is two color tinted but not visible
(siblingSkeleton->getGlobalZOrder() != this->getGlobalZOrder())) { // flush if next sibling is two color tinted but z-order differs
lastTwoColorTrianglesCommand->setForceFlush(true);
}
}
}
}
if (_effect) _effect->end();
if (_debugSlots || _debugBones || _debugMeshes) {
drawDebug(renderer, transform, transformFlags);
}
}
void SkeletonRenderer::drawDebug (Renderer* renderer, const Mat4 &transform, uint32_t transformFlags) {
Director* director = Director::getInstance();
director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, transform);
DrawNode* drawNode = DrawNode::create();
if (_debugSlots) {
// Slots.
// DrawPrimitives::setDrawColor4B(0, 0, 255, 255);
drawNode->setLineWidth(1.0f);
Vec2 points[4];
V3F_C4B_T2F_Quad quad;
for (int i = 0, n = _skeleton->getSlots().size(); i < n; i++) {
Slot* slot = _skeleton->getDrawOrder()[i];
if (!slot->getAttachment() || !slot->getAttachment()->getRTTI().isExactly(RegionAttachment::rtti)) continue;
RegionAttachment* attachment = (RegionAttachment*)slot->getAttachment();
attachment->computeWorldVertices(slot->getBone(), worldVertices, 0, 2);
points[0] = Vec2(worldVertices[0], worldVertices[1]);
points[1] = Vec2(worldVertices[2], worldVertices[3]);
points[2] = Vec2(worldVertices[4], worldVertices[5]);
points[3] = Vec2(worldVertices[6], worldVertices[7]);
drawNode->drawPoly(points, 4, true, Color4F::BLUE);
}
}
if (_debugBones) {
// Bone lengths.
drawNode->setLineWidth(2.0f);
for (int i = 0, n = _skeleton->getBones().size(); i < n; i++) {
Bone *bone = _skeleton->getBones()[i];
float x = bone->getData().getLength() * bone->getA() + bone->getWorldX();
float y = bone->getData().getLength() * bone->getC() + bone->getWorldY();
drawNode->drawLine(Vec2(bone->getWorldX(), bone->getWorldY()), Vec2(x, y), Color4F::RED);
}
// Bone origins.
auto color = Color4F::BLUE; // Root bone is blue.
for (int i = 0, n = _skeleton->getBones().size(); i < n; i++) {
Bone *bone = _skeleton->getBones()[i];
drawNode->drawPoint(Vec2(bone->getWorldX(), bone->getWorldY()), 4, color);
if (i == 0) color = Color4F::GREEN;
}
}
if (_debugMeshes) {
// Meshes.
drawNode->setLineWidth(1.0f);
for (int i = 0, n = _skeleton->getSlots().size(); i < n; ++i) {
Slot* slot = _skeleton->getDrawOrder()[i];
if (!slot->getAttachment() || !slot->getAttachment()->getRTTI().isExactly(MeshAttachment::rtti)) continue;
MeshAttachment* attachment = (MeshAttachment*)slot->getAttachment();
ensureWorldVerticesCapacity(attachment->getWorldVerticesLength());
attachment->computeWorldVertices(*slot, 0, attachment->getWorldVerticesLength(), worldVertices, 0, 2);
for (int ii = 0; ii < attachment->getTriangles().size();) {
Vec2 v1(worldVertices + (attachment->getTriangles()[ii++] * 2));
Vec2 v2(worldVertices + (attachment->getTriangles()[ii++] * 2));
Vec2 v3(worldVertices + (attachment->getTriangles()[ii++] * 2));
drawNode->drawLine(v1, v2, Color4F::YELLOW);
drawNode->drawLine(v2, v3, Color4F::YELLOW);
drawNode->drawLine(v3, v1, Color4F::YELLOW);
}
}
}
drawNode->draw(renderer, transform, transformFlags);
director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
}
Rect SkeletonRenderer::getBoundingBox () const {
float minX = FLT_MAX, minY = FLT_MAX, maxX = -FLT_MAX, maxY = -FLT_MAX;
float scaleX = getScaleX(), scaleY = getScaleY();
for (int i = 0; i < _skeleton->getSlots().size(); ++i) {
Slot* slot = _skeleton->getSlots()[i];
if (!slot->getAttachment()) continue;
int verticesCount;
if (slot->getAttachment()->getRTTI().isExactly(RegionAttachment::rtti)) {
RegionAttachment* attachment = (RegionAttachment*)slot->getAttachment();
attachment->computeWorldVertices(slot->getBone(), worldVertices, 0, 2);
verticesCount = 8;
} else if (slot->getAttachment()->getRTTI().isExactly(MeshAttachment::rtti)) {
MeshAttachment* mesh = (MeshAttachment*)slot->getAttachment();
ensureWorldVerticesCapacity(mesh->getWorldVerticesLength());
mesh->computeWorldVertices(*slot, 0, mesh->getWorldVerticesLength(), worldVertices, 0, 2);
verticesCount = mesh->getWorldVerticesLength();
} else
continue;
for (int ii = 0; ii < verticesCount; ii += 2) {
float x = worldVertices[ii] * scaleX, y = worldVertices[ii + 1] * scaleY;
minX = min(minX, x);
minY = min(minY, y);
maxX = max(maxX, x);
maxY = max(maxY, y);
}
}
Vec2 position = getPosition();
if (minX == FLT_MAX) minX = minY = maxX = maxY = 0;
return Rect(position.x + minX, position.y + minY, maxX - minX, maxY - minY);
}
// --- Convenience methods for Skeleton_* functions.
void SkeletonRenderer::updateWorldTransform () {
_skeleton->updateWorldTransform();
}
void SkeletonRenderer::setToSetupPose () {
_skeleton->setToSetupPose();
}
void SkeletonRenderer::setBonesToSetupPose () {
_skeleton->setBonesToSetupPose();
}
void SkeletonRenderer::setSlotsToSetupPose () {
_skeleton->setSlotsToSetupPose();
}
Bone* SkeletonRenderer::findBone (const std::string& boneName) const {
return _skeleton->findBone(boneName.c_str());
}
Slot* SkeletonRenderer::findSlot (const std::string& slotName) const {
return _skeleton->findSlot(slotName.c_str());
}
void SkeletonRenderer::setSkin (const std::string& skinName) {
_skeleton->setSkin(skinName.empty() ? 0 : skinName.c_str());
}
void SkeletonRenderer::setSkin (const char* skinName) {
_skeleton->setSkin(skinName);
}
Attachment* SkeletonRenderer::getAttachment (const std::string& slotName, const std::string& attachmentName) const {
return _skeleton->getAttachment(slotName.c_str(), attachmentName.c_str());
}
bool SkeletonRenderer::setAttachment (const std::string& slotName, const std::string& attachmentName) {
return _skeleton->getAttachment(slotName.c_str(), attachmentName.empty() ? 0 : attachmentName.c_str()) ? true : false;
}
bool SkeletonRenderer::setAttachment (const std::string& slotName, const char* attachmentName) {
return _skeleton->getAttachment(slotName.c_str(), attachmentName) ? true : false;
}
void SkeletonRenderer::setTwoColorTint(bool enabled) {
setupGLProgramState(enabled);
}
bool SkeletonRenderer::isTwoColorTint() {
return _twoColorTintEnabled;
}
void SkeletonRenderer::setVertexEffect(VertexEffect *effect) {
this->_effect = effect;
}
void SkeletonRenderer::setSlotsRange(int startSlotIndex, int endSlotIndex) {
this->_startSlotIndex = startSlotIndex;
this->_endSlotIndex = endSlotIndex;
}
Skeleton* SkeletonRenderer::getSkeleton () {
return _skeleton;
}
void SkeletonRenderer::setTimeScale (float scale) {
_timeScale = scale;
}
float SkeletonRenderer::getTimeScale () const {
return _timeScale;
}
void SkeletonRenderer::setDebugSlotsEnabled (bool enabled) {
_debugSlots = enabled;
}
bool SkeletonRenderer::getDebugSlotsEnabled () const {
return _debugSlots;
}
void SkeletonRenderer::setDebugBonesEnabled (bool enabled) {
_debugBones = enabled;
}
bool SkeletonRenderer::getDebugBonesEnabled () const {
return _debugBones;
}
void SkeletonRenderer::setDebugMeshesEnabled (bool enabled) {
_debugMeshes = enabled;
}
bool SkeletonRenderer::getDebugMeshesEnabled () const {
return _debugMeshes;
}
void SkeletonRenderer::onEnter () {
#if CC_ENABLE_SCRIPT_BINDING
if (_scriptType == kScriptTypeJavascript && ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnEnter)) return;
#endif
Node::onEnter();
scheduleUpdate();
}
void SkeletonRenderer::onExit () {
#if CC_ENABLE_SCRIPT_BINDING
if (_scriptType == kScriptTypeJavascript && ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnExit)) return;
#endif
Node::onExit();
unscheduleUpdate();
}
// --- CCBlendProtocol
const BlendFunc& SkeletonRenderer::getBlendFunc () const {
return _blendFunc;
}
void SkeletonRenderer::setBlendFunc (const BlendFunc &blendFunc) {
_blendFunc = blendFunc;
}
void SkeletonRenderer::setOpacityModifyRGB (bool value) {
_premultipliedAlpha = value;
}
bool SkeletonRenderer::isOpacityModifyRGB () const {
return _premultipliedAlpha;
}
}
#endif