CCRenderTexture* UILabelTTF::createStroke(CCLabelTTF* label, float size ,ccColor3B cor) { float RealSize = size; if(label == NULL || label->getTexture() == NULL) { return NULL; } label->setAnchorPoint(ccp(0.5, 0.5)); CCRenderTexture* rt = CCRenderTexture::create(label->getContentSize().width + RealSize*2, label->getContentSize().height + RealSize*2); rt->setAnchorPoint(ccp(0.0,0.0)); rt->setPosition(0,0); if(rt->getSprite() && rt->getSprite()->getTexture()) { rt->getSprite()->getTexture()->setAntiAliasTexParameters(); rt->getSprite()->setOpacityModifyRGB(true); } else { CCLog("UILabelTTF null texture"); } CCPoint originalPos = label->getPosition(); ccColor3B originalColor = label->getColor(); bool originalVisibility = label->isVisible(); label->setColor(cor); label->setVisible(true); ccBlendFunc originalBlend = label->getBlendFunc(); ccBlendFunc tBlendFunc = {GL_SRC_ALPHA, GL_ONE }; label->setBlendFunc(tBlendFunc); rt->clear(0, 0, 0, 0); CCPoint center = ccp(label->getContentSize().width/2 ,label->getContentSize().height /2 ); rt->begin(); label->setPosition(ccp(center.x, center.y - RealSize)); label->visit(); label->setPosition(ccp(center.x + 2*RealSize, center.y - 2*RealSize)); label->visit(); label->setPosition(ccp(center.x + RealSize, center.y)); label->visit(); label->setPosition(ccp(center.x + 2*RealSize, center.y - RealSize)); label->visit(); label->setColor(originalColor); label->setPosition(ccp(center.x,center.y)); label->visit(); rt->end(); label->setBlendFunc(originalBlend); label->setVisible(originalVisibility); label->setPosition(originalPos); // for test //char file[100]; //sprintf(file,"../../temp/%s_creat.png",label->getString()); //rt->saveToFile(file,kCCImageFormatPNG); return rt; }
void CCTransitionRadialCCW::onEnter() { CCTransitionScene::onEnter(); // create a transparent color layer // in which we are going to add our rendertextures CCSize size = CCDirector::sharedDirector()->getWinSize(); // create the second render texture for outScene CCRenderTexture *outTexture = CCRenderTexture::renderTextureWithWidthAndHeight((int)size.width, (int)size.height); if (NULL == outTexture) { return; } outTexture->getSprite()->setAnchorPoint(ccp(0.5f,0.5f)); outTexture->setPosition(ccp(size.width/2, size.height/2)); outTexture->setAnchorPoint(ccp(0.5f,0.5f)); // render outScene to its texturebuffer outTexture->clear(0,0,0,1); outTexture->begin(); m_pOutScene->visit(); outTexture->end(); // Since we've passed the outScene to the texture we don't need it. this->hideOutShowIn(); // We need the texture in RenderTexture. CCProgressTimer *outNode = CCProgressTimer::progressWithTexture(outTexture->getSprite()->getTexture()); // but it's flipped upside down so we flip the sprite outNode->getSprite()->setFlipY(true); // Return the radial type that we want to use outNode->setType(radialType()); outNode->setPercentage(100.f); outNode->setPosition(ccp(size.width/2, size.height/2)); outNode->setAnchorPoint(ccp(0.5f,0.5f)); // create the blend action CCAction * layerAction = CCSequence::actions ( CCProgressFromTo::actionWithDuration(m_fDuration, 100.0f, 0.0f), CCCallFunc::actionWithTarget(this, callfunc_selector(CCTransitionScene::finish)), NULL ); // run the blend action outNode->runAction(layerAction); // add the layer (which contains our two rendertextures) to the scene this->addChild(outNode, 2, kSceneRadial); }
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; }
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()); }
CCSprite* GameUtil::mergeSprite(CCSprite* pOrg1, CCSprite* pOrg2) { CCPoint ptOrg1(pOrg1->getPosition()); CCPoint ptOrg2(pOrg2->getPosition()); CCRenderTexture * rt = CCRenderTexture::create((int)pOrg1->getContentSize().width, (int)pOrg1->getContentSize().height); pOrg1->setPosition(ccp(pOrg1->getContentSize().width/2, pOrg1->getContentSize().height/2)); pOrg2->setPosition(ccp(pOrg2->getContentSize().width/2, pOrg2->getContentSize().height/2)); rt->begin(); pOrg1->visit(); pOrg2->visit(); rt->end(); pOrg1->setPosition(ptOrg1); pOrg2->setPosition(ptOrg2); CCSprite *retSprite = CCSprite::createWithTexture(rt->getSprite()->getTexture()); retSprite->setFlipY(true); //CCSize sisze1 = retSprite->getContentSize(); //CCSize size2 = retSprite->getTexture()->getContentSize(); // debugging : 파일로 저장 //string sPath = CCFileUtils::sharedFileUtils()->getWriteablePath(); //sPath.c_str(); //char szSave[MAX_PATH]; //sprintf(szSave, "%ssave.png", sPath.c_str()); //bool bSave = rt->saveToFile(szSave); return retSprite; }
void RenderTextureZbuffer::renderScreenShot() { CCRenderTexture *texture = CCRenderTexture::renderTextureWithWidthAndHeight(512, 512); if (NULL == texture) { return; } texture->setAnchorPoint(ccp(0, 0)); texture->begin(); this->visit(); texture->end(); CCSprite *sprite = CCSprite::spriteWithTexture(texture->getSprite()->getTexture()); sprite->setPosition(ccp(256, 256)); sprite->setOpacity(182); sprite->setFlipY(1); this->addChild(sprite, 999999); sprite->setColor(ccGREEN); sprite->runAction(CCSequence::actions(CCFadeTo::actionWithDuration(2, 0), CCHide::action(), NULL)); }
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(¶ms); return pTex; }
CCRenderTexture* createRenderTextureFromLabel(CCLabelTTF* label) { CCRenderTexture* render = CCRenderTexture::create(label->getContentSize().width, label->getContentSize().height); render->setPosition(label->getPosition()); ccBlendFunc newBlendFuc = { GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA }; render->getSprite()->setBlendFunc( newBlendFuc ); render->setZOrder(label->getZOrder() + 1); label->setVisible(false); label->getParent()->addChild(render); return render; }
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; }
//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()); }
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; }
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(); }
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(); }
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(); }
// CCTransitionProgress void CCTransitionProgress::onEnter() { CCTransitionScene::onEnter(); setupTransition(); // create a transparent color layer // in which we are going to add our rendertextures CCSize size = CCDirector::sharedDirector()->getWinSize(); // create the second render texture for outScene CCRenderTexture *texture = CCRenderTexture::renderTextureWithWidthAndHeight((int)size.width, (int)size.height); texture->getSprite()->setAnchorPoint(ccp(0.5f,0.5f)); texture->setPosition(ccp(size.width/2, size.height/2)); texture->setAnchorPoint(ccp(0.5f,0.5f)); // render outScene to its texturebuffer texture->clear(0, 0, 0, 1); texture->begin(); m_pSceneToBeModified->visit(); texture->end(); // Since we've passed the outScene to the texture we don't need it. if (m_pSceneToBeModified == m_pOutScene) { hideOutShowIn(); } // We need the texture in RenderTexture. CCProgressTimer *pNode = progressTimerNodeWithRenderTexture(texture); // create the blend action CCActionInterval* layerAction = (CCActionInterval*)CCSequence::create( CCProgressFromTo::create(m_fDuration, m_fFrom, m_fTo), CCCallFunc::create(this, callfunc_selector(CCTransitionProgress::finish)), NULL); // run the blend action pNode->runAction(layerAction); // add the layer (which contains our two rendertextures) to the scene addChild(pNode, 2, kCCSceneRadial); }
void TilemapRender::generateBackground() { ostringstream texturePath; texturePath<<"resources.pak/terrain/"; texturePath<<m_SetID<<"-14.png"; CCRenderTexture* rt = CCRenderTexture::create(64,64); rt->begin(); CCSprite* tile1 = CCSprite::create(texturePath.str().c_str()); tile1->setAnchorPoint(ccp(0,0)); tile1->setPosition(ccp(0,0)); tile1->visit(); rt->end(); CCTexture2D* texture = rt->getSprite()->getTexture(); ccTexParams tp = {GL_LINEAR, GL_LINEAR, GL_REPEAT, GL_REPEAT}; texture->setTexParameters(&tp); m_SpriteB->setTexture(texture); m_SpriteB->setTextureRect(CCRectMake(0, 0, m_MapWidth*64, m_MapHeight*64)); }
KDvoid RenderTextureZbuffer::renderScreenShot ( KDvoid ) { CCRenderTexture* pTexture = CCRenderTexture::create ( 512, 512 ); pTexture->setAnchorPoint ( ccp ( 0, 0 ) ); pTexture->begin ( ); this->visit ( ); pTexture->end ( ); CCSprite* pSprite = CCSprite::createWithTexture ( pTexture->getSprite ( )->getTexture ( ) ); pSprite->setPosition ( ccp ( 256, 256 ) ); pSprite->setOpacity ( 182 ); pSprite->setFlipY ( 1 ); pSprite->setColor ( ccGREEN ); pSprite->runAction ( CCSequence::create ( CCFadeTo::create ( 2, 0 ), CCHide::create ( ), KD_NULL ) ); this->addChild ( pSprite, 999999 ); }
void CCControlSwitchSprite::needsLayout() { m_pOnSprite->setPosition(ccp(m_pOnSprite->getContentSize().width / 2 + m_fSliderXPosition, m_pOnSprite->getContentSize().height / 2)); m_pOffSprite->setPosition(ccp(m_pOnSprite->getContentSize().width + m_pOffSprite->getContentSize().width / 2 + m_fSliderXPosition, m_pOffSprite->getContentSize().height / 2)); m_ThumbSprite->setPosition(ccp(m_pOnSprite->getContentSize().width + m_fSliderXPosition, m_pMaskTexture->getContentSize().height / 2)); if (m_pOnLabel) { m_pOnLabel->setPosition(ccp(m_pOnSprite->getPosition().x - m_ThumbSprite->getContentSize().width / 6, m_pOnSprite->getContentSize().height / 2)); } if (m_pOffLabel) { m_pOffLabel->setPosition(ccp(m_pOffSprite->getPosition().x + m_ThumbSprite->getContentSize().width / 6, m_pOffSprite->getContentSize().height / 2)); } CCRenderTexture *rt = CCRenderTexture::create((int)m_pMaskTexture->getContentSize().width, (int)m_pMaskTexture->getContentSize().height); rt->begin(); m_pOnSprite->visit(); m_pOffSprite->visit(); if (m_pOnLabel) { m_pOnLabel->visit(); } if (m_pOffLabel) { m_pOffLabel->visit(); } rt->end(); setTexture(rt->getSprite()->getTexture()); setFlipY(true); }
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()); }
CCSprite* GameUtil::maskedSpriteWithSprite(CCSprite* textureSprite, CCSprite* maskSprite, CCRect& rcFit, bool bCalcRc, char* szFile) { // store the original positions of both sprites CCPoint ptOrgT(textureSprite->getPosition()); CCPoint ptOrgM(maskSprite->getPosition()); // 1. Create a CCRenderTexture with the same width/height as the mask sprite. CCRenderTexture * rt = CCRenderTexture::create((int)maskSprite->getContentSize().width, (int)maskSprite->getContentSize().height); // 2. Positions the mask sprite and texture sprite so their bottom left is at 0,0. maskSprite->setPosition(ccp(maskSprite->getContentSize().width/2, maskSprite->getContentSize().height/2)); textureSprite->setPosition(ccp(textureSprite->getContentSize().width/2, textureSprite->getContentSize().height/2)); // 3. Sets up the blend functions for each sprite as discussed earlier. ccBlendFunc bfMask = ccBlendFunc(); bfMask.src = GL_ONE; bfMask.dst = GL_ZERO; maskSprite->setBlendFunc(bfMask); ccBlendFunc bfTexture = ccBlendFunc(); bfTexture.src = GL_DST_ALPHA; bfTexture.dst = GL_ZERO; textureSprite->setBlendFunc(bfTexture); // 4. Calls begin to start drawing into the CCRenderTexture, draws the mask and then the texture, // and then calls end to finalize drawing. rt->begin(); maskSprite->visit(); textureSprite->visit(); rt->end(); // restore the original sprite positions textureSprite->setPosition(ptOrgT); maskSprite->setPosition(ptOrgM); // 5. Creates a new sprite based on the CCRenderTexture’s sprite’s texture. // Flips it along the y-axis because the texture is upside down. CCSprite *retSprite = CCSprite::createWithTexture(rt->getSprite()->getTexture()); retSprite->setFlipY(true); CCSize sisze1 = retSprite->getContentSize(); CCSize size2 = retSprite->getTexture()->getContentSize(); if (bCalcRc) { // 투명 배경 중 실제 이미지의 영역 rcFit = GameUtil::getFitRect(retSprite); } // debugging : 파일로 저장 //string sPath = CCFileUtils::sharedFileUtils()->getWriteablePath(); //sPath.c_str(); //char szSave[MAX_PATH]; //sprintf(szSave, "%ssave.png", sPath.c_str()); //bool bSave = rt->saveToFile(szSave); if (NULL != szFile) { bool bRet = rt->saveToFile(szFile); if (!bRet) { retSprite->release(); return NULL; } } return retSprite; }
void AdventureLevelClass::loadResAndInit() { CCSize size = LcdAdapClass::sharedLCDADAP()->getWinSize(); CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("Themes/scene/stages_bg-hd.plist"); CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("Themes/scene/themescene1-hd.plist"); memset(comBuffer, 0, sizeof(comBuffer)); sprintf(comBuffer, "Themes/scene/stages_theme%d-hd.plist",GameStageConClass::sharedStageCon()->getGameStageIndex()); CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile(comBuffer); bgSprite = CCSprite::createWithSpriteFrameName("ss_bg.png"); CCSize bgSize = bgSprite->getContentSize(); // bgSprite->setPosition(ccp(size.width / 2, size.height / 2)); LcdAdapClass::sharedLCDADAP()->setAdaptorNodePos(bgSprite, ccp(size.width / 2, size.height / 2)); this->addChild(bgSprite,1); CCSprite* bgCN = CCSprite::createWithSpriteFrameName("ss_bg_CN.png"); bgCN->setPosition(ccp(bgSize.width/2, bgSize.height/2)); bgSprite->addChild(bgCN,1); if (GameStageConClass::sharedStageCon()->getGameStageIndex() != GAME_STAGE_TYPE_EXTRE) { memset(comBuffer, 0, sizeof(comBuffer)); sprintf(comBuffer, "ss_towers_%02d.png",(curPageIndex + 1)); levelTowerTypeSP = CCSprite::createWithSpriteFrameName(comBuffer);//default levelTowerTypeSP->setPosition(ccp(bgSize.width/2, bgSize.height * 18/100)); bgSprite->addChild(levelTowerTypeSP,3); levelNumSP = CCSprite::createWithSpriteFrameName("ss_waves_10.png");//default levelNumSP->setPosition(ccp(bgSize.width/2, bgSize.height * 84/100)); bgSprite->addChild(levelNumSP,3); CCSprite* leftCloud = CCSprite::createWithSpriteFrameName("ss_cloud.png"); CCSize cloudSize = leftCloud->getContentSize(); leftCloud->setPosition(ccp(bgSize.width / 2, bgSize.height / 2)); bgSprite->addChild(leftCloud,5); } ccColor4B color; color.a = 125; color.b = 0; color.g = 0; color.r = 0; scrollLayer = CCLayer::create();//default pos is (0,0) scrollLayer->setContentSize(CCSize(size.width * levelNum / 2, size.height)); bgSprite->addChild(scrollLayer,15); for (int i = 0; i < levelNum; i++) { gameLevelInfoStruct levelInfo = HUDClass::sharedHUD()->getGameCurLevelInfo((GAME_STAGE_TYPE)(GameStageConClass::sharedStageCon()->getGameStageIndex()), (i + 1)); memset(comBuffer, 0, sizeof(comBuffer)); sprintf(comBuffer, "ss_map%02d.png",(i + 1)); CCSprite* sprite = CCSprite::createWithSpriteFrameName(comBuffer); CCSize spriteSize = sprite->getContentSize(); //sprite->setPosition(ccp(size.width / 2, size.height / 2)); memset(comBuffer, 0, sizeof(comBuffer)); sprintf(comBuffer, "%s","gainhonor_1.png");//这里要加个判断,判断用户的荣誉 CCSprite* honorSP = NULL; if (levelInfo.UserGained > 0 && levelInfo.UserGained < 4) { sprintf(comBuffer, "gainhonor_%d.png",levelInfo.UserGained); honorSP = CCSprite::createWithSpriteFrameName(comBuffer); honorSP->setVisible(true);//要不要显示 } else { honorSP = CCSprite::createWithSpriteFrameName(comBuffer); honorSP->setVisible(false);//要不要显示 } honorSP->setPosition(ccp(spriteSize.width * 84/100, spriteSize.height * 22/100)); sprite->addChild(honorSP,1); CCSprite* allClearSP = CCSprite::createWithSpriteFrameName("gainhonor_4.png"); if (!levelInfo.isClearAllObject) { allClearSP->setVisible(false); } allClearSP->setPosition(ccp(spriteSize.width * 60/100, spriteSize.height * 22/100)); sprite->addChild(allClearSP,1); levelNormalArray->addObject(sprite);//normal CCLayerColor* layer = CCLayerColor::create(color); layer->setContentSize(spriteSize); ccBlendFunc blend = {GL_DST_ALPHA, GL_SRC_ALPHA}; layer->setBlendFunc(blend); CCRenderTexture* rt = CCRenderTexture::create(spriteSize.width, spriteSize.height); sprite->setPosition(ccp(spriteSize.width/2, spriteSize.height/2)); rt->begin(); sprite->visit(); layer->visit(); rt->end(); CCSprite* levelShade = CCSprite::createWithTexture(rt->getSprite()->getTexture()); levelShade->setFlipY(true); levelShade->setPosition(ccp((bgSize.width/2) * (i + 1), bgSize.height / 2)); levelShadeArray->addObject(levelShade); sprite->setPosition(ccp((bgSize.width/2) * (i + 1), bgSize.height / 2)); sprite->setVisible(false); scrollLayer->addChild(levelShade,1); scrollLayer->addChild(sprite,1); if (i == curPageIndex) {//加个判断,哪一个关卡点亮 curShadeLevelSprite = levelShade; curNormalLevelSprite = sprite; curShadeLevelSprite->setVisible(false); curNormalLevelSprite->setVisible(true); } } gameStartMenu = CCMenuItemImage::create("ss_play_normal_CN.png", "ss_play_pressed_CN.png", this, menu_selector(AdventureLevelClass::selMenuCallBack)); if (GameStageConClass::sharedStageCon()->checkIsCurLevelLocked((curPageIndex + 1))) { gameStartMenu->setNormalSpriteFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("ss_locked_CN.png")); gameStartMenu->setEnabled(false); } gameStartMenu->setTag(MENU_ID_START_TAG); gameStartMenu->setPosition(ccp(bgSize.width/2, bgSize.height * 8/ 100)); CCMenuItemImage* homeBtn = CCMenuItemImage::create("ss_back_normal.png", "ss_back_pressed.png", this, menu_selector(AdventureLevelClass::selMenuCallBack)); homeBtn->setTag(MENU_ID_HOME_TAG); homeBtn->setPosition(ccp(bgSize.width/25, bgSize.height * 95.5 / 100)); CCMenuItemImage* helpBtn = CCMenuItemImage::create("ss_help_normal.png", "ss_help_pressed.png", this, menu_selector(AdventureLevelClass::selMenuCallBack)); helpBtn->setTag(MENU_ID_HELP_TAG); helpBtn->setPosition(ccp(bgSize.width * 24/25, bgSize.height * 95.5 / 100)); CCMenu* menu = CCMenu::create(homeBtn,helpBtn,gameStartMenu,NULL); menu->setPosition(CCPointZero); bgSprite->addChild(menu,2); loadWaitSprite->removeFromParentAndCleanup(true); // this->gotoPageIndexNow(curPageIndex); scrollLayer->setPosition(ccp((bgSize.width/2) * (-curPageIndex), 0)); }
// Helper bool CCLabelTTF::updateTexture() { //if(this->isFlipY()) // this->setFlipY(false); CCTexture2D *tex; tex = new CCTexture2D(); if (!tex) return false; #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) ccFontDefinition texDef = _prepareTextDefinition(true); tex->initWithString( m_string.c_str(), &texDef ); #else tex->initWithString( m_string.c_str(), m_pFontName->c_str(), m_fFontSize * CC_CONTENT_SCALE_FACTOR(), CC_SIZE_POINTS_TO_PIXELS(m_tDimensions), m_hAlignment, m_vAlignment); #endif // set the texture this->setTexture(tex); // release it tex->release(); // set the size in the sprite CCRect rect =CCRectZero; rect.size = m_pobTexture->getContentSize(); this->setTextureRect(rect); /***************************给文字描黑边begin*********************************/ CCPoint prePos = this->getPosition(); ccColor3B color1 = this->getColor(); float strokeValue = 2; CCSize textureSize = this->getContentSize(); textureSize.width += 2 * strokeValue; textureSize.height += 2 * strokeValue; /* 监测OpenGl的错误状态 */ glGetError(); /* 创建一张纹理画布 */ CCRenderTexture *rt = CCRenderTexture::create(textureSize.width, textureSize.height); if(!rt) { CCLog("create render texture failed !!!!"); //addChild(label); return 0; } /* 设置描边的颜色 */ this->setColor(ccBLACK); /* *拿到源文字的混色机制,存储以备恢复,并设置新的目标混色机制 *混色机制设为:源颜色透明度(影响亮度)和目标颜色(影响颜色) */ ccBlendFunc originalBlend = this->getBlendFunc(); ccBlendFunc func = { GL_SRC_ALPHA, GL_ONE}; this->setBlendFunc(func); CCPoint anchPoint = this->getAnchorPoint(); /* 这是自定义的一些调整,倾斜了一点 */ this->setAnchorPoint(CCPoint(0.5, 0.5)); //label->setRotationX(15); /* 张开画布,开始绘画 */ rt->begin(); this->setFlipY(true); for(int i = 0; i < 360; i += 5)//每变化5度绘制一张 { float r = CC_DEGREES_TO_RADIANS(i); //度数格式的转换 this->setPosition(CCPoint(textureSize.width * 0.5f + sin(r) * strokeValue,textureSize.height * 0.5f + cos(r) * strokeValue)); /* CCRenderTexture的用法,在begin和end之间visit的纹理,都会画在CCRenderTexture里面 */ this->visit();//画了一次该label } /* 恢复原始的label并绘制在最上层 */ this->setColor(color1); this->setBlendFunc(originalBlend); this->setPosition(CCPoint(textureSize.width * 0.5f, textureSize.height * 0.5f)); this->visit(); this->setFlipY(false); /* 在画布上绘制结束,此时会生成一张纹理 */ rt->end(); /* 取出生成的纹理,添加抗锯齿打磨,并返回 */ CCTexture2D *texture = rt->getSprite()->getTexture(); texture->setAntiAliasTexParameters();// setAliasTexParameters(); this->setTexture(texture); // release it rt->release(); //set the size in the sprite rect =CCRectZero; rect.size = m_pobTexture->getContentSize(); //this->setFlipY(true); //this->setFlipX(true); this->setTextureRect(rect); //if(!this->isFlipY()) // this->setFlipY(true); this->setAnchorPoint(anchPoint); this->setColor(color1); this->setPosition(prePos); /***************************给文字描黑边end*********************************/ //ok return true; }
bool CCTubeGridTile::initWithType(int type, bool backGround,TileColor color) { CCRenderTexture* pRt = CCRenderTexture::create(tileWidth,tileHeight); if (backGround) { pRt->begin(); float flineWidth = 0.3; ccDrawSolidRect(ccp(flineWidth,flineWidth),ccp(tileWidth-flineWidth, tileHeight-flineWidth),tileBackgroundColor); pRt->end(); } if(!initWithTexture(pRt->getSprite()->getTexture()))return false; //do image initialization with type CCPoint childPosWithOffset = ccp(tileWidth/2, tileHeight/2); ChildTubeSprite* podChild1 = NULL; ChildTubeSprite* podChild2 = NULL; this->type = type; m_canGetFireLocation = false; if (TILETYPE_LINE_ONE_HORIZONTAL == type|| TILETYPE_LINE_ONE_VERTICAL == type|| TILETYPE_LINE_TWO_HORIZONTALONTOP == type|| TILETYPE_LINE_TWO_VERTICALONTOP == type) { genericType = GENERIC_TYPE_LINE; initLine1(color); if (TILETYPE_LINE_TWO_HORIZONTALONTOP == type|| TILETYPE_LINE_TWO_VERTICALONTOP == type) { initLine2(color); } } else { genericType = GENERIC_TYPE_CURVE; initCurve1(color); if (TILETYPE_CURVE_TWO_BOTTONLEFT_TOPRIGHT == type|| TILETYPE_CURVE_TWO_TOPLEFT_BOTTONRIGHT == type) { initCurve2(color); } } switch (type) { case TILETYPE_VOID: { //create the void tile render texture m_rtVoid = CCRenderTexture::create(tileWidth,tileHeight); m_rtVoid->begin(); ccColor4F colorVoid = {0,0,0 ,1.0f}; ccDrawSolidRect(ccp(0,0),ccp(tileWidth, tileHeight),colorVoid); m_rtVoid->end(); setTexture(m_rtVoid->getSprite()->getTexture()); break; } case TILETYPE_NOTUBE: { break; } case TILETYPE_CURVE_ONE_BOTTONLEFT: { podChild1 = ChildTubeSprite::createWithTexture(m_rtCurve->getSprite()->getTexture()); podChild1->setPosition(childPosWithOffset); podChild1->setRotation(-90); podChild1->setTag(TAG_CURVE_BOTTONLEFT); addChild(podChild1,Z_CHILD1); break; } case TILETYPE_CURVE_ONE_BOTTONRIGHT: { podChild1 = ChildTubeSprite::createWithTexture(m_rtCurve->getSprite()->getTexture()); podChild1->setPosition(childPosWithOffset); podChild1->setRotation(-180); podChild1->setTag(TAG_CURVE_BOTTONRIGHT); addChild(podChild1,Z_CHILD1); break; } case TILETYPE_CURVE_ONE_TOPLEFT: { podChild1 = ChildTubeSprite::createWithTexture(m_rtCurve->getSprite()->getTexture()); podChild1->setPosition(childPosWithOffset); podChild1->setTag(TAG_CURVE_TOPLEFT); addChild(podChild1,Z_CHILD1); break; } case TILETYPE_CURVE_ONE_TOPRIGHT: { podChild1 = ChildTubeSprite::createWithTexture(m_rtCurve->getSprite()->getTexture()); podChild1->setPosition(childPosWithOffset); podChild1->setRotation(90); podChild1->setTag(TAG_CURVE_TOPRIGHT); addChild(podChild1,Z_CHILD1); break; } case TILETYPE_CURVE_TWO_TOPLEFT_BOTTONRIGHT: { podChild1 = ChildTubeSprite::createWithTexture(m_rtCurve->getSprite()->getTexture()); podChild1->setPosition(childPosWithOffset); podChild1->setTag(TAG_CURVE_TOPLEFT); addChild(podChild1,Z_CHILD1); podChild2 = ChildTubeSprite::createWithTexture(m_rtCurve2->getSprite()->getTexture()); podChild2->setPosition(childPosWithOffset); podChild2->setRotation(180); podChild2->setTag(TAG_CURVE_BOTTONRIGHT); addChild(podChild2,Z_CHILD2); break; } case TILETYPE_CURVE_TWO_BOTTONLEFT_TOPRIGHT: { podChild1 = ChildTubeSprite::createWithTexture(m_rtCurve->getSprite()->getTexture()); podChild1->setPosition(childPosWithOffset); podChild1->setRotation(-90); podChild1->setTag(TAG_CURVE_BOTTONLEFT); addChild(podChild1,Z_CHILD1); podChild2 = ChildTubeSprite::createWithTexture(m_rtCurve2->getSprite()->getTexture()); podChild2->setPosition(childPosWithOffset); podChild2->setRotation(90); podChild2->setTag(TAG_CURVE_TOPRIGHT); addChild(podChild2,Z_CHILD2); break; } case TILETYPE_LINE_ONE_HORIZONTAL: { podChild1 = ChildTubeSprite::createWithTexture(m_rtLine->getSprite()->getTexture()); podChild1->setPosition(childPosWithOffset); podChild1->setRotation(-90); podChild1->setTag(TAG_LINE_HORIZONTAL); addChild(podChild1,Z_CHILD1); break; } case TILETYPE_LINE_ONE_VERTICAL: { podChild1 = ChildTubeSprite::createWithTexture(m_rtLine->getSprite()->getTexture()); podChild1->setPosition(childPosWithOffset); podChild1->setTag(TAG_LINE_VERTICAL); addChild(podChild1,Z_CHILD1); break; } case TILETYPE_LINE_TWO_HORIZONTALONTOP: { podChild1 = ChildTubeSprite::createWithTexture(m_rtLine->getSprite()->getTexture()); podChild1->setPosition(childPosWithOffset); podChild1->setTag(TAG_LINE_VERTICAL); addChild(podChild1,Z_CHILD1); podChild2 = ChildTubeSprite::createWithTexture(m_rtLine2->getSprite()->getTexture()); podChild2->setPosition(childPosWithOffset); podChild2->setRotation(-90); podChild2->setTag(TAG_LINE_HORIZONTAL); addChild(podChild2,Z_CHILD2); break; } case TILETYPE_LINE_TWO_VERTICALONTOP: { podChild1 = ChildTubeSprite::createWithTexture(m_rtLine->getSprite()->getTexture()); podChild1->setPosition(childPosWithOffset); podChild1->setRotation(-90); podChild1->setTag(TAG_LINE_HORIZONTAL); addChild(podChild1,Z_CHILD1); podChild2 = ChildTubeSprite::createWithTexture(m_rtLine2->getSprite()->getTexture()); podChild2->setPosition(ccp(tileWidth/2,tileHeight/2)); podChild2->setTag(TAG_LINE_VERTICAL); addChild(podChild2,Z_CHILD2); break; } default: break; } //////solve the not match glitch if (podChild1) { podChild1->setScale(tile_rt_scale); } if (podChild2) { podChild2->setScale(tile_rt_scale); } /////////////////////// //set the render texture if (genericType == GENERIC_TYPE_LINE) { if (podChild1) { podChild1->setRenderTexture(m_rtLine); } if (podChild2) { podChild2->setRenderTexture(m_rtLine2); } } else { if (podChild1) { podChild1->setRenderTexture(m_rtCurve); } if (podChild2) { podChild2->setRenderTexture(m_rtCurve2); } } //decide tubeCount if (TILETYPE_VOID == type||TILETYPE_NOTUBE == type) { m_remainTubes = 0; } else if (TILETYPE_LINE_TWO_VERTICALONTOP == type ||TILETYPE_LINE_TWO_HORIZONTALONTOP == type || TILETYPE_CURVE_TWO_BOTTONLEFT_TOPRIGHT == type ||TILETYPE_CURVE_TWO_TOPLEFT_BOTTONRIGHT == type ) { m_remainTubes = 2; } else { m_remainTubes = 1; } return true; }
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()); }
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()); }
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()); }
void CCTransitionCrossFade::onEnter() { CCTransitionScene::onEnter(); // create a transparent color layer // in which we are going to add our rendertextures ccColor4B color = {0,0,0,0}; CCSize size = CCDirector::sharedDirector()->getWinSize(); CCLayerColor* layer = CCLayerColor::create(color); // create the first render texture for inScene CCRenderTexture* inTexture = CCRenderTexture::create((int)size.width, (int)size.height); if (NULL == inTexture) { return; } inTexture->getSprite()->setAnchorPoint( ccp(0.5f,0.5f) ); inTexture->setPosition( ccp(size.width/2, size.height/2) ); inTexture->setAnchorPoint( ccp(0.5f,0.5f) ); // render inScene to its texturebuffer inTexture->begin(); m_pInScene->visit(); inTexture->end(); // create the second render texture for outScene CCRenderTexture* outTexture = CCRenderTexture::create((int)size.width, (int)size.height); outTexture->getSprite()->setAnchorPoint( ccp(0.5f,0.5f) ); outTexture->setPosition( ccp(size.width/2, size.height/2) ); outTexture->setAnchorPoint( ccp(0.5f,0.5f) ); // render outScene to its texturebuffer outTexture->begin(); m_pOutScene->visit(); outTexture->end(); // create blend functions ccBlendFunc blend1 = {GL_ONE, GL_ONE}; // inScene will lay on background and will not be used with alpha ccBlendFunc 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 CCAction* layerAction = CCSequence::create ( CCFadeTo::create(m_fDuration, 0), CCCallFunc::create(this, callfunc_selector(CCTransitionScene::hideOutShowIn)), CCCallFunc::create(this, callfunc_selector(CCTransitionScene::finish)), NULL ); // run the blend action outTexture->getSprite()->runAction( layerAction ); // add the layer (which contains our two rendertextures) to the scene addChild(layer, 2, kSceneFade); }