Issue with Monogame resizing

I've converted my XNA project into a MonoGame one.

Whenever I start the application and resize the game manually (ie - dragging the window)...

this.Window.AllowUserResizing = true;


...the game scales in a weird manner.

Namely instead of scaling as it should do, it scales to the point where it looks fuzzy and the mouse clicks aren't working correctly.

The main game map has a hard coded "component size" which means that when the screen gets larger, its supposed to show more of the game - but instead I'm getting poorly scaled components.

Part of my code looks as follows:

const int TILEWIDTH = 50;
const int TILEHEIGHT = 50;
//......
Rectangle rec = new Rectangle(0, 0, TILEWIDTH, TILEHEIGHT);


So theoretically the pixels are becoming larger. This was working correctly in XNA.

I am in no way handling or overriding the resize command, it was 'just working'.

I found some threads with similar problems, but no solution. Anything I could do ?

I found that in order to get window resizing to work properly, you need to handle resetting the Graphics Device parameters manually, when the window is resized:

this.Window.AllowUserResizing = true;
this.Window.ClientSizeChanged += new EventHandler<EventArgs>(Window_ClientSizeChanged);

void Window_ClientSizeChanged(object sender, EventArgs e)
{
graphics.PreferredBackBufferWidth = Window.ClientBounds.Width;
graphics.PreferredBackBufferHeight = Window.ClientBounds.Height;
graphics.ApplyChanges();
}


So basically, whenever your window is resized, you reset the Graphics Device with the new width/height.

• GraphicsDevice.Reset() does not exist in Monogame - monogame.codeplex.com/discussions/432200 – Haedrian Jan 16 '14 at 20:30
• You are right. I modified the code. Might help. – jgallant Jan 16 '14 at 20:45
• I get a stack overflow whenever I run the program with that code. The pertinant part of the stack suggests that MonoGame.Framework.dll!MonoGame.Framework.WinFormsGameWindow.OnClientSizeChanged(object sender, System.EventArgs eventArgs) - calls "OnClientSizeChanged()" which starts the loop over again. – Haedrian Jan 16 '14 at 21:17
• I moved "graphics.ApplyChanges()" to the Update function instead and it seems to be working. I'm not sure how its effecting my framerate though - apparently nothing at all - but I need to be sure... – Haedrian Jan 16 '14 at 21:30
• yeah, sorry, I haven't tested this :) – jgallant Jan 16 '14 at 21:37

The answer by jgallant here creates a stack overflow. To fix this I did the following:

//add this in your initialisation

this.Window.AllowUserResizing = true;
this.Window.ClientSizeChanged += new EventHandler<EventArgs>(Window_ClientSizeChanged);


private bool WindowSizeIsBeingChanged = false;
void Window_ClientSizeChanged(object sender, EventArgs e)
{
WindowSizeIsBeingChanged = !WindowSizeIsBeingChanged
if(WindowSizeIsBeingChanged){
graphics.PreferredBackBufferWidth = Window.ClientBounds.Width;
graphics.PreferredBackBufferHeight = Window.ClientBounds.Height;
graphics.ApplyChanges();
}
}


The problem was that graphics.ApplyChanges() calls the ClientSizeChanged to be activated. With this fix, you essentially skip the code from the second call, which makes sure you don't end up with a stack overflow, without having to put any additional calls to graphics.ApplyChanges() in your main game loop.

Fixing the issue was easier than expected

public override void Update(GameTime gameTime)
{
graphics.ApplyChanges();
}


It doesn't seem to impact framerate in any way (still getting 120)

• I went from 0.06 to 0.16 render iteration time by doing this each update call, so at least for me there is a performance cost. – ItzWarty Jan 25 '17 at 23:16
• @ItzWarty - A solution, when need to be sure work is done at next Update, but not on every Update, is to set a flag on the event that causes the change. In Update, test the flag; if set, do the work and clear the flag. – ToolmakerSteve Oct 26 '17 at 23:07

I found that I needed to cache the resolution I had been on, and compare and if they changed, then do apply changes. This made it happen only once. Putting it in game update gave me lots of artifacts and crazy stuff.

void Window_ClientSizeChanged(object sender, EventArgs e)
{
if (Resolution.currentResolution().X != Window.ClientBounds.Width || Resolution.currentResolution().Y != Window.ClientBounds.Height)
{
graphics.PreferredBackBufferWidth = screen.Bounds.Width;
graphics.PreferredBackBufferHeight = screen.Bounds.Height;
graphics.ApplyChanges();
}
}