Skip to content

Commit

Permalink
WinDX: Avoid freezing while moving/resizing window
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelDePiazzi committed Dec 14, 2018
1 parent ba5c722 commit e430755
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions MonoGame.Framework/Windows/WinFormsGameWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ class WinFormsGameWindow : GameWindow, IDisposable
// true if window position was moved either through code or by dragging/resizing the form
private bool _wasMoved;

private bool _isResizeTickEnabled;
private readonly System.Timers.Timer _resizeTickTimer;

#region Internal Properties

internal Game Game { get; private set; }
Expand Down Expand Up @@ -156,9 +159,13 @@ internal WinFormsGameWindow(WinFormsGamePlatform platform)
Form.MouseEnter += OnMouseEnter;
Form.MouseLeave += OnMouseLeave;

_resizeTickTimer = new System.Timers.Timer(1) { SynchronizingObject = Form, AutoReset = false };
_resizeTickTimer.Elapsed += OnResizeTick;

Form.Activated += OnActivated;
Form.Deactivate += OnDeactivate;
Form.Resize += OnResize;
Form.ResizeBegin += OnResizeBegin;
Form.ResizeEnd += OnResizeEnd;

Form.KeyPress += OnKeyPress;
Expand Down Expand Up @@ -376,8 +383,26 @@ private void OnResize(object sender, EventArgs eventArgs)
OnClientSizeChanged();
}

private void OnResizeBegin(object sender, EventArgs e)
{
_isResizeTickEnabled = true;
_resizeTickTimer.Enabled = true;
}

private void OnResizeTick(object sender, System.Timers.ElapsedEventArgs e)
{
if (!_isResizeTickEnabled)
return;
UpdateWindows();
Game.Tick();
_resizeTickTimer.Enabled = true;
}

private void OnResizeEnd(object sender, EventArgs eventArgs)
{
_isResizeTickEnabled = false;
_resizeTickTimer.Enabled = false;

_wasMoved = true;
if (Game.Window == this)
{
Expand Down

0 comments on commit e430755

Please sign in to comment.