From 992d6139d972b756d575fc9c4df85e69b5a7450c Mon Sep 17 00:00:00 2001 From: halx99 Date: Wed, 9 Sep 2020 13:03:31 +0800 Subject: [PATCH 1/4] More clearly programState management --- cocos/2d/CCLabel.cpp | 23 +++++---- cocos/2d/CCLabel.h | 4 +- cocos/2d/CCMotionStreak.cpp | 51 ++++++++++--------- cocos/2d/CCMotionStreak.h | 2 +- cocos/2d/CCNode.cpp | 19 ++++--- cocos/2d/CCNode.h | 15 ++++-- cocos/2d/CCSprite.cpp | 34 +++++-------- cocos/2d/CCSprite.h | 11 ++-- cocos/2d/CCSpriteBatchNode.cpp | 17 ++++--- cocos/2d/CCSpriteBatchNode.h | 4 +- cocos/3d/CCSprite3D.h | 4 +- tests/cpp-tests/Classes/NodeTest/NodeTest.cpp | 21 ++++---- 12 files changed, 107 insertions(+), 98 deletions(-) diff --git a/cocos/2d/CCLabel.cpp b/cocos/2d/CCLabel.cpp index 7bac40c8b6..4cfdef3f3c 100644 --- a/cocos/2d/CCLabel.cpp +++ b/cocos/2d/CCLabel.cpp @@ -689,18 +689,21 @@ void Label::setVertexLayout() vertexLayout->setLayout(sizeof(V3F_C4B_T2F)); } -void Label::setProgramState(backend::ProgramState *programState) +bool Label::attachProgramState(backend::ProgramState* programState) { - Node::setProgramState(programState); - updateUniformLocations(); - for (auto &batch : _batchCommands) - { - updateBatchCommand(batch); - } + if (Node::attachProgramState(programState)) { + updateUniformLocations(); + for (auto& batch : _batchCommands) + { + updateBatchCommand(batch); + } - auto &quadPipeline = _quadCommand.getPipelineDescriptor(); - setVertexLayout(); - quadPipeline.programState = _programState; + auto& quadPipeline = _quadCommand.getPipelineDescriptor(); + setVertexLayout(); + quadPipeline.programState = _programState; + return true; + } + return false; } void Label::updateShaderProgram() diff --git a/cocos/2d/CCLabel.h b/cocos/2d/CCLabel.h index 67aa4776e6..8e0f329109 100644 --- a/cocos/2d/CCLabel.h +++ b/cocos/2d/CCLabel.h @@ -639,9 +639,9 @@ public: float getAdditionalKerning() const; /** - * set ProgramState of current render command + * Attach ProgramState of current render command */ - virtual void setProgramState(backend::ProgramState *programState) override; + bool attachProgramState(backend::ProgramState *programState) override; FontAtlas* getFontAtlas() { return _fontAtlas; } diff --git a/cocos/2d/CCMotionStreak.cpp b/cocos/2d/CCMotionStreak.cpp index 5628bc733c..cc17b13617 100644 --- a/cocos/2d/CCMotionStreak.cpp +++ b/cocos/2d/CCMotionStreak.cpp @@ -217,34 +217,37 @@ void MotionStreak::setTexture(Texture2D *texture) } } -void MotionStreak::setProgramState(backend::ProgramState* programState) +bool MotionStreak::attachProgramState(backend::ProgramState* programState) { - CCASSERT(programState, "argument should not be nullptr"); - auto& pipelineDescriptor = _customCommand.getPipelineDescriptor(); - Node::setProgramState(programState); - pipelineDescriptor.programState = _programState; + if (Node::attachProgramState(programState)) { + CCASSERT(programState, "argument should not be nullptr"); + auto& pipelineDescriptor = _customCommand.getPipelineDescriptor(); + pipelineDescriptor.programState = _programState; - _mvpMatrixLocaiton = _programState->getUniformLocation("u_MVPMatrix"); - _textureLocation = _programState->getUniformLocation("u_texture"); + _mvpMatrixLocaiton = _programState->getUniformLocation("u_MVPMatrix"); + _textureLocation = _programState->getUniformLocation("u_texture"); - auto vertexLayout = _programState->getVertexLayout(); - const auto& attributeInfo = _programState->getProgram()->getActiveAttributes(); - auto iter = attributeInfo.find("a_position"); - if (iter != attributeInfo.end()) - { - vertexLayout->setAttribute("a_position", iter->second.location, backend::VertexFormat::FLOAT2, 0, false); + auto vertexLayout = _programState->getVertexLayout(); + const auto& attributeInfo = _programState->getProgram()->getActiveAttributes(); + auto iter = attributeInfo.find("a_position"); + if (iter != attributeInfo.end()) + { + vertexLayout->setAttribute("a_position", iter->second.location, backend::VertexFormat::FLOAT2, 0, false); + } + iter = attributeInfo.find("a_texCoord"); + if (iter != attributeInfo.end()) + { + vertexLayout->setAttribute("a_texCoord", iter->second.location, backend::VertexFormat::FLOAT2, 2 * sizeof(float), false); + } + iter = attributeInfo.find("a_color"); + if (iter != attributeInfo.end()) + { + vertexLayout->setAttribute("a_color", iter->second.location, backend::VertexFormat::UBYTE4, 4 * sizeof(float), true); + } + vertexLayout->setLayout(4 * sizeof(float) + 4 * sizeof(uint8_t)); + return true; } - iter = attributeInfo.find("a_texCoord"); - if (iter != attributeInfo.end()) - { - vertexLayout->setAttribute("a_texCoord", iter->second.location, backend::VertexFormat::FLOAT2, 2 * sizeof(float), false); - } - iter = attributeInfo.find("a_color"); - if (iter != attributeInfo.end()) - { - vertexLayout->setAttribute("a_color", iter->second.location, backend::VertexFormat::UBYTE4, 4 * sizeof(float), true); - } - vertexLayout->setLayout(4 * sizeof(float) + 4 * sizeof(uint8_t)); + return false; } void MotionStreak::setBlendFunc(const BlendFunc &blendFunc) diff --git a/cocos/2d/CCMotionStreak.h b/cocos/2d/CCMotionStreak.h index 65107f3f46..9fd7a42365 100644 --- a/cocos/2d/CCMotionStreak.h +++ b/cocos/2d/CCMotionStreak.h @@ -147,7 +147,7 @@ public: _startingPositionInitialized = bStartingPositionInitialized; } - void setProgramState(backend::ProgramState* programState) override; + bool attachProgramState(backend::ProgramState* programState) override; CC_CONSTRUCTOR_ACCESS: MotionStreak(); diff --git a/cocos/2d/CCNode.cpp b/cocos/2d/CCNode.cpp index 6f2612c19b..6dfbd05029 100644 --- a/cocos/2d/CCNode.cpp +++ b/cocos/2d/CCNode.cpp @@ -2132,16 +2132,7 @@ void Node::setProgramStateWithRegistry(backend::ProgramType programType, Texture { auto formatEXT = texture ? texture->getTextureFormatEXT() : 0; auto programState = backend::ProgramStateRegistry::getInstance()->newProgramState(programType, formatEXT); - setProgramState(programState); - programState->release(); -} - -void Node::updateProgramStateTexture(Texture2D* texture) -{ - if (texture == nullptr || texture->getBackendTexture() == nullptr || _programState == nullptr) - return; - - _programState->setTexture(texture->getBackendTexture()); + attachProgramState(programState); } void Node::setProgramState(backend::ProgramState* programState) @@ -2161,6 +2152,14 @@ bool Node::attachProgramState(backend::ProgramState* programState) return false; } +void Node::updateProgramStateTexture(Texture2D* texture) +{ + if (texture == nullptr || texture->getBackendTexture() == nullptr || _programState == nullptr) + return; + + _programState->setTexture(texture->getBackendTexture()); +} + backend::ProgramState* Node::getProgramState() const { return _programState; diff --git a/cocos/2d/CCNode.h b/cocos/2d/CCNode.h index a25613d24a..2d019b1456 100644 --- a/cocos/2d/CCNode.h +++ b/cocos/2d/CCNode.h @@ -1768,13 +1768,22 @@ public: */ virtual void setCameraMask(unsigned short mask, bool applyChildren = true); + /** + * Sets ProgramState with retain + * @param programState + */ virtual void setProgramState(backend::ProgramState* programState); - bool attachProgramState(backend::ProgramState* programState); + backend::ProgramState* getProgramState() const; void setProgramStateWithRegistry(backend::ProgramType programType, Texture2D* texture); - void updateProgramStateTexture(Texture2D* texture); - virtual backend::ProgramState* getProgramState() const; + /** + * Attach/Change current ProgramState without retain, override me + * @param programState should be new or retain if it has other owner + */ + virtual bool attachProgramState(backend::ProgramState* programState); + + void updateProgramStateTexture(Texture2D* texture); CC_CONSTRUCTOR_ACCESS: // Nodes should be created using create(); diff --git a/cocos/2d/CCSprite.cpp b/cocos/2d/CCSprite.cpp index 0acbbe49ba..4466c8f55d 100644 --- a/cocos/2d/CCSprite.cpp +++ b/cocos/2d/CCSprite.cpp @@ -376,9 +376,7 @@ void Sprite::setVertexLayout() void Sprite::updateShaders(const char* vert, const char* frag) { auto* program = backend::Device::getInstance()->newProgram(vert, frag); - auto programState = new (std::nothrow) backend::ProgramState(program); - setProgramState(programState); - CC_SAFE_RELEASE(programState); + attachProgramState(new (std::nothrow) backend::ProgramState(program)); CC_SAFE_RELEASE(program); } @@ -387,25 +385,25 @@ void Sprite::setProgramState(backend::ProgramType type) setProgramStateWithRegistry(type, _texture); } -void Sprite::setProgramState(backend::ProgramState *programState) +bool Sprite::attachProgramState(backend::ProgramState *programState) { CCASSERT(programState, "argument should not be nullptr"); - auto& pipelineDescriptor = _trianglesCommand.getPipelineDescriptor(); - Node::setProgramState(programState); - pipelineDescriptor.programState = _programState; + if (Node::attachProgramState(programState)) { + auto& pipelineDescriptor = _trianglesCommand.getPipelineDescriptor(); + pipelineDescriptor.programState = _programState; - _mvpMatrixLocation = pipelineDescriptor.programState->getUniformLocation(backend::Uniform::MVP_MATRIX); - _textureLocation = pipelineDescriptor.programState->getUniformLocation(backend::Uniform::TEXTURE); + _mvpMatrixLocation = _programState->getUniformLocation(backend::Uniform::MVP_MATRIX); - setVertexLayout(); - updateProgramStateTexture(_texture); - setMVPMatrixUniform(); + setVertexLayout(); + updateProgramStateTexture(_texture); + setMVPMatrixUniform(); + return true; + } + return false; } void Sprite::setTexture(Texture2D *texture) { - setProgramStateWithRegistry(backend::ProgramType::POSITION_TEXTURE_COLOR, texture); - CCASSERT(! _batchNode || (texture && texture == _batchNode->getTexture()), "CCSprite: Batched sprites should use the same texture as the batchnode"); // accept texture==nil as argument CCASSERT( !texture || dynamic_cast(texture), "setTexture expects a Texture2D. Invalid argument"); @@ -437,7 +435,8 @@ void Sprite::setTexture(Texture2D *texture) } updateBlendFunc(); } - updateProgramStateTexture(_texture); + + setProgramState(backend::ProgramType::POSITION_TEXTURE_COLOR); } Texture2D* Sprite::getTexture() const @@ -1729,9 +1728,4 @@ void Sprite::setMVPMatrixUniform() programState->setUniform(_mvpMatrixLocation, projectionMat.m, sizeof(projectionMat.m)); } -backend::ProgramState* Sprite::getProgramState() const -{ - return _programState; -} - NS_CC_END diff --git a/cocos/2d/CCSprite.h b/cocos/2d/CCSprite.h index 1b6451761a..25d8d33beb 100644 --- a/cocos/2d/CCSprite.h +++ b/cocos/2d/CCSprite.h @@ -418,14 +418,9 @@ public: TextureAtlas* getTextureAtlas() const { return _textureAtlas; } /** - * Set ProgramState + * Attach new ProgramState */ - virtual void setProgramState(backend::ProgramState *programState) override; - - /** - * Get current ProgramState - */ - virtual backend::ProgramState *getProgramState() const override; + bool attachProgramState(backend::ProgramState *programState) override; /** * Sets the weak reference of the TextureAtlas when the sprite is rendered using via SpriteBatchNode. @@ -625,6 +620,7 @@ CC_CONSTRUCTOR_ACCESS : virtual void setVertexLayout(); virtual void updateShaders(const char* vert, const char* frag); + using Node::setProgramState; void setProgramState(backend::ProgramType type); protected: @@ -663,7 +659,6 @@ protected: TrianglesCommand _trianglesCommand; backend::UniformLocation _mvpMatrixLocation; - backend::UniformLocation _textureLocation; #if CC_SPRITE_DEBUG_DRAW DrawNode *_debugDrawNode = nullptr; #endif //CC_SPRITE_DEBUG_DRAW diff --git a/cocos/2d/CCSpriteBatchNode.cpp b/cocos/2d/CCSpriteBatchNode.cpp index a94843045c..099ae894e8 100644 --- a/cocos/2d/CCSpriteBatchNode.cpp +++ b/cocos/2d/CCSpriteBatchNode.cpp @@ -148,15 +148,18 @@ void SpriteBatchNode::setVertexLayout() vertexLayout->setLayout(sizeof(V3F_C4B_T2F)); } -void SpriteBatchNode::setProgramState(backend::ProgramState *programState) +bool SpriteBatchNode::attachProgramState(backend::ProgramState *programState) { CCASSERT(programState, "programState should not be nullptr"); - auto& pipelineDescriptor = _quadCommand.getPipelineDescriptor(); - Node::setProgramState(programState); - pipelineDescriptor.programState = _programState; - - setVertexLayout(); - setUniformLocation(); + if (Node::attachProgramState(programState)) { + auto& pipelineDescriptor = _quadCommand.getPipelineDescriptor(); + pipelineDescriptor.programState = _programState; + + setVertexLayout(); + setUniformLocation(); + return true; + } + return false; } bool SpriteBatchNode::init() diff --git a/cocos/2d/CCSpriteBatchNode.h b/cocos/2d/CCSpriteBatchNode.h index 445668f105..5492f01f1c 100644 --- a/cocos/2d/CCSpriteBatchNode.h +++ b/cocos/2d/CCSpriteBatchNode.h @@ -212,9 +212,9 @@ public: virtual std::string getDescription() const override; /** - * Set ProgramState + * Attach new ProgramState */ - virtual void setProgramState(backend::ProgramState *programState) override; + bool attachProgramState(backend::ProgramState *programState) override; /** Inserts a quad at a certain index into the texture atlas. The Sprite won't be added into the children array. * This method should be called only when you are dealing with very big AtlasSprite and when most of the Sprite won't be updated. diff --git a/cocos/3d/CCSprite3D.h b/cocos/3d/CCSprite3D.h index aeedd92ac5..8281ad8b87 100644 --- a/cocos/3d/CCSprite3D.h +++ b/cocos/3d/CCSprite3D.h @@ -119,8 +119,8 @@ public: virtual const BlendFunc &getBlendFunc() const override; // overrides - /** set ProgramState, you should bind attributes by yourself */ - virtual void setProgramState(backend::ProgramState *programState) override; + /** Sets ProgramState, you should bind attributes by yourself */ + void setProgramState(backend::ProgramState *programState) override; /* * Get AABB diff --git a/tests/cpp-tests/Classes/NodeTest/NodeTest.cpp b/tests/cpp-tests/Classes/NodeTest/NodeTest.cpp index b0cd647d62..b1b1d4f94a 100644 --- a/tests/cpp-tests/Classes/NodeTest/NodeTest.cpp +++ b/tests/cpp-tests/Classes/NodeTest/NodeTest.cpp @@ -975,7 +975,7 @@ public: sprite->setProgramState(programState); return sprite; } - virtual void setProgramState(backend::ProgramState* programState) override; + bool attachProgramState(backend::ProgramState* programState) override; virtual void draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) override; protected: @@ -983,16 +983,19 @@ protected: }; -void MySprite::setProgramState(backend::ProgramState* programState) +bool MySprite::attachProgramState(backend::ProgramState* programState) { - Sprite::setProgramState(programState); - auto& pipelineDescriptor = _customCommand.getPipelineDescriptor(); - pipelineDescriptor.programState = programState; + if (Sprite::attachProgramState(programState)) { + auto& pipelineDescriptor = _customCommand.getPipelineDescriptor(); + pipelineDescriptor.programState = programState; - _customCommand.setDrawType(CustomCommand::DrawType::ARRAY); - _customCommand.setPrimitiveType(CustomCommand::PrimitiveType::TRIANGLE_STRIP); - _customCommand.createVertexBuffer(sizeof(V3F_C4B_T2F), 4, CustomCommand::BufferUsage::STATIC); - _customCommand.updateVertexBuffer(&_quad, 4*sizeof(V3F_C4B_T2F)); + _customCommand.setDrawType(CustomCommand::DrawType::ARRAY); + _customCommand.setPrimitiveType(CustomCommand::PrimitiveType::TRIANGLE_STRIP); + _customCommand.createVertexBuffer(sizeof(V3F_C4B_T2F), 4, CustomCommand::BufferUsage::STATIC); + _customCommand.updateVertexBuffer(&_quad, 4 * sizeof(V3F_C4B_T2F)); + return true; + } + return false; } void MySprite::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) From f89763b416414c58ba5e4778fe452792521d7577 Mon Sep 17 00:00:00 2001 From: halx99 Date: Wed, 9 Sep 2020 15:29:56 +0800 Subject: [PATCH 2/4] Tidy, update programState texture at attachProgramState --- cocos/2d/CCAtlasNode.cpp | 72 ++++++++++++++++++---------------- cocos/2d/CCAtlasNode.h | 5 ++- cocos/2d/CCLabelAtlas.cpp | 33 +++++++++------- cocos/2d/CCLabelAtlas.h | 11 +++--- cocos/2d/CCMenuItem.cpp | 3 +- cocos/2d/CCMotionStreak.cpp | 5 +-- cocos/2d/CCSpriteBatchNode.cpp | 3 +- cocos/base/CCDirector.cpp | 25 +++++++++--- 8 files changed, 88 insertions(+), 69 deletions(-) diff --git a/cocos/2d/CCAtlasNode.cpp b/cocos/2d/CCAtlasNode.cpp index 1ec0eba182..a9f21c4fdb 100644 --- a/cocos/2d/CCAtlasNode.cpp +++ b/cocos/2d/CCAtlasNode.cpp @@ -42,38 +42,6 @@ NS_CC_BEGIN // AtlasNode - Creation & Init -AtlasNode::AtlasNode() -{ - auto& pipelineDescriptor = _quadCommand.getPipelineDescriptor(); - auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_TEXTURE_COLOR); - attachProgramState(new (std::nothrow) backend::ProgramState(program)); - pipelineDescriptor.programState = _programState; - _mvpMatrixLocation = pipelineDescriptor.programState->getUniformLocation("u_MVPMatrix"); - _textureLocation = pipelineDescriptor.programState->getUniformLocation("u_texture"); - - auto vertexLayout = _programState->getVertexLayout(); - //a_position - vertexLayout->setAttribute(backend::ATTRIBUTE_NAME_POSITION, - _programState->getAttributeLocation(backend::Attribute::POSITION), - backend::VertexFormat::FLOAT3, - 0, - false); - - //a_texCoord - vertexLayout->setAttribute(backend::ATTRIBUTE_NAME_TEXCOORD, - _programState->getAttributeLocation(backend::Attribute::TEXCOORD), - backend::VertexFormat::FLOAT2, offsetof(V3F_C4B_T2F, texCoords), - false); - - //a_color - vertexLayout->setAttribute(backend::ATTRIBUTE_NAME_COLOR, - _programState->getAttributeLocation(backend::Attribute::COLOR), - backend::VertexFormat::UBYTE4, offsetof(V3F_C4B_T2F, colors), - true); - - vertexLayout->setLayout(sizeof(V3F_C4B_T2F)); -} - AtlasNode::~AtlasNode() { CC_SAFE_RELEASE(_textureAtlas); @@ -117,8 +85,9 @@ bool AtlasNode::initWithTexture(Texture2D* texture, int tileWidth, int tileHeigh } _textureAtlas->initWithTexture(texture, itemsToRender); - - updateProgramStateTexture(texture); + + setProgramStateWithRegistry(backend::ProgramType::POSITION_TEXTURE_COLOR, texture); + this->updateBlendFunc(); this->updateOpacityModifyRGB(); @@ -129,6 +98,41 @@ bool AtlasNode::initWithTexture(Texture2D* texture, int tileWidth, int tileHeigh return true; } +bool AtlasNode::attachProgramState(backend::ProgramState* programState) +{ + if (Node::attachProgramState(programState)) + { + auto& pipelineDescriptor = _quadCommand.getPipelineDescriptor(); + pipelineDescriptor.programState = _programState; + _mvpMatrixLocation = _programState->getUniformLocation("u_MVPMatrix"); + + auto vertexLayout = _programState->getVertexLayout(); + //a_position + vertexLayout->setAttribute(backend::ATTRIBUTE_NAME_POSITION, + _programState->getAttributeLocation(backend::Attribute::POSITION), + backend::VertexFormat::FLOAT3, + 0, + false); + + //a_texCoord + vertexLayout->setAttribute(backend::ATTRIBUTE_NAME_TEXCOORD, + _programState->getAttributeLocation(backend::Attribute::TEXCOORD), + backend::VertexFormat::FLOAT2, offsetof(V3F_C4B_T2F, texCoords), + false); + + //a_color + vertexLayout->setAttribute(backend::ATTRIBUTE_NAME_COLOR, + _programState->getAttributeLocation(backend::Attribute::COLOR), + backend::VertexFormat::UBYTE4, offsetof(V3F_C4B_T2F, colors), + true); + + vertexLayout->setLayout(sizeof(V3F_C4B_T2F)); + + updateProgramStateTexture(_textureAtlas->getTexture()); + return true; + } + return false; +} // AtlasNode - Atlas generation diff --git a/cocos/2d/CCAtlasNode.h b/cocos/2d/CCAtlasNode.h index 3cda55abe4..3896af3e8d 100644 --- a/cocos/2d/CCAtlasNode.h +++ b/cocos/2d/CCAtlasNode.h @@ -99,8 +99,10 @@ public: void setQuadsToDraw(ssize_t quadsToDraw); size_t getQuadsToDraw() const; + bool attachProgramState(backend::ProgramState* programState) override; + CC_CONSTRUCTOR_ACCESS: - AtlasNode(); + AtlasNode() = default; virtual ~AtlasNode(); /** Initializes an AtlasNode with an Atlas file the width and height of each item and the quantity of items to render*/ @@ -141,7 +143,6 @@ protected: bool _ignoreContentScaleFactor = false; QuadCommand _quadCommand; - backend::UniformLocation _textureLocation; backend::UniformLocation _mvpMatrixLocation; private: diff --git a/cocos/2d/CCLabelAtlas.cpp b/cocos/2d/CCLabelAtlas.cpp index 9fa24cada0..4157ae2909 100644 --- a/cocos/2d/CCLabelAtlas.cpp +++ b/cocos/2d/CCLabelAtlas.cpp @@ -40,20 +40,7 @@ NS_CC_BEGIN //CCLabelAtlas - Creation & Init -LabelAtlas* LabelAtlas::create() -{ - LabelAtlas* ret = new (std::nothrow) LabelAtlas(); - if (ret) - { - ret->autorelease(); - } - else - { - CC_SAFE_RELEASE_NULL(ret); - } - - return ret; -} + LabelAtlas* LabelAtlas::create(const std::string& string, const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap) { @@ -102,6 +89,24 @@ LabelAtlas* LabelAtlas::create(const std::string& string, const std::string& fnt return ret; } +LabelAtlas* LabelAtlas::create(const std::string& string, Texture2D* texture, int itemWidth, int itemHeight, int startCharMap) +{ + LabelAtlas* ret = new (std::nothrow) LabelAtlas(); + if (ret) + { + if (ret->initWithString(string, texture, itemWidth, itemHeight, startCharMap)) + { + ret->autorelease(); + } + else + { + CC_SAFE_RELEASE_NULL(ret); + } + } + + return ret; +} + bool LabelAtlas::initWithString(const std::string& theString, const std::string& fntFile) { std::string pathStr = FileUtils::getInstance()->fullPathForFilename(fntFile); diff --git a/cocos/2d/CCLabelAtlas.h b/cocos/2d/CCLabelAtlas.h index eb2f59965a..dfb61a11c0 100644 --- a/cocos/2d/CCLabelAtlas.h +++ b/cocos/2d/CCLabelAtlas.h @@ -56,12 +56,6 @@ NS_CC_BEGIN class CC_DLL LabelAtlas : public AtlasNode, public LabelProtocol { public: - /** - * Creates an empty LabelAtlas. - * User need to call initWithString(...) later to make this object work properly. - */ - static LabelAtlas* create(); - /** Creates the LabelAtlas with a string, a char map file(the atlas), the width and height of each element and the starting char of the atlas. */ static LabelAtlas* create(const std::string& string, const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap); @@ -71,6 +65,11 @@ public: */ static LabelAtlas* create(const std::string& string, const std::string& fntFile); + /** + * Creates the LabelAtlas with a string, a texture, the width and height of each element and the starting char of the atlas. + */ + static LabelAtlas* create(const std::string& string, Texture2D* texture, int itemWidth, int itemHeight, int startCharMap); + /** Initializes the LabelAtlas with a string, a char map file(the atlas), the width and height of each element and the starting char of the atlas. */ bool initWithString(const std::string& string, const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap); diff --git a/cocos/2d/CCMenuItem.cpp b/cocos/2d/CCMenuItem.cpp index d1556cf310..cdce33cbd7 100644 --- a/cocos/2d/CCMenuItem.cpp +++ b/cocos/2d/CCMenuItem.cpp @@ -281,8 +281,7 @@ MenuItemAtlasFont * MenuItemAtlasFont::create(const std::string& value, const st bool MenuItemAtlasFont::initWithString(const std::string& value, const std::string& charMapFile, int itemWidth, int itemHeight, char startCharMap, const ccMenuCallback& callback) { CCASSERT( value.size() != 0, "value length must be greater than 0"); - LabelAtlas *label = LabelAtlas::create(); - label->initWithString(value, charMapFile, itemWidth, itemHeight, startCharMap); + LabelAtlas *label = LabelAtlas::create(value, charMapFile, itemWidth, itemHeight, startCharMap); if (MenuItemLabel::initWithLabel(label, callback)) { // do something ? diff --git a/cocos/2d/CCMotionStreak.cpp b/cocos/2d/CCMotionStreak.cpp index cc17b13617..bb97bb7461 100644 --- a/cocos/2d/CCMotionStreak.cpp +++ b/cocos/2d/CCMotionStreak.cpp @@ -211,9 +211,6 @@ void MotionStreak::setTexture(Texture2D *texture) _texture = texture; setProgramStateWithRegistry(backend::ProgramType::POSITION_TEXTURE_COLOR, _texture); - - if (_texture) - _programState->setTexture(_texture->getBackendTexture()); } } @@ -245,6 +242,8 @@ bool MotionStreak::attachProgramState(backend::ProgramState* programState) vertexLayout->setAttribute("a_color", iter->second.location, backend::VertexFormat::UBYTE4, 4 * sizeof(float), true); } vertexLayout->setLayout(4 * sizeof(float) + 4 * sizeof(uint8_t)); + + updateProgramStateTexture(_texture); return true; } return false; diff --git a/cocos/2d/CCSpriteBatchNode.cpp b/cocos/2d/CCSpriteBatchNode.cpp index 099ae894e8..a7d605eca4 100644 --- a/cocos/2d/CCSpriteBatchNode.cpp +++ b/cocos/2d/CCSpriteBatchNode.cpp @@ -103,7 +103,6 @@ bool SpriteBatchNode::initWithTexture(Texture2D *tex, ssize_t capacity/* = DEFAU _textureAtlas->initWithTexture(tex, capacity); setProgramStateWithRegistry(backend::ProgramType::POSITION_TEXTURE_COLOR, tex); - updateProgramStateTexture(_textureAtlas->getTexture()); updateBlendFunc(); @@ -156,6 +155,7 @@ bool SpriteBatchNode::attachProgramState(backend::ProgramState *programState) pipelineDescriptor.programState = _programState; setVertexLayout(); + updateProgramStateTexture(_textureAtlas->getTexture()); setUniformLocation(); return true; } @@ -697,7 +697,6 @@ void SpriteBatchNode::setTexture(Texture2D *texture) { _textureAtlas->setTexture(texture); setProgramStateWithRegistry(backend::ProgramType::POSITION_TEXTURE_COLOR, texture); - updateProgramStateTexture(texture); updateBlendFunc(); } diff --git a/cocos/base/CCDirector.cpp b/cocos/base/CCDirector.cpp index f83dd2abe8..2b5de9ce1c 100644 --- a/cocos/base/CCDirector.cpp +++ b/cocos/base/CCDirector.cpp @@ -1228,6 +1228,22 @@ void Director::getFPSImageData(unsigned char** datapointer, ssize_t* length) *length = cc_fps_images_len(); } +template +static T* newInstance2(std::function pFunc, Ts&&... args) +{ + T* pRet = new(std::nothrow) T(); + if (pRet && std::mem_fn(memf)(pRet, std::forward(args)...)) + { + return pRet; + } + else + { + delete pRet; + pRet = nullptr; + return nullptr; + } +} + void Director::createStatsLabel() { Texture2D *texture = nullptr; @@ -1275,22 +1291,19 @@ void Director::createStatsLabel() */ float scaleFactor = 1 / CC_CONTENT_SCALE_FACTOR(); - _FPSLabel = LabelAtlas::create(); + _FPSLabel = LabelAtlas::create(fpsString, texture, 12, 32, '.'); _FPSLabel->retain(); _FPSLabel->setIgnoreContentScaleFactor(true); - _FPSLabel->initWithString(fpsString, texture, 12, 32 , '.'); _FPSLabel->setScale(scaleFactor); - _drawnBatchesLabel = LabelAtlas::create(); + _drawnBatchesLabel = LabelAtlas::create(drawBatchString, texture, 12, 32, '.'); _drawnBatchesLabel->retain(); _drawnBatchesLabel->setIgnoreContentScaleFactor(true); - _drawnBatchesLabel->initWithString(drawBatchString, texture, 12, 32, '.'); _drawnBatchesLabel->setScale(scaleFactor); - _drawnVerticesLabel = LabelAtlas::create(); + _drawnVerticesLabel = LabelAtlas::create(drawVerticesString, texture, 12, 32, '.'); _drawnVerticesLabel->retain(); _drawnVerticesLabel->setIgnoreContentScaleFactor(true); - _drawnVerticesLabel->initWithString(drawVerticesString, texture, 12, 32, '.'); _drawnVerticesLabel->setScale(scaleFactor); From e537683b3f16b0e06d2c0d6e6b47f9de579cad71 Mon Sep 17 00:00:00 2001 From: halx99 Date: Wed, 9 Sep 2020 15:47:41 +0800 Subject: [PATCH 3/4] fix compile issue[skip appveyor] --- cocos/base/CCDirector.cpp | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/cocos/base/CCDirector.cpp b/cocos/base/CCDirector.cpp index 2b5de9ce1c..86d82ac865 100644 --- a/cocos/base/CCDirector.cpp +++ b/cocos/base/CCDirector.cpp @@ -1228,22 +1228,6 @@ void Director::getFPSImageData(unsigned char** datapointer, ssize_t* length) *length = cc_fps_images_len(); } -template -static T* newInstance2(std::function pFunc, Ts&&... args) -{ - T* pRet = new(std::nothrow) T(); - if (pRet && std::mem_fn(memf)(pRet, std::forward(args)...)) - { - return pRet; - } - else - { - delete pRet; - pRet = nullptr; - return nullptr; - } -} - void Director::createStatsLabel() { Texture2D *texture = nullptr; From 29d21add676dcbc2fcf6ad3c587cb6d0c2c7f2b1 Mon Sep 17 00:00:00 2001 From: halx99 Date: Wed, 9 Sep 2020 16:30:48 +0800 Subject: [PATCH 4/4] Sync lua binding --- .../scripting/lua-bindings/auto/lua_cocos2dx_auto.cpp | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/cocos/scripting/lua-bindings/auto/lua_cocos2dx_auto.cpp b/cocos/scripting/lua-bindings/auto/lua_cocos2dx_auto.cpp index a3f0aab78d..cd8339fd6e 100644 --- a/cocos/scripting/lua-bindings/auto/lua_cocos2dx_auto.cpp +++ b/cocos/scripting/lua-bindings/auto/lua_cocos2dx_auto.cpp @@ -56366,16 +56366,6 @@ int lua_cocos2dx_LabelAtlas_create(lua_State* tolua_S) } while (0); ok = true; do - { - if (argc == 0) - { - cocos2d::LabelAtlas* ret = cocos2d::LabelAtlas::create(); - object_to_luaval(tolua_S, "cc.LabelAtlas",(cocos2d::LabelAtlas*)ret); - return 1; - } - } while (0); - ok = true; - do { if (argc == 2) {