mirror of
https://github.com/EsotericSoftware/spine-runtimes.git
synced 2026-03-26 22:49:01 +08:00
More efficient cococs2d-x batching.
This commit is contained in:
parent
9a404e5c49
commit
a133221acd
@ -98,8 +98,8 @@ bool AppDelegate::applicationDidFinishLaunching () {
|
|||||||
director->setAnimationInterval(1.0f / 60);
|
director->setAnimationInterval(1.0f / 60);
|
||||||
|
|
||||||
// create a scene. it's an autorelease object
|
// create a scene. it's an autorelease object
|
||||||
auto scene = RaptorExample::scene();
|
//auto scene = RaptorExample::scene();
|
||||||
//auto scene = BatchingExample::scene();
|
auto scene = BatchingExample::scene();
|
||||||
|
|
||||||
// run
|
// run
|
||||||
director->runWithScene(scene);
|
director->runWithScene(scene);
|
||||||
|
|||||||
@ -45,28 +45,32 @@ bool BatchingExample::init () {
|
|||||||
if (!LayerColor::initWithColor(Color4B(128, 128, 128, 255))) return false;
|
if (!LayerColor::initWithColor(Color4B(128, 128, 128, 255))) return false;
|
||||||
|
|
||||||
// Load the texture atlas.
|
// Load the texture atlas.
|
||||||
spAtlas* atlas = spAtlas_createFromFile("spineboy.atlas", 0);
|
_atlas = spAtlas_createFromFile("spineboy.atlas", 0);
|
||||||
CCASSERT(atlas, "Error reading atlas file.");
|
CCASSERT(_atlas, "Error reading atlas file.");
|
||||||
|
|
||||||
|
// This attachment loader configures attachments with data needed for cocos2d-x rendering.
|
||||||
|
// Do not dispose the attachment loader until the skeleton data is disposed!
|
||||||
|
_attachmentLoader = (spAttachmentLoader*)Cocos2dAttachmentLoader_create(_atlas);
|
||||||
|
|
||||||
// Load the skeleton data.
|
// Load the skeleton data.
|
||||||
spSkeletonJson* json = spSkeletonJson_create(atlas);
|
spSkeletonJson* json = spSkeletonJson_createWithLoader(_attachmentLoader);
|
||||||
json->scale = 0.6f;
|
json->scale = 0.6f; // Resizes skeleton data to 60% of the size it was in Spine.
|
||||||
spSkeletonData* skeletonData = spSkeletonJson_readSkeletonDataFile(json, "spineboy.json");
|
_skeletonData = spSkeletonJson_readSkeletonDataFile(json, "spineboy.json");
|
||||||
CCASSERT(skeletonData, json->error ? json->error : "Error reading skeleton data file.");
|
CCASSERT(_skeletonData, json->error ? json->error : "Error reading skeleton data file.");
|
||||||
spSkeletonJson_dispose(json);
|
spSkeletonJson_dispose(json);
|
||||||
|
|
||||||
// Setup mix times.
|
// Setup mix times.
|
||||||
spAnimationStateData* stateData = spAnimationStateData_create(skeletonData);
|
_stateData = spAnimationStateData_create(_skeletonData);
|
||||||
spAnimationStateData_setMixByName(stateData, "walk", "jump", 0.2f);
|
spAnimationStateData_setMixByName(_stateData, "walk", "jump", 0.2f);
|
||||||
spAnimationStateData_setMixByName(stateData, "jump", "run", 0.2f);
|
spAnimationStateData_setMixByName(_stateData, "jump", "run", 0.2f);
|
||||||
|
|
||||||
Size windowSize = Director::getInstance()->getWinSize();
|
Size windowSize = Director::getInstance()->getWinSize();
|
||||||
int xMin = (int)(windowSize.width * 0.10f), xMax = (int)windowSize.width - xMin;
|
int xMin = (int)(windowSize.width * 0.10f), xMax = (int)windowSize.width - xMin;
|
||||||
int yMin = 20, yMax = windowSize.height - 350;
|
int yMin = 20, yMax = windowSize.height - 350;
|
||||||
for (int i = 0; i < 50; i++) {
|
for (int i = 0; i < 50; i++) {
|
||||||
// Each skeleton node shares the same atlas, skeleton data, and mix times.
|
// Each skeleton node shares the same atlas, skeleton data, and mix times.
|
||||||
SkeletonAnimation* skeletonNode = SkeletonAnimation::createWithData(skeletonData, false);
|
SkeletonAnimation* skeletonNode = SkeletonAnimation::createWithData(_skeletonData, false);
|
||||||
skeletonNode->setAnimationStateData(stateData);
|
skeletonNode->setAnimationStateData(_stateData);
|
||||||
|
|
||||||
skeletonNode->setAnimation(0, "walk", true);
|
skeletonNode->setAnimation(0, "walk", true);
|
||||||
skeletonNode->addAnimation(0, "jump", false, 3);
|
skeletonNode->addAnimation(0, "jump", false, 3);
|
||||||
@ -90,3 +94,12 @@ bool BatchingExample::init () {
|
|||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BatchingExample::~BatchingExample () {
|
||||||
|
// SkeletonAnimation instances are cocos2d-x nodes and are disposed of automatically as normal, but the data created
|
||||||
|
// manually to be shared across multiple SkeletonAnimations needs to be disposed of manually.
|
||||||
|
spSkeletonData_dispose(_skeletonData);
|
||||||
|
spAnimationStateData_dispose(_stateData);
|
||||||
|
spAttachmentLoader_dispose(_attachmentLoader);
|
||||||
|
spAtlas_dispose(_atlas);
|
||||||
|
}
|
||||||
|
|||||||
@ -40,8 +40,15 @@ public:
|
|||||||
static cocos2d::Scene* scene ();
|
static cocos2d::Scene* scene ();
|
||||||
|
|
||||||
CREATE_FUNC(BatchingExample);
|
CREATE_FUNC(BatchingExample);
|
||||||
|
~BatchingExample ();
|
||||||
|
|
||||||
virtual bool init ();
|
virtual bool init ();
|
||||||
|
|
||||||
|
protected:
|
||||||
|
spAtlas* _atlas;
|
||||||
|
spAttachmentLoader* _attachmentLoader;
|
||||||
|
spSkeletonData* _skeletonData;
|
||||||
|
spAnimationStateData* _stateData;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // _BATCHINGEXAMPLE_H_
|
#endif // _BATCHINGEXAMPLE_H_
|
||||||
|
|||||||
@ -129,6 +129,8 @@
|
|||||||
</PostBuildEvent>
|
</PostBuildEvent>
|
||||||
</ItemDefinitionGroup>
|
</ItemDefinitionGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ClInclude Include="..\..\src\spine\AttachmentVertices.h" />
|
||||||
|
<ClInclude Include="..\..\src\spine\Cocos2dAttachmentLoader.h" />
|
||||||
<ClInclude Include="..\..\src\spine\SkeletonBatch.h" />
|
<ClInclude Include="..\..\src\spine\SkeletonBatch.h" />
|
||||||
<ClInclude Include="..\..\src\spine\SkeletonAnimation.h" />
|
<ClInclude Include="..\..\src\spine\SkeletonAnimation.h" />
|
||||||
<ClInclude Include="..\..\src\spine\SkeletonRenderer.h" />
|
<ClInclude Include="..\..\src\spine\SkeletonRenderer.h" />
|
||||||
@ -142,6 +144,8 @@
|
|||||||
<ClInclude Include="main.h" />
|
<ClInclude Include="main.h" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ClCompile Include="..\..\src\spine\AttachmentVertices.cpp" />
|
||||||
|
<ClCompile Include="..\..\src\spine\Cocos2dAttachmentLoader.c" />
|
||||||
<ClCompile Include="..\..\src\spine\SkeletonBatch.cpp" />
|
<ClCompile Include="..\..\src\spine\SkeletonBatch.cpp" />
|
||||||
<ClCompile Include="..\..\src\spine\SkeletonAnimation.cpp" />
|
<ClCompile Include="..\..\src\spine\SkeletonAnimation.cpp" />
|
||||||
<ClCompile Include="..\..\src\spine\SkeletonRenderer.cpp" />
|
<ClCompile Include="..\..\src\spine\SkeletonRenderer.cpp" />
|
||||||
|
|||||||
@ -45,6 +45,12 @@
|
|||||||
<ClInclude Include="..\..\src\spine\SkeletonBatch.h">
|
<ClInclude Include="..\..\src\spine\SkeletonBatch.h">
|
||||||
<Filter>src</Filter>
|
<Filter>src</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="..\..\src\spine\Cocos2dAttachmentLoader.h">
|
||||||
|
<Filter>src</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="..\..\src\spine\AttachmentVertices.h">
|
||||||
|
<Filter>src</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="main.cpp">
|
<ClCompile Include="main.cpp">
|
||||||
@ -77,5 +83,11 @@
|
|||||||
<ClCompile Include="..\..\src\spine\SkeletonBatch.cpp">
|
<ClCompile Include="..\..\src\spine\SkeletonBatch.cpp">
|
||||||
<Filter>src</Filter>
|
<Filter>src</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="..\..\src\spine\Cocos2dAttachmentLoader.c">
|
||||||
|
<Filter>src</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="..\..\src\spine\AttachmentVertices.cpp">
|
||||||
|
<Filter>src</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
53
spine-cocos2dx/3/src/spine/AttachmentVertices.cpp
Normal file
53
spine-cocos2dx/3/src/spine/AttachmentVertices.cpp
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
/******************************************************************************
|
||||||
|
* Spine Runtimes Software License
|
||||||
|
* Version 2.3
|
||||||
|
*
|
||||||
|
* Copyright (c) 2013-2015, Esoteric Software
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* You are granted a perpetual, non-exclusive, non-sublicensable and
|
||||||
|
* non-transferable license to use, install, execute and perform the Spine
|
||||||
|
* Runtimes Software (the "Software") and derivative works solely for personal
|
||||||
|
* or internal use. Without the written permission of Esoteric Software (see
|
||||||
|
* Section 2 of the Spine Software License Agreement), you may not (a) modify,
|
||||||
|
* translate, adapt or otherwise create derivative works, improvements of the
|
||||||
|
* Software or develop new applications using the Software or (b) remove,
|
||||||
|
* delete, alter or obscure any trademarks or any copyright, trademark, patent
|
||||||
|
* or other intellectual property or proprietary rights notices on or in the
|
||||||
|
* Software, including any copy thereof. Redistributions in binary or source
|
||||||
|
* form must include this license and terms.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE "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 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||||
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||||
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||||
|
* OR BUSINESS INTERRUPTION) 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/AttachmentVertices.h>
|
||||||
|
|
||||||
|
USING_NS_CC;
|
||||||
|
|
||||||
|
namespace spine {
|
||||||
|
|
||||||
|
AttachmentVertices::AttachmentVertices (Texture2D* texture, int verticesCount, unsigned short* triangles, int trianglesCount) {
|
||||||
|
_texture = texture;
|
||||||
|
|
||||||
|
_triangles = new TrianglesCommand::Triangles();
|
||||||
|
_triangles->verts = new V3F_C4B_T2F[verticesCount];
|
||||||
|
_triangles->vertCount = verticesCount;
|
||||||
|
_triangles->indices = triangles;
|
||||||
|
_triangles->indexCount = trianglesCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
AttachmentVertices::~AttachmentVertices () {
|
||||||
|
delete [] _triangles->verts;
|
||||||
|
delete _triangles;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
50
spine-cocos2dx/3/src/spine/AttachmentVertices.h
Normal file
50
spine-cocos2dx/3/src/spine/AttachmentVertices.h
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
/******************************************************************************
|
||||||
|
* Spine Runtimes Software License
|
||||||
|
* Version 2.3
|
||||||
|
*
|
||||||
|
* Copyright (c) 2013-2015, Esoteric Software
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* You are granted a perpetual, non-exclusive, non-sublicensable and
|
||||||
|
* non-transferable license to use, install, execute and perform the Spine
|
||||||
|
* Runtimes Software (the "Software") and derivative works solely for personal
|
||||||
|
* or internal use. Without the written permission of Esoteric Software (see
|
||||||
|
* Section 2 of the Spine Software License Agreement), you may not (a) modify,
|
||||||
|
* translate, adapt or otherwise create derivative works, improvements of the
|
||||||
|
* Software or develop new applications using the Software or (b) remove,
|
||||||
|
* delete, alter or obscure any trademarks or any copyright, trademark, patent
|
||||||
|
* or other intellectual property or proprietary rights notices on or in the
|
||||||
|
* Software, including any copy thereof. Redistributions in binary or source
|
||||||
|
* form must include this license and terms.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE "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 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||||
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||||
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||||
|
* OR BUSINESS INTERRUPTION) 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.
|
||||||
|
*****************************************************************************/
|
||||||
|
|
||||||
|
#ifndef SPINE_ATTACHMENTVERTICES_H_
|
||||||
|
#define SPINE_ATTACHMENTVERTICES_H_
|
||||||
|
|
||||||
|
#include "cocos2d.h"
|
||||||
|
|
||||||
|
namespace spine {
|
||||||
|
|
||||||
|
class AttachmentVertices {
|
||||||
|
public:
|
||||||
|
AttachmentVertices (cocos2d::Texture2D* texture, int verticesCount, unsigned short* triangles, int trianglesCount);
|
||||||
|
virtual ~AttachmentVertices ();
|
||||||
|
|
||||||
|
cocos2d::Texture2D* _texture;
|
||||||
|
cocos2d::TrianglesCommand::Triangles* _triangles;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif /* SPINE_ATTACHMENTVERTICES_H_ */
|
||||||
118
spine-cocos2dx/3/src/spine/Cocos2dAttachmentLoader.c
Normal file
118
spine-cocos2dx/3/src/spine/Cocos2dAttachmentLoader.c
Normal file
@ -0,0 +1,118 @@
|
|||||||
|
/******************************************************************************
|
||||||
|
* Spine Runtimes Software License
|
||||||
|
* Version 2.3
|
||||||
|
*
|
||||||
|
* Copyright (c) 2013-2015, Esoteric Software
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* You are granted a perpetual, non-exclusive, non-sublicensable and
|
||||||
|
* non-transferable license to use, install, execute and perform the Spine
|
||||||
|
* Runtimes Software (the "Software") and derivative works solely for personal
|
||||||
|
* or internal use. Without the written permission of Esoteric Software (see
|
||||||
|
* Section 2 of the Spine Software License Agreement), you may not (a) modify,
|
||||||
|
* translate, adapt or otherwise create derivative works, improvements of the
|
||||||
|
* Software or develop new applications using the Software or (b) remove,
|
||||||
|
* delete, alter or obscure any trademarks or any copyright, trademark, patent
|
||||||
|
* or other intellectual property or proprietary rights notices on or in the
|
||||||
|
* Software, including any copy thereof. Redistributions in binary or source
|
||||||
|
* form must include this license and terms.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE "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 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||||
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||||
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||||
|
* OR BUSINESS INTERRUPTION) 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/Cocos2dAttachmentLoader.h>
|
||||||
|
#include <spine/extension.h>
|
||||||
|
#include <spine/AttachmentVertices.h>
|
||||||
|
|
||||||
|
USING_NS_CC;
|
||||||
|
using namespace spine;
|
||||||
|
|
||||||
|
static unsigned short quadTriangles[6] = {0, 1, 2, 2, 3, 0};
|
||||||
|
|
||||||
|
spAttachment* _Cocos2dAttachmentLoader_createAttachment (spAttachmentLoader* loader, spSkin* skin, spAttachmentType type,
|
||||||
|
const char* name, const char* path) {
|
||||||
|
Cocos2dAttachmentLoader* self = SUB_CAST(Cocos2dAttachmentLoader, loader);
|
||||||
|
return spAttachmentLoader_createAttachment(SUPER(self->atlasAttachmentLoader), skin, type, name, path);
|
||||||
|
}
|
||||||
|
|
||||||
|
void _Cocos2dAttachmentLoader_configureAttachment (spAttachmentLoader* loader, spAttachment* attachment) {
|
||||||
|
switch (attachment->type) {
|
||||||
|
case SP_ATTACHMENT_REGION: {
|
||||||
|
spRegionAttachment* regionAttachment = SUB_CAST(spRegionAttachment, attachment);
|
||||||
|
spAtlasRegion* region = (spAtlasRegion*)regionAttachment->rendererObject;
|
||||||
|
AttachmentVertices* attachmentVertices = new AttachmentVertices((Texture2D*)region->page->rendererObject, 4, quadTriangles, 6);
|
||||||
|
V3F_C4B_T2F* vertices = attachmentVertices->_triangles->verts;
|
||||||
|
for (int i = 0, ii = 0; i < 4; ++i, ii += 2) {
|
||||||
|
vertices[i].texCoords.u = regionAttachment->uvs[ii];
|
||||||
|
vertices[i].texCoords.v = regionAttachment->uvs[ii + 1];
|
||||||
|
}
|
||||||
|
regionAttachment->rendererObject = attachmentVertices;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case SP_ATTACHMENT_MESH: {
|
||||||
|
spMeshAttachment* meshAttachment = SUB_CAST(spMeshAttachment, attachment);
|
||||||
|
spAtlasRegion* region = (spAtlasRegion*)meshAttachment->rendererObject;
|
||||||
|
AttachmentVertices* attachmentVertices = new AttachmentVertices((Texture2D*)region->page->rendererObject,
|
||||||
|
meshAttachment->verticesCount >> 1, meshAttachment->triangles, meshAttachment->trianglesCount);
|
||||||
|
V3F_C4B_T2F* vertices = attachmentVertices->_triangles->verts;
|
||||||
|
for (int i = 0, ii = 0, nn = meshAttachment->verticesCount; ii < nn; ++i, ii += 2) {
|
||||||
|
vertices[i].texCoords.u = meshAttachment->uvs[ii];
|
||||||
|
vertices[i].texCoords.v = meshAttachment->uvs[ii + 1];
|
||||||
|
}
|
||||||
|
meshAttachment->rendererObject = attachmentVertices;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case SP_ATTACHMENT_WEIGHTED_MESH: {
|
||||||
|
spWeightedMeshAttachment* meshAttachment = SUB_CAST(spWeightedMeshAttachment, attachment);
|
||||||
|
spAtlasRegion* region = (spAtlasRegion*)meshAttachment->rendererObject;
|
||||||
|
AttachmentVertices* attachmentVertices = new AttachmentVertices((Texture2D*)region->page->rendererObject,
|
||||||
|
meshAttachment->uvsCount >> 1, meshAttachment->triangles, meshAttachment->trianglesCount);
|
||||||
|
V3F_C4B_T2F* vertices = attachmentVertices->_triangles->verts;
|
||||||
|
for (int i = 0, ii = 0, nn = meshAttachment->uvsCount; ii < nn; ++i, ii += 2) {
|
||||||
|
vertices[i].texCoords.u = meshAttachment->uvs[ii];
|
||||||
|
vertices[i].texCoords.v = meshAttachment->uvs[ii + 1];
|
||||||
|
}
|
||||||
|
meshAttachment->rendererObject = attachmentVertices;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default: ;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void _Cocos2dAttachmentLoader_disposeAttachment (spAttachmentLoader* loader, spAttachment* attachment) {
|
||||||
|
switch (attachment->type) {
|
||||||
|
case SP_ATTACHMENT_REGION: {
|
||||||
|
spRegionAttachment* regionAttachment = SUB_CAST(spRegionAttachment, attachment);
|
||||||
|
delete (AttachmentVertices*)regionAttachment->rendererObject;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case SP_ATTACHMENT_MESH: {
|
||||||
|
spMeshAttachment* meshAttachment = SUB_CAST(spMeshAttachment, attachment);
|
||||||
|
delete (AttachmentVertices*)meshAttachment->rendererObject;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case SP_ATTACHMENT_WEIGHTED_MESH: {
|
||||||
|
spWeightedMeshAttachment* meshAttachment = SUB_CAST(spWeightedMeshAttachment, attachment);
|
||||||
|
delete (AttachmentVertices*)meshAttachment->rendererObject;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default: ;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Cocos2dAttachmentLoader* Cocos2dAttachmentLoader_create (spAtlas* atlas) {
|
||||||
|
Cocos2dAttachmentLoader* self = NEW(Cocos2dAttachmentLoader);
|
||||||
|
_spAttachmentLoader_init(SUPER(self), _spAttachmentLoader_deinit, _Cocos2dAttachmentLoader_createAttachment,
|
||||||
|
_Cocos2dAttachmentLoader_configureAttachment, _Cocos2dAttachmentLoader_disposeAttachment);
|
||||||
|
self->atlasAttachmentLoader = spAtlasAttachmentLoader_create(atlas);
|
||||||
|
return self;
|
||||||
|
}
|
||||||
48
spine-cocos2dx/3/src/spine/Cocos2dAttachmentLoader.h
Normal file
48
spine-cocos2dx/3/src/spine/Cocos2dAttachmentLoader.h
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
/******************************************************************************
|
||||||
|
* Spine Runtimes Software License
|
||||||
|
* Version 2.3
|
||||||
|
*
|
||||||
|
* Copyright (c) 2013-2015, Esoteric Software
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* You are granted a perpetual, non-exclusive, non-sublicensable and
|
||||||
|
* non-transferable license to use, install, execute and perform the Spine
|
||||||
|
* Runtimes Software (the "Software") and derivative works solely for personal
|
||||||
|
* or internal use. Without the written permission of Esoteric Software (see
|
||||||
|
* Section 2 of the Spine Software License Agreement), you may not (a) modify,
|
||||||
|
* translate, adapt or otherwise create derivative works, improvements of the
|
||||||
|
* Software or develop new applications using the Software or (b) remove,
|
||||||
|
* delete, alter or obscure any trademarks or any copyright, trademark, patent
|
||||||
|
* or other intellectual property or proprietary rights notices on or in the
|
||||||
|
* Software, including any copy thereof. Redistributions in binary or source
|
||||||
|
* form must include this license and terms.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE "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 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||||
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||||
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||||
|
* OR BUSINESS INTERRUPTION) 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.
|
||||||
|
*****************************************************************************/
|
||||||
|
|
||||||
|
#ifndef SPINE_COCOS2DATTACHMENTLOADER_H_
|
||||||
|
#define SPINE_COCOS2DATTACHMENTLOADER_H_
|
||||||
|
|
||||||
|
#include <spine/AtlasAttachmentLoader.h>
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
|
||||||
|
typedef struct Cocos2dAttachmentLoader {
|
||||||
|
spAttachmentLoader super;
|
||||||
|
spAtlasAttachmentLoader* atlasAttachmentLoader;
|
||||||
|
} Cocos2dAttachmentLoader;
|
||||||
|
|
||||||
|
Cocos2dAttachmentLoader* Cocos2dAttachmentLoader_create (spAtlas* atlas);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif /* SPINE_COCOS2DATTACHMENTLOADER_H_ */
|
||||||
@ -34,31 +34,26 @@
|
|||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
||||||
USING_NS_CC;
|
USING_NS_CC;
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
namespace spine {
|
namespace spine {
|
||||||
|
|
||||||
static SkeletonBatch* instance = nullptr;
|
static SkeletonBatch* instance = nullptr;
|
||||||
|
|
||||||
void SkeletonBatch::setCommandSize (int maxVertices, int maxTriangles) {
|
void SkeletonBatch::setBufferSize (int vertexCount) {
|
||||||
// 32767 is max index, so 32767 / 3 - (32767 / 3 % 3) = 10920.
|
|
||||||
CCASSERT(maxTriangles <= 10920, "maxTriangles cannot be > 10920");
|
|
||||||
CCASSERT(maxTriangles >= 0, "maxTriangles cannot be < 0");
|
|
||||||
if (instance) delete instance;
|
if (instance) delete instance;
|
||||||
instance = new SkeletonBatch(maxVertices, maxTriangles);
|
instance = new SkeletonBatch(vertexCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
SkeletonBatch* SkeletonBatch::getInstance () {
|
SkeletonBatch* SkeletonBatch::getInstance () {
|
||||||
if (!instance) instance = new SkeletonBatch(64, 64 * 3);
|
if (!instance) instance = new SkeletonBatch(8192);
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
SkeletonBatch::SkeletonBatch (int maxVertices, int maxTriangles) :
|
SkeletonBatch::SkeletonBatch (int capacity) :
|
||||||
_maxVertices(maxVertices), _maxTriangles(maxTriangles),
|
_capacity(capacity), _position(0)
|
||||||
_renderer(nullptr), _transform(nullptr), _transformFlags(0), _globalZOrder(0), _glProgramState(nullptr),
|
|
||||||
_texture(nullptr), _blendMode(SP_BLEND_MODE_NORMAL)
|
|
||||||
{
|
{
|
||||||
_firstCommand = new Command(maxVertices, maxTriangles);
|
_buffer = new V3F_C4B_T2F[capacity];
|
||||||
|
_firstCommand = new Command();
|
||||||
_command = _firstCommand;
|
_command = _firstCommand;
|
||||||
|
|
||||||
Director::getInstance()->getScheduler()->scheduleUpdate(this, -1, false);
|
Director::getInstance()->getScheduler()->scheduleUpdate(this, -1, false);
|
||||||
@ -73,109 +68,44 @@ SkeletonBatch::~SkeletonBatch () {
|
|||||||
delete command;
|
delete command;
|
||||||
command = next;
|
command = next;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void SkeletonBatch::setRendererState (Renderer* renderer, const Mat4* transform, uint32_t transformFlags,
|
delete [] _buffer;
|
||||||
float globalZOrder, GLProgramState* glProgramState, bool premultipliedAlpha) {
|
|
||||||
_renderer = renderer;
|
|
||||||
_transform = transform;
|
|
||||||
_transformFlags = transformFlags;
|
|
||||||
_globalZOrder = globalZOrder;
|
|
||||||
_glProgramState = glProgramState;
|
|
||||||
_premultipliedAlpha = premultipliedAlpha;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SkeletonBatch::update (float delta) {
|
void SkeletonBatch::update (float delta) {
|
||||||
// Reuse commands at the beginning of each frame.
|
_position = 0;
|
||||||
_command = _firstCommand;
|
_command = _firstCommand;
|
||||||
_command->_triangles->vertCount = 0;
|
|
||||||
_command->_triangles->indexCount = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SkeletonBatch::add (const Texture2D* addTexture,
|
void SkeletonBatch::addCommand (cocos2d::Renderer* renderer, float globalZOrder, GLuint textureID, GLProgramState* glProgramState,
|
||||||
const float* addVertices, const float* uvs, int addVerticesCount,
|
BlendFunc blendFunc, const TrianglesCommand::Triangles& triangles, const Mat4& transform, uint32_t transformFlags
|
||||||
const int* addTriangles, int addTrianglesCount,
|
|
||||||
const Color4B& color, spBlendMode blendMode
|
|
||||||
) {
|
) {
|
||||||
if (addTexture != _texture
|
CCASSERT(_position + triangles.vertCount < _capacity, "SkeletonBatch capacity is too small");
|
||||||
|| blendMode != _blendMode
|
|
||||||
|| _command->_triangles->vertCount + (addVerticesCount >> 1) > _maxVertices
|
|
||||||
|| _command->_triangles->indexCount + addTrianglesCount > _maxTriangles
|
|
||||||
) {
|
|
||||||
this->flush(max(addVerticesCount >> 1, _maxVertices), max(addTrianglesCount, _maxTriangles));
|
|
||||||
_texture = addTexture;
|
|
||||||
_blendMode = blendMode;
|
|
||||||
}
|
|
||||||
|
|
||||||
TrianglesCommand::Triangles* triangles = _command->_triangles;
|
memcpy(_buffer + _position, triangles.verts, sizeof(V3F_C4B_T2F) * triangles.vertCount);
|
||||||
for (int i = 0; i < addTrianglesCount; ++i, ++triangles->indexCount)
|
_command->_triangles->verts = _buffer + _position;
|
||||||
triangles->indices[triangles->indexCount] = addTriangles[i] + triangles->vertCount;
|
_position += triangles.vertCount;
|
||||||
|
|
||||||
for (int i = 0; i < addVerticesCount; i += 2, ++triangles->vertCount) {
|
_command->_triangles->vertCount = triangles.vertCount;
|
||||||
V3F_C4B_T2F* vertex = triangles->verts + triangles->vertCount;
|
_command->_triangles->indexCount = triangles.indexCount;
|
||||||
vertex->vertices.x = addVertices[i];
|
_command->_triangles->indices = triangles.indices;
|
||||||
vertex->vertices.y = addVertices[i + 1];
|
|
||||||
vertex->colors = color;
|
|
||||||
vertex->texCoords.u = uvs[i];
|
|
||||||
vertex->texCoords.v = uvs[i + 1];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void SkeletonBatch::flush (int maxVertices, int maxTriangles) {
|
_command->_trianglesCommand->init(globalZOrder, textureID, glProgramState, blendFunc, *_command->_triangles, transform, transformFlags);
|
||||||
if (!_command->_triangles->vertCount) return;
|
renderer->addCommand(_command->_trianglesCommand);
|
||||||
|
|
||||||
BlendFunc blendFunc;
|
if (!_command->_next) _command->_next = new Command();
|
||||||
switch (_blendMode) {
|
|
||||||
case SP_BLEND_MODE_ADDITIVE:
|
|
||||||
blendFunc.src = _premultipliedAlpha ? GL_ONE : GL_SRC_ALPHA;
|
|
||||||
blendFunc.dst = GL_ONE;
|
|
||||||
break;
|
|
||||||
case SP_BLEND_MODE_MULTIPLY:
|
|
||||||
blendFunc.src = GL_DST_COLOR;
|
|
||||||
blendFunc.dst = GL_ONE_MINUS_SRC_ALPHA;
|
|
||||||
break;
|
|
||||||
case SP_BLEND_MODE_SCREEN:
|
|
||||||
blendFunc.src = GL_ONE;
|
|
||||||
blendFunc.dst = GL_ONE_MINUS_SRC_COLOR;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
blendFunc.src = _premultipliedAlpha ? GL_ONE : GL_SRC_ALPHA;
|
|
||||||
blendFunc.dst = GL_ONE_MINUS_SRC_ALPHA;
|
|
||||||
}
|
|
||||||
|
|
||||||
_command->_trianglesCommand->init(_globalZOrder, _texture->getName(), _glProgramState, blendFunc, *_command->_triangles,
|
|
||||||
*_transform, _transformFlags);
|
|
||||||
_renderer->addCommand(_command->_trianglesCommand);
|
|
||||||
|
|
||||||
if (!_command->_next) _command->_next = new Command(maxVertices, maxTriangles);
|
|
||||||
_command = _command->_next;
|
_command = _command->_next;
|
||||||
|
|
||||||
// If not as large as required, insert new command.
|
|
||||||
if (_command->_maxVertices < maxVertices || _command->_maxTriangles < maxTriangles) {
|
|
||||||
Command* next = _command->_next;
|
|
||||||
_command = new Command(maxVertices, maxTriangles);
|
|
||||||
_command->_next = next;
|
|
||||||
}
|
|
||||||
|
|
||||||
_command->_triangles->vertCount = 0;
|
|
||||||
_command->_triangles->indexCount = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SkeletonBatch::Command::Command (int maxVertices, int maxTriangles) :
|
SkeletonBatch::Command::Command () :
|
||||||
_maxVertices(maxVertices), _maxTriangles(maxTriangles), _next(nullptr)
|
_next(nullptr)
|
||||||
{
|
{
|
||||||
_trianglesCommand = new TrianglesCommand();
|
_trianglesCommand = new TrianglesCommand();
|
||||||
|
|
||||||
_triangles = new TrianglesCommand::Triangles();
|
_triangles = new TrianglesCommand::Triangles();
|
||||||
_triangles->verts = new V3F_C4B_T2F[maxVertices];
|
|
||||||
_triangles->indices = new GLushort[maxTriangles];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SkeletonBatch::Command::~Command () {
|
SkeletonBatch::Command::~Command () {
|
||||||
delete [] _triangles->indices;
|
|
||||||
delete [] _triangles->verts;
|
|
||||||
delete _triangles;
|
delete _triangles;
|
||||||
|
|
||||||
delete _trianglesCommand;
|
delete _trianglesCommand;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -37,63 +37,39 @@
|
|||||||
|
|
||||||
namespace spine {
|
namespace spine {
|
||||||
|
|
||||||
/* Batches attachment geometry and issues one or more TrianglesCommands per skeleton. */
|
class SkeletonBatch {
|
||||||
class SkeletonBatch : public cocos2d::Ref {
|
|
||||||
public:
|
public:
|
||||||
/* Sets the default size of each TrianglesCommand. Best to call before getInstance is called for the first time. Default is 64, 192.
|
/* Sets the max number of vertices that can be drawn in a single frame. Best to call before getInstance is called for the
|
||||||
* TrianglesCommands may be larger than the specified sizes if required to hold the geometry for a single attachment. */
|
* first time. Default is 8192. */
|
||||||
static void setCommandSize (int maxVertices, int maxTriangles);
|
static void SkeletonBatch::setBufferSize (int vertexCount);
|
||||||
|
|
||||||
static SkeletonBatch* getInstance ();
|
static SkeletonBatch* getInstance ();
|
||||||
|
|
||||||
void update (float delta);
|
void update (float delta);
|
||||||
|
|
||||||
void setRendererState (cocos2d::Renderer* renderer, const cocos2d::Mat4* transform, uint32_t transformFlags,
|
void addCommand (cocos2d::Renderer* renderer, float globalOrder, GLuint textureID, cocos2d::GLProgramState* glProgramState,
|
||||||
float globalZOrder, cocos2d::GLProgramState* glProgramState, bool premultipliedAlpha);
|
cocos2d::BlendFunc blendType, const cocos2d::TrianglesCommand:: Triangles& triangles, const cocos2d::Mat4& mv, uint32_t flags);
|
||||||
|
|
||||||
void add (const cocos2d::Texture2D* texture,
|
|
||||||
const float* vertices, const float* uvs, int verticesCount,
|
|
||||||
const int* triangles, int trianglesCount,
|
|
||||||
const cocos2d::Color4B& color, spBlendMode blendMode);
|
|
||||||
|
|
||||||
void flush () {
|
|
||||||
flush(_maxVertices, _maxTriangles);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
SkeletonBatch (int maxVertices, int maxTriangles);
|
SkeletonBatch (int capacity);
|
||||||
virtual ~SkeletonBatch ();
|
virtual ~SkeletonBatch ();
|
||||||
|
|
||||||
void flush (int maxVertices, int maxTriangles);
|
cocos2d::V3F_C4B_T2F* _buffer;
|
||||||
|
int _capacity;
|
||||||
|
int _position;
|
||||||
|
|
||||||
class Command {
|
class Command {
|
||||||
public:
|
public:
|
||||||
Command (int maxVertices, int maxTriangles);
|
Command ();
|
||||||
virtual ~Command ();
|
virtual ~Command ();
|
||||||
|
|
||||||
int _maxVertices;
|
|
||||||
int _maxTriangles;
|
|
||||||
cocos2d::TrianglesCommand* _trianglesCommand;
|
cocos2d::TrianglesCommand* _trianglesCommand;
|
||||||
cocos2d::TrianglesCommand::Triangles* _triangles;
|
cocos2d::TrianglesCommand::Triangles* _triangles;
|
||||||
Command* _next;
|
Command* _next;
|
||||||
};
|
};
|
||||||
|
|
||||||
int _maxVertices;
|
|
||||||
int _maxTriangles;
|
|
||||||
Command* _firstCommand;
|
Command* _firstCommand;
|
||||||
Command* _command;
|
Command* _command;
|
||||||
|
|
||||||
// Renderer state.
|
|
||||||
cocos2d::Renderer* _renderer;
|
|
||||||
const cocos2d::Mat4* _transform;
|
|
||||||
uint32_t _transformFlags;
|
|
||||||
float _globalZOrder;
|
|
||||||
cocos2d::GLProgramState* _glProgramState;
|
|
||||||
bool _premultipliedAlpha;
|
|
||||||
|
|
||||||
// Batch state.
|
|
||||||
const cocos2d::Texture2D* _texture;
|
|
||||||
spBlendMode _blendMode;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -30,9 +30,10 @@
|
|||||||
*****************************************************************************/
|
*****************************************************************************/
|
||||||
|
|
||||||
#include <spine/SkeletonRenderer.h>
|
#include <spine/SkeletonRenderer.h>
|
||||||
#include <spine/spine-cocos2dx.h>
|
|
||||||
#include <spine/extension.h>
|
#include <spine/extension.h>
|
||||||
#include <spine/SkeletonBatch.h>
|
#include <spine/SkeletonBatch.h>
|
||||||
|
#include <spine/AttachmentVertices.h>
|
||||||
|
#include <spine/Cocos2dAttachmentLoader.h>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
||||||
USING_NS_CC;
|
USING_NS_CC;
|
||||||
@ -41,8 +42,6 @@ using std::max;
|
|||||||
|
|
||||||
namespace spine {
|
namespace spine {
|
||||||
|
|
||||||
static const int quadTriangles[6] = {0, 1, 2, 2, 3, 0};
|
|
||||||
|
|
||||||
SkeletonRenderer* SkeletonRenderer::createWithData (spSkeletonData* skeletonData, bool ownsSkeletonData) {
|
SkeletonRenderer* SkeletonRenderer::createWithData (spSkeletonData* skeletonData, bool ownsSkeletonData) {
|
||||||
SkeletonRenderer* node = new SkeletonRenderer(skeletonData, ownsSkeletonData);
|
SkeletonRenderer* node = new SkeletonRenderer(skeletonData, ownsSkeletonData);
|
||||||
node->autorelease();
|
node->autorelease();
|
||||||
@ -62,7 +61,7 @@ SkeletonRenderer* SkeletonRenderer::createWithFile (const std::string& skeletonD
|
|||||||
}
|
}
|
||||||
|
|
||||||
void SkeletonRenderer::initialize () {
|
void SkeletonRenderer::initialize () {
|
||||||
_worldVertices = MALLOC(float, 1000); // Max number of vertices per mesh.
|
_worldVertices = new float[1000]; // Max number of vertices per mesh.
|
||||||
|
|
||||||
_blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
|
_blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
|
||||||
setOpacityModifyRGB(true);
|
setOpacityModifyRGB(true);
|
||||||
@ -76,29 +75,30 @@ void SkeletonRenderer::setSkeletonData (spSkeletonData *skeletonData, bool ownsS
|
|||||||
}
|
}
|
||||||
|
|
||||||
SkeletonRenderer::SkeletonRenderer ()
|
SkeletonRenderer::SkeletonRenderer ()
|
||||||
: _atlas(0), _debugSlots(false), _debugBones(false), _timeScale(1) {
|
: _atlas(nullptr), _attachmentLoader(nullptr), _debugSlots(false), _debugBones(false), _timeScale(1) {
|
||||||
}
|
}
|
||||||
|
|
||||||
SkeletonRenderer::SkeletonRenderer (spSkeletonData *skeletonData, bool ownsSkeletonData)
|
SkeletonRenderer::SkeletonRenderer (spSkeletonData *skeletonData, bool ownsSkeletonData)
|
||||||
: _atlas(0), _debugSlots(false), _debugBones(false), _timeScale(1) {
|
: _atlas(nullptr), _attachmentLoader(nullptr), _debugSlots(false), _debugBones(false), _timeScale(1) {
|
||||||
initWithData(skeletonData, ownsSkeletonData);
|
initWithData(skeletonData, ownsSkeletonData);
|
||||||
}
|
}
|
||||||
|
|
||||||
SkeletonRenderer::SkeletonRenderer (const std::string& skeletonDataFile, spAtlas* atlas, float scale)
|
SkeletonRenderer::SkeletonRenderer (const std::string& skeletonDataFile, spAtlas* atlas, float scale)
|
||||||
: _atlas(0), _debugSlots(false), _debugBones(false), _timeScale(1) {
|
: _atlas(nullptr), _attachmentLoader(nullptr), _debugSlots(false), _debugBones(false), _timeScale(1) {
|
||||||
initWithFile(skeletonDataFile, atlas, scale);
|
initWithFile(skeletonDataFile, atlas, scale);
|
||||||
}
|
}
|
||||||
|
|
||||||
SkeletonRenderer::SkeletonRenderer (const std::string& skeletonDataFile, const std::string& atlasFile, float scale)
|
SkeletonRenderer::SkeletonRenderer (const std::string& skeletonDataFile, const std::string& atlasFile, float scale)
|
||||||
: _atlas(0), _debugSlots(false), _debugBones(false), _timeScale(1) {
|
: _atlas(nullptr), _attachmentLoader(nullptr), _debugSlots(false), _debugBones(false), _timeScale(1) {
|
||||||
initWithFile(skeletonDataFile, atlasFile, scale);
|
initWithFile(skeletonDataFile, atlasFile, scale);
|
||||||
}
|
}
|
||||||
|
|
||||||
SkeletonRenderer::~SkeletonRenderer () {
|
SkeletonRenderer::~SkeletonRenderer () {
|
||||||
if (_ownsSkeletonData) spSkeletonData_dispose(_skeleton->data);
|
if (_ownsSkeletonData) spSkeletonData_dispose(_skeleton->data);
|
||||||
if (_atlas) spAtlas_dispose(_atlas);
|
|
||||||
spSkeleton_dispose(_skeleton);
|
spSkeleton_dispose(_skeleton);
|
||||||
FREE(_worldVertices);
|
if (_atlas) spAtlas_dispose(_atlas);
|
||||||
|
if (_attachmentLoader) spAttachmentLoader_dispose(_attachmentLoader);
|
||||||
|
delete _worldVertices;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SkeletonRenderer::initWithData (spSkeletonData* skeletonData, bool ownsSkeletonData) {
|
void SkeletonRenderer::initWithData (spSkeletonData* skeletonData, bool ownsSkeletonData) {
|
||||||
@ -108,7 +108,9 @@ void SkeletonRenderer::initWithData (spSkeletonData* skeletonData, bool ownsSkel
|
|||||||
}
|
}
|
||||||
|
|
||||||
void SkeletonRenderer::initWithFile (const std::string& skeletonDataFile, spAtlas* atlas, float scale) {
|
void SkeletonRenderer::initWithFile (const std::string& skeletonDataFile, spAtlas* atlas, float scale) {
|
||||||
spSkeletonJson* json = spSkeletonJson_create(atlas);
|
_attachmentLoader = SUPER(Cocos2dAttachmentLoader_create(_atlas));
|
||||||
|
|
||||||
|
spSkeletonJson* json = spSkeletonJson_createWithLoader(_attachmentLoader);
|
||||||
json->scale = scale;
|
json->scale = scale;
|
||||||
spSkeletonData* skeletonData = spSkeletonJson_readSkeletonDataFile(json, skeletonDataFile.c_str());
|
spSkeletonData* skeletonData = spSkeletonJson_readSkeletonDataFile(json, skeletonDataFile.c_str());
|
||||||
CCASSERT(skeletonData, json->error ? json->error : "Error reading skeleton data.");
|
CCASSERT(skeletonData, json->error ? json->error : "Error reading skeleton data.");
|
||||||
@ -123,7 +125,9 @@ void SkeletonRenderer::initWithFile (const std::string& skeletonDataFile, const
|
|||||||
_atlas = spAtlas_createFromFile(atlasFile.c_str(), 0);
|
_atlas = spAtlas_createFromFile(atlasFile.c_str(), 0);
|
||||||
CCASSERT(_atlas, "Error reading atlas file.");
|
CCASSERT(_atlas, "Error reading atlas file.");
|
||||||
|
|
||||||
spSkeletonJson* json = spSkeletonJson_create(_atlas);
|
_attachmentLoader = SUPER(Cocos2dAttachmentLoader_create(_atlas));
|
||||||
|
|
||||||
|
spSkeletonJson* json = spSkeletonJson_createWithLoader(_attachmentLoader);
|
||||||
json->scale = scale;
|
json->scale = scale;
|
||||||
spSkeletonData* skeletonData = spSkeletonJson_readSkeletonDataFile(json, skeletonDataFile.c_str());
|
spSkeletonData* skeletonData = spSkeletonJson_readSkeletonDataFile(json, skeletonDataFile.c_str());
|
||||||
CCASSERT(skeletonData, json->error ? json->error : "Error reading skeleton data file.");
|
CCASSERT(skeletonData, json->error ? json->error : "Error reading skeleton data file.");
|
||||||
@ -141,7 +145,6 @@ void SkeletonRenderer::update (float deltaTime) {
|
|||||||
|
|
||||||
void SkeletonRenderer::draw (Renderer* renderer, const Mat4& transform, uint32_t transformFlags) {
|
void SkeletonRenderer::draw (Renderer* renderer, const Mat4& transform, uint32_t transformFlags) {
|
||||||
SkeletonBatch* batch = SkeletonBatch::getInstance();
|
SkeletonBatch* batch = SkeletonBatch::getInstance();
|
||||||
batch->setRendererState(renderer, &transform, transformFlags, _globalZOrder, getGLProgramState(), _premultipliedAlpha);
|
|
||||||
|
|
||||||
Color3B nodeColor = getColor();
|
Color3B nodeColor = getColor();
|
||||||
_skeleton->r = nodeColor.r / (float)255;
|
_skeleton->r = nodeColor.r / (float)255;
|
||||||
@ -149,72 +152,83 @@ void SkeletonRenderer::draw (Renderer* renderer, const Mat4& transform, uint32_t
|
|||||||
_skeleton->b = nodeColor.b / (float)255;
|
_skeleton->b = nodeColor.b / (float)255;
|
||||||
_skeleton->a = getDisplayedOpacity() / (float)255;
|
_skeleton->a = getDisplayedOpacity() / (float)255;
|
||||||
|
|
||||||
int blendMode = -1;
|
|
||||||
Color4B color;
|
Color4B color;
|
||||||
const float* uvs = nullptr;
|
int vertexCount = 0;
|
||||||
int verticesCount = 0;
|
AttachmentVertices* attachmentVertices = nullptr;
|
||||||
const int* triangles = nullptr;
|
for (int i = 0, n = _skeleton->slotsCount; i < n; ++i) {
|
||||||
int trianglesCount = 0;
|
|
||||||
float r = 0, g = 0, b = 0, a = 0;
|
|
||||||
for (int i = 0, n = _skeleton->slotsCount; i < n; i++) {
|
|
||||||
spSlot* slot = _skeleton->drawOrder[i];
|
spSlot* slot = _skeleton->drawOrder[i];
|
||||||
if (!slot->attachment) continue;
|
if (!slot->attachment) continue;
|
||||||
Texture2D *texture = nullptr;
|
|
||||||
switch (slot->attachment->type) {
|
switch (slot->attachment->type) {
|
||||||
case SP_ATTACHMENT_REGION: {
|
case SP_ATTACHMENT_REGION: {
|
||||||
spRegionAttachment* attachment = (spRegionAttachment*)slot->attachment;
|
spRegionAttachment* attachment = (spRegionAttachment*)slot->attachment;
|
||||||
spRegionAttachment_computeWorldVertices(attachment, slot->bone, _worldVertices);
|
spRegionAttachment_computeWorldVertices(attachment, slot->bone, _worldVertices);
|
||||||
texture = getTexture(attachment);
|
attachmentVertices = getAttachmentVertices(attachment);
|
||||||
uvs = attachment->uvs;
|
color.r = attachment->r;
|
||||||
verticesCount = 8;
|
color.g = attachment->g;
|
||||||
triangles = quadTriangles;
|
color.b = attachment->b;
|
||||||
trianglesCount = 6;
|
color.a = attachment->a;
|
||||||
r = attachment->r;
|
|
||||||
g = attachment->g;
|
|
||||||
b = attachment->b;
|
|
||||||
a = attachment->a;
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case SP_ATTACHMENT_MESH: {
|
case SP_ATTACHMENT_MESH: {
|
||||||
spMeshAttachment* attachment = (spMeshAttachment*)slot->attachment;
|
spMeshAttachment* attachment = (spMeshAttachment*)slot->attachment;
|
||||||
spMeshAttachment_computeWorldVertices(attachment, slot, _worldVertices);
|
spMeshAttachment_computeWorldVertices(attachment, slot, _worldVertices);
|
||||||
texture = getTexture(attachment);
|
attachmentVertices = getAttachmentVertices(attachment);
|
||||||
uvs = attachment->uvs;
|
color.r = attachment->r;
|
||||||
verticesCount = attachment->verticesCount;
|
color.g = attachment->g;
|
||||||
triangles = attachment->triangles;
|
color.b = attachment->b;
|
||||||
trianglesCount = attachment->trianglesCount;
|
color.a = attachment->a;
|
||||||
r = attachment->r;
|
|
||||||
g = attachment->g;
|
|
||||||
b = attachment->b;
|
|
||||||
a = attachment->a;
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case SP_ATTACHMENT_WEIGHTED_MESH: {
|
case SP_ATTACHMENT_WEIGHTED_MESH: {
|
||||||
spWeightedMeshAttachment* attachment = (spWeightedMeshAttachment*)slot->attachment;
|
spWeightedMeshAttachment* attachment = (spWeightedMeshAttachment*)slot->attachment;
|
||||||
spWeightedMeshAttachment_computeWorldVertices(attachment, slot, _worldVertices);
|
spWeightedMeshAttachment_computeWorldVertices(attachment, slot, _worldVertices);
|
||||||
texture = getTexture(attachment);
|
attachmentVertices = getAttachmentVertices(attachment);
|
||||||
uvs = attachment->uvs;
|
color.r = attachment->r;
|
||||||
verticesCount = attachment->uvsCount;
|
color.g = attachment->g;
|
||||||
triangles = attachment->triangles;
|
color.b = attachment->b;
|
||||||
trianglesCount = attachment->trianglesCount;
|
color.a = attachment->a;
|
||||||
r = attachment->r;
|
|
||||||
g = attachment->g;
|
|
||||||
b = attachment->b;
|
|
||||||
a = attachment->a;
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default: ;
|
default:
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
if (texture) {
|
|
||||||
color.a = _skeleton->a * slot->a * a * 255;
|
color.a *= _skeleton->a * slot->a * 255;
|
||||||
float multiplier = _premultipliedAlpha ? color.a : 255;
|
float multiplier = _premultipliedAlpha ? color.a : 255;
|
||||||
color.r = _skeleton->r * slot->r * r * multiplier;
|
color.r *= _skeleton->r * slot->r * multiplier;
|
||||||
color.g = _skeleton->g * slot->g * g * multiplier;
|
color.g *= _skeleton->g * slot->g * multiplier;
|
||||||
color.b = _skeleton->b * slot->b * b * multiplier;
|
color.b *= _skeleton->b * slot->b * multiplier;
|
||||||
batch->add(texture, _worldVertices, uvs, verticesCount, triangles, trianglesCount, color, slot->data->blendMode);
|
|
||||||
|
for (int v = 0, w = 0, vn = attachmentVertices->_triangles->vertCount; v < vn; ++v, w += 2) {
|
||||||
|
V3F_C4B_T2F* vertex = attachmentVertices->_triangles->verts + v;
|
||||||
|
vertex->vertices.x = _worldVertices[w];
|
||||||
|
vertex->vertices.y = _worldVertices[w + 1];
|
||||||
|
vertex->colors = color;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BlendFunc blendFunc;
|
||||||
|
switch (slot->data->blendMode) {
|
||||||
|
case SP_BLEND_MODE_ADDITIVE:
|
||||||
|
blendFunc.src = _premultipliedAlpha ? GL_ONE : GL_SRC_ALPHA;
|
||||||
|
blendFunc.dst = GL_ONE;
|
||||||
|
break;
|
||||||
|
case SP_BLEND_MODE_MULTIPLY:
|
||||||
|
blendFunc.src = GL_DST_COLOR;
|
||||||
|
blendFunc.dst = GL_ONE_MINUS_SRC_ALPHA;
|
||||||
|
break;
|
||||||
|
case SP_BLEND_MODE_SCREEN:
|
||||||
|
blendFunc.src = GL_ONE;
|
||||||
|
blendFunc.dst = GL_ONE_MINUS_SRC_COLOR;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
blendFunc.src = _premultipliedAlpha ? GL_ONE : GL_SRC_ALPHA;
|
||||||
|
blendFunc.dst = GL_ONE_MINUS_SRC_ALPHA;
|
||||||
|
}
|
||||||
|
|
||||||
|
batch->addCommand(renderer, _globalZOrder, attachmentVertices->_texture->getName(), _glProgramState, blendFunc,
|
||||||
|
*attachmentVertices->_triangles, transform, transformFlags);
|
||||||
}
|
}
|
||||||
batch->flush();
|
|
||||||
|
|
||||||
if (_debugSlots || _debugBones) {
|
if (_debugSlots || _debugBones) {
|
||||||
_debugCommand.init(_globalZOrder);
|
_debugCommand.init(_globalZOrder);
|
||||||
@ -236,7 +250,7 @@ void SkeletonRenderer::drawDebug (const Mat4 &transform, uint32_t transformFlags
|
|||||||
glLineWidth(1);
|
glLineWidth(1);
|
||||||
Vec2 points[4];
|
Vec2 points[4];
|
||||||
V3F_C4B_T2F_Quad quad;
|
V3F_C4B_T2F_Quad quad;
|
||||||
for (int i = 0, n = _skeleton->slotsCount; i < n; i++) {
|
for (int i = 0, n = _skeleton->slotsCount; i < n; ++i) {
|
||||||
spSlot* slot = _skeleton->drawOrder[i];
|
spSlot* slot = _skeleton->drawOrder[i];
|
||||||
if (!slot->attachment || slot->attachment->type != SP_ATTACHMENT_REGION) continue;
|
if (!slot->attachment || slot->attachment->type != SP_ATTACHMENT_REGION) continue;
|
||||||
spRegionAttachment* attachment = (spRegionAttachment*)slot->attachment;
|
spRegionAttachment* attachment = (spRegionAttachment*)slot->attachment;
|
||||||
@ -252,7 +266,7 @@ void SkeletonRenderer::drawDebug (const Mat4 &transform, uint32_t transformFlags
|
|||||||
// Bone lengths.
|
// Bone lengths.
|
||||||
glLineWidth(2);
|
glLineWidth(2);
|
||||||
DrawPrimitives::setDrawColor4B(255, 0, 0, 255);
|
DrawPrimitives::setDrawColor4B(255, 0, 0, 255);
|
||||||
for (int i = 0, n = _skeleton->bonesCount; i < n; i++) {
|
for (int i = 0, n = _skeleton->bonesCount; i < n; ++i) {
|
||||||
spBone *bone = _skeleton->bones[i];
|
spBone *bone = _skeleton->bones[i];
|
||||||
float x = bone->data->length * bone->a + bone->worldX;
|
float x = bone->data->length * bone->a + bone->worldX;
|
||||||
float y = bone->data->length * bone->c + bone->worldY;
|
float y = bone->data->length * bone->c + bone->worldY;
|
||||||
@ -261,7 +275,7 @@ void SkeletonRenderer::drawDebug (const Mat4 &transform, uint32_t transformFlags
|
|||||||
// Bone origins.
|
// Bone origins.
|
||||||
DrawPrimitives::setPointSize(4);
|
DrawPrimitives::setPointSize(4);
|
||||||
DrawPrimitives::setDrawColor4B(0, 0, 255, 255); // Root bone is blue.
|
DrawPrimitives::setDrawColor4B(0, 0, 255, 255); // Root bone is blue.
|
||||||
for (int i = 0, n = _skeleton->bonesCount; i < n; i++) {
|
for (int i = 0, n = _skeleton->bonesCount; i < n; ++i) {
|
||||||
spBone *bone = _skeleton->bones[i];
|
spBone *bone = _skeleton->bones[i];
|
||||||
DrawPrimitives::drawPoint(Vec2(bone->worldX, bone->worldY));
|
DrawPrimitives::drawPoint(Vec2(bone->worldX, bone->worldY));
|
||||||
if (i == 0) DrawPrimitives::setDrawColor4B(0, 255, 0, 255);
|
if (i == 0) DrawPrimitives::setDrawColor4B(0, 255, 0, 255);
|
||||||
@ -270,16 +284,16 @@ void SkeletonRenderer::drawDebug (const Mat4 &transform, uint32_t transformFlags
|
|||||||
director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
|
director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
|
||||||
}
|
}
|
||||||
|
|
||||||
Texture2D* SkeletonRenderer::getTexture (spRegionAttachment* attachment) const {
|
AttachmentVertices* SkeletonRenderer::getAttachmentVertices (spRegionAttachment* attachment) const {
|
||||||
return (Texture2D*)((spAtlasRegion*)attachment->rendererObject)->page->rendererObject;
|
return (AttachmentVertices*)attachment->rendererObject;
|
||||||
}
|
}
|
||||||
|
|
||||||
Texture2D* SkeletonRenderer::getTexture (spMeshAttachment* attachment) const {
|
AttachmentVertices* SkeletonRenderer::getAttachmentVertices (spMeshAttachment* attachment) const {
|
||||||
return (Texture2D*)((spAtlasRegion*)attachment->rendererObject)->page->rendererObject;
|
return (AttachmentVertices*)attachment->rendererObject;
|
||||||
}
|
}
|
||||||
|
|
||||||
Texture2D* SkeletonRenderer::getTexture (spWeightedMeshAttachment* attachment) const {
|
AttachmentVertices* SkeletonRenderer::getAttachmentVertices (spWeightedMeshAttachment* attachment) const {
|
||||||
return (Texture2D*)((spAtlasRegion*)attachment->rendererObject)->page->rendererObject;
|
return (AttachmentVertices*)attachment->rendererObject;
|
||||||
}
|
}
|
||||||
|
|
||||||
Rect SkeletonRenderer::getBoundingBox () const {
|
Rect SkeletonRenderer::getBoundingBox () const {
|
||||||
|
|||||||
@ -37,7 +37,7 @@
|
|||||||
|
|
||||||
namespace spine {
|
namespace spine {
|
||||||
|
|
||||||
class PolygonBatch;
|
class AttachmentVertices;
|
||||||
|
|
||||||
/* Draws a skeleton. */
|
/* Draws a skeleton. */
|
||||||
class SkeletonRenderer: public cocos2d::Node, public cocos2d::BlendProtocol {
|
class SkeletonRenderer: public cocos2d::Node, public cocos2d::BlendProtocol {
|
||||||
@ -115,12 +115,13 @@ CC_CONSTRUCTOR_ACCESS:
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
void setSkeletonData (spSkeletonData* skeletonData, bool ownsSkeletonData);
|
void setSkeletonData (spSkeletonData* skeletonData, bool ownsSkeletonData);
|
||||||
virtual cocos2d::Texture2D* getTexture (spRegionAttachment* attachment) const;
|
virtual AttachmentVertices* getAttachmentVertices (spRegionAttachment* attachment) const;
|
||||||
virtual cocos2d::Texture2D* getTexture (spMeshAttachment* attachment) const;
|
virtual AttachmentVertices* getAttachmentVertices (spMeshAttachment* attachment) const;
|
||||||
virtual cocos2d::Texture2D* getTexture (spWeightedMeshAttachment* attachment) const;
|
virtual AttachmentVertices* getAttachmentVertices (spWeightedMeshAttachment* attachment) const;
|
||||||
|
|
||||||
bool _ownsSkeletonData;
|
bool _ownsSkeletonData;
|
||||||
spAtlas* _atlas;
|
spAtlas* _atlas;
|
||||||
|
spAttachmentLoader* _attachmentLoader;
|
||||||
cocos2d::CustomCommand _debugCommand;
|
cocos2d::CustomCommand _debugCommand;
|
||||||
cocos2d::BlendFunc _blendFunc;
|
cocos2d::BlendFunc _blendFunc;
|
||||||
float* _worldVertices;
|
float* _worldVertices;
|
||||||
|
|||||||
@ -34,6 +34,7 @@
|
|||||||
|
|
||||||
#include <spine/spine.h>
|
#include <spine/spine.h>
|
||||||
#include "cocos2d.h"
|
#include "cocos2d.h"
|
||||||
|
#include <spine/Cocos2dAttachmentLoader.h>
|
||||||
#include <spine/SkeletonRenderer.h>
|
#include <spine/SkeletonRenderer.h>
|
||||||
#include <spine/SkeletonAnimation.h>
|
#include <spine/SkeletonAnimation.h>
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user