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};
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);
}

View File

@ -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

View File

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

View File

@ -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

View File

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

View File

@ -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
*

View File

@ -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);
}
}

View File

@ -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);
}