mirror of https://github.com/axmolengine/axmol.git
Merge pull request #9 from fusijie/fix_spritepolygon2
fix memory leakage about operation=
This commit is contained in:
commit
db07e21a71
|
@ -35,6 +35,8 @@ THE SOFTWARE.
|
|||
|
||||
USING_NS_CC;
|
||||
|
||||
static unsigned short quadIndices[]={0,1,2, 3,2,1};
|
||||
|
||||
PolygonInfo::PolygonInfo(const PolygonInfo& other):
|
||||
triangles(),
|
||||
isVertsOwner(true),
|
||||
|
@ -51,28 +53,11 @@ rect()
|
|||
memcpy(triangles.indices, other.triangles.indices, other.triangles.indexCount*sizeof(unsigned short));
|
||||
};
|
||||
|
||||
void PolygonInfo::setQuad(V3F_C4B_T2F_Quad *quad)
|
||||
{
|
||||
if(nullptr != triangles.verts && isVertsOwner)
|
||||
{
|
||||
CC_SAFE_DELETE_ARRAY(triangles.verts);
|
||||
}
|
||||
|
||||
if(nullptr != triangles.indices)
|
||||
{
|
||||
CC_SAFE_DELETE_ARRAY(triangles.indices);
|
||||
}
|
||||
|
||||
isVertsOwner = false;
|
||||
triangles.indices = new unsigned short[6]{0,1,2, 3,2,1};
|
||||
triangles.vertCount = 4;
|
||||
triangles.indexCount = 6;
|
||||
triangles.verts = (V3F_C4B_T2F*)quad;
|
||||
}
|
||||
PolygonInfo& PolygonInfo::operator= (const PolygonInfo& other)
|
||||
{
|
||||
if(this != &other)
|
||||
{
|
||||
releaseVertsAndIndices();
|
||||
filename = other.filename;
|
||||
isVertsOwner = other.isVertsOwner;
|
||||
rect = other.rect;
|
||||
|
@ -85,18 +70,38 @@ PolygonInfo& PolygonInfo::operator= (const PolygonInfo& other)
|
|||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
PolygonInfo::~PolygonInfo()
|
||||
{
|
||||
if(nullptr != triangles.verts && isVertsOwner)
|
||||
{
|
||||
CC_SAFE_DELETE_ARRAY(triangles.verts);
|
||||
}
|
||||
releaseVertsAndIndices();
|
||||
}
|
||||
|
||||
if(nullptr != triangles.indices)
|
||||
void PolygonInfo::setQuad(V3F_C4B_T2F_Quad *quad)
|
||||
{
|
||||
releaseVertsAndIndices();
|
||||
isVertsOwner = false;
|
||||
triangles.indices = quadIndices;
|
||||
triangles.vertCount = 4;
|
||||
triangles.indexCount = 6;
|
||||
triangles.verts = (V3F_C4B_T2F*)quad;
|
||||
}
|
||||
|
||||
void PolygonInfo::releaseVertsAndIndices()
|
||||
{
|
||||
if(isVertsOwner)
|
||||
{
|
||||
CC_SAFE_DELETE_ARRAY(triangles.indices);
|
||||
if(nullptr != triangles.verts)
|
||||
{
|
||||
CC_SAFE_DELETE_ARRAY(triangles.verts);
|
||||
}
|
||||
|
||||
if(nullptr != triangles.indices)
|
||||
{
|
||||
CC_SAFE_DELETE_ARRAY(triangles.indices);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const unsigned int PolygonInfo::getVertCount() const
|
||||
{
|
||||
return (unsigned int)triangles.vertCount;
|
||||
|
@ -117,8 +122,6 @@ const float PolygonInfo::getArea() const
|
|||
return area;
|
||||
}
|
||||
|
||||
|
||||
|
||||
AutoPolygon::AutoPolygon(const std::string &filename)
|
||||
:_image(nullptr)
|
||||
,_data(nullptr)
|
||||
|
@ -150,7 +153,6 @@ std::vector<Vec2> AutoPolygon::trace(const Rect& rect, const float& threshold)
|
|||
|
||||
Vec2 AutoPolygon::findFirstNoneTransparentPixel(const Rect& rect, const float& threshold)
|
||||
{
|
||||
Vec2 first(rect.origin);
|
||||
bool found = false;
|
||||
Vec2 i;
|
||||
for(i.y = rect.origin.y; i.y < rect.origin.y+rect.size.height; i.y++)
|
||||
|
@ -174,9 +176,9 @@ unsigned char AutoPolygon::getAlphaByIndex(const unsigned int& i)
|
|||
{
|
||||
return *(_data+i*4+3);
|
||||
}
|
||||
unsigned char AutoPolygon::getAlphaByPos(const Vec2& i)
|
||||
unsigned char AutoPolygon::getAlphaByPos(const Vec2& pos)
|
||||
{
|
||||
return *(_data+((int)i.y*_width+(int)i.x)*4+3);
|
||||
return *(_data+((int)pos.y*_width+(int)pos.x)*4+3);
|
||||
}
|
||||
|
||||
unsigned int AutoPolygon::getSquareValue(const unsigned int& x, const unsigned int& y, const Rect& rect, const float& threshold)
|
||||
|
@ -193,9 +195,6 @@ unsigned int AutoPolygon::getSquareValue(const unsigned int& x, const unsigned i
|
|||
//NOTE: due to the way we pick points from texture, rect needs to be smaller, otherwise it goes outside 1 pixel
|
||||
auto fixedRect = Rect(rect.origin, rect.size-Size(2,2));
|
||||
|
||||
|
||||
|
||||
|
||||
Vec2 tl = Vec2(x-1, y-1);
|
||||
sv += (fixedRect.containsPoint(tl) && getAlphaByPos(tl) > threshold)? 1 : 0;
|
||||
Vec2 tr = Vec2(x, y-1);
|
||||
|
@ -485,7 +484,6 @@ std::vector<Vec2> AutoPolygon::expand(const std::vector<Vec2>& points, const coc
|
|||
co.AddPath(subj, ClipperLib::jtMiter, ClipperLib::etClosedPolygon);
|
||||
co.Execute(solution, epsilon);
|
||||
|
||||
|
||||
ClipperLib::PolyNode* p = solution.GetFirst();
|
||||
if(!p)
|
||||
{
|
||||
|
@ -499,7 +497,7 @@ std::vector<Vec2> AutoPolygon::expand(const std::vector<Vec2>& points, const coc
|
|||
//turn the result into simply polygon (AKA, fix overlap)
|
||||
|
||||
//clamp into the specified rect
|
||||
ClipperLib::Clipper cl= ClipperLib::Clipper();
|
||||
ClipperLib::Clipper cl;
|
||||
cl.StrictlySimple(true);
|
||||
cl.AddPath(p->Contour, ClipperLib::ptSubject, true);
|
||||
//create the clipping rect
|
||||
|
@ -524,11 +522,6 @@ std::vector<Vec2> AutoPolygon::expand(const std::vector<Vec2>& points, const coc
|
|||
return outPoints;
|
||||
}
|
||||
|
||||
bool AutoPolygon::isAConvexPoint(const cocos2d::Vec2& p1, const cocos2d::Vec2& p2)
|
||||
{
|
||||
return p1.cross(p2) >0 ? true : false;
|
||||
}
|
||||
|
||||
TrianglesCommand::Triangles AutoPolygon::triangulate(const std::vector<Vec2>& points)
|
||||
{
|
||||
// if there are less than 3 points, then we can't triangulate
|
||||
|
|
|
@ -55,10 +55,14 @@ public:
|
|||
* @return PolygonInfo object
|
||||
*/
|
||||
PolygonInfo():
|
||||
triangles(),
|
||||
isVertsOwner(true),
|
||||
rect()
|
||||
rect(cocos2d::Rect::ZERO),
|
||||
filename("")
|
||||
{
|
||||
triangles.verts = nullptr;
|
||||
triangles.indices = nullptr;
|
||||
triangles.vertCount = 0;
|
||||
triangles.indexCount = 0;
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -85,10 +89,6 @@ public:
|
|||
*/
|
||||
void setQuad(V3F_C4B_T2F_Quad *quad);
|
||||
|
||||
Rect rect;
|
||||
std::string filename;
|
||||
TrianglesCommand::Triangles triangles;
|
||||
|
||||
/**
|
||||
* get vertex count
|
||||
* @return number of vertices
|
||||
|
@ -101,8 +101,15 @@ public:
|
|||
*/
|
||||
const float getArea() const;
|
||||
|
||||
Rect rect;
|
||||
std::string filename;
|
||||
TrianglesCommand::Triangles triangles;
|
||||
|
||||
protected:
|
||||
bool isVertsOwner;
|
||||
|
||||
private:
|
||||
void releaseVertsAndIndices();
|
||||
};
|
||||
|
||||
|
||||
|
@ -238,7 +245,7 @@ protected:
|
|||
unsigned int getSquareValue(const unsigned int& x, const unsigned int& y, const Rect& rect, const float& threshold);
|
||||
|
||||
unsigned char getAlphaByIndex(const unsigned int& i);
|
||||
unsigned char getAlphaByPos(const Vec2& i);
|
||||
unsigned char getAlphaByPos(const Vec2& pos);
|
||||
|
||||
int getIndexFromPos(const unsigned int& x, const unsigned int& y){return y*_width+x;};
|
||||
cocos2d::Vec2 getPosFromIndex(const unsigned int& i){return cocos2d::Vec2(i%_width, i/_width);};
|
||||
|
@ -246,7 +253,6 @@ protected:
|
|||
std::vector<cocos2d::Vec2> rdp(std::vector<cocos2d::Vec2> v, const float& optimization);
|
||||
float perpendicularDistance(const cocos2d::Vec2& i, const cocos2d::Vec2& start, const cocos2d::Vec2& end);
|
||||
|
||||
bool isAConvexPoint(const cocos2d::Vec2& p1, const cocos2d::Vec2& p2);
|
||||
//real rect is the size that is in scale with the texture file
|
||||
Rect getRealRect(const Rect& rect);
|
||||
|
||||
|
|
Loading…
Reference in New Issue