axmol/extensions/cocostudio/CCBatchNode.cpp

170 lines
4.7 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
2020-10-17 16:32:16 +08:00
Copyright (c) 2013-2017 Chukong Technologies Inc.
2019-11-23 20:27:39 +08:00
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
2021-12-25 10:04:45 +08:00
The above copyright notice and this permission notice shall be included in
2019-11-23 20:27:39 +08:00
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "CCBatchNode.h"
#include "CCArmature.h"
#include "CCSkin.h"
2019-11-23 20:27:39 +08:00
#include "renderer/CCRenderer.h"
#include "renderer/CCGroupCommand.h"
#include "base/CCDirector.h"
using namespace cocos2d;
2021-12-25 10:04:45 +08:00
namespace cocostudio
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
BatchNode* BatchNode::create()
{
BatchNode* batchNode = new BatchNode();
2021-12-08 00:11:53 +08:00
if (batchNode->init())
2019-11-23 20:27:39 +08:00
{
batchNode->autorelease();
return batchNode;
}
CC_SAFE_DELETE(batchNode);
return nullptr;
}
2021-12-25 10:04:45 +08:00
BatchNode::BatchNode() : _groupCommand(nullptr) {}
BatchNode::~BatchNode()
2019-11-23 20:27:39 +08:00
{
CC_SAFE_DELETE(_groupCommand);
}
bool BatchNode::init()
{
bool ret = Node::init();
return ret;
}
2021-12-25 10:04:45 +08:00
void BatchNode::addChild(Node* child, int zOrder, int tag)
2019-11-23 20:27:39 +08:00
{
Node::addChild(child, zOrder, tag);
2021-12-25 10:04:45 +08:00
Armature* armature = dynamic_cast<Armature*>(child);
2019-11-23 20:27:39 +08:00
if (armature != nullptr)
{
armature->setBatchNode(this);
if (_groupCommand == nullptr)
{
2021-12-08 00:11:53 +08:00
_groupCommand = new GroupCommand();
2019-11-23 20:27:39 +08:00
}
}
}
2021-12-25 10:04:45 +08:00
void BatchNode::addChild(cocos2d::Node* child, int zOrder, const std::string& name)
2019-11-23 20:27:39 +08:00
{
Node::addChild(child, zOrder, name);
2021-12-25 10:04:45 +08:00
Armature* armature = dynamic_cast<Armature*>(child);
2019-11-23 20:27:39 +08:00
if (armature != nullptr)
{
armature->setBatchNode(this);
if (_groupCommand == nullptr)
{
2021-12-08 00:11:53 +08:00
_groupCommand = new GroupCommand();
2019-11-23 20:27:39 +08:00
}
}
}
void BatchNode::removeChild(Node* child, bool cleanup)
{
2021-12-25 10:04:45 +08:00
Armature* armature = dynamic_cast<Armature*>(child);
2019-11-23 20:27:39 +08:00
if (armature != nullptr)
{
armature->setBatchNode(nullptr);
}
Node::removeChild(child, cleanup);
}
2021-12-25 10:04:45 +08:00
void BatchNode::visit(Renderer* renderer, const Mat4& parentTransform, uint32_t parentFlags)
2019-11-23 20:27:39 +08:00
{
// quick return if not visible. children won't be drawn.
if (!_visible)
{
return;
}
uint32_t flags = processParentFlags(parentTransform, parentFlags);
if (isVisitableByVisitingCamera())
{
// IMPORTANT:
// To ease the migration to v3.0, we still support the Mat4 stack,
// but it is deprecated and your code should not rely on it
Director* director = Director::getInstance();
director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, _modelViewTransform);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
sortAllChildren();
draw(renderer, _modelViewTransform, flags);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// FIX ME: Why need to set _orderOfArrival to 0??
// Please refer to https://github.com/cocos2d/cocos2d-x/pull/6920
// setOrderOfArrival(0);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
}
}
2021-12-25 10:04:45 +08:00
void BatchNode::draw(Renderer* renderer, const Mat4& transform, uint32_t flags)
2019-11-23 20:27:39 +08:00
{
if (_children.empty())
{
return;
}
2021-12-25 10:04:45 +08:00
// CC_NODE_DRAW_SETUP();
2019-11-23 20:27:39 +08:00
bool pushed = false;
2021-12-25 10:04:45 +08:00
for (auto object : _children)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
Armature* armature = dynamic_cast<Armature*>(object);
2019-11-23 20:27:39 +08:00
if (armature)
{
if (!pushed)
{
generateGroupCommand();
pushed = true;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
armature->visit(renderer, transform, flags);
}
else
{
renderer->popGroup();
pushed = false;
2021-12-25 10:04:45 +08:00
((Node*)object)->visit(renderer, transform, flags);
2019-11-23 20:27:39 +08:00
}
}
}
void BatchNode::generateGroupCommand()
{
Renderer* renderer = Director::getInstance()->getRenderer();
_groupCommand->init(_globalZOrder);
renderer->addCommand(_groupCommand);
renderer->pushGroup(_groupCommand->getRenderQueueID());
}
2021-12-25 10:04:45 +08:00
} // namespace cocostudio