1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-05-29 03:23:37 +02:00
MLEM/MLEM/Extensions/GraphicsExtensions.cs

71 lines
2.7 KiB
C#
Raw Normal View History

2019-12-01 22:58:20 +01:00
using System;
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;
2019-11-08 15:35:15 +01:00
var curr = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode;
manager.PreferredBackBufferWidth = curr.Width;
manager.PreferredBackBufferHeight = curr.Height;
} else {
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-19 03:20:25 +02:00
public static void ResetWidthAndHeight(this GraphicsDeviceManager manager, Game game) {
var (_, _, width, height) = game.Window.ClientBounds;
manager.PreferredBackBufferWidth = Math.Max(height, width);
manager.PreferredBackBufferHeight = Math.Min(height, width);
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() {
this.device.SetRenderTargets(this.lastTargets);
}
}
2019-11-08 15:35:15 +01:00
}
}