/****************************************************************************** * Spine Runtimes Software License * Version 2.1 * * Copyright (c) 2013, Esoteric Software * All rights reserved. * * You are granted a perpetual, non-exclusive, non-sublicensable and * non-transferable license to install, execute and perform the Spine Runtimes * Software (the "Software") solely for internal use. Without the written * permission of Esoteric Software (typically granted by licensing Spine), 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 SOFTARE 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. *****************************************************************************/ #import #import #import #import static const int quadTriangles[6] = {0, 1, 2, 2, 3, 0}; @interface SkeletonRenderer (Private) - (void) initialize:(spSkeletonData*)skeletonData ownsSkeletonData:(bool)ownsSkeletonData; @end @implementation SkeletonRenderer @synthesize skeleton = _skeleton; @synthesize rootBone = _rootBone; @synthesize debugSlots = _debugSlots; @synthesize debugBones = _debugBones; + (id) skeletonWithData:(spSkeletonData*)skeletonData ownsSkeletonData:(bool)ownsSkeletonData { return [[[self alloc] initWithData:skeletonData ownsSkeletonData:ownsSkeletonData] autorelease]; } + (id) skeletonWithFile:(NSString*)skeletonDataFile atlas:(spAtlas*)atlas scale:(float)scale { return [[[self alloc] initWithFile:skeletonDataFile atlas:atlas scale:scale] autorelease]; } + (id) skeletonWithFile:(NSString*)skeletonDataFile atlasFile:(NSString*)atlasFile scale:(float)scale { return [[[self alloc] initWithFile:skeletonDataFile atlasFile:atlasFile scale:scale] autorelease]; } - (void) initialize:(spSkeletonData*)skeletonData ownsSkeletonData:(bool)ownsSkeletonData { _ownsSkeletonData = ownsSkeletonData; 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]]; [self scheduleUpdate]; } - (id) initWithData:(spSkeletonData*)skeletonData ownsSkeletonData:(bool)ownsSkeletonData { NSAssert(skeletonData, @"skeletonData cannot be null."); self = [super init]; if (!self) return nil; [self initialize:skeletonData ownsSkeletonData:ownsSkeletonData]; return self; } - (id) initWithFile:(NSString*)skeletonDataFile atlas:(spAtlas*)atlas scale:(float)scale { self = [super init]; if (!self) return nil; spSkeletonJson* json = spSkeletonJson_create(atlas); json->scale = scale; spSkeletonData* skeletonData = spSkeletonJson_readSkeletonDataFile(json, [skeletonDataFile UTF8String]); NSAssert(skeletonData, ([NSString stringWithFormat:@"Error reading skeleton data file: %@\nError: %s", skeletonDataFile, json->error])); spSkeletonJson_dispose(json); if (!skeletonData) return 0; [self initialize:skeletonData ownsSkeletonData:YES]; return self; } - (id) initWithFile:(NSString*)skeletonDataFile atlasFile:(NSString*)atlasFile scale:(float)scale { self = [super init]; if (!self) return nil; _atlas = spAtlas_createFromFile([atlasFile UTF8String], 0); NSAssert(_atlas, ([NSString stringWithFormat:@"Error reading atlas file: %@", atlasFile])); if (!_atlas) return 0; spSkeletonJson* json = spSkeletonJson_create(_atlas); json->scale = scale; spSkeletonData* skeletonData = spSkeletonJson_readSkeletonDataFile(json, [skeletonDataFile UTF8String]); NSAssert(skeletonData, ([NSString stringWithFormat:@"Error reading skeleton data file: %@\nError: %s", skeletonDataFile, json->error])); spSkeletonJson_dispose(json); if (!skeletonData) return 0; [self initialize:skeletonData ownsSkeletonData:YES]; return self; } - (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); ccColor3B nodeColor = self.color; _skeleton->r = nodeColor.r / (float)255; _skeleton->g = nodeColor.g / (float)255; _skeleton->b = nodeColor.b / (float)255; _skeleton->a = self.opacity / (float)255; int additive = -1; ccColor4B color; const float* uvs = 0; int verticesCount = 0; const int* triangles = 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; CCTexture2D *texture = 0; switch (slot->attachment->type) { case SP_ATTACHMENT_REGION: { spRegionAttachment* attachment = (spRegionAttachment*)slot->attachment; spRegionAttachment_computeWorldVertices(attachment, slot->skeleton->x, slot->skeleton->y, slot->bone, worldVertices); texture = [self getTextureForRegion:attachment]; uvs = attachment->uvs; verticesCount = 8; triangles = quadTriangles; trianglesCount = 6; r = attachment->r; g = attachment->g; b = attachment->b; a = attachment->a; 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]; uvs = attachment->uvs; verticesCount = attachment->verticesCount; triangles = attachment->triangles; trianglesCount = attachment->trianglesCount; r = attachment->r; g = attachment->g; b = attachment->b; a = attachment->a; 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]; uvs = attachment->uvs; verticesCount = attachment->uvsCount; triangles = attachment->triangles; trianglesCount = attachment->trianglesCount; r = attachment->r; g = attachment->g; b = attachment->b; a = attachment->a; break; } default: ; } if (texture) { if (slot->data->additiveBlending != additive) { [batch flush]; ccGLBlendFunc(_blendFunc.src, slot->data->additiveBlending ? GL_ONE : _blendFunc.dst); additive = slot->data->additiveBlending; } color.a = _skeleton->a * slot->a * a * 255; float multiplier = _premultipliedAlpha ? color.a : 255; 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]; } } [batch flush]; 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]; if (!slot->attachment || slot->attachment->type != SP_ATTACHMENT_REGION) continue; spRegionAttachment* attachment = (spRegionAttachment*)slot->attachment; spRegionAttachment_computeWorldVertices(attachment, slot->skeleton->x, slot->skeleton->y, slot->bone, worldVertices); points[0] = ccp(worldVertices[0], worldVertices[1]); 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); } } 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)); } // 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); } } } - (CCTexture2D*) getTextureForRegion:(spRegionAttachment*)attachment { return (CCTexture2D*)((spAtlasRegion*)attachment->rendererObject)->page->rendererObject; } - (CCTexture2D*) getTextureForMesh:(spMeshAttachment*)attachment { return (CCTexture2D*)((spAtlasRegion*)attachment->rendererObject)->page->rendererObject; } - (CCTexture2D*) getTextureForSkinnedMesh:(spSkinnedMeshAttachment*)attachment { return (CCTexture2D*)((spAtlasRegion*)attachment->rendererObject)->page->rendererObject; } - (CGRect) boundingBox { float minX = FLT_MAX, minY = FLT_MAX, maxX = FLT_MIN, maxY = FLT_MIN; float scaleX = self.scaleX, scaleY = self.scaleY; for (int i = 0; i < _skeleton->slotCount; ++i) { spSlot* slot = _skeleton->slots[i]; if (!slot->attachment) continue; int verticesCount; if (slot->attachment->type == SP_ATTACHMENT_REGION) { spRegionAttachment* attachment = (spRegionAttachment*)slot->attachment; spRegionAttachment_computeWorldVertices(attachment, slot->skeleton->x, slot->skeleton->y, slot->bone, worldVertices); verticesCount = 8; } else if (slot->attachment->type == SP_ATTACHMENT_MESH) { spMeshAttachment* mesh = (spMeshAttachment*)slot->attachment; spMeshAttachment_computeWorldVertices(mesh, slot->skeleton->x, slot->skeleton->y, slot, worldVertices); verticesCount = mesh->verticesCount; } else if (slot->attachment->type == SP_ATTACHMENT_SKINNED_MESH) { spSkinnedMeshAttachment* mesh = (spSkinnedMeshAttachment*)slot->attachment; spSkinnedMeshAttachment_computeWorldVertices(mesh, slot->skeleton->x, slot->skeleton->y, slot, worldVertices); verticesCount = mesh->uvsCount; } 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); } } minX = self.position.x + minX; minY = self.position.y + minY; maxX = self.position.x + maxX; maxY = self.position.y + maxY; return CGRectMake(minX, minY, maxX - minX, maxY - minY); } // --- Convenience methods for Skeleton_* functions. - (void) updateWorldTransform { spSkeleton_updateWorldTransform(_skeleton); } - (void) setToSetupPose { spSkeleton_setToSetupPose(_skeleton); } - (void) setBonesToSetupPose { spSkeleton_setBonesToSetupPose(_skeleton); } - (void) setSlotsToSetupPose { spSkeleton_setSlotsToSetupPose(_skeleton); } - (spBone*) findBone:(NSString*)boneName { return spSkeleton_findBone(_skeleton, [boneName UTF8String]); } - (spSlot*) findSlot:(NSString*)slotName { return spSkeleton_findSlot(_skeleton, [slotName UTF8String]); } - (bool) setSkin:(NSString*)skinName { return (bool)spSkeleton_setSkinByName(_skeleton, skinName ? [skinName UTF8String] : 0); } - (spAttachment*) getAttachment:(NSString*)slotName attachmentName:(NSString*)attachmentName { return spSkeleton_getAttachmentForSlotName(_skeleton, [slotName UTF8String], [attachmentName UTF8String]); } - (bool) setAttachment:(NSString*)slotName attachmentName:(NSString*)attachmentName { return (bool)spSkeleton_setAttachment(_skeleton, [slotName UTF8String], [attachmentName UTF8String]); } // --- CCBlendProtocol - (void) setBlendFunc:(ccBlendFunc)func { self.blendFunc = func; } - (ccBlendFunc) blendFunc { return _blendFunc; } - (void) setOpacityModifyRGB:(BOOL)value { _premultipliedAlpha = value; } - (BOOL) doesOpacityModifyRGB { return _premultipliedAlpha; } @end