mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-29 15:58:33 +01:00
added a way to using() a render target while applying the previous ones easily
This commit is contained in:
parent
80f36c78bd
commit
5a8e74c5f3
1 changed files with 26 additions and 0 deletions
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue