Monogame crashes after ToggleFullScreen back to window

1

With Monogame I'm able to switch to fullscreen mode using ToggleFullScreen without any problems, but when I try to switch back to windowed mode I get this:

SharpDX.SharpDXException was unhandled HResult=-2005270527
Message=HRESULT: [0x887A0001], Module: [SharpDX.DXGI], ApiCode: [DXGI_ERROR_INVALID_CALL/InvalidCall], Message: The application made a call that is invalid. Either the parameters of the call or the state of some object was incorrect. Enable the D3D debug layer in order to see details via debug messages.

Source=SharpDX StackTrace: at SharpDX.Result.CheckError() at SharpDX.DXGI.SwapChain.ResizeBuffers(Int32 bufferCount, Int32 width, Int32 height, Format newFormat, SwapChainFlags swapChainFlags) at Microsoft.Xna.Framework.Graphics.GraphicsDevice.CreateSizeDependentResources(Boolean useFullscreenParameter) at Microsoft.Xna.Framework.GraphicsDeviceManager.ApplyChanges() at Microsoft.Xna.Framework.GraphicsDeviceManager.ToggleFullScreen()

Here's some code:

private GraphicsDeviceManager graphics;
private Vector2 baseScreenSize = new Vector2(1280, 720);

public Game1() {
   graphics = new GraphicsDeviceManager(this);
}

protected override void Initialize() {
   base.Initialize();
   GraphicsDevice.SamplerStates[0] = SamplerState.PointWrap;

   //Work out how much we need to scale our graphics to fill the screen
   float horScaling = GraphicsDevice.PresentationParameters.BackBufferWidth / baseScreenSize.X;
   float verScaling = GraphicsDevice.PresentationParameters.BackBufferHeight / baseScreenSize.Y;
   Vector3 screenScalingFactor = new Vector3(horScaling, verScaling, 1);
   globalTransformation = Matrix.CreateScale(screenScalingFactor);
}

public void ToggleFullScreen() {
   graphics.ToggleFullScreen();

}

The ToggleFullScreen method is being called from a button on a form.

What does that exception mean, and how can I fix it?


Edit:

Further testing shows this... I setup in the update method a switch when the F key is pressed:

protected override void Update(GameTime gameTime) {
            if (Keyboard.GetState().IsKeyDown(Keys.F)) {
                if (!debounce) { graphics.ToggleFullScreen(); debounce = true; }
            }
            else
                debounce = false;
}

And this works! I'm able to switch to and from fullscreen mode.

But I want to be able to switch modes from a windows form with the user clicks a button. But since the above code works I thought maybe this just has to be called in the Update method.. So I tried this:

private bool shouldToggleFullscreen = false;

public void ToggleFullScreen() {
   shouldToggleFullscreen = true;

}

protected override void Update(GameTime gameTime) {
   if(shouldToggleFullscreen) {
      graphics.ToggleFullScreen();
      shouldToggleFullscreen = false;
   }
}

And this doesn't work. I get the same error as above.

I just found the answer. It's because the game isn't the active window. Now I just need to find out how to give it focus.

c#
xna
monogame
sharpdx
asked on Stack Overflow Oct 15, 2016 by bwoogie • edited Oct 15, 2016 by bwoogie

1 Answer

0

The problem is that I was trying to toggle the fullscreen mode while the window wasn't focused (Why that's a problem I'm not sure).

So before I toggle I must make sure the game window is focused first.

[DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);
...
if(shouldToggleFullscreen) {
   SetForegroundWindow(Window.Handle);
   if (IsActive) {
      graphics.ToggleFullScreen();
      shouldToggleFullscreen = false;
   }
}
answered on Stack Overflow Oct 15, 2016 by bwoogie

User contributions licensed under CC BY-SA 3.0