summaryrefslogtreecommitdiff
path: root/source/graphics/GraphicsManager.cs
blob: 2e285cf77d398f2477a2cc593ab46697dde8dab2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Celesteia.Graphics {
    public enum FullscreenMode {
        Windowed, Fullscreen, Borderless
    }
    
    public class GraphicsManager : GameComponent {
        private new GameInstance Game => (GameInstance) base.Game;
        private GraphicsDeviceManager _manager;
        public GraphicsManager(GameInstance Game) : base(Game) {
            _manager = new GraphicsDeviceManager(Game);
            _manager.PreferHalfPixelOffset = true;
        }

        private FullscreenMode _screenMode;
        private bool _useBorderless = false;
        private Rectangle _resolution = new Rectangle(0, 0, 1280, 720);
        private Rectangle _lastBounds;

        public FullscreenMode FullScreen {
            get { return _screenMode; }
            set { _screenMode = value; ResolveResolution(); }
        }

        public bool VSync = false;

        public bool IsFullScreen {
            get { return (_screenMode != FullscreenMode.Windowed); }
        }

        public Rectangle Resolution {
            get { return _resolution; }
            set { _lastBounds = _resolution = value; }
        }

        public bool MSAA = false;

        private void ResolveResolution() {
            if (!IsFullScreen) _resolution = _lastBounds;
            else {
                _lastBounds = Game.Window.ClientBounds;
                _resolution = new Rectangle(0, 0, _manager.GraphicsDevice.Adapter.CurrentDisplayMode.Width, _manager.GraphicsDevice.Adapter.CurrentDisplayMode.Height);
            }
        }

        public void Apply() {
            Game.Window.AllowUserResizing = true;
            _manager.PreferMultiSampling = MSAA;
            _manager.PreferredBackBufferWidth = _resolution.Width;
            _manager.PreferredBackBufferHeight = _resolution.Height;
            _manager.PreferredBackBufferFormat = SurfaceFormat.Color;
            _manager.HardwareModeSwitch = (_screenMode == FullscreenMode.Borderless);
            _manager.IsFullScreen = IsFullScreen;
            _manager.SynchronizeWithVerticalRetrace = VSync;
            _manager.ApplyChanges();
        }

        public GraphicsManager ToggleFullScreen() {
            FullScreen = IsFullScreen ? FullscreenMode.Windowed : (_useBorderless ? FullscreenMode.Borderless : FullscreenMode.Fullscreen);
            return this;
        }
    }
}