diff --git a/MLEM/Extensions/GraphicsExtensions.cs b/MLEM/Extensions/GraphicsExtensions.cs index e7b0097..50f5631 100644 --- a/MLEM/Extensions/GraphicsExtensions.cs +++ b/MLEM/Extensions/GraphicsExtensions.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; @@ -38,5 +39,30 @@ namespace MLEM.Extensions { manager.ApplyChanges(); } + public static TargetContext WithRenderTarget(this GraphicsDevice device, RenderTarget2D target) { + return new TargetContext(device, target); + } + + public struct TargetContext : IDisposable { + + private readonly GraphicsDevice device; + private readonly RenderTargetBinding[] lastTargets; + + public TargetContext(GraphicsDevice device, RenderTarget2D target) { + this.device = device; + this.lastTargets = device.RenderTargetCount <= 0 ? null : device.GetRenderTargets(); + device.SetRenderTarget(target); + } + + public void Dispose() { + if (this.lastTargets != null) { + this.device.SetRenderTargets(this.lastTargets); + } else { + this.device.SetRenderTarget(null); + } + } + + } + } } \ No newline at end of file