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

//从大图中截取小图
void CpGameScene::getPngFormTexture(const char* texturePath)
{
	//载入大图片
	CCTexture2D *pTexutre2D=CCTextureCache::sharedTextureCache()->addImage(texturePath);

	//读取plist
	string 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.5
bg_night 288 512 0.28515625 0.0 0.28125 0.5
bird0_0 48 48 0.0 0.9472656 0.046875 0.046875
bird0_1 48 48 0.0546875 0.9472656 0.046875 0.046875
bird0_2 48 48 0.109375 0.9472656 0.046875 0.046875
bird1_0 48 48 0.1640625 0.9472656 0.046875 0.046875
bird1_1 48 48 0.21875 0.6308594 0.046875 0.046875
bird1_2 48 48 0.21875 0.6816406 0.046875 0.046875
bird2_0 48 48 0.21875 0.7324219 0.046875 0.046875
bird2_1 48 48 0.21875 0.7832031 0.046875 0.046875
bird2_2 48 48 0.21875 0.8339844 0.046875 0.046875
black 32 32 0.5703125 0.40234375 0.03125 0.03125


你可能感兴趣的:(Cocos2d-x 分解大图 为 小图 (前提有配置文件))