getVisibleSize(); Point origin = Director::getInstance()->" name="description" />

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
	
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
用户名: 验证码:点击我更换图片
最新评论 更多>>

推荐热点

  • cocos2d-x学习笔记(19)--label 、label atlas
  • cocos2d-x学习笔记(23)--地图的使用3--CCTMXLayer
  • Cocos2d-x学习(一):HelloWorld
  • cocos2dx在xcode下开发,编译到android上(2)
  • cocos2d 设置屏幕默认方向
  • Cocos2d-x 2.0 之 Actions “三板斧” 之一
  • cocos2d-x学习笔记(22)--地图的使用2(TMX) --Z-Order、AnchorPoi
  • cocos2d-x学习笔记(18)--游戏打包(windows平台)
  • cocos2d-x学习笔记(16)--spritesheet(精灵表单)
网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
Copyright © 2008-2015 计算机技术学习交流网. 版权所有

豫ICP备11007008号-1