Use instance of ClippingNode in Cocos2d-x3.1

Source: Internet
Author: User

Use instance of ClippingNode in Cocos2d-x3.1
1. ClippingNode description

ClippingNode uses the template mask to crop the Node region. How can we understand the mask of ClippingNode? Let's look at the example.

A template is a shape that allows you to see the layers on the bottom board. If there is no content on the bottom board, you can directly see the content on the Layer.

Things will not affect things on the Layer, that is, the space outside the baseboard of the template is transparent to the Layer. If an genie moves on the bottom plate outside the template, I'm a beer" http://www.bkjia.com/kf/web/php/ "Target =" _ blank "class =" keylink "> PHPC9wPgo8cD7Kx7 + release/L34yOvEo7DltcTH + release + 0tb3L/KGjPC9wPgo8aDI + release" brush: java; "> # include" 2d/CCNode. h "// use the node header file and OPENGL depth buffer definition header file # include" CCGL. h "# include" renderer/CCGroupCommand. h "// use GroupCommand and CustomCommand rendering # include" renderer/CCCustomCommand. h "NS_CC_BEGIN/** ClippingNode is a subclass Of Node. it draws its content (childs) clipped using a stenric. the stencel is an other Node that will not be drawn. the clipping is done using the alpha part of the stencer (adjusted with an alphaThreshold ). */class CC_DLL ClippingNode: public Node {public:/** Creates and initializes a clipping node without a stencer. */static ClippingNode * create (); // static object creation/** Creates and initializes a clippi Ng node with an other node as its stencel. the stencel node will be retained. */static ClippingNode * create (Node * stenpencil); // use a template to create a crop Node/** The Node to use as a stenpencil to do the clipping. the stencel node will be retained. this default to nil. */Node * getstenpencil () const; // obtain The Node template void setstenpencil (Node * stenpencil); // set The Node template/** The alpha threshold. the content is drawn only where the stencel have pixe L with alpha greater than the alphaThreshold. shocould be a float between 0 and 1. this default to 1 (so alpha test is disabled ). * // ALPHA test reference value, which is used for ALPHA test and comparison. Generally, pixels smaller than this value in the comparison algorithm will be discarded directly. This achieves the image's hollow GLfloat getAlphaThreshold () const; // obtain the ALPHT test reference value void setAlphaThreshold (GLfloat alphaThreshold); // set the ALPHA test reference value/** Inverted. if this is set to true, the stencel is inverted, so the content is drawn where Stencel is NOT drawn. this default to false. */bool isInverted () const; // gets whether the mask operation is reversed and sets void setInverted (bool inverted ); // set the anti-occurrence of the mask operation // Overrides // inherited function/*** @ js NA * @ lua NA */virtual void onEnter () override; /*** @ js NA * @ lua NA */virtual void onEnterTransitionDidFinish () override;/*** @ js NA * @ lua NA */virtual void onExitTransitionDidStart () override; /*** @ js NA * @ lua NA */virtual voi D onExit () override; virtual void visit (Renderer * renderer, const Mat4 & parentTransform, bool parentTransformUpdated) override; CC_CONSTRUCTOR_ACCESS: ClippingNode (); /*** @ js NA * @ lua NA */virtual ~ ClippingNode ();/** Initializes a clipping node without a stencer. */virtual bool init ();/** Initializes a clipping node with an other node as its stencel. the stencel node will be retained, and its parent will be set to this clipping node. */virtual bool init (Node * stencer); protected:/** draw fullscreen quad to clear stencer bits */void Merge (); Node * _ stencer; GLfloat _ alphaThreshold; bool _ inverted; // renderData and callback void onBeforeVisit (); void dispatch (); void onAfterVisit (); GLboolean _ timeout; GLuint _ timeout; GLenum _ timeout; GLint _ currentStencilRef; GLuint _ blank; GLenum _ blank; GLboolean _ currentAlphaTestEnabled; GLenum _ currentAlphaTestFunc; GLclampf _ currentAlphaTestRef; GLint _ blank; GroupCommand _ groupCommand; customCommand _ beforeVisitCmd; CustomCommand _ afterDrawStencilCmd; CustomCommand _ afterVisitCmd; private: CC_DISALLOW_COPY_AND_ASSIGN (ClippingNode);}; NS_CC_END
3. Use instances

class NewClippingNode : public Layer{public:    CREATE_FUNC(NewClippingNode);    virtual bool init();};

Bool NewClippingNode: init ()

{

Auto s = Director: getInstance ()-> getWinSize ();

Bool bRet = false;

Do {

CC_BREAK_IF (! Layer: init ());

Auto clipper = ClippingNode: create ();

Clipper-> setTag (kTagClipperNode );

Clipper-> setContentSize (Size (200,200); // you can specify the Size of the cropping area.

Clipper-> ignoreAnchorPointForPosition (false );

Clipper-> setAnchorPoint (Vec2 (0.5, 0.5); // set the anchor

Clipper-> setPosition (Vec2 (s. width/2, s. height/2 ));

This-> addChild (clipper );

Clipper-> setAlphaThreshold (0.05f); // sets the transparency threshold ALPHA value.

// Set the cropping Template

Auto stenpencil = Sprite: create ("grossini.png ");

Stencel-> setScale (2 );

Stenpencil-> setPosition (Vec2 (s. width/2, s. height/2 ));

Clipper-> setstenpencil (stenpencil); // you can specify a cropping template.


// TODO Fix draw node as clip node

// Auto stenpencil = NewDrawNode: create ();

// Vec2 rectangle [4];

// Rectangle [0] = Vec2 (0, 0 );

// Rectangle [1] = Vec2 (clipper-> getContentSize (). width, 0 );

// Rectangle [2] = Vec2 (clipper-> getContentSize (). width, clipper-> getContentSize (). height );

// Rectangle [3] = Vec2 (0, clipper-> getContentSize (). height );

//

// Color4F white (1, 1, 1, 1 );

// Stenpencil-> drawPolygon (rectangle, 4, white, 1, white );

// Clipper-> setstenpencil (stenpencil );




// Set the content of the cropping Node

Auto content = Sprite: create ("background2.png ");

Content-> setTag (kTagContentNode );

Content-> ignoreAnchorPointForPosition (false );

Content-> setAnchorPoint (Vec2 (0.5, 0.5 ));

Content-& gt; setPosition (Vec2 (s. width/2,200 ));

Clipper-> addChild (content );

BRet = true;

} While (0 );

Return bRet;

}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.