|
@@ -31,9 +31,8 @@
|
|
|
#import <spine/SkeletonRenderer.h>
|
|
|
#import <spine/spine-cocos2d-iphone.h>
|
|
|
#import <spine/extension.h>
|
|
|
-#import <spine/PolygonBatch.h>
|
|
|
#import "CCNode_Private.h"
|
|
|
-#import "CCDrawingPrimitives.h"
|
|
|
+#import "CCDrawNode.h"
|
|
|
|
|
|
static const int quadTriangles[6] = {0, 1, 2, 2, 3, 0};
|
|
|
|
|
@@ -65,16 +64,16 @@ static const int quadTriangles[6] = {0, 1, 2, 2, 3, 0};
|
|
|
|
|
|
worldVertices = MALLOC(float, 1000); // Max number of vertices per mesh.
|
|
|
|
|
|
- batch = [[spPolygonBatch createWithCapacity:2000] retain]; // Max number of vertices and triangles per batch.
|
|
|
-
|
|
|
_skeleton = spSkeleton_create(skeletonData);
|
|
|
_rootBone = _skeleton->bones[0];
|
|
|
-
|
|
|
- _blendFunc.src = GL_ONE;
|
|
|
- _blendFunc.dst = GL_ONE_MINUS_SRC_ALPHA;
|
|
|
- [self setOpacityModifyRGB:YES];
|
|
|
-
|
|
|
- [self setShaderProgram:[[CCShaderCache sharedShaderCache] programForKey:kCCShader_PositionTextureColor]];
|
|
|
+
|
|
|
+ _blendFunc.src = GL_ONE;
|
|
|
+ _blendFunc.dst = GL_ONE_MINUS_SRC_ALPHA;
|
|
|
+ _drawNode = [[CCDrawNode alloc] init];
|
|
|
+ [_drawNode setBlendMode: [CCBlendMode premultipliedAlphaMode]];
|
|
|
+ [self addChild:_drawNode];
|
|
|
+
|
|
|
+ [self setShader:[CCShader positionTextureColorShader]];
|
|
|
}
|
|
|
|
|
|
- (id) initWithData:(spSkeletonData*)skeletonData ownsSkeletonData:(bool)ownsSkeletonData {
|
|
@@ -125,18 +124,16 @@ static const int quadTriangles[6] = {0, 1, 2, 2, 3, 0};
|
|
|
}
|
|
|
|
|
|
- (void) dealloc {
|
|
|
+
|
|
|
if (_ownsSkeletonData) spSkeletonData_dispose(_skeleton->data);
|
|
|
if (_atlas) spAtlas_dispose(_atlas);
|
|
|
spSkeleton_dispose(_skeleton);
|
|
|
- [batch release];
|
|
|
FREE(worldVertices);
|
|
|
[super dealloc];
|
|
|
}
|
|
|
|
|
|
-- (void) draw {
|
|
|
- CC_NODE_DRAW_SETUP();
|
|
|
- ccGLBindVAO(0);
|
|
|
-
|
|
|
+-(void)draw:(CCRenderer *)renderer transform:(const GLKMatrix4 *)transform {
|
|
|
+
|
|
|
CCColor* nodeColor = self.color;
|
|
|
_skeleton->r = nodeColor.red;
|
|
|
_skeleton->g = nodeColor.green;
|
|
@@ -151,6 +148,7 @@ static const int quadTriangles[6] = {0, 1, 2, 2, 3, 0};
|
|
|
int trianglesCount = 0;
|
|
|
float r = 0, g = 0, b = 0, a = 0;
|
|
|
for (int i = 0, n = _skeleton->slotCount; i < n; i++) {
|
|
|
+
|
|
|
spSlot* slot = _skeleton->drawOrder[i];
|
|
|
if (!slot->attachment) continue;
|
|
|
CCTexture *texture = 0;
|
|
@@ -170,6 +168,7 @@ static const int quadTriangles[6] = {0, 1, 2, 2, 3, 0};
|
|
|
break;
|
|
|
}
|
|
|
case SP_ATTACHMENT_MESH: {
|
|
|
+
|
|
|
spMeshAttachment* attachment = (spMeshAttachment*)slot->attachment;
|
|
|
spMeshAttachment_computeWorldVertices(attachment, slot->skeleton->x, slot->skeleton->y, slot, worldVertices);
|
|
|
texture = [self getTextureForMesh:attachment];
|
|
@@ -184,6 +183,7 @@ static const int quadTriangles[6] = {0, 1, 2, 2, 3, 0};
|
|
|
break;
|
|
|
}
|
|
|
case SP_ATTACHMENT_SKINNED_MESH: {
|
|
|
+
|
|
|
spSkinnedMeshAttachment* attachment = (spSkinnedMeshAttachment*)slot->attachment;
|
|
|
spSkinnedMeshAttachment_computeWorldVertices(attachment, slot->skeleton->x, slot->skeleton->y, slot, worldVertices);
|
|
|
texture = [self getTextureForSkinnedMesh:attachment];
|
|
@@ -201,8 +201,7 @@ static const int quadTriangles[6] = {0, 1, 2, 2, 3, 0};
|
|
|
}
|
|
|
if (texture) {
|
|
|
if (slot->data->additiveBlending != additive) {
|
|
|
- [batch flush];
|
|
|
- ccGLBlendFunc(_blendFunc.src, slot->data->additiveBlending ? GL_ONE : _blendFunc.dst);
|
|
|
+ [self setBlendMode:[CCBlendMode blendModeWithOptions:@{CCBlendFuncSrcColor: @(_blendFunc.src),CCBlendFuncDstColor: @(slot->data->additiveBlending ? GL_ONE : _blendFunc.dst)}]];
|
|
|
additive = slot->data->additiveBlending;
|
|
|
}
|
|
|
color.a = _skeleton->a * slot->a * a * 255;
|
|
@@ -210,16 +209,29 @@ static const int quadTriangles[6] = {0, 1, 2, 2, 3, 0};
|
|
|
color.r = _skeleton->r * slot->r * r * multiplier;
|
|
|
color.g = _skeleton->g * slot->g * g * multiplier;
|
|
|
color.b = _skeleton->b * slot->b * b * multiplier;
|
|
|
- [batch add:texture vertices:worldVertices uvs:uvs verticesCount:verticesCount
|
|
|
- triangles:triangles trianglesCount:trianglesCount color:&color];
|
|
|
+ self.texture = texture;
|
|
|
+ CGSize size = texture.contentSize;
|
|
|
+ GLKVector2 center = GLKVector2Make(size.width/2.0, size.height/2.0);
|
|
|
+ GLKVector2 extents = GLKVector2Make(size.width/2.0, size.height/2.0);
|
|
|
+ if(CCRenderCheckVisbility(transform, center, extents)){
|
|
|
+ CCRenderBuffer buffer = [renderer enqueueTriangles:trianglesCount/3 andVertexes:verticesCount withState:self.renderState globalSortOrder:0];
|
|
|
+ for(int i = 0; (i*2) < verticesCount; ++i) {
|
|
|
+ CCVertex vertex;
|
|
|
+ vertex.position = GLKVector4Make(worldVertices[i*2], worldVertices[i*2 + 1], 0.0, 1.0);
|
|
|
+ vertex.color = GLKVector4Make(color.r, color.g, color.b, color.a);
|
|
|
+ vertex.texCoord1 = GLKVector2Make(uvs[i*2], 1 - uvs[(i*2) +1]);
|
|
|
+ CCRenderBufferSetVertex(buffer, i, CCVertexApplyTransform(vertex, transform));
|
|
|
+ }
|
|
|
+ for(int j = 0; j*3 < trianglesCount; ++j) {
|
|
|
+ CCRenderBufferSetTriangle(buffer, j, triangles[j*3], triangles[(j*3) + 1], triangles[(j*3) + 2]);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
}
|
|
|
- [batch flush];
|
|
|
-
|
|
|
+ [_drawNode clear];
|
|
|
if (_debugSlots) {
|
|
|
// Slots.
|
|
|
- ccDrawColor4B(0, 0, 255, 255);
|
|
|
- glLineWidth(1);
|
|
|
CGPoint points[4];
|
|
|
for (int i = 0, n = _skeleton->slotCount; i < n; i++) {
|
|
|
spSlot* slot = _skeleton->drawOrder[i];
|
|
@@ -230,26 +242,24 @@ static const int quadTriangles[6] = {0, 1, 2, 2, 3, 0};
|
|
|
points[1] = ccp(worldVertices[2], worldVertices[3]);
|
|
|
points[2] = ccp(worldVertices[4], worldVertices[5]);
|
|
|
points[3] = ccp(worldVertices[6], worldVertices[7]);
|
|
|
- ccDrawPoly(points, 4, true);
|
|
|
+ [_drawNode drawPolyWithVerts:points count:4 fillColor:[CCColor clearColor] borderWidth:1 borderColor:[CCColor blueColor]];
|
|
|
+
|
|
|
}
|
|
|
}
|
|
|
if (_debugBones) {
|
|
|
// Bone lengths.
|
|
|
- glLineWidth(2);
|
|
|
- ccDrawColor4B(255, 0, 0, 255);
|
|
|
for (int i = 0, n = _skeleton->boneCount; i < n; i++) {
|
|
|
spBone *bone = _skeleton->bones[i];
|
|
|
float x = bone->data->length * bone->m00 + bone->worldX;
|
|
|
float y = bone->data->length * bone->m10 + bone->worldY;
|
|
|
- ccDrawLine(ccp(bone->worldX, bone->worldY), ccp(x, y));
|
|
|
+ [_drawNode drawSegmentFrom:ccp(bone->worldX, bone->worldY) to: ccp(x, y)radius:2 color:[CCColor redColor]];
|
|
|
}
|
|
|
+
|
|
|
// Bone origins.
|
|
|
- ccPointSize(4);
|
|
|
- ccDrawColor4B(0, 0, 255, 255); // Root bone is blue.
|
|
|
for (int i = 0, n = _skeleton->boneCount; i < n; i++) {
|
|
|
spBone *bone = _skeleton->bones[i];
|
|
|
- ccDrawPoint(ccp(bone->worldX, bone->worldY));
|
|
|
- if (i == 0) ccDrawColor4B(0, 255, 0, 255);
|
|
|
+ [_drawNode drawDot:ccp(bone->worldX, bone->worldY) radius:4 color:[CCColor greenColor]];
|
|
|
+ if (i == 0) [_drawNode drawDot:ccp(bone->worldX, bone->worldY) radius:4 color:[CCColor blueColor]];
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -289,10 +299,10 @@ static const int quadTriangles[6] = {0, 1, 2, 2, 3, 0};
|
|
|
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);
|
|
|
+ minX = fmin(minX, x);
|
|
|
+ minY = fmin(minY, y);
|
|
|
+ maxX = fmax(maxX, x);
|
|
|
+ maxY = fmax(maxY, y);
|
|
|
}
|
|
|
}
|
|
|
minX = self.position.x + minX;
|