cocos2d-x 内存和效率的优化(一)

来源:互联网 发布:男生护肤 知乎 编辑:程序博客网 时间:2024/06/08 08:12
首先如往常般的建立一个 cocos2d 项目在 VC2010 Express 里 (为方便起, 我会在 Windows 平台上写和测试, 再移植到 iOS 和Android):

 


建立了这个叫 Demo 的项目後, 我们再把一些图抄到 Resources 里:



 


把这些图像画到萤幕上, 最简单就是把每个图放进一个个的 CCSprite然後加到当前的 Layer 上. 以下这段代码就是把 Background.png, Grass Block.png 和 p8.png 显示在画面:

复制代码
  1.   CCSize size = CCDirector::sharedDirector()->getWinSize();
  2.  
  3.         CCSprite* pSprite = CCSprite::spriteWithFile("Background.png");
  4.         CC_BREAK_IF(! pSprite);
  5.  
  6.         pSprite->setPosition(*****(size.width/2, size.height/2));
  7.         this->addChild(pSprite, 0);
  8.  
  9.         pSprite = CCSprite::spriteWithFile("Grass Block.png");
  10.         CC_BREAK_IF(! pSprite);
  11.  
  12.         pSprite->setPosition(*****(size.width/2, size.height/2));
  13.         this->addChild(pSprite, 0);
  14.  
  15.         pSprite = CCSprite::spriteWithFile("p8.png");
  16.         CC_BREAK_IF(! pSprite);
  17.  
  18.         CCSize dim =  pSprite->getContentSize();
  19.         pSprite->setPosition(*****(size.width/2, size.height/2+dim.height/2));
  20.         this->addChild(pSprite, 0);




程序跑起来就会看到下边这个画面:



 


不过我们最终的目标是把遊戏放到 iOS 或 Android 机子上玩, 所以我们不能不考虑一下有关OpenGL ES 优化的问题, 两大关键问题就是内存(显存)运用和速度.

先看一下内存问题, OpenGL ES  纹理的宽和高都要是2的倍数, 以刚才的例子来说, 虽然 Background.png 本身是 480x320, 但在载入内存後, 它其实会被变成一张 512x512 的纹理, 而Grass Block.png 则由 101x131 变成 128x256, 我们可以看到如此这样会造成不少浪费.

再看看关於渲染速度方面, OpenGL ES 上来说我们应该尽量减少渲染时切换纹理和 glDrawArray 的呼叫, 刚才的例子每画一个图像都会切换一次纹理并呼叫一次 glDrawArray , 我们这里只画3样东西, 所以不会看到有什麽问题, 但如果我们要渲染几十个甚至几百个图像 , 速度上就会被拖慢. 很明显这并不是我们所想要的.

那我们应该怎麽解决这些问题呢? 答案就是利用纹理地图(texture atlas), 比如下面这张纹理就是把我们想用的图像都合併在一起, 而它的大小正好是 512x512:


 


如果人手去做这个合併工作就太痛苦了, 这里要向大家推荐一个十分好用的工具: TexturePacker! (http://www.texturepacker.com/) 这个工具直接支持 cocos2d, 实在是太方便了!

首先我们把想要用的图像都放到一个目录里, 再用TexturePacker 的 "Add Folder"  功能把目录加进去, TexturePacker 的默认输出格式就是 cocos2d:



 

为了节省位置, 我们可以把Border padding 和Shape Padding 都设为1, 而选了 Allow rotation 可以让 TexturePacker 更为有效率的摆放图像在纹理里:


 

在键入了输出的档案名字後, 我们就可以用 Publish 把纹理输出.

接下来, 我们把输出的两个档案(我们这里的例子是images.plist 和 images.png) 放到 Resources 里, 就可以在程序里用 CCSpriteFrameCache 把纹理和有关资料载入:

复制代码
  1. cache->addSpriteFramesWithFile("images.plist", "images.png");



但现在我们只有一张叫 "images.png" 的纹理, 那麽怎样去调用比如是 Background.png 呢? 当然我们还是用 CCSprite 做渲染图像的工作, 但在建立一个 CCSprite  时, 我们换为用 spriteWithSpriteFrameName 而不是 spriteWithFile:

复制代码
  1.  
  2. CCSprite* pSprite = CCSprite::spriteWithSpriteFrameName(" Background.png");
  3.  
  4. CCSpriteFrameCache *cache = CCSpriteFrameCache::sharedSpriteFrameCache();
  5.         cache->addSpriteFramesWithFile("images.plist", "images.png");
  6.  
  7.         // Get window size and place the label upper.
  8.         CCSize size = CCDirector::sharedDirector()->getWinSize();
  9.  
  10.         CCSprite* pSprite = CCSprite::spriteWithSpriteFrameName("Background.png");
  11.         CC_BREAK_IF(! pSprite);
  12.  
  13.         pSprite->setPosition(*****(size.width/2, size.height/2));
  14.         this->addChild(pSprite, 0);
  15.  
  16.         pSprite = CCSprite::spriteWithSpriteFrameName("Grass Block.png");
  17.         CC_BREAK_IF(! pSprite);
  18.  
  19.         pSprite->setPosition(*****(size.width/2, size.height/2));
  20.         this->addChild(pSprite, 0);
  21.  
  22.         pSprite = CCSprite::spriteWithSpriteFrameName("p8.png");
  23.         CC_BREAK_IF(! pSprite);
  24.  
  25.         CCSize dim = pSprite->getContentSize();
  26.         pSprite->setPosition(*****(size.width/2, size.height/2+dim.height/2));
  27.         this->addChild(pSprite, 0);
  28.  
  29.  


来到这里, 我们已经逹到了节省内存和减少纹理切换, 最後一个我们想做的优化是减少 glDrawArray 的次数, 而我们所运用的技巧, 就是批次渲染(Batch Rendering), cocos2d 提供了CCSpriteBatchNode 来方便大家做有关的处理, CCSpriteBatchNode 里的CCSprite 都是要用同一个纹理的, 所以我们在建立一个 CCSpriteBatchNode 是要给它一个纹理, 再把它加到 Layer 里 :

复制代码
  1. CCTexture2D *texture = CCTextureCache::sharedTextureCache()->textureForKey("images.png");
  2.         CCSpriteBatchNode *spriteBatch = CCSpriteBatchNode::batchNodeWithTexture(texture);
  3.         addChild(spriteBatch);


接下来我们如常的建立各个 CCSprite, 但不同的地方是我们不把它们加在 Layer 里而是把它们直接加到 CCSpriteBatchNode 上:

复制代码
  1.  
  2. CCSpriteFrameCache *cache = CCSpriteFrameCache::sharedSpriteFrameCache();
  3.         cache->addSpriteFramesWithFile("images.plist", "images.png");
  4.  
  5.         CCTexture2D *texture = CCTextureCache::sharedTextureCache()->textureForKey("images.png");
  6.         CCSpriteBatchNode *spriteBatch = CCSpriteBatchNode::batchNodeWithTexture(texture);
  7.         addChild(spriteBatch);
  8.  
  9.         // Get window size and place the label upper.
  10.         CCSize size = CCDirector::sharedDirector()->getWinSize();
  11.  
  12.         CCSprite* pSprite = CCSprite::spriteWithSpriteFrameName("Background.png");
  13.  
  14.         pSprite->setPosition(*****(size.width/2, size.height/2));
  15.         spriteBatch->addChild(pSprite, 0);
  16.  
  17.         pSprite = CCSprite::spriteWithSpriteFrameName("Grass Block.png");
  18.  
  19.         pSprite->setPosition(*****(size.width/2, size.height/2));
  20.         spriteBatch->addChild(pSprite, 0);
  21.  
  22.         pSprite = CCSprite::spriteWithSpriteFrameName("p8.png");
  23.  
  24.         CCSize dim = pSprite->getContentSize();
  25.         pSprite->setPosition(*****(size.width/2, size.height/2+dim.height/2));
  26.         spriteBatch->addChild(pSprite, 0);
  27.  


 

原创粉丝点击