CCBatchNode.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /****************************************************************************
  2. Copyright (c) 2013-2017 Chukong Technologies Inc.
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #include "editor-support/cocostudio/CCBatchNode.h"
  21. #include "editor-support/cocostudio/CCArmature.h"
  22. #include "editor-support/cocostudio/CCSkin.h"
  23. #include "renderer/CCRenderer.h"
  24. #include "renderer/CCGroupCommand.h"
  25. #include "renderer/CCGLProgramState.h"
  26. #include "base/CCDirector.h"
  27. using namespace cocos2d;
  28. namespace cocostudio {
  29. BatchNode *BatchNode::create()
  30. {
  31. BatchNode *batchNode = new (std::nothrow) BatchNode();
  32. if (batchNode && batchNode->init())
  33. {
  34. batchNode->autorelease();
  35. return batchNode;
  36. }
  37. CC_SAFE_DELETE(batchNode);
  38. return nullptr;
  39. }
  40. BatchNode::BatchNode()
  41. : _groupCommand(nullptr)
  42. {
  43. }
  44. BatchNode::~BatchNode()
  45. {
  46. CC_SAFE_DELETE(_groupCommand);
  47. }
  48. bool BatchNode::init()
  49. {
  50. bool ret = Node::init();
  51. setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR));
  52. return ret;
  53. }
  54. void BatchNode::addChild(Node *child, int zOrder, int tag)
  55. {
  56. Node::addChild(child, zOrder, tag);
  57. Armature *armature = dynamic_cast<Armature *>(child);
  58. if (armature != nullptr)
  59. {
  60. armature->setBatchNode(this);
  61. if (_groupCommand == nullptr)
  62. {
  63. _groupCommand = new (std::nothrow) GroupCommand();
  64. }
  65. }
  66. }
  67. void BatchNode::addChild(cocos2d::Node *child, int zOrder, const std::string &name)
  68. {
  69. Node::addChild(child, zOrder, name);
  70. Armature *armature = dynamic_cast<Armature *>(child);
  71. if (armature != nullptr)
  72. {
  73. armature->setBatchNode(this);
  74. if (_groupCommand == nullptr)
  75. {
  76. _groupCommand = new (std::nothrow) GroupCommand();
  77. }
  78. }
  79. }
  80. void BatchNode::removeChild(Node* child, bool cleanup)
  81. {
  82. Armature *armature = dynamic_cast<Armature *>(child);
  83. if (armature != nullptr)
  84. {
  85. armature->setBatchNode(nullptr);
  86. }
  87. Node::removeChild(child, cleanup);
  88. }
  89. void BatchNode::visit(Renderer *renderer, const Mat4 &parentTransform, uint32_t parentFlags)
  90. {
  91. // quick return if not visible. children won't be drawn.
  92. if (!_visible)
  93. {
  94. return;
  95. }
  96. uint32_t flags = processParentFlags(parentTransform, parentFlags);
  97. if (isVisitableByVisitingCamera())
  98. {
  99. // IMPORTANT:
  100. // To ease the migration to v3.0, we still support the Mat4 stack,
  101. // but it is deprecated and your code should not rely on it
  102. Director* director = Director::getInstance();
  103. director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
  104. director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, _modelViewTransform);
  105. sortAllChildren();
  106. draw(renderer, _modelViewTransform, flags);
  107. // FIX ME: Why need to set _orderOfArrival to 0??
  108. // Please refer to https://github.com/cocos2d/cocos2d-x/pull/6920
  109. // setOrderOfArrival(0);
  110. director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
  111. }
  112. }
  113. void BatchNode::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
  114. {
  115. if (_children.empty())
  116. {
  117. return;
  118. }
  119. // CC_NODE_DRAW_SETUP();
  120. bool pushed = false;
  121. for(auto object : _children)
  122. {
  123. Armature *armature = dynamic_cast<Armature *>(object);
  124. if (armature)
  125. {
  126. if (!pushed)
  127. {
  128. generateGroupCommand();
  129. pushed = true;
  130. }
  131. armature->visit(renderer, transform, flags);
  132. }
  133. else
  134. {
  135. renderer->popGroup();
  136. pushed = false;
  137. ((Node *)object)->visit(renderer, transform, flags);
  138. }
  139. }
  140. }
  141. void BatchNode::generateGroupCommand()
  142. {
  143. Renderer* renderer = Director::getInstance()->getRenderer();
  144. _groupCommand->init(_globalZOrder);
  145. renderer->addCommand(_groupCommand);
  146. renderer->pushGroup(_groupCommand->getRenderQueueID());
  147. }
  148. }