CCSpriteBatchNode.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. /****************************************************************************
  2. Copyright (c) 2009-2010 Ricardo Quesada
  3. Copyright (c) 2009 Matt Oswald
  4. Copyright (c) 2010-2012 cocos2d-x.org
  5. Copyright (c) 2011 Zynga Inc.
  6. Copyright (c) 2013-2016 Chukong Technologies Inc.
  7. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  8. http://www.cocos2d-x.org
  9. Permission is hereby granted, free of charge, to any person obtaining a copy
  10. of this software and associated documentation files (the "Software"), to deal
  11. in the Software without restriction, including without limitation the rights
  12. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. copies of the Software, and to permit persons to whom the Software is
  14. furnished to do so, subject to the following conditions:
  15. The above copyright notice and this permission notice shall be included in
  16. all copies or substantial portions of the Software.
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. THE SOFTWARE.
  24. ****************************************************************************/
  25. #include "2d/CCSpriteBatchNode.h"
  26. #include "2d/CCSprite.h"
  27. #include "base/CCDirector.h"
  28. #include "base/CCProfiling.h"
  29. #include "base/ccUTF8.h"
  30. #include "renderer/CCTextureCache.h"
  31. #include "renderer/CCRenderer.h"
  32. #include "renderer/CCQuadCommand.h"
  33. NS_CC_BEGIN
  34. /*
  35. * creation with Texture2D
  36. */
  37. SpriteBatchNode* SpriteBatchNode::createWithTexture(Texture2D* tex, ssize_t capacity/* = DEFAULT_CAPACITY*/)
  38. {
  39. SpriteBatchNode *batchNode = new (std::nothrow) SpriteBatchNode();
  40. if(batchNode && batchNode->initWithTexture(tex, capacity))
  41. {
  42. batchNode->autorelease();
  43. return batchNode;
  44. }
  45. delete batchNode;
  46. return nullptr;
  47. }
  48. /*
  49. * creation with File Image
  50. */
  51. SpriteBatchNode* SpriteBatchNode::create(const std::string& fileImage, ssize_t capacity/* = DEFAULT_CAPACITY*/)
  52. {
  53. SpriteBatchNode *batchNode = new (std::nothrow) SpriteBatchNode();
  54. if(batchNode && batchNode->initWithFile(fileImage, capacity))
  55. {
  56. batchNode->autorelease();
  57. return batchNode;
  58. }
  59. delete batchNode;
  60. return nullptr;
  61. }
  62. /*
  63. * init with Texture2D
  64. */
  65. bool SpriteBatchNode::initWithTexture(Texture2D *tex, ssize_t capacity/* = DEFAULT_CAPACITY*/)
  66. {
  67. if(tex == nullptr)
  68. {
  69. return false;
  70. }
  71. CCASSERT(capacity>=0, "Capacity must be >= 0");
  72. _blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
  73. if(!tex->hasPremultipliedAlpha())
  74. {
  75. _blendFunc = BlendFunc::ALPHA_NON_PREMULTIPLIED;
  76. }
  77. _textureAtlas = new (std::nothrow) TextureAtlas();
  78. if (capacity <= 0)
  79. {
  80. capacity = DEFAULT_CAPACITY;
  81. }
  82. _textureAtlas->initWithTexture(tex, capacity);
  83. updateBlendFunc();
  84. _children.reserve(capacity);
  85. _descendants.reserve(capacity);
  86. setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR, tex));
  87. return true;
  88. }
  89. bool SpriteBatchNode::init()
  90. {
  91. Texture2D * texture = new (std::nothrow) Texture2D();
  92. texture->autorelease();
  93. return this->initWithTexture(texture, 0);
  94. }
  95. /*
  96. * init with FileImage
  97. */
  98. bool SpriteBatchNode::initWithFile(const std::string& fileImage, ssize_t capacity/* = DEFAULT_CAPACITY*/)
  99. {
  100. Texture2D *texture2D = Director::getInstance()->getTextureCache()->addImage(fileImage);
  101. return initWithTexture(texture2D, capacity);
  102. }
  103. SpriteBatchNode::SpriteBatchNode()
  104. : _textureAtlas(nullptr)
  105. {
  106. }
  107. SpriteBatchNode::~SpriteBatchNode()
  108. {
  109. CC_SAFE_RELEASE(_textureAtlas);
  110. }
  111. // override visit
  112. // don't call visit on it's children
  113. void SpriteBatchNode::visit(Renderer *renderer, const Mat4 &parentTransform, uint32_t parentFlags)
  114. {
  115. CC_PROFILER_START_CATEGORY(kProfilerCategoryBatchSprite, "CCSpriteBatchNode - visit");
  116. // CAREFUL:
  117. // This visit is almost identical to CocosNode#visit
  118. // with the exception that it doesn't call visit on it's children
  119. //
  120. // The alternative is to have a void Sprite#visit, but
  121. // although this is less maintainable, is faster
  122. //
  123. if (! _visible)
  124. {
  125. return;
  126. }
  127. sortAllChildren();
  128. uint32_t flags = processParentFlags(parentTransform, parentFlags);
  129. if (isVisitableByVisitingCamera())
  130. {
  131. // IMPORTANT:
  132. // To ease the migration to v3.0, we still support the Mat4 stack,
  133. // but it is deprecated and your code should not rely on it
  134. _director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
  135. _director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, _modelViewTransform);
  136. draw(renderer, _modelViewTransform, flags);
  137. _director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
  138. // FIX ME: Why need to set _orderOfArrival to 0??
  139. // Please refer to https://github.com/cocos2d/cocos2d-x/pull/6920
  140. // setOrderOfArrival(0);
  141. CC_PROFILER_STOP_CATEGORY(kProfilerCategoryBatchSprite, "CCSpriteBatchNode - visit");
  142. }
  143. }
  144. void SpriteBatchNode::addChild(Node *child, int zOrder, int tag)
  145. {
  146. CCASSERT(child != nullptr, "child should not be null");
  147. CCASSERT(dynamic_cast<Sprite*>(child) != nullptr, "CCSpriteBatchNode only supports Sprites as children");
  148. Sprite *sprite = static_cast<Sprite*>(child);
  149. // check Sprite is using the same texture id
  150. CCASSERT(sprite->getTexture()->getName() == _textureAtlas->getTexture()->getName(), "CCSprite is not using the same texture id");
  151. Node::addChild(child, zOrder, tag);
  152. appendChild(sprite);
  153. }
  154. void SpriteBatchNode::addChild(Node * child, int zOrder, const std::string &name)
  155. {
  156. CCASSERT(child != nullptr, "child should not be null");
  157. CCASSERT(dynamic_cast<Sprite*>(child) != nullptr, "CCSpriteBatchNode only supports Sprites as children");
  158. Sprite *sprite = static_cast<Sprite*>(child);
  159. // check Sprite is using the same texture id
  160. CCASSERT(sprite->getTexture()->getName() == _textureAtlas->getTexture()->getName(), "CCSprite is not using the same texture id");
  161. Node::addChild(child, zOrder, name);
  162. appendChild(sprite);
  163. }
  164. // override reorderChild
  165. void SpriteBatchNode::reorderChild(Node *child, int zOrder)
  166. {
  167. CCASSERT(child != nullptr, "the child should not be null");
  168. CCASSERT(_children.contains(child), "Child doesn't belong to Sprite");
  169. if (zOrder == child->getLocalZOrder())
  170. {
  171. return;
  172. }
  173. //set the z-order and sort later
  174. Node::reorderChild(child, zOrder);
  175. }
  176. // override remove child
  177. void SpriteBatchNode::removeChild(Node *child, bool cleanup)
  178. {
  179. Sprite *sprite = static_cast<Sprite*>(child);
  180. // explicit null handling
  181. if (sprite == nullptr)
  182. {
  183. return;
  184. }
  185. CCASSERT(_children.contains(sprite), "sprite batch node should contain the child");
  186. // cleanup before removing
  187. removeSpriteFromAtlas(sprite);
  188. Node::removeChild(sprite, cleanup);
  189. }
  190. void SpriteBatchNode::removeChildAtIndex(ssize_t index, bool doCleanup)
  191. {
  192. CCASSERT(index>=0 && index < _children.size(), "Invalid index");
  193. removeChild(_children.at(index), doCleanup);
  194. }
  195. void SpriteBatchNode::removeAllChildrenWithCleanup(bool doCleanup)
  196. {
  197. // Invalidate atlas index. issue #569
  198. // useSelfRender should be performed on all descendants. issue #1216
  199. for(const auto &sprite: _descendants) {
  200. sprite->setBatchNode(nullptr);
  201. }
  202. Node::removeAllChildrenWithCleanup(doCleanup);
  203. _descendants.clear();
  204. if (_textureAtlas) {_textureAtlas->removeAllQuads();}
  205. }
  206. //override sortAllChildren
  207. void SpriteBatchNode::sortAllChildren()
  208. {
  209. if (_reorderChildDirty)
  210. {
  211. sortNodes(_children);
  212. //sorted now check all children
  213. if (!_children.empty())
  214. {
  215. //first sort all children recursively based on zOrder
  216. for(const auto &child: _children) {
  217. child->sortAllChildren();
  218. }
  219. ssize_t index=0;
  220. //fast dispatch, give every child a new atlasIndex based on their relative zOrder (keep parent -> child relations intact)
  221. // and at the same time reorder descendants and the quads to the right index
  222. for(const auto &child: _children) {
  223. Sprite* sp = static_cast<Sprite*>(child);
  224. updateAtlasIndex(sp, &index);
  225. }
  226. }
  227. _reorderChildDirty=false;
  228. }
  229. }
  230. void SpriteBatchNode::updateAtlasIndex(Sprite* sprite, ssize_t* curIndex)
  231. {
  232. auto& array = sprite->getChildren();
  233. auto count = array.size();
  234. ssize_t oldIndex = 0;
  235. if( count == 0 )
  236. {
  237. oldIndex = sprite->getAtlasIndex();
  238. sprite->setAtlasIndex(*curIndex);
  239. if (oldIndex != *curIndex){
  240. swap(oldIndex, *curIndex);
  241. }
  242. (*curIndex)++;
  243. }
  244. else
  245. {
  246. bool needNewIndex=true;
  247. if (array.at(0)->getLocalZOrder() >= 0)
  248. {
  249. //all children are in front of the parent
  250. oldIndex = sprite->getAtlasIndex();
  251. sprite->setAtlasIndex(*curIndex);
  252. if (oldIndex != *curIndex)
  253. {
  254. swap(oldIndex, *curIndex);
  255. }
  256. (*curIndex)++;
  257. needNewIndex = false;
  258. }
  259. for(const auto &child: array) {
  260. Sprite* sp = static_cast<Sprite*>(child);
  261. if (needNewIndex && sp->getLocalZOrder() >= 0)
  262. {
  263. oldIndex = sprite->getAtlasIndex();
  264. sprite->setAtlasIndex(*curIndex);
  265. if (oldIndex != *curIndex) {
  266. this->swap(oldIndex, *curIndex);
  267. }
  268. (*curIndex)++;
  269. needNewIndex = false;
  270. }
  271. updateAtlasIndex(sp, curIndex);
  272. }
  273. if (needNewIndex)
  274. {//all children have a zOrder < 0)
  275. oldIndex = sprite->getAtlasIndex();
  276. sprite->setAtlasIndex(*curIndex);
  277. if (oldIndex != *curIndex) {
  278. swap(oldIndex, *curIndex);
  279. }
  280. (*curIndex)++;
  281. }
  282. }
  283. }
  284. void SpriteBatchNode::swap(ssize_t oldIndex, ssize_t newIndex)
  285. {
  286. CCASSERT(oldIndex>=0 && oldIndex < (int)_descendants.size() && newIndex >=0 && newIndex < (int)_descendants.size(), "Invalid index");
  287. V3F_C4B_T2F_Quad* quads = _textureAtlas->getQuads();
  288. std::swap( quads[oldIndex], quads[newIndex] );
  289. //update the index of other swapped item
  290. auto oldIt = std::next( _descendants.begin(), oldIndex );
  291. auto newIt = std::next( _descendants.begin(), newIndex );
  292. (*newIt)->setAtlasIndex(oldIndex);
  293. // (*oldIt)->setAtlasIndex(newIndex);
  294. std::swap( *oldIt, *newIt );
  295. }
  296. void SpriteBatchNode::reorderBatch(bool reorder)
  297. {
  298. _reorderChildDirty=reorder;
  299. }
  300. void SpriteBatchNode::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
  301. {
  302. // Optimization: Fast Dispatch
  303. if( _textureAtlas->getTotalQuads() == 0 )
  304. {
  305. return;
  306. }
  307. for (const auto &child : _children)
  308. {
  309. child->updateTransform();
  310. }
  311. _batchCommand.init(_globalZOrder, getGLProgram(), _blendFunc, _textureAtlas, transform, flags);
  312. renderer->addCommand(&_batchCommand);
  313. }
  314. void SpriteBatchNode::increaseAtlasCapacity()
  315. {
  316. // if we're going beyond the current TextureAtlas's capacity,
  317. // all the previously initialized sprites will need to redo their texture coords
  318. // this is likely computationally expensive
  319. ssize_t quantity = (_textureAtlas->getCapacity() + 1) * 4 / 3;
  320. CCLOG("cocos2d: SpriteBatchNode: resizing TextureAtlas capacity from [%d] to [%d].",
  321. static_cast<int>(_textureAtlas->getCapacity()),
  322. static_cast<int>(quantity));
  323. if (! _textureAtlas->resizeCapacity(quantity))
  324. {
  325. // serious problems
  326. CCLOGWARN("cocos2d: WARNING: Not enough memory to resize the atlas");
  327. CCASSERT(false, "Not enough memory to resize the atlas");
  328. }
  329. }
  330. void SpriteBatchNode::reserveCapacity(ssize_t newCapacity)
  331. {
  332. if (newCapacity <= _textureAtlas->getCapacity())
  333. return;
  334. if (! _textureAtlas->resizeCapacity(newCapacity))
  335. {
  336. // serious problems
  337. CCLOGWARN("cocos2d: WARNING: Not enough memory to resize the atlas");
  338. CCASSERT(false, "Not enough memory to resize the atlas");
  339. }
  340. }
  341. ssize_t SpriteBatchNode::rebuildIndexInOrder(Sprite *parent, ssize_t index)
  342. {
  343. CCASSERT(index>=0 && index < _children.size(), "Invalid index");
  344. auto& children = parent->getChildren();
  345. for(const auto &child: children) {
  346. Sprite* sp = static_cast<Sprite*>(child);
  347. if (sp && (sp->getLocalZOrder() < 0))
  348. {
  349. index = rebuildIndexInOrder(sp, index);
  350. }
  351. }
  352. // ignore self (batch node)
  353. if (parent != static_cast<Ref*>(this))
  354. {
  355. parent->setAtlasIndex(index);
  356. index++;
  357. }
  358. for(const auto &child: children) {
  359. Sprite* sp = static_cast<Sprite*>(child);
  360. if (sp && (sp->getLocalZOrder() >= 0))
  361. {
  362. index = rebuildIndexInOrder(sp, index);
  363. }
  364. }
  365. return index;
  366. }
  367. ssize_t SpriteBatchNode::highestAtlasIndexInChild(Sprite *sprite)
  368. {
  369. auto& children = sprite->getChildren();
  370. if (children.empty())
  371. {
  372. return sprite->getAtlasIndex();
  373. }
  374. else
  375. {
  376. return highestAtlasIndexInChild( static_cast<Sprite*>(children.back()));
  377. }
  378. }
  379. ssize_t SpriteBatchNode::lowestAtlasIndexInChild(Sprite *sprite)
  380. {
  381. auto& children = sprite->getChildren();
  382. if (children.size() == 0)
  383. {
  384. return sprite->getAtlasIndex();
  385. }
  386. else
  387. {
  388. return lowestAtlasIndexInChild(static_cast<Sprite*>(children.at(0)));
  389. }
  390. }
  391. ssize_t SpriteBatchNode::atlasIndexForChild(Sprite *sprite, int nZ)
  392. {
  393. auto& siblings = sprite->getParent()->getChildren();
  394. auto childIndex = siblings.getIndex(sprite);
  395. // ignore parent Z if parent is spriteSheet
  396. bool ignoreParent = (SpriteBatchNode*)(sprite->getParent()) == this;
  397. Sprite *prev = nullptr;
  398. if (childIndex > 0 && childIndex != -1)
  399. {
  400. prev = static_cast<Sprite*>(siblings.at(childIndex - 1));
  401. }
  402. // first child of the sprite sheet
  403. if (ignoreParent)
  404. {
  405. if (childIndex == 0)
  406. {
  407. return 0;
  408. }
  409. return highestAtlasIndexInChild(prev) + 1;
  410. }
  411. // parent is a Sprite, so, it must be taken into account
  412. // first child of an Sprite ?
  413. if (childIndex == 0)
  414. {
  415. Sprite *p = static_cast<Sprite*>(sprite->getParent());
  416. // less than parent and brothers
  417. if (nZ < 0)
  418. {
  419. return p->getAtlasIndex();
  420. }
  421. else
  422. {
  423. return p->getAtlasIndex() + 1;
  424. }
  425. }
  426. else
  427. {
  428. // previous & sprite belong to the same branch
  429. if ((prev->getLocalZOrder() < 0 && nZ < 0) || (prev->getLocalZOrder() >= 0 && nZ >= 0))
  430. {
  431. return highestAtlasIndexInChild(prev) + 1;
  432. }
  433. // else (previous < 0 and sprite >= 0 )
  434. Sprite *p = static_cast<Sprite*>(sprite->getParent());
  435. return p->getAtlasIndex() + 1;
  436. }
  437. // Should not happen. Error calculating Z on SpriteSheet
  438. CCASSERT(0, "should not run here");
  439. return 0;
  440. }
  441. // addChild helper, faster than insertChild
  442. void SpriteBatchNode::appendChild(Sprite* sprite)
  443. {
  444. _reorderChildDirty=true;
  445. sprite->setBatchNode(this);
  446. sprite->setDirty(true);
  447. if(_textureAtlas->getTotalQuads() == _textureAtlas->getCapacity()) {
  448. increaseAtlasCapacity();
  449. }
  450. _descendants.push_back(sprite);
  451. int index = static_cast<int>(_descendants.size()-1);
  452. sprite->setAtlasIndex(index);
  453. V3F_C4B_T2F_Quad quad = sprite->getQuad();
  454. _textureAtlas->insertQuad(&quad, index);
  455. // add children recursively
  456. auto& children = sprite->getChildren();
  457. for(const auto &child: children) {
  458. #if CC_SPRITE_DEBUG_DRAW
  459. // when using CC_SPRITE_DEBUG_DRAW, a DrawNode is appended to sprites. remove it since only Sprites can be used
  460. // as children in SpriteBatchNode
  461. // Github issue #14730
  462. if (dynamic_cast<DrawNode*>(child)) {
  463. // to avoid calling Sprite::removeChild()
  464. sprite->Node::removeChild(child, true);
  465. }
  466. else
  467. #else
  468. CCASSERT(dynamic_cast<Sprite*>(child) != nullptr, "You can only add Sprites (or subclass of Sprite) to SpriteBatchNode");
  469. #endif
  470. appendChild(static_cast<Sprite*>(child));
  471. }
  472. }
  473. void SpriteBatchNode::removeSpriteFromAtlas(Sprite *sprite)
  474. {
  475. // remove from TextureAtlas
  476. _textureAtlas->removeQuadAtIndex(sprite->getAtlasIndex());
  477. // Cleanup sprite. It might be reused (issue #569)
  478. sprite->setBatchNode(nullptr);
  479. auto it = std::find(_descendants.begin(), _descendants.end(), sprite );
  480. if( it != _descendants.end() )
  481. {
  482. auto next = std::next(it);
  483. Sprite *spr = nullptr;
  484. for(auto nextEnd = _descendants.end(); next != nextEnd; ++next) {
  485. spr = *next;
  486. spr->setAtlasIndex( spr->getAtlasIndex() - 1 );
  487. }
  488. _descendants.erase(it);
  489. }
  490. // remove children recursively
  491. auto& children = sprite->getChildren();
  492. for(const auto &obj: children) {
  493. Sprite* child = static_cast<Sprite*>(obj);
  494. if (child)
  495. {
  496. removeSpriteFromAtlas(child);
  497. }
  498. }
  499. }
  500. void SpriteBatchNode::updateBlendFunc()
  501. {
  502. if (! _textureAtlas->getTexture()->hasPremultipliedAlpha())
  503. {
  504. _blendFunc = BlendFunc::ALPHA_NON_PREMULTIPLIED;
  505. setOpacityModifyRGB(false);
  506. }
  507. else
  508. {
  509. _blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
  510. setOpacityModifyRGB(true);
  511. }
  512. }
  513. // CocosNodeTexture protocol
  514. void SpriteBatchNode::setBlendFunc(const BlendFunc &blendFunc)
  515. {
  516. _blendFunc = blendFunc;
  517. }
  518. const BlendFunc& SpriteBatchNode::getBlendFunc() const
  519. {
  520. return _blendFunc;
  521. }
  522. Texture2D* SpriteBatchNode::getTexture() const
  523. {
  524. return _textureAtlas->getTexture();
  525. }
  526. void SpriteBatchNode::setTexture(Texture2D *texture)
  527. {
  528. _textureAtlas->setTexture(texture);
  529. updateBlendFunc();
  530. }
  531. // SpriteSheet Extension
  532. //implementation SpriteSheet (TMXTiledMapExtension)
  533. void SpriteBatchNode::insertQuadFromSprite(Sprite *sprite, ssize_t index)
  534. {
  535. CCASSERT( sprite != nullptr, "Argument must be non-nullptr");
  536. CCASSERT( dynamic_cast<Sprite*>(sprite), "CCSpriteBatchNode only supports Sprites as children");
  537. // make needed room
  538. while(index >= _textureAtlas->getCapacity() || _textureAtlas->getCapacity() == _textureAtlas->getTotalQuads())
  539. {
  540. this->increaseAtlasCapacity();
  541. }
  542. //
  543. // update the quad directly. Don't add the sprite to the scene graph
  544. //
  545. sprite->setBatchNode(this);
  546. sprite->setAtlasIndex(index);
  547. V3F_C4B_T2F_Quad quad = sprite->getQuad();
  548. _textureAtlas->insertQuad(&quad, index);
  549. // FIXME:: updateTransform will update the textureAtlas too, using updateQuad.
  550. // FIXME:: so, it should be AFTER the insertQuad
  551. sprite->setDirty(true);
  552. sprite->updateTransform();
  553. }
  554. void SpriteBatchNode::updateQuadFromSprite(Sprite *sprite, ssize_t index)
  555. {
  556. CCASSERT(sprite != nullptr, "Argument must be non-nil");
  557. CCASSERT(dynamic_cast<Sprite*>(sprite) != nullptr, "CCSpriteBatchNode only supports Sprites as children");
  558. // make needed room
  559. while (index >= _textureAtlas->getCapacity() || _textureAtlas->getCapacity() == _textureAtlas->getTotalQuads())
  560. {
  561. this->increaseAtlasCapacity();
  562. }
  563. //
  564. // update the quad directly. Don't add the sprite to the scene graph
  565. //
  566. sprite->setBatchNode(this);
  567. sprite->setAtlasIndex(index);
  568. sprite->setDirty(true);
  569. // UpdateTransform updates the textureAtlas quad
  570. sprite->updateTransform();
  571. }
  572. SpriteBatchNode * SpriteBatchNode::addSpriteWithoutQuad(Sprite*child, int z, int aTag)
  573. {
  574. CCASSERT( child != nullptr, "Argument must be non-nullptr");
  575. CCASSERT( dynamic_cast<Sprite*>(child), "CCSpriteBatchNode only supports Sprites as children");
  576. // quad index is Z
  577. child->setAtlasIndex(z);
  578. // FIXME:: optimize with a binary search
  579. auto it = _descendants.begin();
  580. for (auto itEnd = _descendants.end(); it != itEnd; ++it)
  581. {
  582. if((*it)->getAtlasIndex() >= z)
  583. break;
  584. }
  585. _descendants.insert(it, child);
  586. // IMPORTANT: Call super, and not self. Avoid adding it to the texture atlas array
  587. Node::addChild(child, z, aTag);
  588. //#issue 1262 don't use lazy sorting, tiles are added as quads not as sprites, so sprites need to be added in order
  589. reorderBatch(false);
  590. return this;
  591. }
  592. std::string SpriteBatchNode::getDescription() const
  593. {
  594. return StringUtils::format("<SpriteBatchNode | tag = %d>", _tag);
  595. }
  596. NS_CC_END