1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-26 14:38:34 +01:00

added a way to using() a render target while applying the previous ones easily

This commit is contained in:
Ellpeck 2020-04-02 17:47:11 +02:00
parent 80f36c78bd
commit 5a8e74c5f3

View file

@ -1,4 +1,5 @@
using System; using System;
using System.Linq;
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
@ -38,5 +39,30 @@ namespace MLEM.Extensions {
manager.ApplyChanges(); 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);
}
}
}
} }
} }