2019-12-01 22:58:20 +01:00
|
|
|
using System;
|
2020-04-02 17:47:11 +02:00
|
|
|
using System.Linq;
|
2019-11-08 15:35:15 +01:00
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
|
|
|
|
|
|
namespace MLEM.Extensions {
|
|
|
|
public static class GraphicsExtensions {
|
|
|
|
|
|
|
|
private static int lastWidth;
|
|
|
|
private static int lastHeight;
|
|
|
|
|
|
|
|
public static void SetFullscreen(this GraphicsDeviceManager manager, bool fullscreen) {
|
|
|
|
manager.IsFullScreen = fullscreen;
|
|
|
|
if (fullscreen) {
|
2020-01-26 01:31:40 +01:00
|
|
|
var view = manager.GraphicsDevice.Viewport;
|
|
|
|
lastWidth = view.Width;
|
|
|
|
lastHeight = view.Height;
|
2020-01-26 01:20:20 +01:00
|
|
|
|
2019-11-08 15:35:15 +01:00
|
|
|
var curr = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode;
|
|
|
|
manager.PreferredBackBufferWidth = curr.Width;
|
|
|
|
manager.PreferredBackBufferHeight = curr.Height;
|
|
|
|
} else {
|
2020-01-26 01:20:20 +01:00
|
|
|
if (lastWidth <= 0 || lastHeight <= 0)
|
|
|
|
throw new InvalidOperationException("Can't call SetFullscreen to change out of fullscreen mode without going into fullscreen mode first");
|
|
|
|
|
2019-11-08 15:35:15 +01:00
|
|
|
manager.PreferredBackBufferWidth = lastWidth;
|
|
|
|
manager.PreferredBackBufferHeight = lastHeight;
|
|
|
|
}
|
|
|
|
manager.ApplyChanges();
|
|
|
|
}
|
|
|
|
|
2020-01-26 01:31:40 +01:00
|
|
|
public static void ApplyChangesSafely(this GraphicsDeviceManager manager) {
|
|
|
|
// If we don't do this, then applying changes will cause the
|
|
|
|
// graphics device manager to reset the window size to the
|
|
|
|
// size set when starting the game :V
|
|
|
|
var view = manager.GraphicsDevice.Viewport;
|
|
|
|
manager.PreferredBackBufferWidth = view.Width;
|
|
|
|
manager.PreferredBackBufferHeight = view.Height;
|
|
|
|
manager.ApplyChanges();
|
|
|
|
}
|
|
|
|
|
2020-04-02 17:47:11 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-11-08 15:35:15 +01:00
|
|
|
}
|
|
|
|
}
|