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;}
三实验现象
总结
以上是内存溢出为你收集整理的Cocos2dx之精灵批处理全部内容,希望文章能够帮你解决Cocos2dx之精灵批处理所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)