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; }
三实验现象
原文链接:https://www.f2er.com/cocos2dx/341976.html