From 3de665b9cdb3ba02936c993eeadc41e1e4f8e29b Mon Sep 17 00:00:00 2001 From: samuele3hu Date: Mon, 27 Apr 2015 15:19:26 +0800 Subject: [PATCH] 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();