Skip to content

Commit

Permalink
Lock default orientation based on PreferredBackBuffer(Width/Height)
Browse files Browse the repository at this point in the history
GraphicsDeviceManager:
- Set default preferred size to a landscape aspect ratio to cause it to default to landscape if the user does not set the preferred sizes
- Removed calls to ResetClientBounds in setters for PreferredBackBuffer(Width/Height). Setting preferred buffer sizes should not have immediate effect. They are just stored for next device reset.

AndroidGameWindow:
- Use PreferredBackBuffer(Width/Height) to determine landscape vs portrait when handling SupportedOrientations == DisplayOrientation.Default.
  • Loading branch information
Aranda committed Jul 6, 2012
1 parent e4b049b commit d217389
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 24 deletions.
17 changes: 15 additions & 2 deletions MonoGame.Framework/Android/AndroidGameWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,14 +203,27 @@ internal void SetSupportedOrientations(DisplayOrientation orientations)
}

/// <summary>
/// In Xna, DisplayOrientation.Default has the same effect as (DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight)
/// In Xna, setting SupportedOrientations = DisplayOrientation.Default (which is the default value)
/// has the effect of setting SupporteOrientations to landscape only or portrait only, based on the
/// aspect ration of PreferredBackBufferWidth / PreferredBackBufferHeight
/// </summary>
/// <returns></returns>
internal DisplayOrientation GetEffectiveSupportedOrientations()
{
if (supportedOrientations == DisplayOrientation.Default)
{
return DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight;
var deviceManager = (_game.Services.GetService(typeof(IGraphicsDeviceManager)) as GraphicsDeviceManager);
if (deviceManager == null)
return DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight;

if (deviceManager.PreferredBackBufferWidth > deviceManager.PreferredBackBufferHeight)
{
return DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight;
}
else
{
return DisplayOrientation.Portrait | DisplayOrientation.PortraitUpsideDown;
}
}
else
{
Expand Down
45 changes: 23 additions & 22 deletions MonoGame.Framework/Android/GraphicsDeviceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,12 @@ public GraphicsDeviceManager(Game game)
throw new ArgumentNullException("Game Cannot Be Null");
}

_game = game;
_preferredBackBufferHeight = game.Window.ClientBounds.Height;
_preferredBackBufferWidth = game.Window.ClientBounds.Width;
_game = game;

// Preferred buffer width/height is used to determine default supported orientations,
// so set the default values to match Xna behaviour of landscape only by default.
_preferredBackBufferWidth = Math.Max(game.Window.ClientBounds.Height, game.Window.ClientBounds.Width);
_preferredBackBufferHeight = Math.Min(game.Window.ClientBounds.Height, game.Window.ClientBounds.Width);
_supportedOrientations = DisplayOrientation.Default;

if (game.Services.GetService(typeof(IGraphicsDeviceManager)) != null)
Expand Down Expand Up @@ -262,9 +265,8 @@ public int PreferredBackBufferHeight
}
set
{
_preferredBackBufferSetByUser = true;
_preferredBackBufferHeight = value;
ResetClientBounds();
_preferredBackBufferSetByUser = true;
_preferredBackBufferHeight = value;
}
}

Expand All @@ -276,9 +278,8 @@ public int PreferredBackBufferWidth
}
set
{
_preferredBackBufferSetByUser = true;
_preferredBackBufferWidth = value;
ResetClientBounds();
_preferredBackBufferSetByUser = true;
_preferredBackBufferWidth = value;
}
}

Expand All @@ -302,24 +303,24 @@ internal void ResetClientBounds()
Game.Instance.Window.ClientBounds = newClientBounds;
}
else if (GraphicsDevice.DisplayMode.AspectRatio < aspectRatio)
{
var newClientBounds = new Rectangle();

newClientBounds.Width = GraphicsDevice.DisplayMode.Width;
newClientBounds.Height = (int)(newClientBounds.Width / aspectRatio);
newClientBounds.Y = (GraphicsDevice.DisplayMode.Height - newClientBounds.Height) / 2;

Game.Instance.Window.ClientBounds = newClientBounds;
}
{
var newClientBounds = new Rectangle();

newClientBounds.Width = GraphicsDevice.DisplayMode.Width;
newClientBounds.Height = (int)(newClientBounds.Width / aspectRatio);
newClientBounds.Y = (GraphicsDevice.DisplayMode.Height - newClientBounds.Height) / 2;

Game.Instance.Window.ClientBounds = newClientBounds;
}
else
{
Game.Instance.Window.ClientBounds = new Rectangle(0, 0, _preferredBackBufferWidth, _preferredBackBufferHeight);
}
}
}
else
{
else
{
Game.Instance.Window.ClientBounds = new Rectangle(0, 0, GraphicsDevice.DisplayMode.Width, GraphicsDevice.DisplayMode.Height);
}
}
}

public DepthFormat PreferredDepthStencilFormat
Expand Down

0 comments on commit d217389

Please sign in to comment.