From 81a9c60b2007b764dc18cd02d4eb551af2cdcabc Mon Sep 17 00:00:00 2001 From: NathanSweet Date: Mon, 25 Mar 2013 13:34:38 +0100 Subject: [PATCH] Added debug rendering. Thanks to Stefan Nguyen @ Vinova Pte Ltd! http://www.vinova.sg --- .../spine-cocos2dx/example/ExampleScene.cpp | 5 +- .../include/spine-cocos2dx/CCSkeleton.h | 1 + .../src/spine-cocos2dx/CCSkeleton.cpp | 89 +++++++++++++------ 3 files changed, 68 insertions(+), 27 deletions(-) diff --git a/spine-cocos2dx/spine-cocos2dx/example/ExampleScene.cpp b/spine-cocos2dx/spine-cocos2dx/example/ExampleScene.cpp index 38f6f0e93..24eaae823 100644 --- a/spine-cocos2dx/spine-cocos2dx/example/ExampleScene.cpp +++ b/spine-cocos2dx/spine-cocos2dx/example/ExampleScene.cpp @@ -24,9 +24,10 @@ bool ExampleScene::init() { CCSkeleton* skeletonNode = new CCSkeleton(skeletonData); skeletonNode->state->setAnimation(animation, true); + skeletonNode->debug = true; - CCSize winSize = CCDirector::sharedDirector()->getWinSize(); - skeletonNode->setPosition(ccp(winSize.width / 2, 20)); + CCSize windowSize = CCDirector::sharedDirector()->getWinSize(); + skeletonNode->setPosition(ccp(windowSize.width / 2, 20)); addChild(skeletonNode); return true; diff --git a/spine-cocos2dx/spine-cocos2dx/include/spine-cocos2dx/CCSkeleton.h b/spine-cocos2dx/spine-cocos2dx/include/spine-cocos2dx/CCSkeleton.h index 15a62fff9..5d00920f0 100644 --- a/spine-cocos2dx/spine-cocos2dx/include/spine-cocos2dx/CCSkeleton.h +++ b/spine-cocos2dx/spine-cocos2dx/include/spine-cocos2dx/CCSkeleton.h @@ -39,6 +39,7 @@ class CCSkeleton: public cocos2d::CCNode { public: Skeleton *skeleton; AnimationState *state; + bool debug; CCSkeleton (SkeletonData *skeletonData, AnimationStateData *stateData = 0); virtual ~CCSkeleton (); diff --git a/spine-cocos2dx/spine-cocos2dx/src/spine-cocos2dx/CCSkeleton.cpp b/spine-cocos2dx/spine-cocos2dx/src/spine-cocos2dx/CCSkeleton.cpp index d66037875..d72410ad4 100644 --- a/spine-cocos2dx/spine-cocos2dx/src/spine-cocos2dx/CCSkeleton.cpp +++ b/spine-cocos2dx/spine-cocos2dx/src/spine-cocos2dx/CCSkeleton.cpp @@ -1,39 +1,45 @@ /******************************************************************************* - * Copyright (c) 2013, Esoteric Software - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 THE COPYRIGHT OWNER OR CONTRIBUTORS 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. - ******************************************************************************/ +* Copyright (c) 2013, Esoteric Software +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions are met: +* +* 1. Redistributions of source code must retain the above copyright notice, this +* list of conditions and the following disclaimer. +* 2. Redistributions in binary form must reproduce the above copyright notice, +* this list of conditions and the following disclaimer in the documentation +* and/or other materials provided with the distribution. +* +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 THE COPYRIGHT OWNER OR CONTRIBUTORS 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 #include -#include #include +#include +#include #include #include +#include +#include +#include using namespace spine; USING_NS_CC; -CCSkeleton::CCSkeleton (SkeletonData *skeletonData, AnimationStateData *stateData) { +CCSkeleton::CCSkeleton (SkeletonData *skeletonData, AnimationStateData *stateData) : + debug(false) +{ if (!skeletonData) throw std::invalid_argument("skeletonData cannot be null."); skeleton = new Skeleton(skeletonData); state = new AnimationState(stateData); @@ -58,4 +64,37 @@ void CCSkeleton::draw () { CC_NODE_DRAW_SETUP(); glBlendFunc(GL_ONE,GL_ONE_MINUS_SRC_ALPHA); skeleton->draw(); + + if (debug) { + // Slots. + ccDrawColor4B(0, 0, 255, 10); + glLineWidth(1); + CCPoint points[4]; + for (int i = 0, n = skeleton->slots.size(); i < n; i++) { + if (!skeleton->slots[i]->attachment) continue; + ccV3F_C4B_T2F_Quad quad = ((RegionAttachment*)skeleton->slots[i]->attachment)->quad; + points[0] = ccp(quad.bl.vertices.x, quad.bl.vertices.y); + points[1] = ccp(quad.br.vertices.x, quad.br.vertices.y); + points[2] = ccp(quad.tr.vertices.x, quad.tr.vertices.y); + points[3] = ccp(quad.tl.vertices.x, quad.tl.vertices.y); + ccDrawPoly(points, 4, true); + } + // Bone lengths. + glLineWidth(2); + ccDrawColor4B(255, 0, 0, 255); + for (int i = 0, n = skeleton->bones.size(); i < n; i++) { + Bone *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->bones.size(); i < n; i++) { + Bone *bone = skeleton->bones[i]; + ccDrawPoint(ccp(bone->worldX, bone->worldY)); + if (i == 0) ccDrawColor4B(0, 255, 0, 255); + } + } }