Use instance of ClippingNode in Cocos2d-x3.1

Source: Internet
Author: User
# Include2dCCNode. h uses the node header file and OPENGL depth buffer definition header file # includeCCGL. h # includerendererCCGroupCommand. h uses GroupCommand and CustomCommand rendering # includerendererCCCustomCommand. hNS_CC_BEGIN ** constraint. Itdr

# 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. hNS_CC_BEGIN/** ClippingNode is a subclass of Node. it dr

# 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 N 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 clipping 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. T He 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 pixel 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 image hollowing out. 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 the 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 o NEnter () override;/*** @ js NA * @ lua NA */virtual void onEnterTransitionDidFinish () override; /*** @ js NA * @ lua NA */virtual void onExitTransitionDidStart () override;/*** @ js NA * @ lua NA */virtual void 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();};

BoolNewClippingNode: 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.