1
0

CCSpriteBatchNode.cpp 21 KB

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