axmol/cocos/2d/CCSpritePolygonCache.cpp

253 lines
8.4 KiB
C++

/****************************************************************************
Copyright (c) 2008-2010 Ricardo Quesada
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2011 Zynga Inc.
Copyright (c) 2013-2014 Chukong Technologies Inc.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "CCSpritePolygonCache.h"
#include "platform/CCFileUtils.h"
#include "base/CCDirector.h"
#include "renderer/CCTextureCache.h"
#include "poly2tri/poly2tri.h"
USING_NS_CC;
SpritePolygonCache* SpritePolygonCache::s_spritePolygonCache = nullptr;
SpritePolygonCache::SpritePolygonCache()
{
}
SpritePolygonCache::~SpritePolygonCache()
{
removeAllSpritePolygonCache();
}
SpritePolygonCache* SpritePolygonCache::getInstance()
{
if (nullptr == s_spritePolygonCache)
{
s_spritePolygonCache = new (std::nothrow) SpritePolygonCache();
s_spritePolygonCache->init();
}
return s_spritePolygonCache;
}
void SpritePolygonCache::destroyInstance()
{
CC_SAFE_DELETE(s_spritePolygonCache);
}
void SpritePolygonCache::init()
{
_spritePolygonCacheMap.reserve(20);
}
PolygonInfo* SpritePolygonCache::addSpritePolygonCache(const std::string& filePath, const cocos2d::Rect& rect, const cocos2d::TrianglesCommand::Triangles& triangles)
{
auto fullpath = FileUtils::getInstance()->fullPathForFilename(filePath);;
auto it = _spritePolygonCacheMap.find(fullpath);
if (_spritePolygonCacheMap.end() != it)
{
VecSpritePolygonInfo vecInfo = it->second;
auto infoIt = vecInfo.begin();
for (; infoIt != vecInfo.end(); infoIt++)
{
// if ((*infoIt)->rect.equals(rect))
// {
// CC_SAFE_DELETE_ARRAY((*infoIt)->triangles.verts);
// CC_SAFE_DELETE_ARRAY((*infoIt)->triangles.indices);
// (*infoIt)->triangles.verts = new V3F_C4B_T2F[triangles.vertCount];
// (*infoIt)->triangles.indices = new unsigned short[triangles.indexCount];
// (*infoIt)->triangles.vertCount = triangles.vertCount;
// (*infoIt)->triangles.indexCount = triangles.indexCount;
// memcpy((*infoIt)->triangles.verts, triangles.verts, triangles.vertCount*sizeof(V3F_C4B_T2F));
// memcpy((*infoIt)->triangles.indices, triangles.indices, triangles.indexCount*sizeof(unsigned short));
// return *infoIt;
// }
}
}
VecSpritePolygonInfo vecInfo;
vecInfo.clear();
if (it != _spritePolygonCacheMap.end())
{
vecInfo = it->second;
}
PolygonInfo* info = new (std::nothrow) PolygonInfo();
if (nullptr != info)
{
// info->rect = rect;
// info->triangles.verts = new V3F_C4B_T2F[triangles.vertCount];
// info->triangles.indices = new unsigned short[triangles.indexCount];
// info->triangles.vertCount = triangles.vertCount;
// info->triangles.indexCount = triangles.indexCount;
// memcpy(info->triangles.verts, triangles.verts, triangles.vertCount*sizeof(V3F_C4B_T2F));
// memcpy(info->triangles.indices, triangles.indices, triangles.indexCount*sizeof(unsigned short));
//
// vecInfo.push_back(info);
// _spritePolygonCacheMap[filePath] = vecInfo;
}
return info;
}
PolygonInfo* SpritePolygonCache::getSpritePolygonCache(const std::string& filePath, const cocos2d::Rect& rect, float optimization)
{
PolygonInfo* spritePolygonInfo = nullptr;
bool isFound = false;
auto fullpath = FileUtils::getInstance()->fullPathForFilename(filePath);
if(fullpath.size() == 0)
{
return nullptr;
}
auto it = _spritePolygonCacheMap.find(fullpath);
if (_spritePolygonCacheMap.end() != it)
{
auto infoIter = it->second.begin();
for (; infoIter != it->second.end(); infoIter++)
{
// if ((*infoIter)->rect.equals(rect))
// {
// spritePolygonInfo = *infoIter;
// isFound = true;
// }
}
}
// if(!isFound)
// {
// //we need 5 steps to get cocos2d::TrianglesCommand::Triangles for cache.
// //1.trace 2.optimize 3.expand 4.triangulate 5.calculateuv
// if(-1 == optimization) optimization = 1.169;
// AutoPolygon autopolygon(fullpath);
// autopolygon.generateTriangles(rect, optimization);
// spritePolygonInfo = addSpritePolygonCache(fullpath, rect, autopolygon.getTriangles());
// }
return spritePolygonInfo;
}
void SpritePolygonCache::removeSpritePolygonCache(const std::string& filePath, const cocos2d::Rect* rect)
{
std::string fullpath = FileUtils::getInstance()->fullPathForFilename(filePath);
if (fullpath.size() == 0)
return;
auto it = _spritePolygonCacheMap.find(fullpath);
if (_spritePolygonCacheMap.end() == it)
return;
if (nullptr == rect)
return;
auto infoIter = it->second.begin();
for (; infoIter != it->second.end(); infoIter++)
{
// if((*infoIter)->rect.equals(*rect))
// {
// CC_SAFE_RELEASE(*infoIter);
// it->second.erase(infoIter);
// break;
// }
}
return;
}
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_RELEASE(*infoIter);
// }
// it->second.clear();
}
_spritePolygonCacheMap.clear();
}
void SpritePolygonCache::removeUnusedSpritePolygonCache()
{
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++)
{
// if(1==(*infoIter)->getReferenceCount())
// {
// CC_SAFE_RELEASE(*infoIter);
// it->second.erase(infoIter);
// }
}
}
}
bool SpritePolygonCache::isSpritePolygonCacheExist(const std::string& filePath, const cocos2d::Rect& rect)
{
std::string fullpath = FileUtils::getInstance()->fullPathForFilename(filePath);
if (fullpath.size() == 0)
return false;
auto it = _spritePolygonCacheMap.find(fullpath);
if (_spritePolygonCacheMap.end() == it)
return false;
auto infoIter = it->second.begin();
for (; infoIter != it->second.end(); infoIter++)
{
// if ((*infoIter)->rect.equals(rect))
return true;
}
return false;
}
void SpritePolygonCache::printInfo(PolygonInfo &info){
CCLOG("========================");
CCLOG("%zd, %zd", info.triangles.vertCount, info.triangles.indexCount);
auto vertEnd = &info.triangles.verts[info.triangles.vertCount];
for(auto v = info.triangles.verts; v < vertEnd; v++)
{
CCLOG("%f, %f", v->vertices.x, v->vertices.y);
}
auto indEnd = &info.triangles.indices[info.triangles.indexCount];
for(auto i = info.triangles.indices; i < indEnd; i+=3)
{
CCLOG("%d, %d, %d,", *i,*(i+1), *(i+2));
}
auto uvEnd = &info.triangles.verts[info.triangles.vertCount];
for(auto v = info.triangles.verts; v < uvEnd; v++)
{
CCLOG("%f, %f", v->texCoords.u, v->texCoords.v);
}
}
USING_NS_CC;