Cocos2d-x3.0 RenderTexture(一) 保存
来源:互联网 发布:javascript性能测试 编辑:程序博客网 时间:2024/05/16 04:16
.h
#include "cocos2d.h"#include "cocos-ext.h"#include "ui/CocosGUI.h"#include "cocostudio/CocoStudio.h"USING_NS_CC;USING_NS_CC_EXT;using namespace ui;
RenderTexture* _target; Vector<Sprite*> _brushs; void onTouchesMoved(const std::vector<Touch*>& touches, Event* event); void saveImage(cocos2d::Ref *sender); void clearImage(cocos2d::Ref *sender);
.cpp
Size widgetSize = Director::getInstance()->getWinSize(); layout = Layout::create(); layout->setSize(Size(widgetSize.width, widgetSize.height)); //横向排列,这里类似Android里的线性布局 //layout->setLayoutType(LAYOUT_RELATIVE); /*以图片为背景*/ layout->setBackGroundImageScale9Enabled(true); layout->setBackGroundImage("green_edit.png"); layout->setPosition(Point(0,0)); addChild(layout); alert = Text::create("Layout", "fonts/Marker Felt.ttf", 30 ); alert->setColor(Color3B(159, 168, 176)); alert->setPosition(Point(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getSize().height * 3.075f)); layout->addChild(alert); _target = RenderTexture::create(widgetSize.width, widgetSize.height, Texture2D::PixelFormat::RGBA8888); _target->retain(); _target->setPosition(Point(widgetSize.width / 2, widgetSize.height / 2)); layout->addChild(_target); auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesMoved = CC_CALLBACK_2(LayoutTest::onTouchesMoved, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); //save Image menu MenuItemFont::setFontSize(40); auto item1 = MenuItemFont::create("save Image", CC_CALLBACK_1(LayoutTest::saveImage, this)); auto item2 = MenuItemFont::create("Clear", CC_CALLBACK_1(LayoutTest::clearImage, this)); auto menu = Menu::create(item1,item2, NULL); layout->addChild(menu); menu->alignItemsVertically(); menu->setPosition(Point(VisibleRect::rightTop().x - 80, VisibleRect::rightTop().y - 80));
void LayoutTest::onTouchesMoved(const std::vector<Touch *> &touches, cocos2d::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; _brushs.clear(); for (int i = 0; i < d; ++i) { Sprite* sprite = Sprite::create("image.png"); sprite->setColor(Color3B::RED); sprite->setOpacity(20); _brushs.pushBack(sprite); } for (int i = 0 ; i < d; ++i) { float difx = end.x - start.x; float dify = end.y - start.y; float delta = (float) i / distance; _brushs.at(i)->setPosition(Point(start.x + (difx * delta), start.y + (dify * delta))); _brushs.at(i)->setRotation(rand() % 360); float r = (float) (rand() % 50 / 50.0f) + 0.25f; _brushs.at(i)->setScale(r); _brushs.at(i)->setColor(Color3B(rand() % 127 + 128, 255, 255)); _brushs.at(i)->visit(); } } _target->end();}void LayoutTest::saveImage(cocos2d::Ref *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); //向本地写入 std::string fileName = FileUtils::getInstance()->getWritablePath() + jpg; auto action1 = DelayTime::create(1); auto func = [&,fileName]() { auto sprite = Sprite::create(fileName); layout->addChild(sprite); sprite->setScale(0.3f); sprite->setPosition(Point(40, 40)); sprite->setRotation(counter * 3); }; runAction(Sequence::create(action1,CallFunc::create(func), NULL)); counter++;}void LayoutTest::clearImage(cocos2d::Ref *sender){ _target->clear(CCRANDOM_0_1(), CCRANDOM_0_1(), CCRANDOM_0_1(), CCRANDOM_0_1()); }
0 0
- Cocos2d-x3.0 RenderTexture(一) 保存
- cocos2d-x3.0 RenderTexture(二)
- Cocos2d-x3.0 RenderTexture(三)
- cocos2d-x3.0 相对布局(一)
- cocos2d-x3.0 lua学习(一)
- Cocos2d-x3.0 EventListener
- cocos2d-x3.0 SpriteFrameCache
- Cocos2d-x3.0触摸
- Cocos2d-x3.0 Button
- Cocos2d-x3.0 LoadingBar
- cocos2d-x3.0 PageView
- cocos2d-x3.0 RichText
- cocos2d-x3.0 ScrollView
- cocos2d-x3.0 Slider
- cocos2d-x3.0 ListView
- cocos2d-x3.0
- cocos2d-x3.0 柱图
- Cocos2d-x3.0(一)Cocos2d-x3.0简介以及创建新的Cocos2d-x跨平台工程
- zoj 3620(DFS)
- 1017.畅通工程
- 学习工作中常用的mount命令参数
- url编码问题
- 定位new运算符
- Cocos2d-x3.0 RenderTexture(一) 保存
- ubuntu下nginx服务器配置技术总结
- 内网端口映射之80端口映射和全端口映射,在任何网络环境下实现发布网站和访问内网
- 每日一个C++小程序(十一)--两个整数、实数、复数的相加
- DOS下配置手动配置IP地址
- Java与C++的差异
- ZOJ 3612 Median (multiset)
- 每日一个C++小程序(十二)--具有静态数据、函数成员的Point类,多文件组织
- [BP]反向传播