2013-06-06 12:02:54 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013 cocos2d-x.org
|
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-10-16 16:48:39 +08:00
|
|
|
#include "cocostudio/CCBatchNode.h"
|
|
|
|
#include "cocostudio/CCArmatureDefine.h"
|
|
|
|
#include "cocostudio/CCArmature.h"
|
2013-10-30 09:41:40 +08:00
|
|
|
#include "cocostudio/CCSkin.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
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
BatchNode *batchNode = new 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-11-05 15:32:13 +08:00
|
|
|
: _atlas(nullptr)
|
|
|
|
, _textureAtlasDic(nullptr)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-10-30 09:41:40 +08:00
|
|
|
BatchNode::~BatchNode()
|
|
|
|
{
|
|
|
|
CC_SAFE_RELEASE_NULL(_textureAtlasDic);
|
|
|
|
}
|
|
|
|
|
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();
|
2013-09-16 12:02:57 +08:00
|
|
|
setShaderProgram(ShaderCache::getInstance()->getProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR));
|
2013-10-30 09:41:40 +08:00
|
|
|
|
|
|
|
CC_SAFE_DELETE(_textureAtlasDic);
|
|
|
|
_textureAtlasDic = new Dictionary();
|
|
|
|
|
2013-06-07 10:52:32 +08:00
|
|
|
return ret;
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2013-10-30 09:41:40 +08:00
|
|
|
void BatchNode::addChild(Node *pChild)
|
|
|
|
{
|
|
|
|
Node::addChild(pChild);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BatchNode::addChild(Node *child, int zOrder)
|
|
|
|
{
|
|
|
|
Node::addChild(child, zOrder);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void BatchNode::addChild(Node *child, int zOrder, int tag)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Node::addChild(child, zOrder, tag);
|
|
|
|
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-10-30 09:41:40 +08:00
|
|
|
|
2013-11-05 19:53:38 +08:00
|
|
|
const Dictionary *dict = armature->getBoneDic();
|
2013-11-05 15:32:13 +08:00
|
|
|
DictElement *element = nullptr;
|
2013-10-30 09:41:40 +08:00
|
|
|
CCDICT_FOREACH(dict, element)
|
|
|
|
{
|
|
|
|
Bone *bone = static_cast<Bone*>(element->getObject());
|
|
|
|
|
|
|
|
Array *displayList = bone->getDisplayManager()->getDecorativeDisplayList();
|
2013-11-06 15:25:44 +08:00
|
|
|
for(auto object : *displayList)
|
2013-10-30 09:41:40 +08:00
|
|
|
{
|
|
|
|
DecorativeDisplay *display = static_cast<DecorativeDisplay*>(object);
|
|
|
|
|
|
|
|
if (Skin *skin = dynamic_cast<Skin*>(display->getDisplay()))
|
|
|
|
{
|
|
|
|
skin->setTextureAtlas(getTexureAtlasWithTexture(skin->getTexture()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
2013-11-05 19:53:38 +08:00
|
|
|
const Dictionary *dict = armature->getBoneDic();
|
2013-11-05 15:32:13 +08:00
|
|
|
DictElement *element = nullptr;
|
2013-10-30 09:41:40 +08:00
|
|
|
CCDICT_FOREACH(dict, element)
|
|
|
|
{
|
|
|
|
Bone *bone = static_cast<Bone*>(element->getObject());
|
|
|
|
|
|
|
|
Array *displayList = bone->getDisplayManager()->getDecorativeDisplayList();
|
2013-11-06 15:25:44 +08:00
|
|
|
for(auto object : *displayList)
|
2013-10-30 09:41:40 +08:00
|
|
|
{
|
|
|
|
DecorativeDisplay *display = static_cast<DecorativeDisplay*>(object);
|
|
|
|
|
|
|
|
if (Skin *skin = dynamic_cast<Skin*>(display->getDisplay()))
|
|
|
|
{
|
|
|
|
skin->setTextureAtlas(armature->getTexureAtlasWithTexture(skin->getTexture()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Node::removeChild(child, cleanup);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void BatchNode::visit()
|
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.
|
2013-06-15 14:03:30 +08:00
|
|
|
if (!_visible)
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
kmGLPushMatrix();
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_grid && _grid->isActive())
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_grid->beforeDraw();
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
transform();
|
|
|
|
sortAllChildren();
|
|
|
|
draw();
|
|
|
|
|
|
|
|
// reset for next frame
|
2013-06-15 14:03:30 +08:00
|
|
|
_orderOfArrival = 0;
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_grid && _grid->isActive())
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_grid->afterDraw(this);
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
kmGLPopMatrix();
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void BatchNode::draw()
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-11-18 13:23:38 +08:00
|
|
|
if (_children == nullptr)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-07 10:52:32 +08:00
|
|
|
CC_NODE_DRAW_SETUP();
|
2013-11-06 15:25:44 +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)
|
|
|
|
{
|
|
|
|
armature->visit();
|
2013-06-15 14:03:30 +08:00
|
|
|
_atlas = armature->getTextureAtlas();
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-13 18:07:37 +08:00
|
|
|
((Node *)object)->visit();
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_atlas)
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_atlas->drawQuads();
|
|
|
|
_atlas->removeAllQuads();
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2013-11-05 19:53:38 +08:00
|
|
|
TextureAtlas *BatchNode::getTexureAtlasWithTexture(Texture2D *texture) const
|
2013-10-30 09:41:40 +08:00
|
|
|
{
|
|
|
|
int key = texture->getName();
|
|
|
|
|
|
|
|
TextureAtlas *atlas = static_cast<TextureAtlas *>(_textureAtlasDic->objectForKey(key));
|
2013-11-05 15:32:13 +08:00
|
|
|
if (atlas == nullptr)
|
2013-10-30 09:41:40 +08:00
|
|
|
{
|
|
|
|
atlas = CCTextureAtlas::createWithTexture(texture, 4);
|
|
|
|
_textureAtlasDic->setObject(atlas, key);
|
|
|
|
}
|
|
|
|
return atlas;
|
|
|
|
}
|
|
|
|
|
2013-10-15 18:00:03 +08:00
|
|
|
}
|