Cocos2d-x3.0 RenderTexture (1) save

Source: Internet
Author: User


. 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
 
   _brushs;        void onTouchesMoved(const std::vector
  
   & 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); // horizontal arrangement, similar to the linear layout in Android. // layout-> setLayoutType (LAYOUT_RELATIVE ); /* set the image as the background */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
 
  
& Touches, cocos2d: Event * event) {auto touch = touches [0]; auto start = touch-> getLocation (); auto end = touch-> getpreviuslocation (); // 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-0000d.png", counter); char jpg [20]; sprintf (jpg, "image-0000d.jpg ", counter); _ target-> saveToFile (png, Image: Format: PNG); _ target-> saveToFile (jpg, Image: Format: JPG ); // write 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 ());}
 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.