5-GL calls
2018-12-08 本文已影响20人
porridgechen890
渲染调用的次数越少,游戏的执行效率越高。
一、渲染一张图片就有一次draw call
bool HelloWorld::init()
{
if ( !Scene::init() )
{
return false;
}
auto visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
auto sp1 = Sprite::create("CloseNormal.png");
sp1->setPosition(Vec2(origin.x + visibleSize.width/2 - 200, origin.y + visibleSize.height/2));
this->addChild(sp1);
auto sp2 = Sprite::create("HelloWorld.png");
sp2->setPosition(Vec2(origin.x + visibleSize.width/2, origin.y + visibleSize.height/2));
this->addChild(sp2);
return true;
}
image.png
这里是2张不同的图,所以GL calls是2。
二、cocos 3.x 版本里自动实现了批量渲染
bool HelloWorld::init()
{
if ( !Scene::init() )
{
return false;
}
auto visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
auto sp1 = Sprite::create("CloseNormal.png");
sp1->setPosition(Vec2(origin.x + visibleSize.width/2 - 200, origin.y + visibleSize.height/2));
this->addChild(sp1);
auto sp2 = Sprite::create("HelloWorld.png");
sp2->setPosition(Vec2(origin.x + visibleSize.width/2, origin.y + visibleSize.height/2));
this->addChild(sp2);
for (int i = 0; i < 5; ++i)
{
auto sp = Sprite::create("CloseNormal.png");
sp->setPosition(Vec2(origin.x + visibleSize.width/2 - 200 + i*100, origin.y + 150));
this->addChild(sp);
}
return true;
}
image.png
比我预想的要多一次,具体原因不清楚。
三、利用SpriteFrameCache来减少GL calls
bool HelloWorld::init()
{
if ( !Scene::init() )
{
return false;
}
SpriteFrameCache::getInstance()->addSpriteFramesWithFile("x.plist");
auto visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
auto sp1 = Sprite::createWithSpriteFrameName("CloseNormal.png");
sp1->setPosition(Vec2(origin.x + visibleSize.width/2 - 200, origin.y + visibleSize.height/2));
this->addChild(sp1);
auto sp2 = Sprite::createWithSpriteFrameName("HelloWorld.png");
sp2->setPosition(Vec2(origin.x + visibleSize.width/2, origin.y + visibleSize.height/2));
this->addChild(sp2);
for (int i = 0; i < 5; ++i)
{
auto sp = Sprite::createWithSpriteFrameName("CloseNormal.png");
sp->setPosition(Vec2(origin.x + visibleSize.width/2 - 200 + i*100, origin.y + 150));
this->addChild(sp);
}
return true;
}
image.png
三、cocos 3.x 版本里的绘制剔除
当一个元素移动到屏幕之外,就不进行绘制。
bool HelloWorld::init()
{
if ( !Scene::init() )
{
return false;
}
auto visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
auto sp1 = Sprite::create("CloseNormal.png");
sp1->setPosition(Vec2(origin.x + visibleSize.width/2 - 200, origin.y + visibleSize.height/2));
this->addChild(sp1);
auto sp2 = Sprite::create("HelloWorld.png");
sp2->setPosition(Vec2(origin.x - sp2->getContentSize().width*10, origin.y + visibleSize.height/2));
this->addChild(sp2);
return true;
}
image.png
三、相关话题
cocos2dx性能优化
https://blog.csdn.net/wxc237786026/article/details/50924408
四、参考文章
https://www.cnblogs.com/zsychanpin/p/7250314.html
https://www.cocos.com/docs/native/v2/optimizations/how-to-optimise-memory-usage/zh.html