2013-06-06 12:02:54 +08:00
|
|
|
/****************************************************************************
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2013-06-06 12:02:54 +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:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-03-20 21:53:44 +08:00
|
|
|
#include "editor-support/cocostudio/CCBatchNode.h"
|
|
|
|
#include "editor-support/cocostudio/CCArmature.h"
|
|
|
|
#include "editor-support/cocostudio/CCSkin.h"
|
2014-01-17 13:35:58 +08:00
|
|
|
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "renderer/CCRenderer.h"
|
|
|
|
#include "renderer/CCGroupCommand.h"
|
2014-05-10 09:39:25 +08:00
|
|
|
#include "renderer/CCGLProgramState.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/CCDirector.h"
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-10-15 18:00:03 +08:00
|
|
|
using namespace cocos2d;
|
|
|
|
|
|
|
|
namespace cocostudio {
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
BatchNode *BatchNode::create()
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
BatchNode *batchNode = new (std::nothrow) BatchNode();
|
2013-06-07 10:52:32 +08:00
|
|
|
if (batchNode && batchNode->init())
|
|
|
|
{
|
|
|
|
batchNode->autorelease();
|
|
|
|
return batchNode;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(batchNode);
|
2013-11-05 15:32:13 +08:00
|
|
|
return nullptr;
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
BatchNode::BatchNode()
|
2013-12-26 18:11:23 +08:00
|
|
|
: _groupCommand(nullptr)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-10-30 09:41:40 +08:00
|
|
|
BatchNode::~BatchNode()
|
|
|
|
{
|
2013-12-26 18:11:23 +08:00
|
|
|
CC_SAFE_DELETE(_groupCommand);
|
2013-10-30 09:41:40 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
bool BatchNode::init()
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
bool ret = Node::init();
|
2014-05-13 10:12:56 +08:00
|
|
|
setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR));
|
2013-10-30 09:41:40 +08:00
|
|
|
|
2013-06-07 10:52:32 +08:00
|
|
|
return ret;
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2014-06-25 11:27:48 +08:00
|
|
|
void BatchNode::addChild(Node *child, int zOrder, int tag)
|
2013-10-30 09:41:40 +08:00
|
|
|
{
|
2014-06-25 11:27:48 +08:00
|
|
|
Node::addChild(child, zOrder, tag);
|
|
|
|
Armature *armature = dynamic_cast<Armature *>(child);
|
|
|
|
if (armature != nullptr)
|
|
|
|
{
|
|
|
|
armature->setBatchNode(this);
|
|
|
|
if (_groupCommand == nullptr)
|
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
_groupCommand = new (std::nothrow) GroupCommand();
|
2014-06-25 11:27:48 +08:00
|
|
|
}
|
|
|
|
}
|
2013-10-30 09:41:40 +08:00
|
|
|
}
|
|
|
|
|
2014-06-25 11:27:48 +08:00
|
|
|
void BatchNode::addChild(cocos2d::Node *child, int zOrder, const std::string &name)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2014-06-25 11:27:48 +08:00
|
|
|
Node::addChild(child, zOrder, name);
|
2013-06-20 14:15:53 +08:00
|
|
|
Armature *armature = dynamic_cast<Armature *>(child);
|
2013-11-05 15:32:13 +08:00
|
|
|
if (armature != nullptr)
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
|
|
|
armature->setBatchNode(this);
|
2013-12-26 18:11:23 +08:00
|
|
|
if (_groupCommand == nullptr)
|
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
_groupCommand = new (std::nothrow) GroupCommand();
|
2013-12-26 18:11:23 +08:00
|
|
|
}
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2013-10-30 09:41:40 +08:00
|
|
|
void BatchNode::removeChild(Node* child, bool cleanup)
|
|
|
|
{
|
|
|
|
Armature *armature = dynamic_cast<Armature *>(child);
|
2013-11-05 15:32:13 +08:00
|
|
|
if (armature != nullptr)
|
2013-10-30 09:41:40 +08:00
|
|
|
{
|
2013-11-05 15:32:13 +08:00
|
|
|
armature->setBatchNode(nullptr);
|
2013-10-30 09:41:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Node::removeChild(child, cleanup);
|
|
|
|
}
|
|
|
|
|
2014-05-31 07:42:05 +08:00
|
|
|
void BatchNode::visit(Renderer *renderer, const Mat4 &parentTransform, uint32_t parentFlags)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-07 10:52:32 +08:00
|
|
|
// quick return if not visible. children won't be drawn.
|
2015-06-18 10:32:30 +08:00
|
|
|
if (!_visible)
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-05-31 07:42:05 +08:00
|
|
|
uint32_t flags = processParentFlags(parentTransform, parentFlags);
|
2014-03-01 03:20:53 +08:00
|
|
|
|
2015-06-18 10:32:30 +08:00
|
|
|
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);
|
|
|
|
|
|
|
|
sortAllChildren();
|
|
|
|
draw(renderer, _modelViewTransform, flags);
|
|
|
|
|
|
|
|
// FIX ME: Why need to set _orderOfArrival to 0??
|
|
|
|
// Please refer to https://github.com/cocos2d/cocos2d-x/pull/6920
|
|
|
|
// setOrderOfArrival(0);
|
|
|
|
|
|
|
|
director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
|
|
|
|
}
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2014-05-31 07:42:05 +08:00
|
|
|
void BatchNode::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-11-28 18:23:06 +08:00
|
|
|
if (_children.empty())
|
2013-11-18 13:23:38 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-06 07:49:08 +08:00
|
|
|
// CC_NODE_DRAW_SETUP();
|
2013-11-06 15:25:44 +08:00
|
|
|
|
2013-12-26 18:11:23 +08:00
|
|
|
bool pushed = false;
|
2013-11-28 16:02:03 +08:00
|
|
|
for(auto object : _children)
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Armature *armature = dynamic_cast<Armature *>(object);
|
2013-06-07 10:52:32 +08:00
|
|
|
if (armature)
|
|
|
|
{
|
2013-12-26 18:11:23 +08:00
|
|
|
if (!pushed)
|
2013-12-17 17:00:01 +08:00
|
|
|
{
|
|
|
|
generateGroupCommand();
|
2013-12-26 18:11:23 +08:00
|
|
|
pushed = true;
|
2013-12-17 17:00:01 +08:00
|
|
|
}
|
|
|
|
|
2014-05-31 07:42:05 +08:00
|
|
|
armature->visit(renderer, transform, flags);
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-03-01 08:10:48 +08:00
|
|
|
renderer->popGroup();
|
2013-12-26 18:11:23 +08:00
|
|
|
pushed = false;
|
2013-12-17 17:00:01 +08:00
|
|
|
|
2014-05-31 07:42:05 +08:00
|
|
|
((Node *)object)->visit(renderer, transform, flags);
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
|
|
|
}
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2013-12-17 17:00:01 +08:00
|
|
|
void BatchNode::generateGroupCommand()
|
2013-10-30 09:41:40 +08:00
|
|
|
{
|
2013-12-18 09:50:17 +08:00
|
|
|
Renderer* renderer = Director::getInstance()->getRenderer();
|
2014-01-19 03:35:27 +08:00
|
|
|
_groupCommand->init(_globalZOrder);
|
2013-12-26 18:11:23 +08:00
|
|
|
renderer->addCommand(_groupCommand);
|
2013-12-17 17:00:01 +08:00
|
|
|
|
2013-12-26 18:11:23 +08:00
|
|
|
renderer->pushGroup(_groupCommand->getRenderQueueID());
|
2013-10-30 09:41:40 +08:00
|
|
|
}
|
|
|
|
|
2013-10-15 18:00:03 +08:00
|
|
|
}
|