axmol/cocos/2d/CCClippingRectangleNode.cpp

75 lines
2.0 KiB
C++
Raw Normal View History

2014-09-29 15:07:05 +08:00
#include "CCClippingRectangleNode.h"
2014-09-29 15:07:05 +08:00
#include "base/CCDirector.h"
#include "renderer/CCRenderer.h"
#include "math/Vec2.h"
#include "CCGLView.h"
NS_CC_BEGIN
ClippingRectangleNode* ClippingRectangleNode::create(const Rect& clippingRegion)
2014-09-29 15:07:05 +08:00
{
ClippingRectangleNode* node = new ClippingRectangleNode();
2014-09-29 15:07:05 +08:00
if (node && node->init()) {
node->setClippingRegion(clippingRegion);
node->autorelease();
} else {
CC_SAFE_DELETE(node);
}
return node;
}
ClippingRectangleNode* ClippingRectangleNode::create()
2014-09-29 15:07:05 +08:00
{
ClippingRectangleNode* node = new ClippingRectangleNode();
2014-09-29 15:07:05 +08:00
if (node && node->init()) {
node->autorelease();
} else {
CC_SAFE_DELETE(node);
}
return node;
}
void ClippingRectangleNode::setClippingRegion(const Rect &clippingRegion)
2014-09-29 15:07:05 +08:00
{
_clippingRegion = clippingRegion;
}
void ClippingRectangleNode::onBeforeVisitScissor()
2014-09-29 15:07:05 +08:00
{
if (_clippingEnabled) {
glEnable(GL_SCISSOR_TEST);
2014-09-29 15:07:05 +08:00
GLView* glView = Director::getInstance()->getOpenGLView();
glView->setScissorInPoints(_clippingRegion.origin.x,
_clippingRegion.origin.y,
_clippingRegion.size.width,
_clippingRegion.size.height);
2014-09-29 15:07:05 +08:00
}
}
void ClippingRectangleNode::onAfterVisitScissor()
2014-09-29 15:07:05 +08:00
{
if (_clippingEnabled)
{
glDisable(GL_SCISSOR_TEST);
}
}
void ClippingRectangleNode::visit(Renderer *renderer, const Mat4 &parentTransform, uint32_t parentFlags)
2014-09-29 15:07:05 +08:00
{
_beforeVisitCmdScissor.init(_globalZOrder);
_beforeVisitCmdScissor.func = CC_CALLBACK_0(ClippingRectangleNode::onBeforeVisitScissor, this);
2014-09-29 15:07:05 +08:00
renderer->addCommand(&_beforeVisitCmdScissor);
Node::visit(renderer, parentTransform, parentFlags);
_afterVisitCmdScissor.init(_globalZOrder);
_afterVisitCmdScissor.func = CC_CALLBACK_0(ClippingRectangleNode::onAfterVisitScissor, this);
2014-09-29 15:07:05 +08:00
renderer->addCommand(&_afterVisitCmdScissor);
}
NS_CC_END