From 3de665b9cdb3ba02936c993eeadc41e1e4f8e29b Mon Sep 17 00:00:00 2001 From: samuele3hu Date: Mon, 27 Apr 2015 15:19:26 +0800 Subject: [PATCH 1/3] Update SpritePolygon --- cocos/2d/SpritePolygon.cpp | 16 +++++--------- cocos/2d/SpritePolygonCache.cpp | 38 ++++++++++++++++++++++----------- cocos/2d/SpritePolygonCache.h | 6 +++--- 3 files changed, 33 insertions(+), 27 deletions(-) diff --git a/cocos/2d/SpritePolygon.cpp b/cocos/2d/SpritePolygon.cpp index eb382769e9..dfb3e54b29 100644 --- a/cocos/2d/SpritePolygon.cpp +++ b/cocos/2d/SpritePolygon.cpp @@ -251,14 +251,11 @@ bool SpritePolygon::initWithMarching(const std::string &file, const cocos2d::Rec marcher->optimize(optimization); auto p = marcher->getPoints(); - auto _triangles = triangulate(p); + auto triangles = triangulate(p); delete marcher; //save result to cache - SpritePolygonInfo info; - info._rect = rect; - info._triangles = _triangles; - _polygonInfo = SpritePolygonCache::getInstance()->addSpritePolygonCache(file, info); + _polygonInfo = SpritePolygonCache::getInstance()->addSpritePolygonCache(file, rect, triangles); calculateUVandContentSize(); // SpritePolygonCache::printInfo(*_polygonInfo); #if CC_SPRITE_DEBUG_DRAW @@ -277,8 +274,7 @@ bool SpritePolygon::initWithPoly2tri(const std::string &filename, std::vectoraddSpritePolygonCache(filename, info); + _polygonInfo = SpritePolygonCache::getInstance()->addSpritePolygonCache(filename, Rect, triangulate(verts)); calculateUVandContentSize(); #if CC_SPRITE_DEBUG_DRAW debugDraw(); @@ -331,8 +327,7 @@ bool SpritePolygon::initWithVerts(const std::string& filename,std::vectoraddSpritePolygonCache(filename, info); + _polygonInfo = SpritePolygonCache::getInstance()->addSpritePolygonCache(filename, _textureRect, _triangles); #if CC_SPRITE_DEBUG_DRAW debugDraw(); #endif @@ -356,8 +351,7 @@ bool SpritePolygon::initWithRect(const std::string& filename, std::vectoraddSpritePolygonCache(filename, info); + _polygonInfo = SpritePolygonCache::getInstance()->addSpritePolygonCache(filename, rect, _triangles); calculateUVandContentSize(); #if CC_SPRITE_DEBUG_DRAW debugDraw(); diff --git a/cocos/2d/SpritePolygonCache.cpp b/cocos/2d/SpritePolygonCache.cpp index 14eb66757f..57119219a2 100644 --- a/cocos/2d/SpritePolygonCache.cpp +++ b/cocos/2d/SpritePolygonCache.cpp @@ -73,7 +73,7 @@ void SpritePolygonCache::init() } -SpritePolygonInfo* SpritePolygonCache::addSpritePolygonCache(const std::string& filePath, const SpritePolygonInfo& spritePolygonInfo) +SpritePolygonInfo* SpritePolygonCache::addSpritePolygonCache(const std::string& filePath, const cocos2d::Rect& rect, const cocos2d::TrianglesCommand::Triangles trianglesCommand) { auto fullpath = filePath; @@ -85,16 +85,23 @@ SpritePolygonInfo* SpritePolygonCache::addSpritePolygonCache(const std::string& auto infoIt = vecInfo.begin(); for (; infoIt != vecInfo.end(); infoIt++) { - if ((*infoIt)->_rect.equals(spritePolygonInfo._rect)) + if ((*infoIt)->_rect.equals(rect)) { - (*infoIt)->_triangles = spritePolygonInfo._triangles; -// (*infoIt)->_textureRect = SpritePolygonInfo._textureRect; - return nullptr; + //Update + CC_SAFE_DELETE((*infoIt)->_triangles.verts); + CC_SAFE_DELETE((*infoIt)->_triangles.indices); + (*infoIt)->_triangles.verts = new V3F_C4B_T2F[trianglesCommand.vertCount]; + (*infoIt)->_triangles.indices = new unsigned short[trianglesCommand.indexCount]; + (*infoIt)->_triangles.vertCount = trianglesCommand.vertCount; + (*infoIt)->_triangles.indexCount = trianglesCommand.indexCount; + memcpy((*infoIt)->_triangles.verts, trianglesCommand.verts, trianglesCommand.vertCount*sizeof(V3F_C4B_T2F)); + memcpy((*infoIt)->_triangles.indices, trianglesCommand.indices, trianglesCommand.indexCount*sizeof(unsigned short)); + return *infoIt; } } } - VecSpritePolygonInfo vecInfo ; + VecSpritePolygonInfo vecInfo; vecInfo.clear(); if (it != _SpritePolygonCacheMap.end()) { @@ -103,15 +110,15 @@ SpritePolygonInfo* SpritePolygonCache::addSpritePolygonCache(const std::string& SpritePolygonInfo* info = new SpritePolygonInfo; if (nullptr != info) { - info->_rect = spritePolygonInfo._rect; + info->_rect = rect; - info->_triangles.verts = new V3F_C4B_T2F[spritePolygonInfo._triangles.vertCount]; - info->_triangles.indices = new unsigned short[spritePolygonInfo._triangles.indexCount]; - info->_triangles.vertCount = spritePolygonInfo._triangles.vertCount; - info->_triangles.indexCount = spritePolygonInfo._triangles.indexCount; + info->_triangles.verts = new V3F_C4B_T2F[trianglesCommand.vertCount]; + info->_triangles.indices = new unsigned short[trianglesCommand.indexCount]; + info->_triangles.vertCount = trianglesCommand.vertCount; + info->_triangles.indexCount = trianglesCommand.indexCount; - memcpy(info->_triangles.verts, spritePolygonInfo._triangles.verts, spritePolygonInfo._triangles.vertCount*sizeof(V3F_C4B_T2F)); - memcpy(info->_triangles.indices, spritePolygonInfo._triangles.indices, spritePolygonInfo._triangles.indexCount*sizeof(unsigned short)); + memcpy(info->_triangles.verts, trianglesCommand.verts, trianglesCommand.vertCount*sizeof(V3F_C4B_T2F)); + memcpy(info->_triangles.indices, trianglesCommand.indices, trianglesCommand.indexCount*sizeof(unsigned short)); // info->_textureRect = SpritePolygonInfo._textureRect; vecInfo.push_back(info); @@ -156,6 +163,7 @@ void SpritePolygonCache::removeSpritePolygonCache(const std::string& filePath, { if((*infoIter)->_rect.equals(*rect)) { + CC_SAFE_DELETE(*infoIter); it->second.erase(infoIter); break; } @@ -168,6 +176,10 @@ void SpritePolygonCache::removeAllSpritePolygonCache() { for (std::unordered_map::iterator it = _SpritePolygonCacheMap.begin(); it != _SpritePolygonCacheMap.end(); ++it) { + for (auto infoIter = it->second.begin(); infoIter != it->second.end(); infoIter++) + { + CC_SAFE_DELETE(*infoIter); + } it->second.clear(); } _SpritePolygonCacheMap.clear(); diff --git a/cocos/2d/SpritePolygonCache.h b/cocos/2d/SpritePolygonCache.h index a8463dd2b6..2199169787 100644 --- a/cocos/2d/SpritePolygonCache.h +++ b/cocos/2d/SpritePolygonCache.h @@ -44,12 +44,12 @@ typedef struct CC_DLL _SpritePolygonInfo { if(nullptr != _triangles.verts) { - CC_SAFE_DELETE(_triangles.verts); + CC_SAFE_DELETE_ARRAY(_triangles.verts); } if(nullptr != _triangles.indices) { - CC_SAFE_DELETE(_triangles.indices); + CC_SAFE_DELETE_ARRAY(_triangles.indices); } } } SpritePolygonInfo; @@ -63,7 +63,7 @@ public: virtual ~SpritePolygonCache(); static SpritePolygonCache* getInstance(); static void destroyInstance(); - SpritePolygonInfo* addSpritePolygonCache(const std::string& filePath, const SpritePolygonInfo& SpritePolygonInfo); + SpritePolygonInfo* addSpritePolygonCache(const std::string& filePath, const cocos2d::Rect& rect, const cocos2d::TrianglesCommand::Triangles trianglesCommand); SpritePolygonInfo* getSpritePolygonCache(const std::string& filePath, const cocos2d::Rect& rect); void removeSpritePolygonCache(const std::string& filePath, const cocos2d::Rect* rect = nullptr); void removeAllSpritePolygonCache(); From 815c6a9b741dc7f09b4b466523a419b07ff8a082 Mon Sep 17 00:00:00 2001 From: WuHao Date: Mon, 27 Apr 2015 16:33:54 +0800 Subject: [PATCH 2/3] merge the start and end points --- cocos/2d/MarchingSquare.cpp | 8 ++++++++ cocos/2d/MarchingSquare.h | 3 ++- cocos/2d/SpritePolygon.cpp | 9 +++++++-- cocos/2d/SpritePolygon.h | 5 ----- cocos/2d/SpritePolygonCache.cpp | 1 - 5 files changed, 17 insertions(+), 9 deletions(-) diff --git a/cocos/2d/MarchingSquare.cpp b/cocos/2d/MarchingSquare.cpp index 8d61094d4d..ba4a479d92 100644 --- a/cocos/2d/MarchingSquare.cpp +++ b/cocos/2d/MarchingSquare.cpp @@ -343,4 +343,12 @@ void MarchingSquare::optimize(float level) return; epsilon = level; points = rdp(points); + auto last = points.back(); + + if(last.y > points.front().y) + points.front().y = last.y; + points.pop_back(); + + //delete the last point, because its almost the same as the starting point +// CCLOG("%.1f, %.1f, %.1f, %.1f", points[0].x, points[0].y, points.back().x, points.back().y); } \ No newline at end of file diff --git a/cocos/2d/MarchingSquare.h b/cocos/2d/MarchingSquare.h index fc20d1d1f6..f2bf792038 100644 --- a/cocos/2d/MarchingSquare.h +++ b/cocos/2d/MarchingSquare.h @@ -49,7 +49,6 @@ public: void printPoints(); //using Ramer–Douglas–Peucker algorithm void optimize(float level = 0); - protected: unsigned int findFirstNoneTransparentPixel(); void marchSquare(int startx, int starty); @@ -70,6 +69,8 @@ protected: std::vector rdp(std::vector v); float perpendicularDistance(cocos2d::Vec2 i, cocos2d::Vec2 start, cocos2d::Vec2 end); float scaleFactor; + + }; NS_CC_END diff --git a/cocos/2d/SpritePolygon.cpp b/cocos/2d/SpritePolygon.cpp index eb382769e9..14d55373bb 100644 --- a/cocos/2d/SpritePolygon.cpp +++ b/cocos/2d/SpritePolygon.cpp @@ -249,7 +249,8 @@ bool SpritePolygon::initWithMarching(const std::string &file, const cocos2d::Rec auto marcher = new MarchingSquare(file); marcher->trace(); marcher->optimize(optimization); - +// marcher->test(); + auto p = marcher->getPoints(); auto _triangles = triangulate(p); delete marcher; @@ -259,6 +260,7 @@ bool SpritePolygon::initWithMarching(const std::string &file, const cocos2d::Rec info._rect = rect; info._triangles = _triangles; _polygonInfo = SpritePolygonCache::getInstance()->addSpritePolygonCache(file, info); +// test(); calculateUVandContentSize(); // SpritePolygonCache::printInfo(*_polygonInfo); #if CC_SPRITE_DEBUG_DRAW @@ -352,13 +354,13 @@ bool SpritePolygon::initWithRect(const std::string& filename, std::vectoraddSpritePolygonCache(filename, info); calculateUVandContentSize(); + #if CC_SPRITE_DEBUG_DRAW debugDraw(); #endif @@ -454,6 +456,9 @@ void SpritePolygon::debugDraw() _debugDrawNode = DrawNode::create(); addChild(_debugDrawNode); } + else{ + _debugDrawNode->clear(); + } //draw all points auto positions = new (std::nothrow) Vec2[_polygonInfo->_triangles.vertCount]; Vec2 *pos = &positions[0]; diff --git a/cocos/2d/SpritePolygon.h b/cocos/2d/SpritePolygon.h index 886fbb44e6..c17ff006fd 100644 --- a/cocos/2d/SpritePolygon.h +++ b/cocos/2d/SpritePolygon.h @@ -78,14 +78,9 @@ protected: void calculateUVandContentSize(); - bool _triangleDirty; //if triangle is dirty, then it needs to be recalculated from verts and indices -// std::vector _verts; -// std::vector _indices; cocos2d::TrianglesCommand _tcmd; cocos2d::BlendFunc _blendFunc; -// cocos2d::TrianglesCommand::Triangles _triangles; cocos2d::Texture2D *_texture; -// cocos2d::Rect _textureRect; SpritePolygonInfo *_polygonInfo; virtual void draw(cocos2d::Renderer *renderer, const cocos2d::Mat4 &transform, uint32_t flags); diff --git a/cocos/2d/SpritePolygonCache.cpp b/cocos/2d/SpritePolygonCache.cpp index 14eb66757f..05ddf2f0db 100644 --- a/cocos/2d/SpritePolygonCache.cpp +++ b/cocos/2d/SpritePolygonCache.cpp @@ -112,7 +112,6 @@ SpritePolygonInfo* SpritePolygonCache::addSpritePolygonCache(const std::string& memcpy(info->_triangles.verts, spritePolygonInfo._triangles.verts, spritePolygonInfo._triangles.vertCount*sizeof(V3F_C4B_T2F)); memcpy(info->_triangles.indices, spritePolygonInfo._triangles.indices, spritePolygonInfo._triangles.indexCount*sizeof(unsigned short)); -// info->_textureRect = SpritePolygonInfo._textureRect; vecInfo.push_back(info); _SpritePolygonCacheMap[filePath] = vecInfo; From a046f74c4d36a8a5c62e5f2d7182e645536e7fe5 Mon Sep 17 00:00:00 2001 From: WuHao Date: Mon, 27 Apr 2015 17:30:38 +0800 Subject: [PATCH 3/3] fix anchor point bug --- cocos/2d/SpritePolygon.cpp | 12 +++++++++++- .../Classes/SpritePolygonTest/SpritePolygonTest.cpp | 6 +++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/cocos/2d/SpritePolygon.cpp b/cocos/2d/SpritePolygon.cpp index 2800e8b992..3609156a2b 100644 --- a/cocos/2d/SpritePolygon.cpp +++ b/cocos/2d/SpritePolygon.cpp @@ -229,6 +229,16 @@ bool SpritePolygon::initWithCache(const std::string &file, SpritePolygonInfo *in CCASSERT(texture, "texture was not loaded properly"); _polygonInfo = info; initWithTexture(texture); + if(_polygonInfo->_rect.equals(Rect::ZERO)) + { + setContentSize(Size(texture->getPixelsWide(), texture->getPixelsHigh())/Director::getInstance()->getContentScaleFactor()); + } + else + { + setContentSize(_polygonInfo->_rect.size); + } + + setAnchorPoint(Vec2(0.5,0.5)); return true; } @@ -318,7 +328,7 @@ bool SpritePolygon::initWithVerts(const std::string& filename,std::vectorgetContentScaleFactor()); setAnchorPoint(Vec2(0.5,0.5)); _transformDirty = true; auto _triangles = TrianglesCommand::Triangles{&verts[0], &indices[0], (ssize_t)verts.size(), (ssize_t)indices.size()}; diff --git a/tests/cpp-tests/Classes/SpritePolygonTest/SpritePolygonTest.cpp b/tests/cpp-tests/Classes/SpritePolygonTest/SpritePolygonTest.cpp index cc0f06d7ab..2b7b3b2a73 100644 --- a/tests/cpp-tests/Classes/SpritePolygonTest/SpritePolygonTest.cpp +++ b/tests/cpp-tests/Classes/SpritePolygonTest/SpritePolygonTest.cpp @@ -94,7 +94,11 @@ SpritePolygonTest1::SpritePolygonTest1() SpritePolygonCache::getInstance()->removeAllSpritePolygonCache(); _title = "SpritePolygon Creation"; _subtitle = "SpritePolygon::create(\"Images/grossini.png\")"; - auto s = experimental::SpritePolygon::create(s_pathGrossini); + cocos2d::experimental::SpritePolygon *s; + for(int i = 0; i < 10; i ++) + { + s= experimental::SpritePolygon::create(s_pathGrossini); + } initDefaultSprite(s_pathGrossini, s); }