forked from Rampastring/Rampastring.XNAUI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RenderTargetStack.cs
84 lines (69 loc) · 2.57 KB
/
RenderTargetStack.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
using Microsoft.Xna.Framework.Graphics;
using System;
namespace Rampastring.XNAUI;
/// <summary>
/// Handles render targets.
/// </summary>
internal static class RenderTargetStack
{
public static void Initialize(RenderTarget2D finalRenderTarget, GraphicsDevice _graphicsDevice)
{
FinalRenderTarget = finalRenderTarget;
graphicsDevice = _graphicsDevice;
currentContext = new RenderContext(finalRenderTarget, null);
}
internal static void InitDetachedScaledControlRenderTarget(int renderWidth, int renderHeight)
{
if (DetachedScaledControlRenderTarget != null)
{
DetachedScaledControlRenderTarget.Dispose();
}
DetachedScaledControlRenderTarget = new RenderTarget2D(graphicsDevice,
renderWidth, renderHeight,
false, SurfaceFormat.Color,
DepthFormat.None, 0,
RenderTargetUsage.DiscardContents);
}
public static RenderTarget2D FinalRenderTarget { get; internal set; }
/// <summary>
/// A render target for controls that
/// are detached and have scaling applied to them.
/// </summary>
public static RenderTarget2D DetachedScaledControlRenderTarget { get; internal set; }
private static RenderContext currentContext;
private static GraphicsDevice graphicsDevice;
public static void PushRenderTarget(RenderTarget2D renderTarget, SpriteBatchSettings newSettings)
{
Renderer.EndDraw();
var context = new RenderContext(renderTarget, currentContext);
currentContext = context;
graphicsDevice.SetRenderTarget(renderTarget);
Renderer.PushSettingsInternal();
Renderer.CurrentSettings = newSettings;
Renderer.BeginDraw();
}
public static void PopRenderTarget()
{
currentContext = currentContext.PreviousContext;
if (currentContext == null)
{
throw new InvalidOperationException("No render context left! This usually " +
"indicates that a control with an unique render target has " +
"double-popped their render target.");
}
Renderer.EndDraw();
Renderer.PopSettingsInternal();
graphicsDevice.SetRenderTarget(currentContext.RenderTarget);
Renderer.BeginDraw();
}
}
internal class RenderContext
{
public RenderContext(RenderTarget2D renderTarget, RenderContext previousContext)
{
RenderTarget = renderTarget;
PreviousContext = previousContext;
}
public RenderTarget2D RenderTarget { get; }
public RenderContext PreviousContext { get; }
}