Пример #1
0
void DecorateScene::saveTheScreenShot(){
    NotificationCenter::getInstance()->postNotification(kShotScreenEvent, __String::create("No"));
    RenderTexture* saveImageTexture = RenderTexture::create(canEatLayer->getContentSize().width, canEatLayer->getContentSize().height, Texture2D::PixelFormat::RGBA8888);
    
    saveImageTexture->begin();
    canEatLayer->visit();
    saveImageTexture->end();
    
    saveImageTexture->getSprite()->setFlippedY(false);
    saveImageTexture->getSprite()->getTexture()->setAntiAliasTexParameters();
    Director::getInstance()->getRenderer()->render();
    
    log("the path is %s", (STFileUtility::getStoragePath()+"temp.png").c_str());
    Image* pImage = saveImageTexture->newImage();
    pImage->saveToFile(STFileUtility::getStoragePath()+"temp.png", false);
    pImage->release();
    
    saveImageTexture->beginWithClear(1.0, 1.0, 1.0, 0);
    cannotEatLayer->visit();
    saveImageTexture->end();
    
    saveImageTexture->getSprite()->setFlippedY(false);
    saveImageTexture->getSprite()->getTexture()->setAntiAliasTexParameters();
    Director::getInstance()->getRenderer()->render();
    Image* pImage2 = saveImageTexture->newImage();
    pImage2->saveToFile(STFileUtility::getStoragePath()+"temp1.png", false);
    pImage2->release();
    
}
Пример #2
0
Sprite* Utils::maskedSpriteWithSprite(Sprite* textureSprite, Sprite* maskSprite)
{
    // 1
    RenderTexture * rt = RenderTexture::create( maskSprite->getContentSize().width,
                                               maskSprite->getContentSize().height );
    
    // 2
    maskSprite->setPosition(maskSprite->getContentSize().width/2,
                            maskSprite->getContentSize().height/2);
    textureSprite->setPosition(textureSprite->getContentSize().width/2,
                               textureSprite->getContentSize().height/2);
    
    // 3
    maskSprite->setBlendFunc( BlendFunc{GL_ONE, GL_ZERO} );
    textureSprite->setBlendFunc( BlendFunc{GL_DST_ALPHA, GL_ZERO} );
    
    // 4
    rt->begin();
    maskSprite->visit();
    textureSprite->visit();
    rt->end();
    
    // 5
    Sprite *retval = Sprite::createWithTexture(rt->getSprite()->getTexture());
    retval->setFlippedY(true);
    return retval;
}
Пример #3
0
Sprite* HelloWorld::spriteWithColor(Color4F bgColor, float textureWidth, float textureHeight)
{
	// 1: Create new RenderTexture
	RenderTexture *rt = RenderTexture::create(textureWidth, textureHeight);

	// 2: Call RenderTexture:begin
	rt->beginWithClear(bgColor.r, bgColor.g, bgColor.b, bgColor.a);

	// 3: Draw into the texture

	//// gradient
	_gradient_command.init(rt->getGlobalZOrder());
	_gradient_command.func = std::bind(&HelloWorld::onDrawGradient, this, textureWidth, textureHeight);
	auto renderer = Director::getInstance()->getRenderer();
	renderer->addCommand(&_gradient_command);

	//// noise cloud
	BlendFunc blendFunc;
	blendFunc.src = GL_DST_COLOR;
	blendFunc.dst = GL_ZERO;

	Sprite *noise = Sprite::create("Noise.png");
	noise->setBlendFunc(blendFunc);
	noise->setPosition(Vec2(textureWidth / 2, textureHeight / 2));
	noise->visit();

	// 4: Call CCRenderTexture:end
	rt->end();

	// 5: Create a new Sprite from the texture
	return Sprite::createWithTexture(rt->getSprite()->getTexture());
}
Пример #4
0
void TextureBlur::create(Texture2D* target, const int radius, const std::string& fileName, std::function<void()> callback, const int step)
{
    CCASSERT(target != nullptr, "Null pointer passed as a texture to blur");
    CCASSERT(radius <= maxRadius, "Blur radius is too big");
    CCASSERT(radius > 0, "Blur radius is too small");
    CCASSERT(!fileName.empty(), "File name can not be empty");
    CCASSERT(step <= radius/2 + 1 , "Step is too big");
    CCASSERT(step > 0 , "Step is too small");
    
	Size textureSize = target->getContentSize();
    Vec2 pixelSize = Vec2(float(step)/textureSize.width, float(step)/textureSize.height);
    int radiusWithStep = radius/step;
    
    float* weights = new float[maxRadius];
    calculateGaussianWeights(radiusWithStep, weights);
    
    Sprite* stepX = CCSprite::createWithTexture(target);
    stepX->retain();
    stepX->setPosition(Point(0.5f*textureSize.width, 0.5f*textureSize.height));
    stepX->setFlippedY(true);
    
    GLProgram* blurX = getBlurShader(pixelSize, Vec2(1.0f, 0.0f), radiusWithStep, weights);
    stepX->setGLProgram(blurX);
    
    RenderTexture* rtX = RenderTexture::create(textureSize.width, textureSize.height);
    rtX->retain();
    rtX->begin();
    stepX->visit();
    rtX->end();
    
    Sprite* stepY = CCSprite::createWithTexture(rtX->getSprite()->getTexture());
    stepY->retain();
    stepY->setPosition(Point(0.5f*textureSize.width, 0.5f*textureSize.height));
    stepY->setFlippedY(true);
    
    GLProgram* blurY = getBlurShader(pixelSize, Vec2(0.0f, 1.0f), radiusWithStep, weights);
    stepY->setGLProgram(blurY);
    
    RenderTexture* rtY = RenderTexture::create(textureSize.width, textureSize.height);
    rtY->retain();
    rtY->begin();
    stepY->visit();
    rtY->end();
    
	auto completionCallback = [rtX, rtY, stepX, stepY, callback](RenderTexture* rt, const std::string& filename)
	{
        stepX->release();
        stepY->release();
        rtX->release();
        rtY->release();
        callback();
    };
	
    rtY->saveToFile(fileName, true, completionCallback);
}
Пример #5
0
	virtual void visit(Renderer *renderer,
		const Mat4& parentTransform, uint32_t parentFlags) override
	{
		m_renderTexture->beginWithClear(0, 0, 0, 0);
		Node::visit(renderer, parentTransform, parentFlags);
		m_renderTexture->end();

		m_renderTexture->getSprite()->setBlendFunc(m_blendFunc);
		m_renderTexture->setLocalZOrder(getLocalZOrder());
		m_renderTexture->visit(renderer, Mat4::IDENTITY, 0);
	}
Пример #6
0
	virtual bool init() override
	{
		Size size = Director::getInstance()->getVisibleSize();
		m_renderTexture = RenderTexture::create(size.width, size.height);
		if (!m_renderTexture)
			return false;
		m_renderTexture->retain();
		m_renderTexture->setKeepMatrix(true);
		m_renderTexture->setAnchorPoint(Vec2::ZERO);
		m_renderTexture->getSprite()->setAnchorPoint(Vec2::ZERO);
		return Node::init();
	}
Пример #7
0
Sprite *HelloWorld::spriteWithColor(Color4F color, float texWidth, float texHeight)
{
    RenderTexture *rt = RenderTexture::create(texWidth, texHeight);
    rt->beginWithClear(color.r, color.g, color.b, color.a);
    
    // 3: Draw into the texture
    // You'll add this later
    
    setGLProgramState(cocos2d::GLProgramState::getOrCreateWithGLProgramName(cocos2d::GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR_NO_MVP));
    
    _customCommand.init(_globalZOrder);
    _customCommand.func = [texWidth, texHeight, this]() {
        float gradientAlpha = 0.7f;
        Vec2 vertices[4];
        Color4F colors[4];
        int nVertices = 0;
        
        vertices[nVertices] = Vec2 {0, 0};
        colors[nVertices++] = Color4F {0, 0, 0, 0 };
        vertices[nVertices] = Vec2 {texWidth, 0};
        colors[nVertices++] = Color4F {0, 0, 0, 0};
        vertices[nVertices] = Vec2 {0, texHeight};
        colors[nVertices++] = Color4F {0, 0, 0, gradientAlpha};
        vertices[nVertices] = Vec2 {texWidth, texHeight};
        colors[nVertices++] = Color4F {0, 0, 0, gradientAlpha};
        
        getGLProgram()->use();
        getGLProgram()->setUniformsForBuiltins();
        
        cocos2d::GL::enableVertexAttribs(cocos2d::GL::VERTEX_ATTRIB_FLAG_POSITION | cocos2d::GL::VERTEX_ATTRIB_FLAG_COLOR);
        
        glBindBuffer(GL_ARRAY_BUFFER, 0);
        glVertexAttribPointer(cocos2d::GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
        glVertexAttribPointer(cocos2d::GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_FLOAT, GL_FALSE, 0, colors);
//        glBlendFunc(CC_BLEND_SRC, CC_BLEND_DST);
        cocos2d::GL::blendFunc(CC_BLEND_SRC, CC_BLEND_DST);
        glDrawArrays(GL_TRIANGLE_STRIP, 0, (GLsizei)nVertices);

    };
    
    auto renderer = Director::getInstance()->getRenderer();
    renderer->addCommand(&_customCommand);
    
    Sprite *noise = Sprite::create("Noise-iphone5hd.png");
    noise->setBlendFunc(BlendFunc {GL_DST_COLOR, GL_ZERO});
    noise->setPosition(texWidth/2, texHeight/2);
    noise->visit();
    
    rt->end();
    
    return Sprite::createWithTexture(rt->getSprite()->getTexture());
}
Пример #8
0
CCMask* createMasked(Sprite* mask, Color3B color) {
    auto size = mask->getContentSize();
    RenderTexture* pRenderTexture = RenderTexture::create(size.width, size.height);
    auto r = color.r / 255.0f;
    auto g = color.g / 255.0f;
    auto b = color.b / 255.0f;
    pRenderTexture->beginWithClear(r, g, b, 1);
    pRenderTexture->end();
    
    auto sprite = Sprite::createWithTexture(pRenderTexture->getSprite()->getTexture());
    sprite->setPosition(Point(mask->getContentSize().width/2, mask->getContentSize().height/2));
    
    CCMask* masked = CCMask::create(mask , sprite);
    return masked;
}
Пример #9
0
Sprite* HelloWorld::stripedSpriteWithColor(Color4F c1, Color4F c2, float textureWidth, float textureHeight, int nStripes)
{
	// 1: Create new RenderTexture
	RenderTexture *rt = RenderTexture::create(textureWidth, textureHeight);

	// 2: Call RenderTexture:begin
	rt->beginWithClear(c1.r, c1.g, c1.b, c1.a);

	// 3: Draw into the texture

	// we have to run custom command on renderer
	_stripes_command.init(rt->getGlobalZOrder());
	_stripes_command.func = std::bind(&HelloWorld::onDrawStripes, this, c2, textureWidth, textureHeight, nStripes);
	auto renderer = Director::getInstance()->getRenderer();
	renderer->addCommand(&_stripes_command);

	// gradient
	_gradient_command.init(rt->getGlobalZOrder());
	_gradient_command.func = std::bind(&HelloWorld::onDrawGradient, this, textureWidth, textureHeight);
	renderer->addCommand(&_gradient_command);

	// gradient
	_tophighlight_command.init(rt->getGlobalZOrder());
	_tophighlight_command.func = std::bind(&HelloWorld::onDrawTopHighlight, this, textureWidth, textureHeight);
	renderer->addCommand(&_tophighlight_command);

	// noise cloud
	BlendFunc blendFunc;
	blendFunc.src = GL_DST_COLOR;
	blendFunc.dst = GL_ZERO;

	Sprite *noise = Sprite::create("Noise.png");
	noise->setBlendFunc(blendFunc);
	noise->setPosition(Vec2(textureWidth / 2, textureHeight / 2));
	noise->visit();

	// 4: Call CCRenderTexture:end
	rt->end();

	// 5: Create a new Sprite from the texture
	return Sprite::createWithTexture(rt->getSprite()->getTexture());
}
// TransitionProgress
void TransitionProgress::onEnter()
{
    TransitionScene::onEnter();

    setupTransition();
    
    // create a transparent color layer
    // in which we are going to add our rendertextures
    Size size = Director::getInstance()->getWinSize();

    // create the second render texture for outScene
    RenderTexture *texture = RenderTexture::create((int)size.width, (int)size.height);
    texture->getSprite()->setAnchorPoint(Vector2(0.5f,0.5f));
    texture->setPosition(Vector2(size.width/2, size.height/2));
    texture->setAnchorPoint(Vector2(0.5f,0.5f));

    // render outScene to its texturebuffer
    texture->beginWithClear(0, 0, 0, 1);
    _sceneToBeModified->visit();
    texture->end();


    //    Since we've passed the outScene to the texture we don't need it.
    if (_sceneToBeModified == _outScene)
    {
        hideOutShowIn();
    }
    //    We need the texture in RenderTexture.
    ProgressTimer *node = progressTimerNodeWithRenderTexture(texture);

    // create the blend action
    ActionInterval* layerAction = (ActionInterval*)Sequence::create(
        ProgressFromTo::create(_duration, _from, _to),
        CallFunc::create(CC_CALLBACK_0(TransitionScene::finish,this)),
        nullptr);
    // run the blend action
    node->runAction(layerAction);

    // add the layer (which contains our two rendertextures) to the scene
    addChild(node, 2, kSceneRadial);
}
Пример #11
0
void ControlSwitchSprite::needsLayout()
{
    _onSprite->setPosition(Point(_onSprite->getContentSize().width / 2 + _sliderXPosition,
        _onSprite->getContentSize().height / 2));
    _offSprite->setPosition(Point(_onSprite->getContentSize().width + _offSprite->getContentSize().width / 2 + _sliderXPosition, 
        _offSprite->getContentSize().height / 2));
    _thumbSprite->setPosition(Point(_onSprite->getContentSize().width + _sliderXPosition,
        _maskTexture->getContentSize().height / 2));

    if (_onLabel)
    {
        _onLabel->setPosition(Point(_onSprite->getPosition().x - _thumbSprite->getContentSize().width / 6,
            _onSprite->getContentSize().height / 2));
    }
    if (_offLabel)
    {
        _offLabel->setPosition(Point(_offSprite->getPosition().x + _thumbSprite->getContentSize().width / 6,
            _offSprite->getContentSize().height / 2));
    }

    RenderTexture *rt = RenderTexture::create((int)_maskTexture->getContentSize().width, (int)_maskTexture->getContentSize().height);

    rt->begin();
    _onSprite->visit();
    _offSprite->visit();

    if (_onLabel)
    {
        _onLabel->visit();
    }
    if (_offLabel)
    {
        _offLabel->visit();
    }

    rt->end();

    setTexture(rt->getSprite()->getTexture());
    setFlippedY(true);
}
Пример #12
0
Sprite* PhotoLayer::mask()
{
    assert(this->userHead);

    float uiSacle = 0.38f;
    uiSacle = 1;
    Sprite* userHeadSprite = (Sprite*)userHead->getVirtualRenderer();
    Sprite* textureSprite = Sprite::createWithSpriteFrame(userHeadSprite->getSpriteFrame());
    Point userHeadPos = Node::convertToWorldSpaceAR(this->userHead->getPosition());
    Point texturePosition = this->convertToNodeSpace(userHeadPos);
    textureSprite->setPosition(texturePosition);
    textureSprite->setScale(mscale * uiSacle);
    textureSprite->setRotation(this->userHead->getRotation());
    
    Sprite* maskSprite = Sprite::create("face/mask.png");
    maskSprite->setPosition(Point(512, 384));
    // 这里是为了适配动画里的骨骼大小,大概是114*121,UI层就做放大处理了。
    maskSprite->setScale(uiSacle);

    // Size size = Director::getInstance()->getVisibleSize();
    Size size = Director::getInstance()->getWinSize();
    RenderTexture* rt = RenderTexture::create(size.width, size.height, Texture2D::PixelFormat::RGBA8888);
    
    BlendFunc maskBlendFunc = { GL_ONE, GL_ZERO };
    maskSprite->setBlendFunc(maskBlendFunc);
    BlendFunc textureBlendFunc = { GL_DST_ALPHA, GL_ZERO };
    textureSprite->setBlendFunc(textureBlendFunc);
    
    rt->begin();
    maskSprite->visit();
    textureSprite->visit();
    rt->end();
    
    Sprite* retval = Sprite::createWithTexture(rt->getSprite()->getTexture());
    return retval;
}
Пример #13
0
bool ShopLayer::init(Ref* pSender, SHOP_TYPE TYPE){
    if (!Layer::init()) {
        return false;
    }
    auto winSize = Director::getInstance()->getWinSize();
    auto visibleSize = Director::getInstance()->getVisibleSize();
    auto Origin = Director::getInstance()->getVisibleOrigin();
  
    controllNode = Node::create();
    this->addChild(controllNode,1);
    
    SpriteFrameCache::getInstance()->addSpriteFramesWithFile("ui_shop.plist");
 
    
    auto ShopBg = Sprite::createWithSpriteFrameName("shop_bg.png");
    ShopBg->setAnchorPoint(Point::ANCHOR_MIDDLE_RIGHT);
    ShopBg->setPosition(Point(winSize.width,Origin.y+visibleSize.height/2));
    controllNode->addChild(ShopBg,0);
    
    m_shopGiftLayer = shopGiftLayer::create();
    m_shopGiftLayer->setPosition(Point::ZERO);
    m_shopGiftLayer->setVisible(false);
    
    m_shopPowerLayer = shopPowerLayer::create();
    m_shopPowerLayer->setPosition(Point::ZERO);
    m_shopPowerLayer->setVisible(false);
    
    m_shopTipLayer = shopTipLayer::create();
    m_shopTipLayer->setPosition(Point::ZERO);
    m_shopTipLayer->setVisible(false);
    
    controllNode->addChild(m_shopTipLayer,2);
    controllNode->addChild(m_shopPowerLayer,2);
    controllNode->addChild(m_shopGiftLayer,2);
    
    auto giftBtn2 = MenuItemSprite::create(Sprite::createWithSpriteFrameName("shop_tab_gift1.png"),
                                           Sprite::createWithSpriteFrameName("shop_tab_gift1.png"),
                                           CC_CALLBACK_0(ShopLayer::changeToGiftCallFunc, this));
    
    giftBtn2->setAnchorPoint(Point::ANCHOR_MIDDLE_RIGHT);
    giftBtn2->setPosition(Point(winSize.width-508,winSize.height/2+100));
    
    auto powerBtn2 = MenuItemSprite::create(Sprite::createWithSpriteFrameName("shop_tab_energy1.png"),
                                           Sprite::createWithSpriteFrameName("shop_tab_energy1.png"),
                                           CC_CALLBACK_0(ShopLayer::changeToPowerCallFunc, this));
    
    powerBtn2->setAnchorPoint(Point::ANCHOR_MIDDLE_RIGHT);
    powerBtn2->setPosition(Point(winSize.width-508,winSize.height/2));

    auto tipBtn2 = MenuItemSprite::create(Sprite::createWithSpriteFrameName("shop_tab_tip1.png"),
                                           Sprite::createWithSpriteFrameName("shop_tab_tip1.png"),
                                           CC_CALLBACK_0(ShopLayer::changeTpTipCallFunc, this));
    
    tipBtn2->setAnchorPoint(Point::ANCHOR_MIDDLE_RIGHT);
    tipBtn2->setPosition(Point(winSize.width-508,winSize.height/2-100));
    

    
    auto menu = Menu::create(giftBtn2,powerBtn2,tipBtn2,NULL);
    menu->setPosition(Point::ZERO);
    controllNode->addChild(menu,1);
    
    SpriteFrameCache::getInstance()->addSpriteFramesWithFile("ui_common.plist");
    auto closeBtn = MenuItemSprite::create(Sprite::createWithSpriteFrameName("common_btn_close.png"),
                                           Sprite::createWithSpriteFrameName("common_btn_close.png"),
                                           CC_CALLBACK_0(ShopLayer::pushLayer, this));
    
    CCLOG("asfasfasdfasdfadsf");
    //closeBtn->setAnchorPoint(Point::ANCHOR_MIDDLE_RIGHT);
    closeBtn->setPosition(Point(winSize.width-150,
                                       winSize.height/2+200));
    
    auto frontMenu = Menu::create(closeBtn, NULL);
    frontMenu->setPosition(Point::ZERO);
    controllNode->addChild(frontMenu, 9);
    
    
    
    
    switch (TYPE) {
        case SHOP_TYPE_GOLD:
            break;
            m_shopGiftLayer->setVisible(true);
            case SHOP_TYPE_POWER:
            m_shopPowerLayer->setVisible(true);
            break;
            case SHOP_TYPE_TIP:
            m_shopTipLayer->setVisible(true);
            break;
        case SHOP_TYPE_WITHOUTTIP:
            m_shopGiftLayer->setVisible(true);
            tipBtn2->setVisible(false);
    }
    
    
    controllNode->setPosition(Origin+Point(visibleSize.width/2,visibleSize.height+100));
    
	//截图
	RenderTexture* renderTexture = RenderTexture::create(winSize.width, winSize.height);
	renderTexture->retain();
	Scene *runningScene = CCDirector::getInstance()->getRunningScene();
	renderTexture->begin();
	runningScene->visit();
	renderTexture->end();
	//下面这句用来测试截图是否成功,经测试成功
	//renderTexture->saveToFile("123.png", Image::Format::PNG);
    
	//将截到的图做背景
	Sprite *_spr = Sprite::createWithTexture(renderTexture->getSprite()->getTexture());
	_spr->setPosition(Point(winSize.width / 2, winSize.height / 2));
	_spr->setFlippedY(true);  //翻转
	_spr->setColor(Color3B::GRAY);  //颜色(变灰暗)
	this->addChild(_spr, 0, BGTAG);
    
    auto moveTo = MoveTo::create(0.5f, Point::ZERO);
    
	auto easeBackInOut = EaseBackInOut::create(moveTo);
    //auto pasueAction = Sequence::create(easeBackInOut,CallFunc::create(CC_CALLBACK_0(ShopLayer::pauseCallFunc, this)), NULL);
    controllNode->runAction(easeBackInOut);
    
    return true;
}
Пример #14
0
void TransitionCrossFade::onEnter()
{
    TransitionScene::onEnter();

    // create a transparent color layer
    // in which we are going to add our rendertextures
    Color4B  color(0,0,0,0);
    Size size = _director->getWinSize();
    LayerColor* layer = LayerColor::create(color);

    // create the first render texture for inScene
    RenderTexture* inTexture = RenderTexture::create((int)size.width, (int)size.height,Texture2D::PixelFormat::RGBA8888,GL_DEPTH24_STENCIL8);

    if (nullptr == inTexture)
    {
        return;
    }

    inTexture->getSprite()->setAnchorPoint( Vec2::ANCHOR_MIDDLE );
    inTexture->setPosition(size.width/2, size.height/2);
    inTexture->setAnchorPoint( Vec2::ANCHOR_MIDDLE );

    // render inScene to its texturebuffer
    inTexture->begin();
    _inScene->visit();
    inTexture->end();

    // create the second render texture for outScene
    RenderTexture* outTexture = RenderTexture::create((int)size.width, (int)size.height,Texture2D::PixelFormat::RGBA8888,GL_DEPTH24_STENCIL8);
    outTexture->getSprite()->setAnchorPoint( Vec2::ANCHOR_MIDDLE );
    outTexture->setPosition(size.width/2, size.height/2);
    outTexture->setAnchorPoint( Vec2::ANCHOR_MIDDLE );

    // render outScene to its texturebuffer
    outTexture->begin();
    _outScene->visit();
    outTexture->end();

    // create blend functions

    BlendFunc blend1 = {GL_ONE, GL_ONE}; // inScene will lay on background and will not be used with alpha
    BlendFunc blend2 = {GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA}; // we are going to blend outScene via alpha

    // set blendfunctions
    inTexture->getSprite()->setBlendFunc(blend1);
    outTexture->getSprite()->setBlendFunc(blend2);

    // add render textures to the layer
    layer->addChild(inTexture);
    layer->addChild(outTexture);

    // initial opacity:
    inTexture->getSprite()->setOpacity(255);
    outTexture->getSprite()->setOpacity(255);

    // create the blend action
    Action* layerAction = Sequence::create
    (
        FadeTo::create(_duration, 0),
        CallFunc::create(CC_CALLBACK_0(TransitionScene::hideOutShowIn,this)),
        CallFunc::create(CC_CALLBACK_0(TransitionScene::finish,this)),
        nullptr
    );


    // run the blend action
    outTexture->getSprite()->runAction( layerAction );

    // add the layer (which contains our two rendertextures) to the scene
    addChild(layer, 2, kSceneFade);
}
Пример #15
0
Sprite *HelloWorld::spriteWithColor1(Color4F color1, Color4F color2, float texWidth, float texHeight, int nStripes)
{
    RenderTexture *rt = RenderTexture::create(texWidth, texHeight);
    rt->beginWithClear(color1.r, color1.g, color1.b, color1.a);
    
    // 3: Draw into the texture
    // You'll add this later
    
    setGLProgramState(cocos2d::GLProgramState::getOrCreateWithGLProgramName(cocos2d::GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR_NO_MVP));
    
    
    _customCommand.init(_globalZOrder);
    _customCommand.func = [texWidth, texHeight, nStripes, color2, this]() {
        Vec2 *vertices = new Vec2[6*nStripes];
        Color4F *colors = new Color4F[6*nStripes];
        
        int nVertices = 0;
        float x1 = -texHeight;
        float x2;
        float y1 = texHeight;
        float y2 = 0;
        float dx = texWidth / nStripes * 2;
        float stripeWidth = dx/2;
        for (int i=0; i<nStripes; i++) {
            x2 = x1 + texHeight;
            
            vertices[nVertices] = Vec2 {x1, y1};
            colors[nVertices++] = Color4F{color2.r, color2.g, color2.b, color2.a};
            
            vertices[nVertices] = Vec2 {x1+stripeWidth, y1};
            colors[nVertices++] = Color4F {color2.r, color2.g, color2.b, color2.a};
            
            vertices[nVertices] = Vec2 {x2, y2};
            colors[nVertices++] = Color4F {color2.r, color2.g, color2.b, color2.a};
            
            vertices[nVertices] = vertices[nVertices-2];
            colors[nVertices++] = Color4F {color2.r, color2.g, color2.b, color2.a};
            
            vertices[nVertices] = vertices[nVertices-2];
            colors[nVertices++] = Color4F {color2.r, color2.g, color2.b, color2.a};
            
            vertices[nVertices] = Vec2 {x2+stripeWidth, y2};
            colors[nVertices++] = Color4F {color2.r, color2.g, color2.b, color2.a};
            x1 += dx;
        }
        
        getGLProgram()->use();
        getGLProgram()->setUniformsForBuiltins();
        
        cocos2d::GL::enableVertexAttribs(cocos2d::GL::VERTEX_ATTRIB_FLAG_POSITION | cocos2d::GL::VERTEX_ATTRIB_FLAG_COLOR);
        
//        glBindBuffer(GL_ARRAY_BUFFER, 0);
        glVertexAttribPointer(cocos2d::GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
        glVertexAttribPointer(cocos2d::GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_FLOAT, GL_TRUE, 0, colors);
        glDrawArrays(GL_TRIANGLES, 0, (GLsizei)nVertices);
        
        float gradientAlpha = 0.7f;
        nVertices = 0;
        
        vertices[nVertices] = Vec2 {0, 0};
        colors[nVertices++] = Color4F {0, 0, 0, 0 };
        vertices[nVertices] = Vec2 {texWidth, 0};
        colors[nVertices++] = Color4F {0, 0, 0, 0};
        vertices[nVertices] = Vec2 {0, texHeight};
        colors[nVertices++] = Color4F {0, 0, 0, gradientAlpha};
        vertices[nVertices] = Vec2 {texWidth, texHeight};
        colors[nVertices++] = Color4F {0, 0, 0, gradientAlpha};
        
        glVertexAttribPointer(cocos2d::GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
        glVertexAttribPointer(cocos2d::GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_FLOAT, GL_FALSE, 0, colors);

//        glBlendFunc(CC_BLEND_SRC, CC_BLEND_DST);
        cocos2d::GL::blendFunc(CC_BLEND_SRC, CC_BLEND_DST);
        glDrawArrays(GL_TRIANGLE_STRIP, 0, (GLsizei)nVertices);
        
        
        // layer 3: top highlight
        float borderHeight = texHeight/16;
        float borderAlpha = 0.3f;
        nVertices = 0;
        
        vertices[nVertices] = Vec2 {0, 0};
        colors[nVertices++] = Color4F {1, 1, 1, borderAlpha};
        
        vertices[nVertices] = Vec2 {texWidth, 0};
        colors[nVertices++] = Color4F {1, 1, 1, borderAlpha};
        
        vertices[nVertices] = Vec2 {0, borderHeight};
        colors[nVertices++] = Color4F {0, 0, 0, 0};
        
        vertices[nVertices] = Vec2 {texWidth, borderHeight};
        colors[nVertices++] = Color4F {0, 0, 0, 0};
        
        glVertexAttribPointer(cocos2d::GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
        glVertexAttribPointer(cocos2d::GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_FLOAT, GL_FALSE, 0, colors);
        glBlendFunc(CC_BLEND_SRC, CC_BLEND_DST);
        glDrawArrays(GL_TRIANGLE_STRIP, 0, (GLsizei)nVertices);
        
        CC_SAFE_DELETE_ARRAY(vertices);
        CC_SAFE_DELETE_ARRAY(colors);
        
    };
    
    auto renderer = Director::getInstance()->getRenderer();
    renderer->addCommand(&_customCommand);
    
    Sprite *noise = Sprite::create("Noise-iphone5hd.png");
    noise->setBlendFunc(BlendFunc {GL_DST_COLOR, GL_ZERO});
    noise->setPosition(texWidth/2, texHeight/2);
    noise->visit();
    
    rt->end();
    
    return Sprite::createWithTexture(rt->getSprite()->getTexture());

}