Created
October 19, 2018 02:49
-
-
Save tongoclinh/a9e7fb920d9eff597c34d9d3a98aa850 to your computer and use it in GitHub Desktop.
Render Texture (Draw)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| auto render_texture = RenderTexture::create(this->getContentSize().width, this->getContentSize().height); | |
| render_texture->setPosition(this->getContentSize() / 2); | |
| this->addChild(render_texture, 10000); | |
| auto brush = Sprite::create("water.png"); | |
| brush->retain(); | |
| auto draw_overlay = ui::Layout::create(); | |
| draw_overlay->setContentSize(this->getContentSize()); | |
| draw_overlay->setTouchEnabled(true); | |
| this->addChild(draw_overlay, 9999); | |
| draw_overlay->addTouchEventListener([this, render_texture, brush](Ref * sender, ui::Widget::TouchEventType type) { | |
| auto wg = reinterpret_cast<ui::Widget *>(sender); | |
| auto current_point = wg->getTouchMovePosition(); | |
| if (last_point == Point::ZERO) { | |
| last_point = current_point; | |
| return; | |
| } | |
| render_texture->begin(); | |
| auto distance = current_point.distance(last_point); | |
| for (float i = 0; i < distance; i += 0.5) { | |
| float difx = last_point.x - current_point.x; | |
| float dify = last_point.y - current_point.y; | |
| float delta = (float)i / distance; | |
| auto pos = Point(current_point.x + (difx * delta), current_point.y + (dify * delta)); | |
| brush->setPosition(pos); | |
| brush->visit(); | |
| } | |
| render_texture->end(); | |
| last_point = current_point; | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment