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

#include <CUTexturedNode.h>

Inheritance diagram for TexturedNode:
PathNode PolygonNode WireNode AnimationNode

Public Member Functions

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)
 

Public Attributes

CC_CONSTRUCTOR_ACCESS __pad0__: TexturedNode(void)
 

Protected Member Functions

TrianglesCommand::Triangles & allocTriangles (const Poly2 &poly)
 
void clearRenderData ()
 
void updateColor () override
 
void updateBlendFunc ()
 
void updateTextureCoords ()
 
virtual void generateRenderData ()
 

Protected Attributes

Texture2D * _texture
 
Poly2 _polygon
 
BlendFunc _blendFunc
 
bool _insideBounds
 
bool _opacityModifyRGB
 
TrianglesCommand _command
 
TrianglesCommand::Triangles _triangles
 
bool _flipHorizontal
 
bool _flipVertical
 

Detailed Description

Abstract scene graph node represent a textured shape.

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 node shape is stored as polygon. This is true regardless of whether the node is displaying solid shapes or a wireframe. The polygon is itself 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

TexturedNode::~TexturedNode ( void  )
virtual

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.

Releases all resources allocated with this sprite.

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

TrianglesCommand::Triangles & TexturedNode::allocTriangles ( const Poly2 poly)
protected

Generates the triangles data to render a shape from the polygon.

The shape may either be solid or a wireframe. This is determined by the specific draw method for this class. This method simply generates the triangles and and traversal information from the Poly2 class.

Parameters
polyThe polygon to traverse
Returns
Reference to the filled triangle data
void TexturedNode::clearRenderData ( )
protected

Clears the render data, releasing all vertices and indices.

void TexturedNode::flipHorizontal ( bool  flag)
inline

Flips the texture coordinates horizontally if flag is true.

Flipping the texture coordinates replaces each u coordinate with 1-u. Hence this operation is defined even if the texture coordinates are outside the range 0..1.

Parameters
flagwhether to flip the coordinates horizontally
void TexturedNode::flipVertical ( bool  flag)
inline

Flips the texture coordinates vertically if flag is true.

Flipping the texture coordinates replaces each v coordinate with 1-v. Hence this operation is defined even if the texture coordinates are outside the range 0..1.

Parameters
flagwhether to flip the coordinates vertically
virtual void TexturedNode::generateRenderData ( )
inlineprotectedvirtual

Allocate the render data necessary to render this node.

This method is abstract, since it needs access to the implementation specific render data.

Reimplemented in PathNode, WireNode, and PolygonNode.

const BlendFunc& TexturedNode::getBlendFunc ( ) const
inlineoverride

Returns the current blend function for this node

This is part of TextureProtocol.

Returns
the node blend function.
const Rect& TexturedNode::getBoundingRect ( ) const
inline

Returns the rect of the Polygon in points

The bounding rect is the smallest rectangle containing all of the points in the polygon.

This value also defines the content size of the node. The polygon will be shifted so that its bounding rect is centered at the node center.

const Poly2& TexturedNode::getPolygon ( ) const
inline

Returns the texture polygon for this scene graph node

Returns
the texture polygon for this scene graph node
virtual Texture2D* TexturedNode::getTexture ( ) const
inlineoverridevirtual

Returns the Texture2D object used by this node.

Returns
the Texture2D object used by this node
bool TexturedNode::init ( )
overridevirtual

Intializes 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
true if the sprite is initialized properly, false otherwise.
bool TexturedNode::init ( float *  vertices,
int  size,
int  offset = 0 
)
virtual

Intializes 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
true if the sprite is initialized properly, false otherwise.
bool TexturedNode::init ( const Poly2 poly)
virtual

Intializes 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
true if the sprite is initialized properly, false otherwise.
bool TexturedNode::init ( const Rect &  rect)
virtual

Intializes 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
true if the sprite is initialized properly, false otherwise.
bool TexturedNode::initWithFile ( const std::string &  filename)
virtual

Intializes 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
true if the sprite is initialized properly, false otherwise.
bool TexturedNode::initWithFile ( const std::string &  filename,
float *  vertices,
int  size,
int  offset = 0 
)
virtual

Initializes 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
true if the sprite is initialized properly, false otherwise.

Initializes 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)
offsetThe offset in vertices
sizeThe number of elements in vertices

a reference to the newly loaded texture

Returns
true if the sprite is initialized properly, false otherwise.
bool TexturedNode::initWithFile ( const std::string &  filename,
const Poly2 poly 
)
virtual

Initializes 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
true if the sprite is initialized properly, false otherwise.
bool TexturedNode::initWithFile ( const std::string &  filename,
const Rect &  rect 
)
virtual

Initializes 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 TexturedNode 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
true if the sprite is initialized properly, false otherwise.
bool TexturedNode::initWithTexture ( Texture2D *  texture)
virtual

Initializes 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
true if the sprite is initialized properly, false otherwise.
bool TexturedNode::initWithTexture ( Texture2D *  texture,
float *  vertices,
int  size,
int  offset = 0 
)
virtual

Initializes 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
true if the sprite is initialized properly, false otherwise.
bool TexturedNode::initWithTexture ( Texture2D *  texture,
const Poly2 poly 
)
virtual

Initializes 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
true if the sprite is initialized properly, false otherwise.
bool TexturedNode::initWithTexture ( Texture2D *  texture,
const Rect &  rect 
)
virtual

Initializes 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 TexturedNode 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
true if the sprite is initialized properly, false otherwise.
bool TexturedNode::isFlipHorizontal ( ) const
inline

Returns true if the texture coordinates are flipped horizontally.

Returns
true if the texture coordinates are flipped horizontally.
bool TexturedNode::isFlipVertical ( ) const
inline

Returns true if the texture coordinates are flipped vertically.

Returns
true if the texture coordinates are flipped vertically.
bool TexturedNode::isOpacityModifyRGB ( void  ) const
overridevirtual

Returns true if the opacity should be premultiplied into the color.

This method is necessary because of how scene graphs work

Returns
true if the opacity should be premultiplied into the color
void TexturedNode::setBlendFunc ( const BlendFunc &  blendFunc)
inlineoverride

Sets the blend function to the one specified

This is part of TextureProtocol.

Parameters
blendFuncthe new blend function.
void TexturedNode::setOpacityModifyRGB ( bool  modify)
overridevirtual

Sets whether the opacity should be premultiplied into the color.

This method is necessary because of how scene graphs work

Parameters
modifywhether the opacity should be premultiplied into the color
void TexturedNode::setPolygon ( float *  vertices,
int  size,
int  offset = 0 
)
virtual

Sets the polgon to the vertices expressed in texture space.

The polygon will be triangulated according to the rules of Poly2.

Parameters
verticesThe vertices to texture
sizeThe number of elements in vertices
offsetThe offset in vertices

Sets the texture polgon to the vertices expressed in image space.

The polygon will be triangulated according to the rules of Poly2.

Parameters
verticesThe vertices to texture
offsetThe offset in vertices
sizeThe number of elements in vertices
void TexturedNode::setPolygon ( const Poly2 poly)
virtual

Sets the polygon to the given one in texture space.

Parameters
polyThe polygon to texture

Sets the texture polygon to the given one in image space.

Parameters
polyThe polygon to texture

Reimplemented in PathNode.

void TexturedNode::setPolygon ( const Rect &  rect)
virtual

Sets the texture polygon to one equivalent to the given rect.

The rectangle will be converted into a Poly2. Unless you are constructing wireframes, there is little benefit to using a TexturedNode in this way over a normal Sprite. The option is here only for completion.

Parameters
rectThe rectangle to texture

Reimplemented in PathNode.

void TexturedNode::setTexture ( const std::string &  filename)
virtual

Sets the node texture to a new sprite allocated from a filename.

This method will have no effect on the polygon vertices. Unlike Sprite, TexturedNode decouples the geometry from the texture. That is because we expect the vertices to not match the texture perfectly.

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

a reference to the newly loaded texture the previous scene texture used by this object

void TexturedNode::setTexture ( Texture2D *  texture)
overridevirtual

Sets the node texture to the one specified.

This method will have no effect on the polygon vertices. Unlike Sprite, TexturedNode decouples the geometry from the texture. That is because we expect the vertices to not match the texture perfectly.

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

a reference to this texture the previous scene texture used by this object

void TexturedNode::shiftPolygon ( float  dx,
float  dy 
)
virtual

Translates the polygon by the given amount.

Remember that translating the polygon has no effect on the shape or position. Because the polygon is expressed in texture coordinates, all it does is shift the texture coords of the polygon. Hence this method can be used for animation and filmstrips.

Calling this method is faster than changing the polygon and resetting it.

Parameters
dxThe amount to shift horizontally.
dxThe amount to shift horizontally.
void TexturedNode::updateBlendFunc ( void  )
protected

Updates the blend options for this node to use in a render pass

Update the blend options for this node to use in a render pass

void TexturedNode::updateColor ( void  )
overrideprotected

Updates the color for each vertices to match the node settings.

void TexturedNode::updateTextureCoords ( )
protected

Updates the texture coordinates for this polygon

The texture coordinates are computed assuming that the polygon is defined in image space, with the origin in the bottom left corner of the texture.

Member Data Documentation

BlendFunc TexturedNode::_blendFunc
protected

Blending function is required to implement TextureProtocol

TrianglesCommand TexturedNode::_command
protected

The command for the Cocos2D rendering pipeline

bool TexturedNode::_flipHorizontal
protected

Whether or not to flip the texture horizontally

bool TexturedNode::_flipVertical
protected

Whether or not to flip the texture vertically

bool TexturedNode::_insideBounds
protected

Whether or not the sprite was inside bounds the previous frame

bool TexturedNode::_opacityModifyRGB
protected

Whether to support opacity and RGB protocol

Poly2 TexturedNode::_polygon
protected

The polygon defining this node, with vertices in texture space

Texture2D* TexturedNode::_texture
protected

Texture to be applied to the polygon

TrianglesCommand::Triangles TexturedNode::_triangles
protected

Rendering data representing a solid shape


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