Cornell Cocos
Cornell Extensions to Cocos2d
Public Member Functions | Static Public Member Functions | Public Attributes | Protected Member Functions | List of all members
PolygonNode Class Reference

#include <CUPolygonNode.h>

Inheritance diagram for PolygonNode:
TexturedNode AnimationNode

Public Member Functions

virtual std::string getDescription () const override
 
virtual void draw (Renderer *renderer, const Mat4 &transform, uint32_t flags) override
 
virtual ~PolygonNode (void)
 
- Public Member Functions inherited from TexturedNode
virtual void setTexture (const std::string &filename)
 
virtual void setTexture (Texture2D *texture) override
 
virtual Texture2D * getTexture () const override
 
virtual void setPolygon (float *vertices, int size, int offset=0)
 
virtual void setPolygon (const Poly2 &poly)
 
virtual void setPolygon (const Rect &rect)
 
const Poly2getPolygon () const
 
virtual void shiftPolygon (float dx, float dy)
 
const Rect & getBoundingRect () const
 
void setBlendFunc (const BlendFunc &blendFunc) override
 
const BlendFunc & getBlendFunc () const override
 
virtual void setOpacityModifyRGB (bool modify) override
 
virtual bool isOpacityModifyRGB (void) const override
 
void flipHorizontal (bool flag)
 
bool isFlipHorizontal () const
 
void flipVertical (bool flag)
 
bool isFlipVertical () const
 
virtual ~TexturedNode (void)
 
virtual bool init () override
 
virtual bool init (float *vertices, int size, int offset=0)
 
virtual bool init (const Poly2 &poly)
 
virtual bool init (const Rect &rect)
 
virtual bool initWithFile (const std::string &filename)
 
virtual bool initWithFile (const std::string &filename, float *vertices, int size, int offset=0)
 
virtual bool initWithFile (const std::string &filename, const Poly2 &poly)
 
virtual bool initWithFile (const std::string &filename, const Rect &rect)
 
virtual bool initWithTexture (Texture2D *texture)
 
virtual bool initWithTexture (Texture2D *texture, float *vertices, int size, int offset=0)
 
virtual bool initWithTexture (Texture2D *texture, const Poly2 &poly)
 
virtual bool initWithTexture (Texture2D *texture, const Rect &rect)
 

Static Public Member Functions

static PolygonNodecreate ()
 
static PolygonNodecreate (float *vertices, int size, int offset=0)
 
static PolygonNodecreate (const Poly2 &poly)
 
static PolygonNodecreate (const Rect &rect)
 
static PolygonNodecreateWithFile (const std::string &filename)
 
static PolygonNodecreateWithFile (const std::string &filename, float *vertices, int size, int offset=0)
 
static PolygonNodecreateWithFile (const std::string &filename, const Poly2 &poly)
 
static PolygonNodecreateWithFile (const std::string &filename, const Rect &rect)
 
static PolygonNodecreateWithTexture (Texture2D *texture)
 
static PolygonNodecreateWithTexture (Texture2D *texture, float *vertices, int size, int offset=0)
 
static PolygonNodecreateWithTexture (Texture2D *texture, const Poly2 &poly)
 
static PolygonNodecreateWithTexture (Texture2D *texture, const Rect &rect)
 

Public Attributes

CC_CONSTRUCTOR_ACCESS __pad0__: PolygonNode(void)
 
- Public Attributes inherited from TexturedNode
CC_CONSTRUCTOR_ACCESS __pad0__: TexturedNode(void)
 

Protected Member Functions

virtual void generateRenderData () override
 
- Protected Member Functions inherited from TexturedNode
TrianglesCommand::Triangles & allocTriangles (const Poly2 &poly)
 
void clearRenderData ()
 
void updateColor () override
 
void updateBlendFunc ()
 
void updateTextureCoords ()
 

Additional Inherited Members

- Protected Attributes inherited from TexturedNode
Texture2D * _texture
 
Poly2 _polygon
 
BlendFunc _blendFunc
 
bool _insideBounds
 
bool _opacityModifyRGB
 
TrianglesCommand _command
 
TrianglesCommand::Triangles _triangles
 
bool _flipHorizontal
 
bool _flipVertical
 

Detailed Description

Scene graph node represent a solid 2D polygon textured by a sprite.

This class uses the Cocos2D rendering pipeline to batch graphics information into a single mesh (with one drawing call), whenever possible. Changes to the textures, or drawing a line instead of a solid shape will require a new batch. You should play with your scene graph ordering to best improve performance.

All graphics data sent to the rendering pipeline must have a texture. If no texture is specified, the node will use the degenerate texture "/cc_2x2_white_image". This is an all white texture that produces solid images.

The polygon is specified in image coordinates. Image coordinates are different from texture coordinates. Their origin is at the bottom-left corner of the file, and each pixel is one unit. This makes specifying to polygon more natural for irregular shapes.

This means that a polygon with vertices (0,0), (width,0), (width,height), and (0,height) would be identical to a sprite node. However, a polygon with vertices (0,0), (2*width,0), (2*width,2*height), and (0,2*height) would tile the sprite (given the wrap settings) twice both horizontally and vertically.

The content size of this node is defined by the size (but not the offset) of the bounding box. The anchor point is relative to this content size. The default anchor point in TexturedNode is (0.5, 0.5). This means that a uniform translation of the polygon (in contrast to the node itself) will not move the shape on the the screen. Instead, it will just change the part of the texture it uses.

For example, suppose the texture has given width and height. We have one polygon with vertices (0,0), (width/2,0), (width/2,height/2), and (0,height/2). We have another polygon with vertices (width/2,height/2), (width,height/2), (width,height), and (width/2,height). Both polygons would create a rectangle of size (width/2,height/2). centered at the node position. However, the first would use the bottom left part of the texture, while the second would use the top right.

Constructor & Destructor Documentation

virtual PolygonNode::~PolygonNode ( void  )
inlinevirtual

Releases all resources allocated with this node.

This will release, but not necessarily delete the associated texture. However, the polygon and drawing commands will be deleted and no longer safe to use.

Member Function Documentation

NS_CC_BEGIN PolygonNode * PolygonNode::create ( )
static

Creates an empty polygon with the degenerate texture.

You do not need to set the texture. The polygon, however, will also be empty, and must be set via setPolygon.

Returns
An autoreleased sprite object.
PolygonNode * PolygonNode::create ( float *  vertices,
int  size,
int  offset = 0 
)
static

Creates a solid polygon with the given vertices.

The node will use the degenerate texture, which is solid white. Hence the polygon will have a solid color.

The polygon will be triangulated using the rules of Poly2.

Parameters
verticesThe vertices to texture (expressed in image space)
sizeThe number of elements in vertices
offsetThe offset in vertices
Returns
An autoreleased sprite object

Creates a solid polygon with the given vertices.

The node will use the degenerate texture, which is solid white. Hence the polygon will have a solid color.

The polygon will be triangulated using the rules of Poly2.

Parameters
verticesThe vertices to texture (expressed in image space)
offsetThe offset in vertices
sizeThe number of elements in vertices
Returns
An autoreleased sprite object
PolygonNode * PolygonNode::create ( const Poly2 poly)
static

Creates a solid polygon given polygon shape.

The node will use the degenerate texture, which is solid white. Hence the polygon will have a solid color.

Parameters
polyThe polygon to texture
Returns
An autoreleased sprite object
PolygonNode * PolygonNode::create ( const Rect &  rect)
static

Creates a solid polygon with the given rect.

The rectangle will be converted into a Poly2. There is little benefit to using a PolygonNode in this way over a normal Sprite. The option is here only for completion.

Parameters
rectThe rectangle to texture
Returns
An autoreleased sprite object
PolygonNode * PolygonNode::createWithFile ( const std::string &  filename)
static

Creates a textured polygon from the image filename.

After creation, the polygon will be a rectangle. The vertices of this polygon will be the corners of the image.

Parameters
filenameA path to image file, e.g., "scene1/earthtile.png"

a reference to the newly loaded texture

Returns
An autoreleased sprite object.
PolygonNode * PolygonNode::createWithFile ( const std::string &  filename,
float *  vertices,
int  size,
int  offset = 0 
)
static

Creates a textured polygon from the image filename and the given vertices.

The polygon will be triangulated using the rules of Poly2.

Parameters
filenameA path to image file, e.g., "scene1/earthtile.png"
verticesThe vertices to texture (expressed in image space)
sizeThe number of elements in vertices
offsetThe offset in vertices

a reference to the newly loaded texture

Returns
An autoreleased sprite object
PolygonNode * PolygonNode::createWithFile ( const std::string &  filename,
const Poly2 poly 
)
static

Creates a textured polygon from the image filename and the given polygon.

Parameters
filenameA path to image file, e.g., "scene1/earthtile.png"
polyThe polygon to texture

a reference to the newly loaded texture

Returns
An autoreleased sprite object
PolygonNode * PolygonNode::createWithFile ( const std::string &  filename,
const Rect &  rect 
)
static

Creates a textured polygon from the image filename and the given rect.

The rectangle will be converted into a Poly2. There is little benefit to using a PolygonNode in this way over a normal Sprite. The option is here only for completion.

Parameters
filenameA path to image file, e.g., "scene1/earthtile.png"
rectThe rectangle to texture

a reference to the newly loaded texture

Returns
An autoreleased sprite object
PolygonNode * PolygonNode::createWithTexture ( Texture2D *  texture)
static

Creates a textured polygon from a Texture2D object.

After creation, the polygon will be a rectangle. The vertices of this polygon will be the corners of the texture.

Parameters
textureA pointer to a Texture2D object.

a reference to this texture

Returns
An autoreleased sprite object
PolygonNode * PolygonNode::createWithTexture ( Texture2D *  texture,
float *  vertices,
int  size,
int  offset = 0 
)
static

Creates a textured polygon from a Texture2D object and the given vertices.

The polygon will be triangulated using the rules of Poly2.

Parameters
textureA pointer to an existing Texture2D object. You can use a Texture2D object for many sprites.
verticesThe vertices to texture (expressed in image space)
sizeThe number of elements in vertices
offsetThe offset in vertices

a reference to this texture

Returns
An autoreleased sprite object

Creates a textured polygon from a Texture2D object and the given vertices.

The polygon will be triangulated using the rules of Poly2.

Parameters
textureA pointer to an existing Texture2D object. You can use a Texture2D object for many sprites.
verticesThe vertices to texture (expressed in image space)
offsetThe offset in vertices
sizeThe number of elements in vertices

a reference to this texture

Returns
An autoreleased sprite object
PolygonNode * PolygonNode::createWithTexture ( Texture2D *  texture,
const Poly2 poly 
)
static

Creates a textured polygon from a Texture2D object and the given polygon.

Parameters
textureA pointer to an existing Texture2D object. You can use a Texture2D object for many sprites.
polyThe polygon to texture

a reference to this texture

Returns
An autoreleased sprite object
PolygonNode * PolygonNode::createWithTexture ( Texture2D *  texture,
const Rect &  rect 
)
static

Creates a textured polygon from a Texture2D object and the given rect.

The rectangle will be converted into a Poly2. There is little benefit to using a PolygonNode in this way over a normal Sprite. The option is here only for completion.

Parameters
textureA pointer to an existing Texture2D object. You can use a Texture2D object for many sprites.
rectThe rectangle to texture

a reference to this texture

Returns
An autoreleased sprite object
void PolygonNode::draw ( Renderer *  renderer,
const Mat4 &  transform,
uint32_t  flags 
)
overridevirtual

Sends drawing commands to the renderer

This method is overridden from Node, to provide the custom commands. PolygonNodes are drawn to the Triangle mesh (which is different from the Quad mesh used for Sprites) to cut down on the number of drawing calls.

Parameters
rendererReference to the render thread
transformThe accumulated transform from the parent
flagsSpecialized Cocos2d drawing flags

Sends drawing commands to the renderer

This method is overridden from Node, to provide the custom commands. PolygonSprites are drawn to the Triangle mesh (which is different from the Quad mesh used for Sprites) to cut down on the number of drawing calls.

Parameters
rendererReference to the render thread
transformThe accumulated transform from the parent
flagsSpecialized Cocos2d drawing flags
void PolygonNode::generateRenderData ( )
overrideprotectedvirtual

Allocate the render data necessary to render this node.

This method allocates the Triangles data used by the TrianglesCommand in the rendering pipeline.

Reimplemented from TexturedNode.

std::string PolygonNode::getDescription ( ) const
overridevirtual

Returns a string description of this object

This method is useful for debugging.

Returns
a string description of this object

The documentation for this class was generated from the following files: