通过cocos2dx将图像切割成20x20个不同形状的面具的方法

时间:2015-12-04 09:23:43

标签: c++ cocos2d-x

我正在拼图游戏。但现在我一直在关注剪切图像的问题。我有一张高分辨率的图像,我想把它剪成20x20片我已经有了掩码。现在我试图使用ClippingNode,但fps并不令人满意,而且计算位置信息太复杂了。我尝试了渲染,但它&因为我的作品太多了,所以更糟糕.PLZ帮帮我。这是我的演示。

m_obWinsize=CCDirector::sharedDirector()->getWinSize();

CCPoint obOriginPos=ccp(m_obWinsize.width/2,m_obWinsize.height/2);

CCSprite* pBg=CCSprite::create("Preview-20.png");
pBg->setScale(640.0/500);
this->addChild(pBg);

CCSprite* pSizeSp=CCSprite::create("test.jpg");
CCSize obClipSize=pSizeSp->getContentSize();
CCLog("clipsize width %f",obClipSize.width);

pBg->setPosition(ccp(obOriginPos.x,obOriginPos.y));

CCDictionary* pDic=CCDictionary::create();
char szBuf[32]={0};
sprintf(szBuf, "20.txt");
std::string strPath=CCFileUtils::sharedFileUtils()->fullPathForFilename(szBuf);
unsigned long ulSize=0;
unsigned char* pData=CCFileUtils::sharedFileUtils()->getFileData(strPath.c_str(),"rb",&ulSize);
std::string strContent;
if (isFileHasBOM(pData))
{
    strContent = (char const*) (pData + 3);
}
else
{
    strContent = (char const*) pData;
}

delete[] pData;
CC_SAFE_RELEASE_NULL(pDic);
pDic= CCJSONConverter::sharedConverter()->dictionaryFrom(strContent.c_str());
if (pDic == NULL)
{
    CCLog("JSON:%s",cJSON_GetErrorPtr());
}

int a=((CCArray*)pDic->objectForKey("tile"))->count();
CCLog("line %d",a);
float fOffset=obClipSize.width/a;
CCLog("offset %f",fOffset);

for (int i=0; i<a;i++) {
    for (int j=0; j<a; j++) {
        const char* pName=((CCString*)((CCArray*)((CCArray*)pDic->objectForKey("tile"))->objectAtIndex(i))->objectAtIndex(j))->getCString();

        CCSprite* pMask=CCSprite::create(CCString::createWithFormat("B%s.png",pName)->getCString());
        pMask->setScale(0.14f);
        pMask->setAnchorPoint(anchorWithName(pName));
        pMask->setPosition(ccp((j-(a-1)*0.5)*fOffset, ((a-1)*0.5-i)*fOffset));

        CCSprite* pSp=CCSprite::create("test.jpg");

        CCClippingNode* pClipNode=CCClippingNode::create();
        pClipNode->setPosition(obOriginPos);
        pClipNode->setStencil(pMask);
        pClipNode->addChild(pSp);
        pClipNode->setInverted(false);
        pClipNode->setAlphaThreshold(0);
        this->addChild(pClipNode);
    }
 }

使用当前代码,它看起来像这样: enter image description here

0 个答案:

没有答案