Skip to content
Permalink
Browse files

Fix Rendering to CCRenderTarget. Issue #122

+ Enabled Clipping
+ Enabled Visit
+ Enabled Clipping of Child Nodes
+ Fix AnchorPoint offsetting
+ Fix RenderTarget Texture so that one can use the Texture to create a new Sprite.
+ Added new tests to the Test Suite
  • Loading branch information...
kjpou1 committed Feb 1, 2015
1 parent 2b6417c commit f1eb03c54fbc1eb15456ed12e93cfc158055f01f
@@ -841,10 +841,10 @@ public virtual CCViewport Viewport
set { Scene.Viewport = value; }
}

protected internal virtual Matrix XnaLocalMatrix
internal virtual Matrix XnaLocalMatrix
{
get { return xnaLocalMatrix; }
protected set
set
{
xnaLocalMatrix = value;
}
@@ -2018,32 +2018,44 @@ protected virtual void Draw()
{
}

internal virtual CCDrawManager DrawManager
{
get
{
return CCDrawManager.SharedDrawManager.CurrentRenderTarget == null ? Window.DrawManager : CCDrawManager.SharedDrawManager;
}
}

// This is called with every call to the MainLoop on the CCDirector class. In XNA, this is the same as the Draw() call.
public virtual void Visit()
{
if (!Visible || Scene == null)
bool renderTarget = CCDrawManager.SharedDrawManager.CurrentRenderTarget != null;

if ((!Visible || Scene == null) && !renderTarget)
{
return;
}

var drawManager = DrawManager;

// Set camera view/proj matrix even if ChildClippingMode is None
if(Camera != null)
if(Camera != null && !renderTarget)
{
Window.DrawManager.ViewMatrix = Camera.ViewMatrix;
Window.DrawManager.ProjectionMatrix = Camera.ProjectionMatrix;
drawManager.ViewMatrix = Camera.ViewMatrix;
drawManager.ProjectionMatrix = Camera.ProjectionMatrix;
}


Window.DrawManager.PushMatrix();
drawManager.PushMatrix();

if (Grid != null && Grid.Active)
{
Grid.BeforeDraw();
Transform ();
Transform (drawManager);
}
else
{
Transform();
Transform(drawManager);
}

int i = 0;
@@ -2093,16 +2105,21 @@ public virtual void Visit()
if (Grid != null && Grid.Active)
{
Grid.AfterDraw(this);
Window.DrawManager.SetIdentityMatrix();
drawManager.SetIdentityMatrix();
Grid.Blit();
}

Window.DrawManager.PopMatrix();
drawManager.PopMatrix();
}

internal void Transform(CCDrawManager drawManager)
{
drawManager.MultMatrix(ref xnaLocalMatrix);
}

public void Transform()
{
Window.DrawManager.MultMatrix(ref xnaLocalMatrix);
Transform(Window.DrawManager);
}

public void TransformAncestors()
@@ -6,6 +6,7 @@
namespace CocosSharp
{

//GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT
public enum CCRenderTargetUsage
{
DiscardContents,
@@ -15,6 +16,18 @@ public enum CCRenderTargetUsage

public partial class CCRenderTexture : CCNode
{

[Flags]
enum ClearFlags
{
None = 0x0,
ColorBuffer = 0x1,
DepthBuffer = 0x2,
StencilBuffer = 0x4,
All = DepthBuffer | StencilBuffer | ColorBuffer
}


bool firstUsage = true;
RenderTarget2D renderTarget2D;

@@ -26,6 +39,11 @@ public partial class CCRenderTexture : CCNode

#endregion Properties

CCColor4B beginClearColor = CCColor4B.Transparent;
float beginDepthValue = 0;
int beginStencilValue = 0;

ClearFlags clearFlags = ClearFlags.None;

#region Constructors

@@ -52,6 +70,9 @@ public CCRenderTexture()
Sprite.ContentSize = contentSize;
Sprite.BlendFunc = CCBlendFunc.AlphaBlend;

// Make sure we set our content size or AnchorPoint will not work correctly
ContentSize = textureSizeInPixels;

AddChild(Sprite);
}

@@ -77,79 +98,155 @@ public virtual void Begin()

if (firstUsage)
{
drawManager.Clear(CCColor4B.Transparent);
drawManager.Clear(beginClearColor);
firstUsage = false;
}
}

public void BeginWithClear(float r, float g, float b, float a)
{
Begin();
CCDrawManager.SharedDrawManager.Clear(new CCColor4B(r, g, b, a));
beginClearColor = new CCColor4B(r, g, b, a);
clearFlags = ClearFlags.ColorBuffer;
CCDrawManager.SharedDrawManager.Clear(beginClearColor);
}

public void BeginWithClear(float r, float g, float b, float a, float depthValue)
{
Begin();
CCDrawManager.SharedDrawManager.Clear(new CCColor4B(r, g, b, a), depthValue);
beginClearColor = new CCColor4B(r, g, b, a);
beginDepthValue = depthValue;
clearFlags = ClearFlags.ColorBuffer | ClearFlags.DepthBuffer;
CCDrawManager.SharedDrawManager.Clear(beginClearColor, depthValue);
}

public void BeginWithClear(float r, float g, float b, float a, float depthValue, int stencilValue)
{
Begin();
CCDrawManager.SharedDrawManager.Clear(new CCColor4B(r, g, b, a), depthValue, stencilValue);
beginClearColor = new CCColor4B(r, g, b, a);
beginDepthValue = depthValue;
beginStencilValue = stencilValue;
clearFlags = ClearFlags.All;
CCDrawManager.SharedDrawManager.Clear(beginClearColor, depthValue, stencilValue);
}

public void BeginWithClear(CCColor4B col)
{
Begin();
beginClearColor = col;
clearFlags = ClearFlags.ColorBuffer;
CCDrawManager.SharedDrawManager.Clear(col);
}

public void BeginWithClear(CCColor4B col, float depthValue)
{
Begin();
beginClearColor = col;
beginDepthValue = depthValue;
clearFlags = ClearFlags.ColorBuffer | ClearFlags.DepthBuffer;

CCDrawManager.SharedDrawManager.Clear(col, depthValue);
}

public void BeginWithClear(CCColor4B col, float depthValue, int stencilValue)
{
Begin();
beginClearColor = col;
beginDepthValue = depthValue;
beginStencilValue = stencilValue;
clearFlags = ClearFlags.All;
CCDrawManager.SharedDrawManager.Clear(col, depthValue, stencilValue);
}

public void ClearDepth(float depthValue)
{
Begin();
beginDepthValue = depthValue;
clearFlags |= ClearFlags.DepthBuffer;
CCDrawManager.SharedDrawManager.Clear(ClearOptions.DepthBuffer, Microsoft.Xna.Framework.Color.White, depthValue, 0);
End();
}

public void ClearStencil(int stencilValue)
{
Begin();
beginStencilValue = stencilValue;
clearFlags |= ClearFlags.StencilBuffer;
CCDrawManager.SharedDrawManager.Clear(ClearOptions.Stencil, Microsoft.Xna.Framework.Color.White, 1, stencilValue);
End();
}

public void Clear(float r, float g, float b, float a)
{
Clear(new CCColor4B(r, g, b, a));
}

public void Clear(CCColor4B col)
{
Begin();
beginClearColor = col;
clearFlags |= ClearFlags.ColorBuffer;
CCDrawManager.SharedDrawManager.Clear(beginClearColor);
End();
}

void Clear(ClearFlags clearFlags, CCColor4B color, float depth, int stencil)
{
if (clearFlags.HasFlag(ClearFlags.ColorBuffer))
CCDrawManager.SharedDrawManager.Clear(color);

if (clearFlags.HasFlag(ClearFlags.DepthBuffer))
CCDrawManager.SharedDrawManager.Clear(ClearOptions.DepthBuffer, Microsoft.Xna.Framework.Color.White, depth, 0);

if (clearFlags.HasFlag(ClearFlags.StencilBuffer))
CCDrawManager.SharedDrawManager.Clear(ClearOptions.Stencil, Microsoft.Xna.Framework.Color.White, 1, stencil);

}

public virtual void End()
{
CCDrawManager.SharedDrawManager.PopMatrix();

CCDrawManager.SharedDrawManager.SetRenderTarget((CCTexture2D) null);
}

public void Clear(float r, float g, float b, float a)
{
BeginWithClear(r, g, b, a);
End();
}
public override void Visit()
{

public void Clear(CCColor4B col)
{
BeginWithClear(col);
End();
}
if (!Visible)
return;

Sprite.XnaLocalMatrix = XnaLocalMatrix;
Sprite.Visit();
Draw();

}

public bool AutoDraw = false;
protected override void Draw()
{
if (AutoDraw)
{
Begin();

//Begin will create a render group using new render target
Clear(clearFlags, beginClearColor, beginDepthValue, beginStencilValue);

//! make sure all children are drawn
SortAllChildren();

foreach(var child in Children)
{
if (child != Sprite)
child.Visit();
}

//End will pop the current render group
End();
}

//base.Draw();
}

public bool SaveToStream(Stream stream, CCImageFormat format)
{
@@ -502,7 +502,7 @@ protected override void Draw()

Debug.Assert(batchNode == null);

CCDrawManager drawManager = Window.DrawManager;
CCDrawManager drawManager = DrawManager;

drawManager.BlendFunc(BlendFunc);
drawManager.BindTexture(Texture);

0 comments on commit f1eb03c

Please sign in to comment.
You can’t perform that action at this time.