Example #1
0
CCTexture2D* Sky::generateTexture()
{
    CCRenderTexture* pRenderTexture = CCRenderTexture::create(m_nTextureSize, m_nTextureSize);
    pRenderTexture->beginWithClear(0.55f, 0.80f, 0.86f, 1.0f);
    
    //gradient
    float alpha = 1.0f;
    ccColor4B color1 = {255, 255, 255, 0};
    ccColor4B color2 = {255, 255, 255, 255};
    CCLayerGradient* pLayerColor = CCLayerGradient::create(color1, color2);
    pLayerColor->setContentSize(CCSizeMake(m_nTextureSize, m_nTextureSize));
    pLayerColor->setPosition(ccp(0, 0));
    pLayerColor->visit();
    
    //noise
    CCSprite* pSprite = CCSprite::create("noise.png");
    pSprite->setPosition(ccp(m_nTextureSize / 2, m_nTextureSize / 2));
    pSprite->setBlendFunc((ccBlendFunc){GL_DST_COLOR, GL_ZERO});
    pSprite->setScale(m_nTextureSize / 512.0f);
    pSprite->visit();
    
    pRenderTexture->end();
    CCTexture2D* pTex = pRenderTexture->getSprite()->getTexture();
    ccTexParams params = {GL_NEAREST, GL_NEAREST, GL_REPEAT, GL_REPEAT};
    pTex->setTexParameters(&params);
    return pTex;

}
CCSprite* CCSprite::createWithColor(ccColor4F bgColor, float textureWidth, float textureHeight)
{
    CCRenderTexture *rt = CCRenderTexture::create(textureWidth, textureHeight);
    rt->beginWithClear(bgColor.r, bgColor.g, bgColor.b, bgColor.a);
    rt->end();
    return CCSprite::createWithTexture(rt->getSprite()->getTexture());
}
Example #3
0
CCSprite *Puzzle::spriteImageMask() {
	
    // 1: Create new CCRenderTexture
    CCRenderTexture *rt = CCRenderTexture::renderTextureWithWidthAndHeight(Puzzle::canvasSize.width, Puzzle::canvasSize.height);
	
    // 2: Call CCRenderTexture:begin
    rt->beginWithClear(0, 0, 0, 0);
	
    // 3: Draw into the texture
	CCPoint vertices[4];
	
	glColor4ub(255, 255, 255, 255);
	for (int i = 0; i < numtaken; i++) {
		vertices[0] = ccp(taken[i].x, taken[i].y);
		vertices[1] = ccp(taken[i].x, taken[i].y + dimY);
		vertices[2] = ccp(taken[i].x + dimX, taken[i].y + dimY);
		vertices[3] = ccp(taken[i].x + dimX, taken[i].y);
		
		ccDrawPoly(vertices, 4, true, true);
	}
	
    // 4: Call CCRenderTexture:end
    rt->end();
	
    // 5: Create a new Sprite from the texture
	CCSprite *ret = CCSprite::spriteWithTexture(rt->getSprite()->getTexture());
	ret->setFlipY(true);
    return ret;
}
//label 描边
void LabelUtils::createStroke(CCLabelTTF *label, float size, ccColor3B color)
{
    CCSize originalSize = label->getTexture()->getContentSize();
    float wid = originalSize.width + size * 2;
    float hei = originalSize.height + size * 2;
    CCPoint originalPos = label->getPosition();
    CCPoint originalAnchor = label->getAnchorPoint();
    ccColor3B originalColor = label->getColor();
    ccBlendFunc originalBlend = label->getBlendFunc();
    CCNode* node = label->getChildByTag(TAG_STROKE);
    CCRenderTexture* rt = NULL;
    if(node) {
        rt = dynamic_cast<CCRenderTexture*>(node);
        if(rt) {
//            rt->clear(0, 0, 0, 0);        //这里有问题,会有一个黑色块
//            CCLOG("use pre-existing CCrenderTexture!");
            rt->removeFromParentAndCleanup(true);
            rt = NULL;
        }
    }
    if(!rt) {
        rt = CCRenderTexture::create(wid, hei);
    }
    label->setColor(color);
    label->setBlendFunc((ccBlendFunc) {
        GL_SRC_ALPHA, GL_ONE
    });

    //    rt->setAnchorPoint(originalAnchor); //没用
    CCPoint center = ccp(wid * originalAnchor.x, hei * originalAnchor.y);
    rt->beginWithClear(0, 0, 0, 0);
    for(int i=0; i<360; i+= 15) {
        float radians = CC_DEGREES_TO_RADIANS(i);
        label->cocos2d::CCNode::setPosition(center.x + sin(radians)*size, center.y + cos(radians)*size);
        label->visit();
    }
    rt->end();

    label->setPosition(originalPos);
    label->setColor(originalColor);
    label->setBlendFunc(originalBlend);

    float rtX = originalSize.width / 2 + size;
    float rtY = originalSize.height / 2 - size;
    rt->setPosition(rtX, rtY);

    if(rt->getParent() == NULL) label->addChild(rt, -100, TAG_STROKE);

    //测试
    //    CCLayerColor* layer = CCLayerColor::create(ccc4(0, 0, 0, 128), wid, hei);
    //    layer->setPosition(rt->getPosition());
    //    label->addChild(layer);
}
KDvoid RenderTextureTestDepthStencil::onEnter ( KDvoid )
{
	TestRenderTexture::onEnter ( );

    const CCSize&  s = this->getContentSize ( );

    CCSprite*  pSprite = CCSprite::create ( "Images/fire.png" );
    pSprite->setPosition( ccp ( s.cx * 0.25f, 0 ) );
    pSprite->setScale ( 10 );
    CCRenderTexture*  pRend = CCRenderTexture::create ( (KDuint) s.cx, (KDuint) s.cy, kCCTexture2DPixelFormat_RGBA4444, CC_GL_DEPTH24_STENCIL8 );

    glStencilMask ( 0xFF );
    pRend->beginWithClear ( 0, 0, 0, 0, 0, 0 );

    //! mark sprite quad into stencil buffer
    glEnable	  ( GL_STENCIL_TEST );
    glStencilFunc ( GL_ALWAYS, 1, 0xFF );
    glStencilOp	  ( GL_KEEP, GL_KEEP, GL_REPLACE);
    glColorMask	  ( 0, 0, 0, 1 );
    pSprite->visit ( );

    //! move sprite half width and height, and draw only where not marked
    pSprite->setPosition 
	(
		ccpAdd
		(
			pSprite->getPosition ( ), 
			ccpMult 
			(
				ccp 
				(
					pSprite->getContentSize ( ).cx * pSprite->getScale ( ), 
					pSprite->getContentSize ( ).cy * pSprite->getScale ( ) 
				),
				0.5f
			)
		)
	);
    glStencilFunc ( GL_NOTEQUAL, 1, 0xFF );
    glColorMask ( 1, 1, 1, 1 );
    pSprite->visit ( );

    pRend->end ( );

    glDisable ( GL_STENCIL_TEST );

    pRend->setPosition ( ccpMid ( s ) );

    this->addChild ( pRend );	
}
Example #6
0
void ADAds::fillBanner(Banner* banner)
{
#if defined(_DEBUG) && CC_TARGET_PLATFORM == CC_PLATFORM_WIN32
    CCRenderTexture* texture = CCRenderTexture::create(100, 100);
    texture->beginWithClear(0.5f, 0.5f, 0.5f, 1);

    texture->end();

    CCSprite* s = CCSprite::createWithTexture(texture->getSprite()->getTexture());

    s->setScaleX(banner->getContentSize().width/s->getContentSize().width);
    s->setScaleY(banner->getContentSize().height/s->getContentSize().width);
    banner->addChild(s);
    s->setAnchorPoint(ccp(0,0));
    s->setPosition(ccp(0,0));
#endif

    if(_home_banners.size())
    {
        //std::random_shuffle(_home_banners.begin(), _home_banners.end());
        CustomBanner* home_ads = getCustomBanner();

        CCMenuItem* item = CCMenuItem::create(
                               home_ads, menu_selector(CustomBanner::onClick));


        CCMenu* menu = CCMenu::create();
        menu->addChild(item);

        banner->addChild(menu, 0, HOME_ADS_NODE_TAG);
        menu->setAnchorPoint(ccp(0,0));

        menu->setPosition(ccp(0,0));

        CCNode* banner_content = home_ads->getBanner();
        CCSize content_size = banner_content->getContentSize();
        CCSize zone_size = banner->getContentSize();

        float scale = MIN(zone_size.width/content_size.width,
                          zone_size.height/content_size.height);
        banner_content->setScale(scale);
        banner_content->setAnchorPoint(ccp(0, 0));
        banner_content->setPosition(ccp(0,0));

        item->setContentSize(content_size*scale);
        item->setAnchorPoint(ccp(0.5f,0.5f));
        item->setPosition(zone_size*0.5f);
        item->addChild(banner_content);
    }
}
CCSprite* EXDynamicTimeLayer::spriteWithColor(float alpha)
{
    CCRenderTexture* rt = CCRenderTexture::create(mBackgroundArea.width, mBackgroundArea.height, kCCTexture2DPixelFormat_RGBA8888);
    ccColor4F fColor = {0.0f,0.0f,0.0f,alpha};
    
    if (rt)
    {
        rt->beginWithClear(fColor.r, fColor.g, fColor.b, fColor.a);
        rt->end();
        
        return CCSprite::createWithTexture(rt->getSprite()->getTexture());
    }
    
    return NULL;
}
Example #8
0
void GameLayer::generateBackground() {
    int textureSize = 512;
	ccColor4F c = ccc4f(140.0f/255.0f,205.0f/255.0f,221.0f/255.0f, 255.0f);
    CCRenderTexture * rt = CCRenderTexture::create(textureSize, textureSize);
    rt->beginWithClear(c.r, c.g, c.b, c.a);
    rt->end();
    
    setBackground(rt->getSprite());
    background->removeFromParentAndCleanup(true);
    
    background->setPosition(ccp(screenW/2,screenH/2));
    ccTexParams tp = {GL_LINEAR, GL_LINEAR, GL_REPEAT, GL_REPEAT};
    background->getTexture()->setTexParameters(&tp);
    
    addChild(background);
    return;
}
Example #9
0
//This is the old function
cocos2d::CCSprite* HelloWorld::spriteWithColor(cocos2d::ccColor4F bgColor, float textureWidth, float textureHeight)
{
    // 1: Create new CCRenderTexture
    CCRenderTexture *rt = CCRenderTexture::create(textureWidth, textureHeight);
    
    // 2: Call CCRenderTexture:begin
    rt->beginWithClear(bgColor.r, bgColor.g, bgColor.b, bgColor.a);
    
    this->setShaderProgram(CCShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionColor));
    
    CC_NODE_DRAW_SETUP();
    
    // 3: Draw into the texture
    float gradientAlpha = 0.7f;
    CCPoint vertices[4];
    ccColor4F colors[4];
    int nVertices = 0;
    
    vertices[nVertices] = ccp(0, 0);
    colors[nVertices++] = (ccColor4F){0, 0, 0, 0 };
    vertices[nVertices] = ccp(textureWidth, 0);
    colors[nVertices++] = (ccColor4F){0, 0, 0, 0};
    vertices[nVertices] = ccp(0, textureHeight);
    colors[nVertices++] = (ccColor4F){0, 0, 0, gradientAlpha};
    vertices[nVertices] = ccp(textureWidth, textureHeight);
    colors[nVertices++] = (ccColor4F){0, 0, 0, gradientAlpha};
    
    ccGLEnableVertexAttribs(kCCVertexAttribFlag_Position  | kCCVertexAttribFlag_Color);
    
    glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, vertices);
    glVertexAttribPointer(kCCVertexAttrib_Color, 4, GL_FLOAT, GL_FALSE, 0, colors);
    glBlendFunc(CC_BLEND_SRC, CC_BLEND_DST);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, (GLsizei)nVertices);
    
    CCSprite *noise = CCSprite::create("Noise.png");
    ccBlendFunc bf = {GL_DST_COLOR, GL_ZERO};
    noise->setBlendFunc(bf);
    noise->setPosition(ccp(textureWidth/2, textureHeight/2));
    noise->visit();
    
    // 4: Call CCRenderTexture:end
    rt->end();
    
    // 5: Create a new Sprite from the texture
    return CCSprite::createWithTexture(rt->getSprite()->getTexture());    
}
Example #10
0
CAImage* CAProgress::getImage(CAImage* image)
{
    CCRect rect;
    rect.origin = ccpSub(ccpMult(image->getContentSize(), 0.5f), CCPoint(0.5f, 0.5f));
    rect.size = CCSize(1, 1);
    
	CAScale9ImageView *scale9Image = CAScale9ImageView::createWithImage(rect, image);
    scale9Image->setAnchorPoint(CCPointZero);
	scale9Image->setPreferredSize(this->getBounds().size);
    this->addSubview(scale9Image);
    
	CCRenderTexture* render = CCRenderTexture::create(this->getBounds().size.width, this->getBounds().size.height, kCAImagePixelFormat_RGBA8888);
	render->beginWithClear(0, 0, 0, 0);
    scale9Image->visit();
	render->end();
    scale9Image->removeFromSuperview();
	return render->getSprite()->getImage();
}
Example #11
0
CAImage* CASwitch::getImage(CAImage* image, CCSize size)
{
	CAScale9ImageView *scale9Image = CAScale9ImageView::createWithImage(image);
    CCRect insetRect;
    insetRect.origin = scale9Image->getBounds().size / 2;
    insetRect.origin = ccpSub(insetRect.origin, CCPoint(1, 1));
    insetRect.size = CCPoint(2, 2);
    scale9Image->setCapInsets(insetRect);
    scale9Image->setAnchorPoint(CCPointZero);
	scale9Image->setFrame(CCRect(0, 0, size.width, size.height));
    this->addSubview(scale9Image);
    
	CCRenderTexture* render = CCRenderTexture::create(size.width, size.height, kCAImagePixelFormat_RGBA8888);
	render->beginWithClear(0, 0, 0, 0);
    scale9Image->visit();
	render->end();
    scale9Image->removeFromSuperview();
	return render->getSprite()->getImage();
}
Example #12
0
CAImage* CAProgress::getImage(CAImage* image)
{
	CCScale9Sprite *scale9Image = CCScale9Sprite::createWithImage(image);
    CCRect rect;
    rect.origin = scale9Image->getFrame().size/2;
    rect.origin = ccpSub(rect.origin, CCPoint(0.5f, 0.5f));
    rect.size = CCSize(1, 1);
    scale9Image->setCapInsets(rect);
	scale9Image->setPreferredSize(this->getBounds().size);
    scale9Image->setAnchorPoint(CCPointZero);
    this->addSubview(scale9Image);
    
	CCRenderTexture* render = CCRenderTexture::create(this->getBounds().size.width, this->getBounds().size.height, kCCTexture2DPixelFormat_RGBA8888);
	render->beginWithClear(0, 0, 0, 0);
    scale9Image->visit();
	render->end();
    scale9Image->removeFromSuperview();
	return render->getSprite()->getImage();
}
void Main::offscreenRendering(const char* listName, const char* atlasName, const char* spriteName){
    CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
    
    CCSpriteFrameCache* cache = CCSpriteFrameCache::sharedSpriteFrameCache();
    cache->addSpriteFramesWithFile(listName, atlasName);
    
    CCSprite* sprite = CCSprite::createWithSpriteFrameName(spriteName);
    
    CCRenderTexture* renderTexture = CCRenderTexture::create(visibleSize.width, visibleSize.height);
    renderTexture->setPosition(ccp(visibleSize.width/2, visibleSize.height/2));
    this->addChild(renderTexture);
    
    renderTexture->beginWithClear(0, 0, 1, 1);
    for(int i= 0; i < 10000; i++){
        sprite->setPosition(ccp( rand() % (int)visibleSize.width, rand() % (int)visibleSize.height ));
        sprite->visit();
    }
    renderTexture->end();
}
void Main::offScreenRenderingAndBatching(const char* listName, const char* atlasName, const char* spriteName){
    CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
    
    CCSpriteFrameCache* cache = CCSpriteFrameCache::sharedSpriteFrameCache();
    cache->addSpriteFramesWithFile(listName, atlasName);
    
    CCTexture2D* texture2D = CCTextureCache::sharedTextureCache()->addImage(atlasName);
    CCSpriteBatchNode* batchNode = CCSpriteBatchNode::createWithTexture(texture2D);
    this->addChild(batchNode, 0);
    
    //render
    CCRenderTexture* renderTexture = CCRenderTexture::create(visibleSize.width, visibleSize.height);
    renderTexture->setPosition(ccp(visibleSize.width/2, visibleSize.height/2));
    this->addChild(renderTexture);
    
    //オフスクリーンレンダリング
    renderTexture->beginWithClear(0, 0, 1, 1);
    for(int i=0; i < 150000; i++){
        CCSprite* sprite = CCSprite::createWithSpriteFrameName(spriteName);
        sprite->setPosition(ccp( rand() % (int)visibleSize.width, rand() % (int)visibleSize.height ));
        sprite->visit();
    }
    renderTexture->end();
}
void GameLayer::generateBackground() {
	
	//CCSize textureSize = CCSizeMake(screenW, screenH);
    int textureSize = 512;
    
	ccColor3B c = (ccColor3B){140,205,221};
	//ccColor3B c = generateDarkColor();
    ccColor4F cf = ccc4FFromccc3B(c);
    
    //CCRenderTexture *rt = [CCRenderTexture renderTextureWithWidth:textureSize.width height:textureSize.height];
    CCRenderTexture * rt = CCRenderTexture::renderTextureWithWidthAndHeight(textureSize, textureSize);
    //[rt beginWithClear:(float)c.r/256.0f g:(float)c.g/256.0f b:(float)c.b/256.0f a:1];
    rt->beginWithClear(cf.r, cf.g, cf.b, cf.a);
    
	// layer 1: gradient
    
	float gradientAlpha = 0.25f;
    
	glDisable(GL_TEXTURE_2D);
	glDisableClientState(GL_TEXTURE_COORD_ARRAY);
	
    CCPoint vertices[4];
	ccColor4F colors[4];
    int nVertices = 0;
	
    vertices[nVertices] = ccp(0, 0);
    colors[nVertices++] = (ccColor4F){1, 1, 1, 0};
    vertices[nVertices] = ccp(textureSize, 0);
    colors[nVertices++] = (ccColor4F){1, 1, 1, 0};
    vertices[nVertices] = ccp(0, textureSize);
    colors[nVertices++] = (ccColor4F){1, 1, 1, gradientAlpha};
    vertices[nVertices] = ccp(textureSize, textureSize);
    colors[nVertices++] = (ccColor4F){1, 1, 1, gradientAlpha};
	
    glVertexPointer(2, GL_FLOAT, 0, vertices);
	glColorPointer(4, GL_FLOAT, 0, colors);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, (GLsizei)nVertices);
	
	glEnableClientState(GL_TEXTURE_COORD_ARRAY);
	glEnable(GL_TEXTURE_2D);	
    
	// layer 2: noise
	
	//CCSprite *s = [CCSprite spriteWithFile:@"noise.png"];
    CCSprite *s = CCSprite::spriteWithFile("noise.png");
    
    CCSize winSize = CCDirector::sharedDirector()->getWinSize();
    
	//[s setBlendFunc:(ccBlendFunc){GL_DST_COLOR, GL_ZERO}];
    s->setBlendFunc((ccBlendFunc){GL_DST_COLOR, GL_ZERO});
	//s.position = ccp(textureSize.width/2, textureSize.height/2);
    s->setPosition(ccp(textureSize/2, textureSize/2));
    //s.scale = (float)textureSize/512.0f;
    s->setScale((float)textureSize/512.0f);
    
    //s->setScaleX(winSize.width/s->getContentSize().width);
    //s->setScaleY(winSize.height/s->getContentSize().height);
    
    glColor4f(1,1,1,1);
	//[s visit];
	s->visit();
    //[rt end];
    rt->end();
    
    //self.background = [CCSprite spriteWithTexture:rt.sprite.texture];
    setBackground(CCSprite::spriteWithTexture(rt->getSprite()->getTexture()));
    ccTexParams tp = {GL_NEAREST, GL_NEAREST, GL_REPEAT, GL_REPEAT};
    //[_background.texture setTexParameters:&tp];
    getBackground()->getTexture()->setTexParameters(&tp);
    //_background.position = ccp(screenW/2,screenH/2);
    getBackground()->setPosition(ccp(screenW/2,screenH/2));
    //    _background.scale = 0.5f;
    
    getBackground()->setScaleX(winSize.width/getBackground()->getContentSize().width);
    getBackground()->setScaleY(winSize.height/getBackground()->getContentSize().height);
	
	//return [CCSprite spriteWithTexture:rt.sprite.texture];
    //return CCSprite::spriteWithTexture(rt->getSprite()->getTexture());
    
    addChild(getBackground());
}
Example #16
0
CCSprite *HelloWorld::spriteWithColor(ccColor4F c1, ccColor4F c2, float textureWidth, float textureHeight, int nStripes)
{
    CCRenderTexture *rt = CCRenderTexture::create(textureWidth, textureHeight);

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

    // 3: Draw into the texture
    this->setShaderProgram(CCShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionColor));
    CC_NODE_DRAW_SETUP();

    // Layer 1: Stripes
    CCPoint *vertices = new CCPoint[nStripes * 6];
    ccColor4F *colors = new ccColor4F[nStripes * 6];

    int nVertices = 0;
    float x1 = -textureHeight;
    float x2;
    float y1 = textureHeight;
    float y2 = 0;
    float dx = textureWidth / nStripes * 2;
    float stripeWidth = dx / 2;
    for (int i = 0; i < nStripes; ++i)
    {
        x2  = x1 + textureHeight;

        vertices[nVertices] = ccp(x1, y1);
        colors[nVertices++] = ccc4f(c2.r, c2.g, c2.b, c2.a);

        vertices[nVertices] = ccp(x1 + stripeWidth, y1);
        colors[nVertices++] = ccc4f(c2.r, c2.g, c2.b, c2.a);

        vertices[nVertices] = ccp(x2, y2);
        colors[nVertices++] = ccc4f(c2.r, c2.g, c2.b, c2.a);

        vertices[nVertices] = vertices[nVertices - 2];
        colors[nVertices++] = ccc4f(c2.r, c2.g, c2.b, c2.a);

        vertices[nVertices] = vertices[nVertices - 2];
        colors[nVertices++] = ccc4f(c2.r, c2.g, c2.b, c2.a);

        vertices[nVertices] = ccp(x2 + stripeWidth, y2);
        colors[nVertices++] = ccc4f(c2.r, c2.g, c2.b, c2.a);
        x1 += dx;
    }

    ccGLEnableVertexAttribs(kCCVertexAttribFlag_Position | kCCVertexAttribFlag_Color);
    glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, vertices);
    glVertexAttribPointer(kCCVertexAttrib_Color, 4, GL_FLOAT, GL_TRUE, 0, colors);
    glDrawArrays(GL_TRIANGLES, 0, (GLsizei)nVertices);

    CC_SAFE_DELETE_ARRAY(vertices);
    CC_SAFE_DELETE_ARRAY(colors);

    // Layer 4: Noise
    CCSprite *noise = CCSprite::create("Noise.png");
    ccBlendFunc blendFunc = {GL_DST_COLOR, GL_ZERO};
    noise->setBlendFunc(blendFunc);
    noise->setPosition(ccp(textureWidth / 2, textureHeight / 2));
    noise->visit();

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

    // 5: Create a new Sprite from the texture
    return CCSprite::createWithTexture(rt->getSprite()->getTexture());
}
//--------------------------------------------------------
CCSprite * CRenderTextureLayer::Create()
{
	ccColor4F c1 = RandomOneBrightColor();
	ccColor4F c2 = RandomOneBrightColor();
	int nStripes = ((rand() % 4) + 1) * 2;

	// 第一步:创建 RenderTexture 对象
	CCRenderTexture *rt = CCRenderTexture::create(RENDER_TEX_WIDTH, RENDER_TEX_HEIGHT);

	// 第二步:调用 Begin 函数
	rt->beginWithClear(c1.r, c1.g, c1.b, c1.a);

	// 第三步:开始向纹理中绘制
	this->setShaderProgram(CCShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionColor));
	CC_NODE_DRAW_SETUP();

	// 创建顶点和顶点色
	CCPoint *vertices = new CCPoint[nStripes * 6];
	ccColor4F *colors = new ccColor4F[nStripes * 6];

	int nBoolOpenEffect = m_nStep % (CUR_EFFECT_NUM + 1);

	switch ( nBoolOpenEffect )
	{
	case 0:
		// 特效全部开启
		AddStripes( vertices, colors, nStripes, c1, c2 );
		AddGradient( vertices, colors );
		AddTopLight( vertices, colors );
		AddNoiseBlend( s_szRenderTexNoiseSprite );
		break;
	case 1:
		// 增加条纹
		AddStripes( vertices, colors, nStripes, c1, c2 );
		break;
	case 2:
		// 颜色渐变
		AddGradient( vertices, colors );
		break;
	case 3:
		// 增加顶部光照
		AddTopLight( vertices, colors );
		break;
	case 4:
		// 混合噪声图
		AddNoiseBlend( s_szRenderTexNoiseSprite );
		break;
	default:
		break;
	}

	// 释放顶点和定点色
	CC_SAFE_DELETE_ARRAY(vertices);
	CC_SAFE_DELETE_ARRAY(colors);

	// 第四步:调用 end 函数
	rt->end();

	// 下一次修改特效显示
	m_nStep++;

	// 第五步:通过纹理创建精灵
	return CCSprite::createWithTexture(rt->getSprite()->getTexture());
}
Example #18
0
cocos2d::CCSprite* HelloWorld::stripedSpriteWithColor1(cocos2d::ccColor4F c1, cocos2d::ccColor4F c2, float textureWidth, float textureHeight, int nStripes)
{
    // 1: Create new CCRenderTexture
    CCRenderTexture *rt = CCRenderTexture::create(textureWidth, textureHeight);
    
    // 2: Call CCRenderTexture:begin
    rt->beginWithClear(c1.r, c1.g, c1.b, c1.a);
    
    this->setShaderProgram(CCShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionColor));
    
    // 3: Draw into the texture
    
    // Layer 1: Stripes
    CCPoint* vertices = new CCPoint[nStripes*6];
    ccColor4F colors[nStripes*6];
    
    int nVertices = 0;
    float x1 = -textureHeight;
    float x2;
    float y1 = textureHeight;
    float y2 = 0;
    float dx = textureWidth / nStripes * 2;
    float stripeWidth = dx/2;
    
    for (int i=0; i<nStripes; i++)
    {
        x2 = x1 + textureHeight;
        
        vertices[nVertices] = ccp(x1, y1);
        colors[nVertices++] = (ccColor4F){c2.r, c2.g, c2.b, c2.a};
        
        vertices[nVertices] = ccp(x1+stripeWidth, y1);
        colors[nVertices++] = (ccColor4F){c2.r, c2.g, c2.b, c2.a};
        
        vertices[nVertices] = ccp(x2, y2);
        colors[nVertices++] = (ccColor4F){c2.r, c2.g, c2.b, c2.a};
        
        vertices[nVertices] = vertices[nVertices-2];
        colors[nVertices++] = (ccColor4F){c2.r, c2.g, c2.b, c2.a};
        
        vertices[nVertices] = vertices[nVertices-2];
        colors[nVertices++] = (ccColor4F){c2.r, c2.g, c2.b, c2.a};
        
        vertices[nVertices] = ccp(x2+stripeWidth, y2);
        colors[nVertices++] = (ccColor4F){c2.r, c2.g, c2.b, c2.a};
        x1 += dx;
    }
    
    this->setShaderProgram(CCShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionColor));
    
    // Layer 2: Noise
    CCSprite *noise = CCSprite::create("Noise.png");
    ccBlendFunc bf = {GL_DST_COLOR, GL_ZERO};
    noise->setBlendFunc(bf);
    noise->setPosition(ccp(textureWidth/2, textureHeight/2));
    noise->visit();
    
    // Layer 3: Stripes
    CC_NODE_DRAW_SETUP();
    glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, vertices);
    glVertexAttribPointer(kCCVertexAttrib_Color, 4, GL_FLOAT, GL_TRUE, 0, colors);
    glDrawArrays(GL_TRIANGLES, 0, (GLsizei)nVertices);
    
    float gradientAlpha = 0.7;
    
    nVertices = 0;
    
    vertices[nVertices] = ccp(0, 0);
    colors[nVertices++] = (ccColor4F){0, 0, 0, 0};
    
    vertices[nVertices] = ccp(textureWidth, 0);
    colors[nVertices++] = (ccColor4F){0, 0, 0, 0};
    
    vertices[nVertices] = ccp(0, textureHeight);
    colors[nVertices++] = (ccColor4F){0, 0, 0, gradientAlpha};
    
    vertices[nVertices] = ccp(textureWidth, textureHeight);
    colors[nVertices++] = (ccColor4F){0, 0, 0, gradientAlpha};
    
    glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, vertices);
    glVertexAttribPointer(kCCVertexAttrib_Color, 4, GL_FLOAT, GL_TRUE, 0, colors);
    glBlendFunc(CC_BLEND_SRC, CC_BLEND_DST);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, (GLsizei)nVertices);
    
    // layer 3: top highlight
    float borderHeight = textureHeight/16;
    float borderAlpha = 0.3f;
    nVertices = 0;
    
    vertices[nVertices] = ccp(0, 0);
    colors[nVertices++] = (ccColor4F){1, 1, 1, borderAlpha};
    
    vertices[nVertices] = ccp(textureWidth, 0);
    colors[nVertices++] = (ccColor4F){1, 1, 1, borderAlpha};
    
    vertices[nVertices] = ccp(0, borderHeight);
    colors[nVertices++] = (ccColor4F){0, 0, 0, 0};
    
    vertices[nVertices] = ccp(textureWidth, borderHeight);
    colors[nVertices++] = (ccColor4F){0, 0, 0, 0};
    
    glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, vertices);
    glVertexAttribPointer(kCCVertexAttrib_Color, 4, GL_FLOAT, GL_TRUE, 0, colors);
    glBlendFunc(CC_BLEND_SRC, CC_BLEND_DST);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, (GLsizei)nVertices);
    
    // 4: Call CCRenderTexture:end
    rt->end();
    
    delete[] vertices;
    
    // 5: Create a new Sprite from the texture
    return CCSprite::createWithTexture(rt->getSprite()->getTexture());
}
Example #19
0
CCSprite *HelloWorld::spriteWithColor(ccColor4F bgColor, float textureWidth, float textureHeight)
{
    CCRenderTexture *rt = CCRenderTexture::create(textureWidth,textureHeight);

    rt->beginWithClear(bgColor.r, bgColor.g, bgColor.b, bgColor.a);

    this->setShaderProgram(CCShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionColor));

    CC_NODE_DRAW_SETUP();

    // 3: Draw into the texture
    float gradientAlpha = 0.7f;
    CCPoint vertices[4];
    ccColor4F colors[4];
    int nVertices = 0;

    /**
      *矩形的四个顶点(0,0) (1,0) ,(0,1) ,(1,1)
      *
      */

    vertices[nVertices] = CCPointMake(0, 0);
    colors[nVertices++] = ccc4f(0, 0, 0, 0);
    vertices[nVertices] = CCPointMake(textureWidth, 0);
    colors[nVertices++] = ccc4f(0, 0, 0, 0);
    vertices[nVertices] = CCPointMake(0, textureHeight);
    colors[nVertices++] = ccc4f(0, 0, 0, gradientAlpha);
    vertices[nVertices] = CCPointMake(textureWidth, textureHeight);
    colors[nVertices++] = ccc4f(0, 0, 0, gradientAlpha);

    ccGLEnableVertexAttribs(kCCVertexAttribFlag_Position | kCCVertexAttribFlag_Color);

    glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, vertices);
    glVertexAttribPointer(kCCVertexAttrib_Color, 4, GL_FLOAT, GL_FALSE, 0, colors);
    glBlendFunc(CC_BLEND_SRC, CC_BLEND_DST);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, (GLsizei)nVertices);

	nVertices = 0;

	vertices[nVertices] = ccp(0, 0);
	colors[nVertices++] = ccc4f(0, 0, 0, 0);

	vertices[nVertices] = ccp(textureWidth, 0);
	colors[nVertices++] = ccc4f(0, 0, 0, 0);

	vertices[nVertices] = ccp(0, textureHeight);
	colors[nVertices++] = ccc4f(0, 0, 0, gradientAlpha);

	vertices[nVertices] = ccp(textureWidth, textureHeight);
	colors[nVertices++] = ccc4f(0, 0, 0, gradientAlpha);

	glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, vertices);
	glVertexAttribPointer(kCCVertexAttrib_Color, 4, GL_FLOAT, GL_TRUE, 0, colors);
	glBlendFunc(CC_BLEND_SRC, CC_BLEND_DST);
	glDrawArrays(GL_TRIANGLE_STRIP, 0, (GLsizei)nVertices);

	// Layer 3: top highlight
	float borderHeight = textureHeight / 16;
	float borderAlpha = 0.3f;
	nVertices = 0;

	vertices[nVertices] = ccp(0, 0);
	colors[nVertices++] = ccc4f(1, 1, 1, borderAlpha);

	vertices[nVertices] = ccp(textureWidth, 0);
	colors[nVertices++] = ccc4f(1, 1, 1, borderAlpha);

	vertices[nVertices] = ccp(0, borderHeight);
	colors[nVertices++] = ccc4f(0, 0, 0, 0);

	vertices[nVertices] = ccp(textureWidth, borderHeight);
	colors[nVertices++] = ccc4f(0, 0, 0, 0);

	glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, vertices);
	glVertexAttribPointer(kCCVertexAttrib_Color, 4, GL_FLOAT, GL_TRUE, 0, colors);
	glBlendFunc(CC_BLEND_SRC, CC_BLEND_DST);
	glDrawArrays(GL_TRIANGLE_STRIP, 0, (GLsizei)nVertices);


    CCSprite *noise = CCSprite::create("Noise.png");
    ccBlendFunc blendFunc = {GL_DST_COLOR, GL_ZERO};
    noise->setBlendFunc(blendFunc);
    noise->setPosition(ccp(textureWidth / 2, textureHeight / 2));
    noise->visit();

    rt->end();


    return CCSprite::createWithTexture(rt->getSprite()->getTexture());

}