Cocos2d-x3.0 区域裁切 的具体应用之游戏公告显示
来源:未知 责任编辑:责任编辑 发表时间:2014-05-10 12:17 点击:次
// on "init" you need to initialize your instance bool HelloWorld::init() { // 1. super init first if ( !Layer::init() ) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Point origin = Director::getInstance()->getVisibleOrigin(); // add "HelloWorld" splash screen" auto sprite = Sprite::create("HelloWorld.png"); sprite->setPosition(Point(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y)); this->addChild(sprite, 0); glEnable(GL_SCISSOR_TEST);// 开启显示指定区域 return true; }
void HelloWorld::visit(Renderer *renderer, const kmMat4& parentTransform, bool parentTransformUpdated){ float x = 100; float y = 100; float n_width = Director::getInstance()->getWinSize().width; float n_height = Director::getInstance()->getWinSize().height/2; log("x = %f,y = %f,w = %f,h = %f",x,y,n_width,n_height); glScissor(x, y, n_width, n_height); // 只显示当前窗口的区域 Layer::visit(renderer, parentTransform, parentTransformUpdated);//显示父类的内容 //glDisable(GL_SCISSOR_TEST); // 禁用 return; }
glScissor 显示当前窗口的区域
利用这个函数可以实现 类似游戏公告的显示
// on "init" you need to initialize your instance bool HelloWorld::init() { // 1. super init first if ( !Layer::init() ) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Point origin = Director::getInstance()->getVisibleOrigin(); // add "HelloWorld" splash screen" auto sprite = Sprite::create("HelloWorld.png"); sprite->setPosition(Point(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y)); this->addChild(sprite, 0); sprite->setTag(1000); return true; } void HelloWorld::visit(Renderer *renderer, const kmMat4& parentTransform, bool parentTransformUpdated){ this->beforeDraw(renderer, parentTransform, parentTransformUpdated); Node::visit(renderer, parentTransform, parentTransformUpdated);//显示父类的内容 this->afterDraw(renderer, parentTransform, parentTransformUpdated); } void HelloWorld::setNeedClip(bool need){ mIsNeedClip=need; } bool HelloWorld::isNeedClip(){ return mIsNeedClip; } void HelloWorld::beforeDraw(Renderer *renderer, const kmMat4& parentTransform, bool parentTransformUpdated) { _beforeDrawCommand.init(_globalZOrder); _beforeDrawCommand.func = CC_CALLBACK_0(HelloWorld::onBeforeDraw, this,parentTransform, parentTransformUpdated); renderer->addCommand(&_beforeDrawCommand); } static Rect mClip; static int mClipCounter=0; float math_abs(float v){ if(v>0){ return v; } return 0-v; } float math_max(float v1,float v2){ if(v1>v2){ return v1; } return v2; } float math_min(float v1,float v2){ if(v1>v2){ return v2; } return v1; } void HelloWorld::onBeforeDraw(const kmMat4& parentTransform, bool parentTransformUpdated){ int clipW=1024; int clipH=768/4; bool needClip=clipW>0&&clipH>0; if(isNeedClip()&&needClip){ glEnable(GL_SCISSOR_TEST); Point selfPos =Point(790,500);//this->getPosition(); float scaleX = 1; float scaleY = 1; Rect viewPortRect = Director::getInstance()->getOpenGLView()->getViewPortRect(); float clipX=selfPos.x *scaleX + viewPortRect.origin.x; float clipY=selfPos.y * scaleY + viewPortRect.origin.y; clipW*=scaleX; clipH*=scaleX; if(mClipCounter>0){ saveOldRect=mClip; float right=math_min(clipX+clipW,mClip.origin.x+mClip.size.width); float top=math_min(clipY+clipH,mClip.origin.y+mClip.size.height); clipX=math_max(clipX,mClip.origin.x); clipY=math_max(clipY,mClip.origin.y); clipW=right-clipX; clipH=top-clipY; if(clipW
相关新闻>>
最新推荐更多>>>
- 发表评论
-
- 最新评论 更多>>