Skip to content
This repository has been archived by the owner on Mar 6, 2018. It is now read-only.

Commit

Permalink
Workaround for Windows not firing OnClientSizeChanged on window maxim…
Browse files Browse the repository at this point in the history
…ize (#179)
  • Loading branch information
hach-que committed May 25, 2017
1 parent 545973a commit c510ef6
Showing 1 changed file with 46 additions and 13 deletions.
59 changes: 46 additions & 13 deletions Protogame/Core/CoreGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,23 @@ public abstract class CoreGame<TInitialWorld, TWorldManager> : ICoreGame
/// </summary>
private HostGame _hostGame;

/// <summary>
/// Whether resize events can be handled.
/// </summary>
private bool _shouldHandleResize;

/// <summary>
/// The current known window width, this is used to detect window maximize / minimize, which
/// doesn't seem to fire OnClientSizeChanged.
/// </summary>
private int _windowWidth;

/// <summary>
/// The current known window height, this is used to detect window maximize / minimize, which
/// doesn't seem to fire OnClientSizeChanged.
/// </summary>
private int _windowHeight;

/// <summary>
/// Gets the current game context. You should not generally access this property; outside
/// an explicit Update or Render loop, the state of the game context is not guaranteed. Inside
Expand Down Expand Up @@ -305,6 +322,10 @@ public void AssignHost(HostGame hostGame)
_hostGame = hostGame;
_hostGame.Exiting += _hostGame_Exiting;

_shouldHandleResize = true;
_windowWidth = _hostGame.Window.ClientBounds.Width;
_windowHeight = _hostGame.Window.ClientBounds.Height;

var assetContentManager = new AssetContentManager(_hostGame.Services);
_hostGame.Content = assetContentManager;
_kernel.Bind<IAssetContentManager>().ToMethod(x => assetContentManager);
Expand Down Expand Up @@ -367,6 +388,22 @@ public void Exit()
#endif
}

private void OnClientSizeChanged()
{
if (!_shouldHandleResize)
{
return;
}

_shouldHandleResize = false;
_windowWidth = _hostGame.Window.ClientBounds.Width;
_windowHeight = _hostGame.Window.ClientBounds.Height;
GameContext.Graphics.PreferredBackBufferWidth = _windowWidth;
GameContext.Graphics.PreferredBackBufferHeight = _windowHeight;
GameContext.Graphics.ApplyChanges();
_shouldHandleResize = true;
}

protected virtual async Task LoadContentAsync()
{
if (!_hasDoneInitialLoadContent)
Expand All @@ -384,21 +421,9 @@ protected virtual async Task LoadContentAsync()
#if PLATFORM_WINDOWS
// Register for the window resize event so we can scale
// the window correctly.
var shouldHandleResize = true;
_hostGame.Window.ClientSizeChanged += (sender, e) =>
{
if (!shouldHandleResize)
{
return;
}
shouldHandleResize = false;
var width = _hostGame.Window.ClientBounds.Width;
var height = _hostGame.Window.ClientBounds.Height;
GameContext.Graphics.PreferredBackBufferWidth = width;
GameContext.Graphics.PreferredBackBufferHeight = height;
GameContext.Graphics.ApplyChanges();
shouldHandleResize = true;
OnClientSizeChanged();
};

// Register for the window close event so we can dispatch
Expand Down Expand Up @@ -531,6 +556,14 @@ public void Update(GameTime gameTime)

StartupTrace.EmittedTimingEntries = true;
}

#if PLATFORM_WINDOWS
if (_windowWidth != _hostGame.Window.ClientBounds.Width ||
_windowHeight != _hostGame.Window.ClientBounds.Height)
{
OnClientSizeChanged();
}
#endif

using (_profiler.Measure("update", GameContext.FrameCount.ToString()))
{
Expand Down

0 comments on commit c510ef6

Please sign in to comment.