This commit is contained in:
samuele3hu 2015-04-27 15:19:26 +08:00
Родитель c7e990dcae
Коммит 3de665b9cd
3 изменённых файлов: 33 добавлений и 27 удалений

Просмотреть файл

@ -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::vector<co
else{
throw "some error";
}
SpritePolygonInfo info = SpritePolygonInfo{Rect, triangulate(verts)};
_polygonInfo = SpritePolygonCache::getInstance()->addSpritePolygonCache(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::vector<cocos2
setContentSize(_textureRect.size);
_transformDirty = true;
auto _triangles = TrianglesCommand::Triangles{&verts[0], &indices[0], (ssize_t)verts.size(), (ssize_t)indices.size()};
SpritePolygonInfo info = SpritePolygonInfo{_textureRect, _triangles};
_polygonInfo = SpritePolygonCache::getInstance()->addSpritePolygonCache(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::vector<cocos2
}
auto _triangles = TrianglesCommand::Triangles{&_verts[0], &indices[0], (ssize_t)_verts.size(), (ssize_t)indices.size()};
SpritePolygonInfo info = SpritePolygonInfo{rect, _triangles};
_polygonInfo = SpritePolygonCache::getInstance()->addSpritePolygonCache(filename, info);
_polygonInfo = SpritePolygonCache::getInstance()->addSpritePolygonCache(filename, rect, _triangles);
calculateUVandContentSize();
#if CC_SPRITE_DEBUG_DRAW
debugDraw();

Просмотреть файл

@ -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<std::string, VecSpritePolygonInfo>::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();

Просмотреть файл

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