axmol/cocos/2d/SpritePolygon.h

105 lines
5.0 KiB
C
Raw Normal View History

/****************************************************************************
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__
#include <vector>
#include "platform/CCPlatformMacros.h"
#include "2d/CCNode.h"
#include "renderer/CCTrianglesCommand.h"
#include "CCDrawNode.h"
2015-04-23 14:34:56 +08:00
#include "SpritePolygonCache.h"
NS_CC_BEGIN
namespace experimental{
2015-04-23 14:34:56 +08:00
class CC_DLL SpritePolygon : public cocos2d::Node
{
public:
//create with manually created vertices, color and uv and
2015-04-23 14:34:56 +08:00
static SpritePolygon *create(const std::string&, std::vector<cocos2d::V3F_C4B_T2F>&, std::vector<unsigned short>&);
bool initWithVerts(const std::string&, std::vector<cocos2d::V3F_C4B_T2F>&, std::vector<unsigned short>&);
//create from list of vertices, with texture rect, and triangle indices, UV is calculated from the texture rect
2015-04-23 14:34:56 +08:00
static SpritePolygon *create(const std::string&, std::vector<cocos2d::Vec2>&, std::vector<unsigned short>&, const cocos2d::Rect& rect = cocos2d::Rect::ZERO, bool rotated = false);
bool initWithRect(const std::string&, std::vector<cocos2d::Vec2>&, std::vector<unsigned short>&, const cocos2d::Rect&, bool);
// //create from ordered list of vertices, with texture rect, and rotated property, Triangles and UV is calculated on the fly
2015-04-23 14:34:56 +08:00
static SpritePolygon *create(const std::string&, std::vector<cocos2d::Vec2>&, const cocos2d::Rect& rect = cocos2d::Rect::ZERO, bool rotated = false);
bool initWithPoly2tri(const std::string&, std::vector<cocos2d::Vec2>&, const cocos2d::Rect&, bool);
//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-04-23 14:34:56 +08:00
static SpritePolygon *create(const std::string&, const cocos2d::Rect& rect = cocos2d::Rect::ZERO, unsigned short optimization = 1);
bool initWithMarching(const std::string &file, const cocos2d::Rect &rect, unsigned short optimization);
2015-04-23 14:34:56 +08:00
bool initWithCache(const std::string &file, const SpritePolygonInfo *info);
2015-04-23 14:34:56 +08:00
bool initWithTexture(cocos2d::Texture2D *texture, const cocos2d::Rect &rect = cocos2d::Rect::ZERO);
virtual void setTexture(const std::string &filename );
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-23 14:34:56 +08:00
const ssize_t getTrianglesCount(){return _polygonInfo->_triangles.vertCount/3;};
protected:
#if CC_SPRITE_DEBUG_DRAW
cocos2d::DrawNode *_debugDrawNode;
void debugDraw();
#endif //CC_SPRITE_DEBUG_DRAW
bool _triangleDirty; //if triangle is dirty, then it needs to be recalculated from verts and indices
bool _rectRotated;
2015-04-23 14:34:56 +08:00
// std::vector<cocos2d::V3F_C4B_T2F> _verts;
// std::vector<unsigned short> _indices;
cocos2d::TrianglesCommand _tcmd;
cocos2d::BlendFunc _blendFunc;
2015-04-23 14:34:56 +08:00
// cocos2d::TrianglesCommand::Triangles _triangles;
cocos2d::Texture2D *_texture;
2015-04-23 14:34:56 +08:00
// cocos2d::Rect _textureRect;
SpritePolygonInfo *_polygonInfo;
virtual void draw(cocos2d::Renderer *renderer, const cocos2d::Mat4 &transform, uint32_t flags);
2015-04-23 14:34:56 +08:00
cocos2d::Rect getTextRectFromTriangles();
//======Triangulation
2015-04-23 14:34:56 +08:00
cocos2d::TrianglesCommand::Triangles triangulate(std::vector<cocos2d::Vec2> & verts);
cocos2d::Rect setContentSizeFromVecs(std::vector<cocos2d::Vec2>& verts);
//TODO: move those to ccUtils
};
}
NS_CC_END
2015-04-23 14:34:56 +08:00
#endif // #ifndef COCOS_2D_SpritePolygon_H__