extend SpriteFrameCache to load PolygonInfo from plist file

This commit is contained in:
Joachim Grill 2015-09-29 15:42:04 +02:00
parent 4a2edc8ce8
commit ac24612e98
5 changed files with 133 additions and 1 deletions

View File

@ -1029,6 +1029,11 @@ void Sprite::setSpriteFrame(SpriteFrame *spriteFrame)
// update rect
_rectRotated = spriteFrame->isRotated();
setTextureRect(spriteFrame->getRect(), _rectRotated, spriteFrame->getOriginalSize());
if(spriteFrame->hasPolygonInfo())
{
_polyInfo = spriteFrame->getPolygonInfo();
}
}
void Sprite::setDisplayFrameWithAnimationName(const std::string& animationName, ssize_t frameIndex)

View File

@ -195,4 +195,19 @@ Texture2D* SpriteFrame::getTexture()
return nullptr;
}
void SpriteFrame::setPolygonInfo(const PolygonInfo &polygonInfo)
{
_polygonInfo = polygonInfo;
}
const PolygonInfo &SpriteFrame::getPolygonInfo() const
{
return _polygonInfo;
}
bool SpriteFrame::hasPolygonInfo() const
{
return _polygonInfo.triangles.vertCount != 0;
}
NS_CC_END

View File

@ -29,6 +29,7 @@ THE SOFTWARE.
#define __SPRITE_CCSPRITE_FRAME_H__
#include "2d/CCNode.h"
#include "2d/CCAutoPolygon.h"
#include "base/CCRef.h"
#include "math/CCGeometry.h"
@ -187,7 +188,25 @@ public:
// Overrides
virtual SpriteFrame *clone() const override;
/** Set the polygon info for polygon mesh sprites
*
* @param polygonInfo triangle mesh of teh sprite
*/
void setPolygonInfo(const PolygonInfo &polygonInfo);
/** Get the polygonInfo for this sprite
*
* @return polygonInfo structure
*/
const PolygonInfo &getPolygonInfo() const;
/** Check if sprite frame is a polygon sprite
*
* @return true if polygonInfo is available
*/
bool hasPolygonInfo() const;
CC_CONSTRUCTOR_ACCESS:
/**
* @lua NA
@ -231,6 +250,7 @@ protected:
Size _originalSizeInPixels;
Texture2D *_texture;
std::string _textureFilename;
PolygonInfo _polygonInfo;
};
// end of _2d group

View File

@ -33,6 +33,7 @@ THE SOFTWARE.
#include "2d/CCSprite.h"
#include "2d/CCAutoPolygon.h"
#include "platform/CCFileUtils.h"
#include "base/CCNS.h"
#include "base/ccMacros.h"
@ -79,6 +80,63 @@ SpriteFrameCache::~SpriteFrameCache()
CC_SAFE_DELETE(_loadedFileNames);
}
void SpriteFrameCache::parseIntegerList(const std::string string, std::vector<int> &res)
{
std::string delim(" ");
size_t n = std::count(string.begin(), string.end(), ' ');
res.resize(n+1);
size_t start = 0U;
size_t end = string.find(delim);
int i=0;
while (end != std::string::npos)
{
res[i++] = std::stoi(string.substr(start, end - start));
start = end + delim.length();
end = string.find(delim, start);
}
res[i] = std::stoi(string.substr(start, end));
}
void SpriteFrameCache::initializePolygonInfo(const Size &textureSize,
const Size &spriteSize,
const std::vector<int> &vertices,
const std::vector<int> &verticesUV,
const std::vector<int> &triangleIndices,
PolygonInfo &info)
{
size_t vertexCount = vertices.size();
size_t indexCount = triangleIndices.size();
float scaleFactor = CC_CONTENT_SCALE_FACTOR();
V3F_C4B_T2F *vertexData = new V3F_C4B_T2F[vertexCount];
for (size_t i = 0; i < vertexCount/2; i++)
{
vertexData[i].colors = Color4B::WHITE;
vertexData[i].vertices = Vec3(vertices[i*2] / scaleFactor,
(spriteSize.height - vertices[i*2+1]) / scaleFactor,
0);
vertexData[i].texCoords = Tex2F(verticesUV[i*2] / textureSize.width,
verticesUV[i*2+1] / textureSize.height);
}
unsigned short *indexData = new unsigned short[indexCount];
for (size_t i = 0; i < indexCount; i++)
{
indexData[i] = static_cast<unsigned short>(triangleIndices[i]);
}
info.triangles.vertCount = vertexCount;
info.triangles.verts = vertexData;
info.triangles.indexCount = indexCount;
info.triangles.indices = indexData;
info.rect = Rect(0, 0, spriteSize.width, spriteSize.height);
}
void SpriteFrameCache::addSpriteFramesWithDictionary(ValueMap& dictionary, Texture2D* texture)
{
/*
@ -88,17 +146,26 @@ void SpriteFrameCache::addSpriteFramesWithDictionary(ValueMap& dictionary, Textu
ZWTCoordinatesFormatOptionXML1_0 = 1, // Desktop Version 0.0 - 0.4b
ZWTCoordinatesFormatOptionXML1_1 = 2, // Desktop Version 1.0.0 - 1.0.1
ZWTCoordinatesFormatOptionXML1_2 = 3, // Desktop Version 1.0.2+
Version 3 with TexturePacker 4.0 polygon mesh packing
*/
ValueMap& framesDict = dictionary["frames"].asValueMap();
int format = 0;
Size textureSize;
// get the format
if (dictionary.find("metadata") != dictionary.end())
{
ValueMap& metadataDict = dictionary["metadata"].asValueMap();
format = metadataDict["format"].asInt();
if(metadataDict.find("size") != metadataDict.end())
{
textureSize = SizeFromString(metadataDict["size"].asString());
}
}
// check the format
@ -194,6 +261,20 @@ void SpriteFrameCache::addSpriteFramesWithDictionary(ValueMap& dictionary, Textu
textureRotated,
spriteOffset,
spriteSourceSize);
if(frameDict.find("vertices") != frameDict.end())
{
std::vector<int> vertices;
parseIntegerList(frameDict["vertices"].asString(), vertices);
std::vector<int> verticesUV;
parseIntegerList(frameDict["verticesUV"].asString(), verticesUV);
std::vector<int> indices;
parseIntegerList(frameDict["triangles"].asString(), indices);
PolygonInfo info;
initializePolygonInfo(textureSize, spriteSourceSize, vertices, verticesUV, indices, info);
spriteFrame->setPolygonInfo(info);
}
}
bool flag = NinePatchImageParser::isNinePatchImage(spriteFrameName);

View File

@ -45,6 +45,7 @@ NS_CC_BEGIN
class Sprite;
class Texture2D;
class PolygonInfo;
/**
* @addtogroup _2d
@ -223,6 +224,16 @@ protected:
*/
void removeSpriteFramesFromDictionary(ValueMap& dictionary);
/** Parses list of space-separated integers */
void parseIntegerList(const std::string string, std::vector<int> &res);
/** Configures PolygonInfo class with the passed sizes + triangles */
void initializePolygonInfo(const Size &textureSize,
const Size &spriteSize,
const std::vector<int> &vertices,
const std::vector<int> &verticesUV,
const std::vector<int> &triangleIndices,
PolygonInfo &polygonInfo);
Map<std::string, SpriteFrame*> _spriteFrames;
ValueMap _spriteFramesAliases;