2013-06-06 12:02:54 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013 cocos2d-x.org
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "CCSkin.h"
|
|
|
|
#include "../utils/CCTransformHelp.h"
|
|
|
|
|
|
|
|
NS_CC_EXT_BEGIN
|
|
|
|
|
|
|
|
#if CC_SPRITEBATCHNODE_RENDER_SUBPIXEL
|
|
|
|
#define RENDER_IN_SUBPIXEL
|
|
|
|
#else
|
|
|
|
#define RENDER_IN_SUBPIXEL(__ARGS__) (ceil(__ARGS__))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
CCSkin *CCSkin::create()
|
|
|
|
{
|
2013-06-07 10:52:32 +08:00
|
|
|
CCSkin *skin = new CCSkin();
|
|
|
|
if(skin && skin->init())
|
|
|
|
{
|
|
|
|
skin->autorelease();
|
|
|
|
return skin;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(skin);
|
|
|
|
return NULL;
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CCSkin *CCSkin::createWithSpriteFrameName(const char *pszSpriteFrameName)
|
|
|
|
{
|
2013-06-07 10:52:32 +08:00
|
|
|
CCSkin *skin = new CCSkin();
|
|
|
|
if(skin && skin->initWithSpriteFrameName(pszSpriteFrameName))
|
|
|
|
{
|
|
|
|
skin->autorelease();
|
|
|
|
return skin;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(skin);
|
|
|
|
return NULL;
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CCSkin::CCSkin()
|
2013-06-07 10:52:32 +08:00
|
|
|
: m_pBone(NULL)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-06-07 10:52:32 +08:00
|
|
|
void CCSkin::setSkinData(const CCBaseData &var)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-07 10:52:32 +08:00
|
|
|
m_sSkinData = var;
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-06-07 10:52:32 +08:00
|
|
|
setScaleX(m_sSkinData.scaleX);
|
|
|
|
setScaleY(m_sSkinData.scaleY);
|
|
|
|
setRotation(CC_RADIANS_TO_DEGREES(m_sSkinData.skewX));
|
|
|
|
setPosition(ccp(m_sSkinData.x, m_sSkinData.y));
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-06-07 10:52:32 +08:00
|
|
|
m_tSkinTransform = nodeToParentTransform();
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2013-06-06 16:22:58 +08:00
|
|
|
const CCBaseData &CCSkin::getSkinData()
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-07 10:52:32 +08:00
|
|
|
return m_sSkinData;
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCSkin::updateTransform()
|
|
|
|
{
|
2013-06-07 10:52:32 +08:00
|
|
|
m_sTransform = CCAffineTransformConcat(m_tSkinTransform, m_pBone->nodeToArmatureTransform());
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCSkin::draw()
|
|
|
|
{
|
2013-06-07 10:52:32 +08:00
|
|
|
// If it is not visible, or one of its ancestors is not visible, then do nothing:
|
|
|
|
if( !m_bVisible)
|
|
|
|
{
|
|
|
|
m_sQuad.br.vertices = m_sQuad.tl.vertices = m_sQuad.tr.vertices = m_sQuad.bl.vertices = vertex3(0, 0, 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// calculate the Quad based on the Affine Matrix
|
|
|
|
//
|
|
|
|
|
|
|
|
CCSize size = m_obRect.size;
|
|
|
|
|
|
|
|
float x1 = m_obOffsetPosition.x;
|
|
|
|
float y1 = m_obOffsetPosition.y;
|
|
|
|
|
|
|
|
float x2 = x1 + size.width;
|
|
|
|
float y2 = y1 + size.height;
|
|
|
|
|
|
|
|
float x = m_sTransform.tx;
|
|
|
|
float y = m_sTransform.ty;
|
|
|
|
|
|
|
|
float cr = m_sTransform.a;
|
|
|
|
float sr = m_sTransform.b;
|
|
|
|
float cr2 = m_sTransform.d;
|
|
|
|
float sr2 = -m_sTransform.c;
|
|
|
|
float ax = x1 * cr - y1 * sr2 + x;
|
|
|
|
float ay = x1 * sr + y1 * cr2 + y;
|
|
|
|
|
|
|
|
float bx = x2 * cr - y1 * sr2 + x;
|
|
|
|
float by = x2 * sr + y1 * cr2 + y;
|
|
|
|
|
|
|
|
float cx = x2 * cr - y2 * sr2 + x;
|
|
|
|
float cy = x2 * sr + y2 * cr2 + y;
|
|
|
|
|
|
|
|
float dx = x1 * cr - y2 * sr2 + x;
|
|
|
|
float dy = x1 * sr + y2 * cr2 + y;
|
|
|
|
|
|
|
|
m_sQuad.bl.vertices = vertex3( RENDER_IN_SUBPIXEL(ax), RENDER_IN_SUBPIXEL(ay), m_fVertexZ );
|
|
|
|
m_sQuad.br.vertices = vertex3( RENDER_IN_SUBPIXEL(bx), RENDER_IN_SUBPIXEL(by), m_fVertexZ );
|
|
|
|
m_sQuad.tl.vertices = vertex3( RENDER_IN_SUBPIXEL(dx), RENDER_IN_SUBPIXEL(dy), m_fVertexZ );
|
|
|
|
m_sQuad.tr.vertices = vertex3( RENDER_IN_SUBPIXEL(cx), RENDER_IN_SUBPIXEL(cy), m_fVertexZ );
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARMALADE CHANGE: ADDED CHECK FOR NULL, TO PERMIT SPRITES WITH NO BATCH NODE / TEXTURE ATLAS
|
|
|
|
if (m_pobTextureAtlas)
|
|
|
|
{
|
|
|
|
m_pobTextureAtlas->updateQuad(&m_sQuad, m_pobTextureAtlas->getTotalQuads());
|
|
|
|
}
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2013-06-07 10:52:32 +08:00
|
|
|
NS_CC_EXT_END
|