2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2011 Zynga Inc.
|
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
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:
|
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
#include "2d/CCAtlasNode.h"
|
2021-12-25 10:04:45 +08:00
|
|
|
#include <stddef.h> // offsetof
|
2019-11-23 20:27:39 +08:00
|
|
|
#include "base/ccTypes.h"
|
|
|
|
#include "renderer/CCTextureAtlas.h"
|
|
|
|
#include "base/CCDirector.h"
|
|
|
|
#include "renderer/CCTextureCache.h"
|
|
|
|
#include "base/ccUtils.h"
|
|
|
|
#include "renderer/ccShaders.h"
|
|
|
|
#include "renderer/CCRenderer.h"
|
|
|
|
#include "renderer/backend/ProgramState.h"
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
// implementation AtlasNode
|
|
|
|
|
|
|
|
// AtlasNode - Creation & Init
|
|
|
|
|
|
|
|
AtlasNode::~AtlasNode()
|
|
|
|
{
|
|
|
|
CC_SAFE_RELEASE(_textureAtlas);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
AtlasNode* AtlasNode::create(const std::string& tile, int tileWidth, int tileHeight, int itemsToRender)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
AtlasNode* ret = new AtlasNode();
|
2019-11-23 20:27:39 +08:00
|
|
|
if (ret->initWithTileFile(tile, tileWidth, tileHeight, itemsToRender))
|
|
|
|
{
|
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(ret);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AtlasNode::initWithTileFile(const std::string& tile, int tileWidth, int tileHeight, int itemsToRender)
|
|
|
|
{
|
2020-08-18 14:29:09 +08:00
|
|
|
CCASSERT(!tile.empty(), "file size should not be empty");
|
2021-12-25 10:04:45 +08:00
|
|
|
Texture2D* texture = _director->getTextureCache()->addImage(tile);
|
2019-11-23 20:27:39 +08:00
|
|
|
return initWithTexture(texture, tileWidth, tileHeight, itemsToRender);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AtlasNode::initWithTexture(Texture2D* texture, int tileWidth, int tileHeight, int itemsToRender)
|
|
|
|
{
|
|
|
|
_itemWidth = tileWidth;
|
|
|
|
_itemHeight = tileHeight;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
_colorUnmodified = Color3B::WHITE;
|
2019-11-23 20:27:39 +08:00
|
|
|
_isOpacityModifyRGB = true;
|
|
|
|
|
|
|
|
_blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
|
|
|
|
|
2021-12-08 00:11:53 +08:00
|
|
|
_textureAtlas = new TextureAtlas();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
_textureAtlas->initWithTexture(texture, itemsToRender);
|
2020-09-09 15:29:56 +08:00
|
|
|
|
|
|
|
setProgramStateWithRegistry(backend::ProgramType::POSITION_TEXTURE_COLOR, texture);
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
this->updateBlendFunc();
|
|
|
|
this->updateOpacityModifyRGB();
|
|
|
|
|
|
|
|
this->calculateMaxItems();
|
|
|
|
|
|
|
|
_quadsToDraw = itemsToRender;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-10-16 16:23:14 +08:00
|
|
|
bool AtlasNode::setProgramState(backend::ProgramState* programState, bool needsRetain)
|
2020-09-09 15:29:56 +08:00
|
|
|
{
|
2020-10-16 16:23:14 +08:00
|
|
|
if (Node::setProgramState(programState, needsRetain))
|
2020-09-09 15:29:56 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto& pipelineDescriptor = _quadCommand.getPipelineDescriptor();
|
2020-09-09 15:29:56 +08:00
|
|
|
pipelineDescriptor.programState = _programState;
|
2021-12-25 10:04:45 +08:00
|
|
|
_mvpMatrixLocation = _programState->getUniformLocation("u_MVPMatrix");
|
2020-09-09 15:29:56 +08:00
|
|
|
|
|
|
|
auto vertexLayout = _programState->getVertexLayout();
|
2021-12-25 10:04:45 +08:00
|
|
|
// a_position
|
2020-09-09 15:29:56 +08:00
|
|
|
vertexLayout->setAttribute(backend::ATTRIBUTE_NAME_POSITION,
|
2021-12-25 10:04:45 +08:00
|
|
|
_programState->getAttributeLocation(backend::Attribute::POSITION),
|
|
|
|
backend::VertexFormat::FLOAT3, 0, false);
|
2020-09-09 15:29:56 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// a_texCoord
|
2020-09-09 15:29:56 +08:00
|
|
|
vertexLayout->setAttribute(backend::ATTRIBUTE_NAME_TEXCOORD,
|
2021-12-25 10:04:45 +08:00
|
|
|
_programState->getAttributeLocation(backend::Attribute::TEXCOORD),
|
|
|
|
backend::VertexFormat::FLOAT2, offsetof(V3F_C4B_T2F, texCoords), false);
|
2020-09-09 15:29:56 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// a_color
|
2020-09-09 15:29:56 +08:00
|
|
|
vertexLayout->setAttribute(backend::ATTRIBUTE_NAME_COLOR,
|
2021-12-25 10:04:45 +08:00
|
|
|
_programState->getAttributeLocation(backend::Attribute::COLOR),
|
|
|
|
backend::VertexFormat::UBYTE4, offsetof(V3F_C4B_T2F, colors), true);
|
2020-09-09 15:29:56 +08:00
|
|
|
|
|
|
|
vertexLayout->setLayout(sizeof(V3F_C4B_T2F));
|
|
|
|
|
|
|
|
updateProgramStateTexture(_textureAtlas->getTexture());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
// AtlasNode - Atlas generation
|
|
|
|
|
|
|
|
void AtlasNode::calculateMaxItems()
|
|
|
|
{
|
2021-10-23 23:27:14 +08:00
|
|
|
Vec2 s = _textureAtlas->getTexture()->getContentSize();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (_ignoreContentScaleFactor)
|
|
|
|
{
|
|
|
|
s = _textureAtlas->getTexture()->getContentSizeInPixels();
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
_itemsPerColumn = (int)(s.height / _itemHeight);
|
2021-12-25 10:04:45 +08:00
|
|
|
_itemsPerRow = (int)(s.width / _itemWidth);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void AtlasNode::updateAtlasValues()
|
|
|
|
{
|
|
|
|
CCASSERT(false, "CCAtlasNode:Abstract updateAtlasValue not overridden");
|
|
|
|
}
|
|
|
|
|
|
|
|
// AtlasNode - draw
|
2021-12-25 10:04:45 +08:00
|
|
|
void AtlasNode::draw(Renderer* renderer, const Mat4& transform, uint32_t flags)
|
|
|
|
{
|
|
|
|
if (_textureAtlas->getTotalQuads() == 0)
|
2019-11-23 20:27:39 +08:00
|
|
|
return;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
auto programState = _quadCommand.getPipelineDescriptor().programState;
|
|
|
|
|
2021-04-22 22:01:47 +08:00
|
|
|
const auto& projectionMat = _director->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
|
2019-11-23 20:27:39 +08:00
|
|
|
programState->setUniform(_mvpMatrixLocation, projectionMat.m, sizeof(projectionMat.m));
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
_quadCommand.init(_globalZOrder, _textureAtlas->getTexture(), _blendFunc, _textureAtlas->getQuads(), _quadsToDraw,
|
|
|
|
transform, flags);
|
2019-11-23 20:27:39 +08:00
|
|
|
renderer->addCommand(&_quadCommand);
|
|
|
|
}
|
|
|
|
|
|
|
|
// AtlasNode - RGBA protocol
|
|
|
|
|
|
|
|
const Color3B& AtlasNode::getColor() const
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_isOpacityModifyRGB)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return _colorUnmodified;
|
|
|
|
}
|
|
|
|
return Node::getColor();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AtlasNode::setColor(const Color3B& color3)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Color3B tmp = color3;
|
2019-11-23 20:27:39 +08:00
|
|
|
_colorUnmodified = color3;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_isOpacityModifyRGB)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
tmp.r = tmp.r * _displayedOpacity / 255;
|
|
|
|
tmp.g = tmp.g * _displayedOpacity / 255;
|
|
|
|
tmp.b = tmp.b * _displayedOpacity / 255;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
Node::setColor(tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AtlasNode::setOpacity(uint8_t opacity)
|
|
|
|
{
|
|
|
|
Node::setOpacity(opacity);
|
|
|
|
|
|
|
|
// special opacity for premultiplied textures
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_isOpacityModifyRGB)
|
2019-11-23 20:27:39 +08:00
|
|
|
this->setColor(_colorUnmodified);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AtlasNode::setOpacityModifyRGB(bool value)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Color3B oldColor = this->getColor();
|
2019-11-23 20:27:39 +08:00
|
|
|
_isOpacityModifyRGB = value;
|
|
|
|
this->setColor(oldColor);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AtlasNode::isOpacityModifyRGB() const
|
|
|
|
{
|
|
|
|
return _isOpacityModifyRGB;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AtlasNode::updateOpacityModifyRGB()
|
|
|
|
{
|
|
|
|
_isOpacityModifyRGB = _textureAtlas->getTexture()->hasPremultipliedAlpha();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AtlasNode::setIgnoreContentScaleFactor(bool ignoreContentScaleFactor)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_ignoreContentScaleFactor != ignoreContentScaleFactor)
|
|
|
|
{
|
2020-09-16 13:34:20 +08:00
|
|
|
_ignoreContentScaleFactor = ignoreContentScaleFactor;
|
|
|
|
this->calculateMaxItems();
|
2020-09-22 16:35:24 +08:00
|
|
|
this->updateAtlasValues();
|
|
|
|
|
|
|
|
auto label = dynamic_cast<LabelProtocol*>(this);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (label)
|
|
|
|
{
|
2021-10-23 23:27:14 +08:00
|
|
|
Vec2 s = Vec2(label->getString().size() * _itemWidth, _itemHeight);
|
2020-09-22 16:35:24 +08:00
|
|
|
this->setContentSize(s);
|
|
|
|
}
|
2020-09-16 13:34:20 +08:00
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// AtlasNode - CocosNodeTexture protocol
|
|
|
|
|
|
|
|
const BlendFunc& AtlasNode::getBlendFunc() const
|
|
|
|
{
|
|
|
|
return _blendFunc;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void AtlasNode::setBlendFunc(const BlendFunc& blendFunc)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_blendFunc = blendFunc;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AtlasNode::updateBlendFunc()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!_textureAtlas->getTexture()->hasPremultipliedAlpha())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_blendFunc = BlendFunc::ALPHA_NON_PREMULTIPLIED;
|
|
|
|
setOpacityModifyRGB(false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
|
|
|
|
setOpacityModifyRGB(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void AtlasNode::setTexture(Texture2D* texture)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_textureAtlas->setTexture(texture);
|
|
|
|
updateProgramStateTexture(texture);
|
|
|
|
|
|
|
|
this->updateBlendFunc();
|
|
|
|
this->updateOpacityModifyRGB();
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Texture2D* AtlasNode::getTexture() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return _textureAtlas->getTexture();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AtlasNode::setTextureAtlas(TextureAtlas* textureAtlas)
|
|
|
|
{
|
|
|
|
CC_SAFE_RETAIN(textureAtlas);
|
|
|
|
CC_SAFE_RELEASE(_textureAtlas);
|
|
|
|
_textureAtlas = textureAtlas;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
TextureAtlas* AtlasNode::getTextureAtlas() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return _textureAtlas;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t AtlasNode::getQuadsToDraw() const
|
|
|
|
{
|
|
|
|
return _quadsToDraw;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AtlasNode::setQuadsToDraw(ssize_t quadsToDraw)
|
|
|
|
{
|
|
|
|
_quadsToDraw = quadsToDraw;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|