RenderTexture動態創建紋理


CCRenderTexture,它允許你來動態創建紋理,並且可以在游戲中重用這些紋理。

  使用 CCRenderTexture非常簡單 – 你只需要做以下5步就行了:

  1. 創建一個新的CCRenderTexture. 這里,你可以指定將要創建的紋理的寬度和高度。.
  2. 調用 CCRenderTexture:begin. 這個方法會啟動OpenGL,並且接下來,任何繪圖的命令都會渲染到CCRenderTexture里面去,而不是畫到屏幕上。
  3. 繪制紋理. 你可以使用原始的OpenGL調用來繪圖,或者你也可以使用cocos2d對象里面已經定義好的visit方法。(這個visit方法就會調用一些opengl命令來繪制cocos2d對象)
  4. 調用 CCRenderTexture:end. 這個方法會渲染紋理,並且會關閉渲染至CCRenderTexture的通道。
  5. 從生成的紋理中創建一個sprite. 你現在可以用CCRenderTexture的sprite.texture屬性來輕松創建新的精靈了。
class RenderTextureSave : public RenderTextureTest
{
public:
    RenderTextureSave();
    ~RenderTextureSave();
    virtual std::string title();
    virtual std::string subtitle();
    virtual void onTouchesMoved(const std::vector<Touch*>& touches, Event* event);
    void clearImage(Object *pSender);
    void saveImage(Object *pSender);

private:
    RenderTexture *_target;
    Sprite *_brush;
};
void RenderTextureSave::onTouchesMoved(const std::vector<Touch*>& touches, Event* event)
{
    auto touch = touches[0];
    auto start = touch->getLocation();
    auto end = touch->getPreviousLocation();

    // begin drawing to the render texture
    _target->begin();

    // for extra points, we'll draw this smoothly from the last position and vary the sprite's
    // scale/rotation/offset
    float distance = start.getDistance(end);
    if (distance > 1)
    {
        int d = (int)distance;
        for (int i = 0; i < d; i++)
        {
            float difx = end.x - start.x;
            float dify = end.y - start.y;
            float delta = (float)i / distance;
            _brush->setPosition(Point(start.x + (difx * delta), start.y + (dify * delta)));
            _brush->setRotation(rand() % 360);
            float r = (float)(rand() % 50 / 50.f) + 0.25f;
            _brush->setScale(r);
            /*_brush->setColor(Color3B(CCRANDOM_0_1() * 127 + 128, 255, 255));*/
            // Use CCRANDOM_0_1() will cause error when loading libtests.so on android, I don't know why.
            _brush->setColor(Color3B(rand() % 127 + 128, 255, 255));
            // Call visit to draw the brush, don't call draw..
            _brush->visit();
        }
    }

    // finish drawing and return context back to the screen
    _target->end();
}
void RenderTextureSave::saveImage(cocos2d::Object *sender)
{
    static int counter = 0;

    char png[20];
    sprintf(png, "image-%d.png", counter);
    char jpg[20];
    sprintf(jpg, "image-%d.jpg", counter);

    _target->saveToFile(png, Image::Format::PNG);
    _target->saveToFile(jpg, Image::Format::JPG);
    

    auto image = _target->newImage();

    auto tex = TextureCache::getInstance()->addImage(image, png);

    CC_SAFE_DELETE(image);

    auto sprite = Sprite::createWithTexture(tex);

    sprite->setScale(0.3f);
    addChild(sprite);
    sprite->setPosition(Point(40, 40));
    sprite->setRotation(counter * 3);

    CCLOG("Image saved %s and %s", png, jpg);

    counter++;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM