2015-04-23 00:51:47 +08:00
|
|
|
/****************************************************************************
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-04-23 14:34:56 +08:00
|
|
|
#ifndef COCOS_2D_SpritePolygon_H__
|
|
|
|
#define COCOS_2D_SpritePolygon_H__
|
2015-04-23 00:51:47 +08:00
|
|
|
|
2015-05-18 15:10:41 +08:00
|
|
|
#include "CCNode.h"
|
2015-04-23 00:51:47 +08:00
|
|
|
#include "CCDrawNode.h"
|
2015-04-23 14:34:56 +08:00
|
|
|
#include "SpritePolygonCache.h"
|
2015-04-23 00:51:47 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
namespace experimental{
|
|
|
|
|
2015-04-23 14:34:56 +08:00
|
|
|
class CC_DLL SpritePolygon : public cocos2d::Node
|
2015-04-23 00:51:47 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
//create with manually created vertices, color and uv and
|
2015-05-15 17:18:19 +08:00
|
|
|
static SpritePolygon *create(const std::string& file, std::vector<cocos2d::V3F_C4B_T2F>& verts, std::vector<unsigned short>& indices);
|
|
|
|
bool initWithVerts(const std::string& file, std::vector<cocos2d::V3F_C4B_T2F>& verts, std::vector<unsigned short>& indices);
|
2015-04-23 00:51:47 +08:00
|
|
|
|
|
|
|
//create from list of vertices, with texture rect, and triangle indices, UV is calculated from the texture rect
|
2015-05-15 17:18:19 +08:00
|
|
|
static SpritePolygon *create(const std::string& file, std::vector<cocos2d::Vec2>& verts, std::vector<unsigned short>& indices, const cocos2d::Rect&rect = cocos2d::Rect::ZERO);
|
|
|
|
bool initWithRect(const std::string& file, std::vector<cocos2d::Vec2>& verts, std::vector<unsigned short>& indices, const cocos2d::Rect&rect = cocos2d::Rect::ZERO);
|
2015-04-23 00:51:47 +08:00
|
|
|
|
2015-05-15 17:18:19 +08:00
|
|
|
//create from ordered list of vertices, with texture rect, Triangles and UV is calculated on the fly
|
|
|
|
static SpritePolygon *create(const std::string& file, std::vector<cocos2d::Vec2>& verts, const cocos2d::Rect& rect = cocos2d::Rect::ZERO);
|
|
|
|
bool initWithPoly2tri(const std::string& file, std::vector<cocos2d::Vec2>& verts, const cocos2d::Rect& rect);
|
2015-04-23 00:51:47 +08:00
|
|
|
|
|
|
|
//create from a texture (rect), and automatically trace and optimize the points.
|
|
|
|
//not recommended for production, its better to use the vec2 list for better performance
|
2015-05-15 17:18:19 +08:00
|
|
|
static SpritePolygon *create(const std::string& file, const cocos2d::Rect& rect = cocos2d::Rect::ZERO, float optimization = -1);
|
2015-05-18 15:10:41 +08:00
|
|
|
bool initWithMarching(const std::string &file, const cocos2d::Rect &rect, float optimization = -1);
|
2015-04-27 17:18:26 +08:00
|
|
|
bool initWithCache(const std::string &file, SpritePolygonInfo *info);
|
2015-04-23 00:51:47 +08:00
|
|
|
|
2015-04-27 13:34:47 +08:00
|
|
|
bool initWithTexture(cocos2d::Texture2D *texture);
|
2015-04-23 00:51:47 +08:00
|
|
|
|
2015-05-15 17:18:19 +08:00
|
|
|
virtual void setTexture(const std::string &file );
|
2015-04-23 00:51:47 +08:00
|
|
|
virtual void setTexture(cocos2d::Texture2D *texture);
|
|
|
|
|
|
|
|
/** returns the Texture2D object used by the sprite */
|
|
|
|
virtual cocos2d::Texture2D* getTexture(){return _texture;};
|
|
|
|
const float getArea();
|
2015-04-24 17:39:43 +08:00
|
|
|
const ssize_t getTrianglesCount(){return _polygonInfo->_triangles.indexCount/3;};
|
|
|
|
const ssize_t getVertCount(){return _polygonInfo->_triangles.vertCount;};
|
2015-04-23 18:03:03 +08:00
|
|
|
|
|
|
|
void showDebug(const bool val);
|
2015-04-23 00:51:47 +08:00
|
|
|
protected:
|
2015-04-28 20:10:56 +08:00
|
|
|
SpritePolygon()
|
|
|
|
: _debugDrawNode(nullptr)
|
|
|
|
, _texture(nullptr)
|
|
|
|
, _polygonInfo(nullptr)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
2015-05-18 15:52:46 +08:00
|
|
|
~SpritePolygon();
|
2015-04-28 20:10:56 +08:00
|
|
|
|
2015-04-24 17:39:43 +08:00
|
|
|
cocos2d::DrawNode *_debugDrawNode;
|
|
|
|
void debugDraw();
|
2015-04-24 18:17:12 +08:00
|
|
|
void calculateUVandContentSize();
|
2015-04-23 00:51:47 +08:00
|
|
|
virtual void draw(cocos2d::Renderer *renderer, const cocos2d::Mat4 &transform, uint32_t flags);
|
2015-04-27 13:34:47 +08:00
|
|
|
cocos2d::Rect getTextRectFromTriangles(std::vector<cocos2d::V3F_C4B_T2F>& verts);
|
2015-05-15 17:18:19 +08:00
|
|
|
void triangulate(const std::string& file, const cocos2d::Rect& rect, std::vector<cocos2d::Vec2> & verts);
|
2015-04-23 14:34:56 +08:00
|
|
|
|
2015-05-15 17:18:19 +08:00
|
|
|
cocos2d::TrianglesCommand _tcmd;
|
|
|
|
cocos2d::BlendFunc _blendFunc;
|
|
|
|
cocos2d::Texture2D *_texture;
|
|
|
|
SpritePolygonInfo *_polygonInfo;
|
2015-04-23 00:51:47 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
|
2015-04-23 14:34:56 +08:00
|
|
|
#endif // #ifndef COCOS_2D_SpritePolygon_H__
|