Search code examples
c#fullscreenmonogame

access GraphicsDeviceManager in other class in monogame with c#


I made an options menu for my monogame in c#. This is a part of my class with the initialize function. I want to initalize the game in a window screen. This works perfect. Now I want that I can change the window screen into fullscreen in my options. How i have to access to the Graphics Device Manager, that it is working?

I have access when I am writing

Game.Game1.mGraphicsDeviceMgr.PreferredBackBufferWidth = 1600;
Game.Game1.mGraphicsDeviceMgr.PreferredBackBufferHeight = 900;

But then I get the error CS0120 C# An object reference is required for the non-static field, method, or property. And I don`t know how to handle it.

namespace Game1.Game
{
    internal class Game1 : Microsoft.Xna.Framework.Game
    {
        private InputManager mInput;
        private ScreenManager mScreenManager;
        public GraphicsDeviceManager mGraphicsDeviceMgr;
        private bool mEscapeDown;

        public Game1()
        {
            mGraphicsDeviceMgr = new GraphicsDeviceManager(this);

            Content.RootDirectory = "Content";
            mEscapeDown = false;
        }
        protected override void Initialize()
        {

            IsMouseVisible = true;
            mGraphicsDeviceMgr.ApplyChanges();

            // GameEngine
            mInput = new InputManager();
            mScreenManager = new ScreenManager(mGraphicsDeviceMgr, Content, mInput);
            SoundManager.InitializeContent(Content);

            base.Initialize();

Then I have a class MainMenuScreen:

public sealed class MainMenuScreen : Screen
    {
        private readonly ScreenManager mScreenManager;
        private readonly List<Button> mButtons;
        private int mStatistic;
        private readonly InputManager mInput;
[...]
private void ButtonOptions()
        {
            OptionScreen optionScreen = new OptionScreen(mScreenManager, mInput);
            mScreenManager.AddScreen(optionScreen);
            optionScreen.AddLabel("Options:");

            optionScreen.AddButton("Fullscreen",ButtonFullScreen);
            optionScreen.AddButton("Back", ButtonBack);
        }
private void ButtonFullScreen()
        {
            mGraphicsDeviceMgr.PreferredBackBufferWidth = 1600;
            mGraphicsDeviceMgr.PreferredBackBufferHeight = 900;
        }

Solution

  • Since there will only ever be a single deviceManager. Make it static:

    public static GraphicsDeviceManager mGraphicsDeviceMgr;
    
    

    This will allow access to the mGraphicsDeviceMgr variable from anywhere in the project:

     Game1.mGraphicsDeviceMgr.PreferredBackBufferWidth = 1600;
     Game1.mGraphicsDeviceMgr.PreferredBackBufferHeight = 900;
    

    Due to your specific namespace choice of "Game1.Game", for completeness, the lines would need to be changed to a fully qualified name of:

     Game1.Game.Game1.mGraphicsDeviceMgr.PreferredBackBufferWidth = 1600;
     Game1.Game.Game1.mGraphicsDeviceMgr.PreferredBackBufferHeight = 900;