Cocos2d-x 分解大图 为 小图 (前提有配置文件)

来源:互联网 发布:五分钟倒计时 js代码 编辑:程序博客网 时间:2024/04/30 09:14
//从大图中截取小图void CpGameScene::getPngFormTexture(const char* texturePath){//载入大图片CCTexture2D *pTexutre2D=CCTextureCache::sharedTextureCache()->addImage(texturePath);//读取pliststring tmpStr;vector<string> altasVec;ifstream tempStream("atlas.txt");while(!(tempStream.eof())){tempStream>>tmpStr;altasVec.push_back(tmpStr);}struct picInfo{string name;float x;float y;float width;float height;};vector<picInfo> picInfoVec;for (UINT i = 0; i < altasVec.size(); i++){picInfo info;info.name="images/"+altasVec[i]+".png";info.width=stringToNum(altasVec[++i]);info.height=stringToNum(altasVec[++i]);info.x=stringToNum(altasVec[++i])*1024;info.y=stringToNum(altasVec[++i])*1024;i+=2;picInfoVec.push_back(info);CCSprite* pSprite=CCSprite::createWithSpriteFrame(CCSpriteFrame::createWithTexture(pTexutre2D,CCRectMake(info.x,info.y,info.width,info.height)));CCPoint p=pSprite->getAnchorPoint();pSprite->setAnchorPoint(ccp(0,0));CCRenderTexture *render=CCRenderTexture::create(pSprite->getContentSize().width,pSprite->getContentSize().height);render->begin();pSprite->visit();render->end();pSprite->setAnchorPoint(p);CCImage* pImage=render->newCCImage(true);pImage->saveToFile(info.name.c_str(),false);}}


配置文件如下:

bg_day 288 512 0.0 0.0 0.28125 0.5bg_night 288 512 0.28515625 0.0 0.28125 0.5bird0_0 48 48 0.0 0.9472656 0.046875 0.046875bird0_1 48 48 0.0546875 0.9472656 0.046875 0.046875bird0_2 48 48 0.109375 0.9472656 0.046875 0.046875bird1_0 48 48 0.1640625 0.9472656 0.046875 0.046875bird1_1 48 48 0.21875 0.6308594 0.046875 0.046875bird1_2 48 48 0.21875 0.6816406 0.046875 0.046875bird2_0 48 48 0.21875 0.7324219 0.046875 0.046875bird2_1 48 48 0.21875 0.7832031 0.046875 0.046875bird2_2 48 48 0.21875 0.8339844 0.046875 0.046875black 32 32 0.5703125 0.40234375 0.03125 0.03125


0 0