mirror of https://github.com/axmolengine/axmol.git
Merge branch 'v3.6' of https://github.com/cocos2d/cocos2d-x into v3.6_test1
This commit is contained in:
commit
dc765aee25
|
@ -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);
|
||||
}
|
|
@ -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<cocos2d::Vec2> rdp(std::vector<cocos2d::Vec2> v);
|
||||
float perpendicularDistance(cocos2d::Vec2 i, cocos2d::Vec2 start, cocos2d::Vec2 end);
|
||||
float scaleFactor;
|
||||
|
||||
|
||||
};
|
||||
|
||||
NS_CC_END
|
||||
|
|
|
@ -222,19 +222,24 @@ TrianglesCommand::Triangles SpritePolygon::triangulate(std::vector<cocos2d::Vec2
|
|||
delete cdt;
|
||||
return TrianglesCommand::Triangles{&(*_verts)[0], &(*_indices)[0], (ssize_t)_verts->size(), (ssize_t)_indices->size()};
|
||||
}
|
||||
bool SpritePolygon::initWithCache(const std::string &file, const SpritePolygonInfo *info)
|
||||
bool SpritePolygon::initWithCache(const std::string &file, SpritePolygonInfo *info)
|
||||
{
|
||||
CCASSERT(file.size()>0, "Invalid filename for sprite");
|
||||
Texture2D *texture = Director::getInstance()->getTextureCache()->addImage(file);
|
||||
if (texture)
|
||||
CCASSERT(texture, "texture was not loaded properly");
|
||||
_polygonInfo = info;
|
||||
initWithTexture(texture);
|
||||
if(_polygonInfo->_rect.equals(Rect::ZERO))
|
||||
{
|
||||
initWithTexture(texture);
|
||||
setContentSize(Size(texture->getPixelsWide(), texture->getPixelsHigh())/Director::getInstance()->getContentScaleFactor());
|
||||
}
|
||||
else{
|
||||
throw "some error";
|
||||
else
|
||||
{
|
||||
setContentSize(_polygonInfo->_rect.size);
|
||||
}
|
||||
// _textureRect = info->_textureRect;
|
||||
// _triangles = info->_triangles;
|
||||
|
||||
|
||||
setAnchorPoint(Vec2(0.5,0.5));
|
||||
return true;
|
||||
}
|
||||
bool SpritePolygon::initWithMarching(const std::string &file, const cocos2d::Rect &rect, float optimization)
|
||||
|
@ -249,17 +254,17 @@ 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);
|
||||
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);
|
||||
setAnchorPoint(Vec2(0.5,0.5));
|
||||
calculateUVandContentSize();
|
||||
|
||||
// SpritePolygonCache::printInfo(*_polygonInfo);
|
||||
#if CC_SPRITE_DEBUG_DRAW
|
||||
debugDraw();
|
||||
|
@ -270,15 +275,10 @@ bool SpritePolygon::initWithPoly2tri(const std::string &filename, std::vector<co
|
|||
{
|
||||
CCASSERT(filename.size()>0, "Invalid filename for sprite");
|
||||
Texture2D *texture = Director::getInstance()->getTextureCache()->addImage(filename);
|
||||
if (texture)
|
||||
{
|
||||
initWithTexture(texture);
|
||||
}
|
||||
else{
|
||||
throw "some error";
|
||||
}
|
||||
SpritePolygonInfo info = SpritePolygonInfo{Rect, triangulate(verts)};
|
||||
_polygonInfo = SpritePolygonCache::getInstance()->addSpritePolygonCache(filename, info);
|
||||
CCASSERT(texture, "texture was not loaded properly");
|
||||
initWithTexture(texture);
|
||||
_polygonInfo = SpritePolygonCache::getInstance()->addSpritePolygonCache(filename, Rect, triangulate(verts));
|
||||
setAnchorPoint(Vec2(0.5,0.5));
|
||||
calculateUVandContentSize();
|
||||
#if CC_SPRITE_DEBUG_DRAW
|
||||
debugDraw();
|
||||
|
@ -328,11 +328,12 @@ bool SpritePolygon::initWithVerts(const std::string& filename,std::vector<cocos2
|
|||
|
||||
initWithTexture(texture);
|
||||
auto _textureRect = getTextRectFromTriangles(verts);
|
||||
setContentSize(_textureRect.size);
|
||||
setContentSize(_textureRect.size/Director::getInstance()->getContentScaleFactor());
|
||||
setAnchorPoint(Vec2(0.5,0.5));
|
||||
_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
|
||||
|
@ -352,13 +353,12 @@ bool SpritePolygon::initWithRect(const std::string& filename, std::vector<cocos2
|
|||
auto c4b = Color4B::WHITE;
|
||||
auto t2f = Tex2F(0,0);
|
||||
_verts.push_back(V3F_C4B_T2F{v3,c4b,t2f});
|
||||
// _indices.push_back(idx);
|
||||
}
|
||||
|
||||
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();
|
||||
setAnchorPoint(Vec2(0.5,0.5));
|
||||
#if CC_SPRITE_DEBUG_DRAW
|
||||
debugDraw();
|
||||
#endif
|
||||
|
@ -370,24 +370,14 @@ bool SpritePolygon::initWithTexture(Texture2D *texture)
|
|||
Node::init();
|
||||
//init the triangles command
|
||||
setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR_NO_MVP));
|
||||
|
||||
_blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
|
||||
|
||||
setTexture(texture);
|
||||
// _textureRect = (rect.equals(Rect::ZERO))? Rect(0,0,texture->getPixelsWide(), texture->getPixelsHigh()) : rect;
|
||||
setAnchorPoint(Vec2(0.5,0.5));
|
||||
|
||||
return true;
|
||||
}
|
||||
void SpritePolygon::setTexture(const std::string &filename)
|
||||
{
|
||||
Texture2D *texture = Director::getInstance()->getTextureCache()->addImage(filename);
|
||||
setTexture(texture);
|
||||
|
||||
// Rect rect = Rect::ZERO;
|
||||
// if (texture)
|
||||
// rect.size = texture->getContentSize();
|
||||
// setTextureRect(rect);
|
||||
}
|
||||
static unsigned char cc_2x2_white_image[] = {
|
||||
// RGBA8888
|
||||
|
@ -425,7 +415,6 @@ void SpritePolygon::setTexture(Texture2D *texture)
|
|||
CC_SAFE_RETAIN(texture);
|
||||
CC_SAFE_RELEASE(_texture);
|
||||
_texture = texture;
|
||||
// updateBlendFunc();
|
||||
}
|
||||
}
|
||||
const float SpritePolygon::getArea(){
|
||||
|
@ -454,6 +443,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];
|
||||
|
|
|
@ -58,7 +58,7 @@ public:
|
|||
//not recommended for production, its better to use the vec2 list for better performance
|
||||
static SpritePolygon *create(const std::string&, const cocos2d::Rect& rect = cocos2d::Rect::ZERO, float optimization = -1);
|
||||
bool initWithMarching(const std::string &file, const cocos2d::Rect &rect, float optimization);
|
||||
bool initWithCache(const std::string &file, const SpritePolygonInfo *info);
|
||||
bool initWithCache(const std::string &file, SpritePolygonInfo *info);
|
||||
|
||||
bool initWithTexture(cocos2d::Texture2D *texture);
|
||||
|
||||
|
@ -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<cocos2d::V3F_C4B_T2F> _verts;
|
||||
// std::vector<unsigned short> _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);
|
||||
|
||||
|
|
|
@ -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,16 +110,16 @@ 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));
|
||||
// info->_textureRect = SpritePolygonInfo._textureRect;
|
||||
|
||||
memcpy(info->_triangles.verts, trianglesCommand.verts, trianglesCommand.vertCount*sizeof(V3F_C4B_T2F));
|
||||
memcpy(info->_triangles.indices, trianglesCommand.indices, trianglesCommand.indexCount*sizeof(unsigned short));
|
||||
|
||||
vecInfo.push_back(info);
|
||||
_SpritePolygonCacheMap[filePath] = vecInfo;
|
||||
|
@ -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();
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue