fix: make PolygonCode cleaner

not sure if this fixes the bug, but it makes the interfaces cleaner.

Github issue #15154
This commit is contained in:
Ricardo Quesada 2016-05-24 20:57:40 -03:00
parent 735dce1e91
commit 9ad3db0635
8 changed files with 42 additions and 37 deletions

View File

@ -38,20 +38,32 @@ USING_NS_CC;
static unsigned short quadIndices[]={0,1,2, 3,2,1}; static unsigned short quadIndices[]={0,1,2, 3,2,1};
const static float PRECISION = 10.0f; const static float PRECISION = 10.0f;
PolygonInfo::PolygonInfo(const PolygonInfo& other): PolygonInfo::PolygonInfo()
triangles(), : rect(cocos2d::Rect::ZERO)
rect(), , filename("")
isVertsOwner(true) , 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; filename = other.filename;
isVertsOwner = true; isVertsOwner = true;
rect = other.rect; rect = other.rect;
triangles.verts = new (std::nothrow) V3F_C4B_T2F[other.triangles.vertCount]; triangles.verts = new (std::nothrow) V3F_C4B_T2F[other.triangles.vertCount];
triangles.indices = new (std::nothrow) unsigned short[other.triangles.indexCount]; triangles.indices = new (std::nothrow) unsigned short[other.triangles.indexCount];
CCASSERT(triangles.verts && triangles.indices, "not enough memory");
triangles.vertCount = other.triangles.vertCount; triangles.vertCount = other.triangles.vertCount;
triangles.indexCount = other.triangles.indexCount; triangles.indexCount = other.triangles.indexCount;
memcpy(triangles.verts, other.triangles.verts, other.triangles.vertCount*sizeof(V3F_C4B_T2F)); memcpy(triangles.verts, other.triangles.verts, other.triangles.vertCount * sizeof(other.triangles.verts[0]));
memcpy(triangles.indices, other.triangles.indices, other.triangles.indexCount*sizeof(unsigned short)); memcpy(triangles.indices, other.triangles.indices, other.triangles.indexCount * sizeof(other.triangles.indices[0]));
}; };
PolygonInfo& PolygonInfo::operator= (const PolygonInfo& other) PolygonInfo& PolygonInfo::operator= (const PolygonInfo& other)
@ -64,10 +76,11 @@ PolygonInfo& PolygonInfo::operator= (const PolygonInfo& other)
rect = other.rect; rect = other.rect;
triangles.verts = new (std::nothrow) V3F_C4B_T2F[other.triangles.vertCount]; triangles.verts = new (std::nothrow) V3F_C4B_T2F[other.triangles.vertCount];
triangles.indices = new (std::nothrow) unsigned short[other.triangles.indexCount]; triangles.indices = new (std::nothrow) unsigned short[other.triangles.indexCount];
CCASSERT(triangles.verts && triangles.indices, "not enough memory");
triangles.vertCount = other.triangles.vertCount; triangles.vertCount = other.triangles.vertCount;
triangles.indexCount = other.triangles.indexCount; triangles.indexCount = other.triangles.indexCount;
memcpy(triangles.verts, other.triangles.verts, other.triangles.vertCount*sizeof(V3F_C4B_T2F)); memcpy(triangles.verts, other.triangles.verts, other.triangles.vertCount * sizeof(other.triangles.verts[0]));
memcpy(triangles.indices, other.triangles.indices, other.triangles.indexCount*sizeof(unsigned short)); memcpy(triangles.indices, other.triangles.indices, other.triangles.indexCount * sizeof(other.triangles.indices[0]));
} }
return *this; return *this;
} }
@ -87,7 +100,7 @@ void PolygonInfo::setQuad(V3F_C4B_T2F_Quad *quad)
triangles.verts = (V3F_C4B_T2F*)quad; triangles.verts = (V3F_C4B_T2F*)quad;
} }
void PolygonInfo::setTriangles(TrianglesCommand::Triangles other) void PolygonInfo::setTriangles(const TrianglesCommand::Triangles& other)
{ {
this->releaseVertsAndIndices(); this->releaseVertsAndIndices();
isVertsOwner = false; 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) PolygonInfo AutoPolygon::generatePolygon(const std::string& filename, const Rect& rect, const float epsilon, const float threshold)
{ {
AutoPolygon ap(filename); AutoPolygon ap(filename);
auto ret = ap.generateTriangles(rect, epsilon, threshold); return ap.generateTriangles(rect, epsilon, threshold);
return ret;
} }

View File

@ -54,17 +54,8 @@ public:
* @memberof PolygonInfo * @memberof PolygonInfo
* @return PolygonInfo object * @return PolygonInfo object
*/ */
PolygonInfo(): PolygonInfo();
rect(cocos2d::Rect::ZERO),
filename(""),
isVertsOwner(true)
{
triangles.verts = nullptr;
triangles.indices = nullptr;
triangles.vertCount = 0;
triangles.indexCount = 0;
};
/** /**
* Create an polygoninfo from the data of another Polygoninfo * Create an polygoninfo from the data of another Polygoninfo
* @param other another PolygonInfo to be copied * @param other another PolygonInfo to be copied
@ -95,7 +86,7 @@ public:
* as the verts memory are managed by other objects * as the verts memory are managed by other objects
* @param triangles a pointer to the TrianglesCommand::Triangles object * @param triangles a pointer to the TrianglesCommand::Triangles object
*/ */
void setTriangles(TrianglesCommand::Triangles triangles); void setTriangles(const TrianglesCommand::Triangles& triangles);
/** /**
* get vertex count * get vertex count

View File

@ -1183,7 +1183,7 @@ std::string Sprite::getDescription() const
return StringUtils::format("<Sprite | Tag = %d, TextureID = %d>", _tag, texture_id ); return StringUtils::format("<Sprite | Tag = %d, TextureID = %d>", _tag, texture_id );
} }
PolygonInfo& Sprite::getPolygonInfo() const PolygonInfo& Sprite::getPolygonInfo() const
{ {
return _polyInfo; return _polyInfo;
} }

View File

@ -406,9 +406,9 @@ public:
/** /**
* returns a reference of the polygon information associated with this sprite * 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 * set the sprite to use this new PolygonInfo

View File

@ -218,7 +218,7 @@ void SpriteFrame::setPolygonInfo(const PolygonInfo &polygonInfo)
_polygonInfo = polygonInfo; _polygonInfo = polygonInfo;
} }
const PolygonInfo &SpriteFrame::getPolygonInfo() const const PolygonInfo& SpriteFrame::getPolygonInfo() const
{ {
return _polygonInfo; return _polygonInfo;
} }

View File

@ -215,9 +215,9 @@ public:
/** Get the polygonInfo for this sprite /** 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 /** Check if sprite frame is a polygon sprite
* *

View File

@ -552,7 +552,9 @@ namespace ui {
auto vertices = this->calculateVertices(capInsets, originalSize, offsets); auto vertices = this->calculateVertices(capInsets, originalSize, offsets);
auto triangles = this->calculateTriangles(uv, vertices); auto triangles = this->calculateTriangles(uv, vertices);
_scale9Image->getPolygonInfo().setTriangles(triangles); auto polyInfo = _scale9Image->getPolygonInfo();
polyInfo.setTriangles(triangles);
_scale9Image->setPolygonInfo(polyInfo);
} }
} }

View File

@ -91,23 +91,23 @@ void SpritePolygonTestCase::updateDrawNode()
auto drawnode = _drawNodes.at(i); auto drawnode = _drawNodes.at(i);
auto sp = (Sprite*)drawnode->getParent(); auto sp = (Sprite*)drawnode->getParent();
if(!sp) return; if(!sp) return;
auto polygoninfo = sp->getPolygonInfo(); const auto& polygoninfo = sp->getPolygonInfo();
drawnode->clear(); drawnode->clear();
auto count = polygoninfo.triangles.indexCount/3; const auto count = polygoninfo.triangles.indexCount/3;
auto indices = polygoninfo.triangles.indices; const auto indices = polygoninfo.triangles.indices;
auto verts = polygoninfo.triangles.verts; const auto verts = polygoninfo.triangles.verts;
for(ssize_t i = 0; i < count; i++) for(ssize_t i = 0; i < count; i++)
{ {
//draw 3 lines //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; Vec3 to = verts[indices[i*3+1]].vertices;
drawnode->drawLine(Vec2(from.x, from.y), Vec2(to.x,to.y), Color4F::GREEN); 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; to = verts[indices[i*3+2]].vertices;
drawnode->drawLine(Vec2(from.x, from.y), Vec2(to.x,to.y), Color4F::GREEN); 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; to = verts[indices[i*3]].vertices;
drawnode->drawLine(Vec2(from.x, from.y), Vec2(to.x,to.y), Color4F::GREEN); drawnode->drawLine(Vec2(from.x, from.y), Vec2(to.x,to.y), Color4F::GREEN);
} }