/
RenderTextureSave.cs
132 lines (107 loc) · 4.91 KB
/
RenderTextureSave.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
using System.Collections.Generic;
using System.IO;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Cocos2D;
namespace tests
{
public class RenderTextureSave : RenderTextureTestDemo
{
private readonly CCSprite m_pBrush;
private readonly CCRenderTexture m_pTarget;
private static int counter = 0;
public RenderTextureSave()
{
CCSize s = CCDirector.SharedDirector.WinSize;
// create a render texture, this is what we are going to draw into
m_pTarget = new CCRenderTexture((int) s.Width, (int) s.Height, SurfaceFormat.Color, DepthFormat.None, RenderTargetUsage.PreserveContents);
// Let's clear the rendertarget here so that we start off fresh.
// Some platforms do not seem to be initializing the rendertarget color so this will make sure the background shows up colored instead of
// what looks like non initialized. Mostly MacOSX for now.
m_pTarget.Clear(0,0,0,255);
m_pTarget.Position = new CCPoint(s.Width / 2, s.Height / 2);
// It's possible to modify the RenderTexture blending function by
//CCBlendFunc tbf = new CCBlendFunc (OGLES.GL_ONE, OGLES.GL_ONE_MINUS_SRC_ALPHA);
//m_pTarget.Sprite.BlendFunc = tbf;
// note that the render texture is a CCNode, and contains a sprite of its texture for convience,
// so we can just parent it to the scene like any other CCNode
AddChild(m_pTarget, -1);
// create a brush image to draw into the texture with
m_pBrush = new CCSprite("Images/fire");
// It's possible to modify the Brushes blending function by
CCBlendFunc bbf = new CCBlendFunc (CCOGLES.GL_ONE, CCOGLES.GL_ONE_MINUS_SRC_ALPHA);
m_pBrush.BlendFunc = bbf;
m_pBrush.Color = new CCColor3B (Color.Red);
m_pBrush.Opacity = 20;
TouchEnabled = true;
// Save Image menu
CCMenuItemFont.FontSize = 16;
CCMenuItem item1 = new CCMenuItemFont("Save Image", saveImage);
CCMenuItem item2 = new CCMenuItemFont("Clear", clearImage);
var menu = new CCMenu(item1, item2);
AddChild(menu);
menu.AlignItemsVertically();
menu.Position = new CCPoint(s.Width - 80, s.Height - 30);
}
public override string title()
{
return "Touch the screen";
}
public override string subtitle()
{
return "Press 'Save Image' to create an snapshot of the render texture";
}
public override void TouchesMoved(List<CCTouch> touches)
{
CCTouch touch = touches[0];
CCPoint start = touch.Location;
CCPoint end = touch.PreviousLocation;
// begin drawing to the render texture
m_pTarget.Begin();
// for extra points, we'll draw this smoothly from the last position and vary the sprite's
// scale/rotation/offset
float distance = CCPoint.Distance(start, end);
if (distance > 1)
{
var d = (int) distance;
for (int i = 0; i < d; i++)
{
float difx = end.X - start.X;
float dify = end.Y - start.Y;
float delta = i / distance;
m_pBrush.Position = new CCPoint(start.X + (difx * delta), start.Y + (dify * delta));
m_pBrush.Rotation = CCRandom.Next() % 360;
float r = (CCRandom.Next() % 50 / 50f) + 0.25f;
m_pBrush.Scale = r;
// Comment out the following line to show just the initial red color set
m_pBrush.Color = new CCColor3B((byte) (CCRandom.Next() % 127 + 128), 255, 255);
// Call visit to draw the brush, don't call draw..
m_pBrush.Visit();
}
}
// finish drawing and return context back to the screen
m_pTarget.End();
}
public void clearImage(object pSender)
{
m_pTarget.Clear(CCMacros.CCRandomBetween0And1(), CCMacros.CCRandomBetween0And1(), CCMacros.CCRandomBetween0And1(), CCMacros.CCRandomBetween0And1());
}
public void saveImage(object pSender)
{
using (var stream = new MemoryStream())
{
m_pTarget.SaveToStream(stream, CCImageFormat.Png);
//m_pTarget.saveToFile(jpg, ImageFormat.JPG);
stream.Position = 0;
var tex = new CCTexture2D();
tex.InitWithStream(stream);
CCSprite sprite = new CCSprite(tex);
sprite.Scale = 0.3f;
AddChild(sprite);
sprite.Position = new CCPoint(40, 40);
sprite.Rotation = counter * 3;
}
counter++;
}
}
}