axmol/core/2d/AtlasNode.cpp

276 lines
7.4 KiB
C++
Raw Normal View History

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
2022-10-01 16:24:52 +08:00
https://axmolengine.github.io/
2019-11-23 20:27:39 +08:00
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/AtlasNode.h"
2021-12-25 10:04:45 +08:00
#include <stddef.h> // offsetof
#include "base/Types.h"
#include "renderer/TextureAtlas.h"
#include "base/Director.h"
#include "renderer/TextureCache.h"
#include "base/Utils.h"
#include "renderer/Shaders.h"
#include "renderer/Renderer.h"
2019-11-23 20:27:39 +08:00
#include "renderer/backend/ProgramState.h"
NS_AX_BEGIN
2019-11-23 20:27:39 +08:00
// implementation AtlasNode
// AtlasNode - Creation & Init
AtlasNode::~AtlasNode()
{
2022-07-16 10:43:05 +08:00
AX_SAFE_RELEASE(_textureAtlas);
2019-11-23 20:27:39 +08:00
}
AtlasNode* AtlasNode::create(std::string_view 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;
}
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(ret);
2019-11-23 20:27:39 +08:00
return nullptr;
}
bool AtlasNode::initWithTileFile(std::string_view tile, int tileWidth, int tileHeight, int itemsToRender)
2019-11-23 20:27:39 +08:00
{
2022-07-16 10:43:05 +08:00
AXASSERT(!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);
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;
}
2023-08-13 22:09:02 +08:00
bool AtlasNode::setProgramState(backend::ProgramState* programState, bool ownPS /*= false*/)
{
2023-08-13 22:09:02 +08:00
if (Node::setProgramState(programState, ownPS))
{
2021-12-25 10:04:45 +08:00
auto& pipelineDescriptor = _quadCommand.getPipelineDescriptor();
pipelineDescriptor.programState = _programState;
2021-12-25 10:04:45 +08:00
_mvpMatrixLocation = _programState->getUniformLocation("u_MVPMatrix");
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()
{
2022-07-16 10:43:05 +08:00
AXASSERT(false, "CCAtlasNode:Abstract updateAtlasValue not overridden");
2019-11-23 20:27:39 +08:00
}
// 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;
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)
{
_ignoreContentScaleFactor = ignoreContentScaleFactor;
this->calculateMaxItems();
this->updateAtlasValues();
auto label = dynamic_cast<LabelProtocol*>(this);
2021-12-25 10:04:45 +08:00
if (label)
{
Vec2 s = Vec2(static_cast<float>(label->getString().size() * _itemWidth), static_cast<float>(_itemHeight));
this->setContentSize(s);
}
}
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)
{
2022-07-16 10:43:05 +08:00
AX_SAFE_RETAIN(textureAtlas);
AX_SAFE_RELEASE(_textureAtlas);
2019-11-23 20:27:39 +08:00
_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_AX_END