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); }
std::string ProfileScreen::saveScreenshot() const { Size screen = Director::getInstance()->getWinSize(); RenderTexture *tex = RenderTexture::create(int(screen.width), int(screen.height)); tex->retain(); tex->setPosition(Vec2(screen.width/2.0f, screen.height/2.0f)); tex->begin(); Director::getInstance()->getRunningScene()->visit(); tex->end(); __String *path = __String::createWithFormat("%s%d.png", "screenshot_", int(time(0))); tex->saveToFile(path->getCString(), Image::Format::PNG); return FileUtils::getInstance()->getWritablePath() + path->getCString(); }