Cocos2dx之精灵批处理

C++之旅 QQ群:201704374

伸手党的救星,1T资料等你来战



一、什么是精灵批处理?

设想一下,在图片为精灵的情况下 ,此时精灵为节点,GPU经行渲染一次,如果有很多相同的精灵,那该如何经行渲染呢?渲染是根据节点数来进行渲染的,也就是说节点的数量决定了渲染的效率。如何提高效率呢?我们先用CCSpritebatchNode进行一次渲染,在这渲染之后,就去存入内存或者缓存中,当我们再次创建精灵时,可以通过CCSprite* sprite = CCSprite::createWithTexture(_batchNode->getTexture())中获取,从而解决多次渲染问题,这也是提高效率的方法之一;



二、精灵批处理程序设计

#ifndef __T09BatchNode_H__
#define __T09BatchNode_H__
#include "cocos2d.h"
USING_NS_CC;

class T09BatchNode : public CCLayer
{
public:
	T09BatchNode(void);
	~T09BatchNode(void);

	static CCScene* scene();
	CREATE_FUNC(T09BatchNode);
	bool init();

	bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
	CCSpriteBatchNode* _batchNode;
};

#endif
#include "T09BatchNode.h"


T09BatchNode::T09BatchNode(void)
{
}


T09BatchNode::~T09BatchNode(void)
{
}


CCScene* T09BatchNode::scene()
{
	CCScene* scene = CCScene::create();
	T09BatchNode* layer = T09BatchNode::create();
	scene->addChild(layer);
	return scene;
}

bool T09BatchNode::init()
{
	CCLayer::init();

	CCSpriteBatchNode* batchNode = CCSpriteBatchNode::create("CloseNormal.png");
	addChild(batchNode);
	_batchNode = batchNode;

	setTouchEnabled(true);
	setTouchMode(kCCTouchesOneByOne);
	
	return true;
}

bool T09BatchNode::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
{
	CCSize winSize = CCDirector::sharedDirector()->getWinSize();

	// 创建2千个精灵
	for (int i = 0; i < 2000; i++)
	{
		CCSprite* sprite = CCSprite::createWithTexture(_batchNode->getTexture());
		_batchNode->addChild(sprite);
		sprite->setPosition(ccp(CCRANDOM_0_1() * winSize.width, CCRANDOM_0_1() * winSize.height));
	}
	return true;
}




三实验现象

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值