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

版权声明:本文博主原创文章。博客,未经同意不得转载。

Cocos2d-x3.0 RenderTexture(一) 保存的更多相关文章

  1. 如何在Cocos2D 1.0 中掩饰一个精灵(六)

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 掩饰一个精灵:实现代码 打开HelloWorldLayer.m并 ...

  2. 如何在Cocos2D 1.0 中掩饰一个精灵(一)

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 原帖来自Ray Wunderlich写的精彩的文章 How To ...

  3. Cocos2D v2.0至v3.x简洁转换指南(三)

    Cocos2D 3.3中的注意事项 如果你在使用Cocos2D 3.3+(是SpriteBuilder 1.3+的一部分)你将不得不替分别的换所有存在的UITouch和UITouchEvent为CCT ...

  4. Cocos2D v2.0至v3.x简洁转换指南(二)

    触摸处理 我们在稍后将完成Cocos2d 3.0中触摸处理的完整教程.而现在最重要的是知道如何去启用触摸处理在你的CCNode中: self.userInteractionEnabled = TRUE ...

  5. 如何将各种低版本的discuz版本升级到discuz x3.0

    最近在做discuz改版的项目,遇到了很多问题,相信很多拥有discuz论坛的版主,站长和程序猿在升级或改版discuz的过程中遇到过和我一样的问题,所以我开了一个discuz专栏,为大家讲解一下di ...

  6. cocos2d 2.0和UIKit混合编程, Push CCDirector的时候出现黑屏的天坑

    症状 使用cocos2d 2.0和UIKit混合编程, 有一块用cocos2d编写的小程序, 将CCDirector push到一个UINavigationController里面. 虽然事先在后台初 ...

  7. cocos2d-x3.0创建第一个jsb游戏

    第一步: 最新的cocos2d-x.下载地址https://github.com/cocos2d/cocos2d-x github上最新的引擎,值得注意的是官网上发布的引擎是稳定版.选择哪种就看个人喜 ...

  8. Cocos2d-x3.0 RenderTexture(三)

    .h #include "cocos2d.h" #include "cocos-ext.h" #include "ui/CocosGUI.h" ...

  9. cocos2d 3.0自定义事件答疑解惑

    疑惑一:在事件分发中修改订阅者 ,对于这个的理解. 事件的分发是可以嵌套的,cocos2dx使用_inDispatch来保存当前嵌套的深度,当调用第一个dispatchEvent的时候,_inDisp ...

随机推荐

  1. lua 远程调试 【zeroBrane 使用mobdebug】(good转)

    最近基于业务需求,学习了如何使用zeroBrane这个IDE实现C/S 模式下的 lua远程调试,废话不多,上效果图: ---------------------------------------- ...

  2. J2SE习题(2)

    第四.五周练习题 1.a. Define a class called BlogEntry that could be used to store an entry for a  Weblog. Th ...

  3. c++学习笔记4,调用派生类的顺序构造和析构函数(一个)

    测试源代码: //測试派生类的构造函数的调用顺序何时调用 //Fedora20 gcc version=4.8.2 #include <iostream> using namespace ...

  4. IIS在W7下使用

    1.0.发布程序

  5. ubuntu12.04硬盘安装

    ubuntu12.04发布了 , 安装又是一个话题.安装系统有很多方法,比如livecd,和u盘,但这些都需借用外部设备,所以硬盘安装是最好不过的方法了.u盘,cd安装都非常的简 单,对于那些讨厌用光 ...

  6. JDK源代码学习系列07----Stack

                                                                   JDK源代码学习系列07----Stack 1.Stack源代码很easy ...

  7. 同一时候使用windows和linux系统

    相信非常多人又想使用方便的windows,可是在开发中必须使用linux,怎样选择呢? 没关系,这里教你怎样制作双系统. 下载wubi,仅仅有几兆大,直接在windows下安装,安装好以后,双系统就制 ...

  8. Directx11学习笔记【十三】 实现一个简单地形

    本文由zhangbaochong原创,转载请注明出处http://www.cnblogs.com/zhangbaochong/p/5510294.html 上一个教程我们实现了渲染一个会旋转的立方体, ...

  9. window.history.back()的改进方法window.history.go()

    今天在做项目时,測试人员提出了一条bug,起初没当回事,在改动过程中才意识到其重要性,故记录下来. 依照需求,系统应该实现例如以下的功能:有三个关联的页面a.aspx(简称a),b.aspx(简称b) ...

  10. hdu 4661 Message Passing(木DP&amp;组合数学)

    Message Passing Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Other ...