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()); }
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()); }