diff --git a/cocos/2d/CCAutoPolygon.cpp b/cocos/2d/CCAutoPolygon.cpp index fe80c26f3e..315bdc12ae 100644 --- a/cocos/2d/CCAutoPolygon.cpp +++ b/cocos/2d/CCAutoPolygon.cpp @@ -38,20 +38,32 @@ USING_NS_CC; static unsigned short quadIndices[]={0,1,2, 3,2,1}; const static float PRECISION = 10.0f; -PolygonInfo::PolygonInfo(const PolygonInfo& other): -triangles(), -rect(), -isVertsOwner(true) +PolygonInfo::PolygonInfo() +: rect(cocos2d::Rect::ZERO) +, filename("") +, isVertsOwner(true) +{ + triangles.verts = nullptr; + triangles.indices = nullptr; + triangles.vertCount = 0; + triangles.indexCount = 0; +}; + +PolygonInfo::PolygonInfo(const PolygonInfo& other) +: triangles() +, rect() +, isVertsOwner(true) { filename = other.filename; isVertsOwner = true; rect = other.rect; triangles.verts = new (std::nothrow) V3F_C4B_T2F[other.triangles.vertCount]; triangles.indices = new (std::nothrow) unsigned short[other.triangles.indexCount]; + CCASSERT(triangles.verts && triangles.indices, "not enough memory"); triangles.vertCount = other.triangles.vertCount; triangles.indexCount = other.triangles.indexCount; - memcpy(triangles.verts, other.triangles.verts, other.triangles.vertCount*sizeof(V3F_C4B_T2F)); - memcpy(triangles.indices, other.triangles.indices, other.triangles.indexCount*sizeof(unsigned short)); + memcpy(triangles.verts, other.triangles.verts, other.triangles.vertCount * sizeof(other.triangles.verts[0])); + memcpy(triangles.indices, other.triangles.indices, other.triangles.indexCount * sizeof(other.triangles.indices[0])); }; PolygonInfo& PolygonInfo::operator= (const PolygonInfo& other) @@ -64,10 +76,11 @@ PolygonInfo& PolygonInfo::operator= (const PolygonInfo& other) rect = other.rect; triangles.verts = new (std::nothrow) V3F_C4B_T2F[other.triangles.vertCount]; triangles.indices = new (std::nothrow) unsigned short[other.triangles.indexCount]; + CCASSERT(triangles.verts && triangles.indices, "not enough memory"); triangles.vertCount = other.triangles.vertCount; triangles.indexCount = other.triangles.indexCount; - memcpy(triangles.verts, other.triangles.verts, other.triangles.vertCount*sizeof(V3F_C4B_T2F)); - memcpy(triangles.indices, other.triangles.indices, other.triangles.indexCount*sizeof(unsigned short)); + memcpy(triangles.verts, other.triangles.verts, other.triangles.vertCount * sizeof(other.triangles.verts[0])); + memcpy(triangles.indices, other.triangles.indices, other.triangles.indexCount * sizeof(other.triangles.indices[0])); } return *this; } @@ -87,7 +100,7 @@ void PolygonInfo::setQuad(V3F_C4B_T2F_Quad *quad) triangles.verts = (V3F_C4B_T2F*)quad; } -void PolygonInfo::setTriangles(TrianglesCommand::Triangles other) +void PolygonInfo::setTriangles(const TrianglesCommand::Triangles& other) { this->releaseVertsAndIndices(); isVertsOwner = false; @@ -679,6 +692,5 @@ PolygonInfo AutoPolygon::generateTriangles(const Rect& rect, const float& epsilo PolygonInfo AutoPolygon::generatePolygon(const std::string& filename, const Rect& rect, const float epsilon, const float threshold) { AutoPolygon ap(filename); - auto ret = ap.generateTriangles(rect, epsilon, threshold); - return ret; + return ap.generateTriangles(rect, epsilon, threshold); } diff --git a/cocos/2d/CCAutoPolygon.h b/cocos/2d/CCAutoPolygon.h index 5863351407..422128dd60 100644 --- a/cocos/2d/CCAutoPolygon.h +++ b/cocos/2d/CCAutoPolygon.h @@ -54,17 +54,8 @@ public: * @memberof PolygonInfo * @return PolygonInfo object */ - PolygonInfo(): - rect(cocos2d::Rect::ZERO), - filename(""), - isVertsOwner(true) - { - triangles.verts = nullptr; - triangles.indices = nullptr; - triangles.vertCount = 0; - triangles.indexCount = 0; - }; - + PolygonInfo(); + /** * Create an polygoninfo from the data of another Polygoninfo * @param other another PolygonInfo to be copied @@ -95,7 +86,7 @@ public: * as the verts memory are managed by other objects * @param triangles a pointer to the TrianglesCommand::Triangles object */ - void setTriangles(TrianglesCommand::Triangles triangles); + void setTriangles(const TrianglesCommand::Triangles& triangles); /** * get vertex count diff --git a/cocos/2d/CCSprite.cpp b/cocos/2d/CCSprite.cpp index 347932859f..18b8848509 100644 --- a/cocos/2d/CCSprite.cpp +++ b/cocos/2d/CCSprite.cpp @@ -1183,7 +1183,7 @@ std::string Sprite::getDescription() const return StringUtils::format("", _tag, texture_id ); } -PolygonInfo& Sprite::getPolygonInfo() +const PolygonInfo& Sprite::getPolygonInfo() const { return _polyInfo; } diff --git a/cocos/2d/CCSprite.h b/cocos/2d/CCSprite.h index 883a08a0e5..b58166da34 100644 --- a/cocos/2d/CCSprite.h +++ b/cocos/2d/CCSprite.h @@ -406,9 +406,9 @@ public: /** * returns a reference of the polygon information associated with this sprite * - * @return a copy of PolygonInfo + * @return a reference of PolygonInfo */ - PolygonInfo& getPolygonInfo(); + const PolygonInfo& getPolygonInfo() const; /** * set the sprite to use this new PolygonInfo diff --git a/cocos/2d/CCSpriteFrame.cpp b/cocos/2d/CCSpriteFrame.cpp index 516d4f7157..18bf7fbc61 100644 --- a/cocos/2d/CCSpriteFrame.cpp +++ b/cocos/2d/CCSpriteFrame.cpp @@ -218,7 +218,7 @@ void SpriteFrame::setPolygonInfo(const PolygonInfo &polygonInfo) _polygonInfo = polygonInfo; } -const PolygonInfo &SpriteFrame::getPolygonInfo() const +const PolygonInfo& SpriteFrame::getPolygonInfo() const { return _polygonInfo; } diff --git a/cocos/2d/CCSpriteFrame.h b/cocos/2d/CCSpriteFrame.h index ae443b1ff3..a91ac54bea 100644 --- a/cocos/2d/CCSpriteFrame.h +++ b/cocos/2d/CCSpriteFrame.h @@ -215,9 +215,9 @@ public: /** Get the polygonInfo for this sprite * - * @return polygonInfo structure + * @return a reference to the polygonInfo structure */ - const PolygonInfo &getPolygonInfo() const; + const PolygonInfo& getPolygonInfo() const; /** Check if sprite frame is a polygon sprite * diff --git a/cocos/ui/UIScale9Sprite.cpp b/cocos/ui/UIScale9Sprite.cpp index 41e70471cb..6c4ec6c563 100644 --- a/cocos/ui/UIScale9Sprite.cpp +++ b/cocos/ui/UIScale9Sprite.cpp @@ -552,7 +552,9 @@ namespace ui { auto vertices = this->calculateVertices(capInsets, originalSize, offsets); auto triangles = this->calculateTriangles(uv, vertices); - _scale9Image->getPolygonInfo().setTriangles(triangles); + auto polyInfo = _scale9Image->getPolygonInfo(); + polyInfo.setTriangles(triangles); + _scale9Image->setPolygonInfo(polyInfo); } } diff --git a/tests/cpp-tests/Classes/SpritePolygonTest/SpritePolygonTest.cpp b/tests/cpp-tests/Classes/SpritePolygonTest/SpritePolygonTest.cpp index acecb2ce3e..618e421bf6 100644 --- a/tests/cpp-tests/Classes/SpritePolygonTest/SpritePolygonTest.cpp +++ b/tests/cpp-tests/Classes/SpritePolygonTest/SpritePolygonTest.cpp @@ -91,23 +91,23 @@ void SpritePolygonTestCase::updateDrawNode() auto drawnode = _drawNodes.at(i); auto sp = (Sprite*)drawnode->getParent(); if(!sp) return; - auto polygoninfo = sp->getPolygonInfo(); + const auto& polygoninfo = sp->getPolygonInfo(); drawnode->clear(); - auto count = polygoninfo.triangles.indexCount/3; - auto indices = polygoninfo.triangles.indices; - auto verts = polygoninfo.triangles.verts; + const auto count = polygoninfo.triangles.indexCount/3; + const auto indices = polygoninfo.triangles.indices; + const auto verts = polygoninfo.triangles.verts; for(ssize_t i = 0; i < count; i++) { //draw 3 lines - Vec3 from =verts[indices[i*3]].vertices; + Vec3 from = verts[indices[i*3]].vertices; Vec3 to = verts[indices[i*3+1]].vertices; drawnode->drawLine(Vec2(from.x, from.y), Vec2(to.x,to.y), Color4F::GREEN); - from =verts[indices[i*3+1]].vertices; + from = verts[indices[i*3+1]].vertices; to = verts[indices[i*3+2]].vertices; drawnode->drawLine(Vec2(from.x, from.y), Vec2(to.x,to.y), Color4F::GREEN); - from =verts[indices[i*3+2]].vertices; + from = verts[indices[i*3+2]].vertices; to = verts[indices[i*3]].vertices; drawnode->drawLine(Vec2(from.x, from.y), Vec2(to.x,to.y), Color4F::GREEN); }