How to get phone orientation with SupportedOrientations = "Portrait"

I am writing a simple game using the MonoGame library. As far as I know, MonoGame (unlike XNA) does not support automatically changing the orientation of the phone, and you need to use RenderTarget2D, and then draw it with the correct orientation. To do this, I need to determine the current orientation of the phone.

To get the OrientationChanged event, I have to allow GamePage.xaml to use different orientations:

SupportedOrientations="PortraitOrLandscape" Orientation="Portrait"

The problem is that in this case my GamePage automatically changes orientation and in the landscape arrangement the sprites are stretched horizontally. It seems that in the landscape position the phone believes that it has the same number of horizontal pixels as in portrait orientation (480 pixels).

enter image description hereenter image description here

. :

protected override void Draw(GameTime gameTime)
{
    GraphicsDevice.Clear(Color.CornflowerBlue);

    spriteBatch.Begin();
    spriteBatch.Draw(t, Vector2.Zero, null, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 1);
    spriteBatch.End();

    base.Draw(gameTime);
}

- GamePage.xaml:

SupportedOrientations="Portrait" Orientation="Portrait"

OrientationChanged (GamePage.OrientationChanged Game.Window.OrientationChanged). Game,

graphics.SupportedOrientations = DisplayOrientation.Portrait |
                                 DisplayOrientation.PortraitDown |
                                 DisplayOrientation.LandscapeLeft |
                                 DisplayOrientation.LandscapeRight;

graphics.ApplyChanges();
this.Window.OrientationChanged += OrientationChanged;

, , OrientationChanged GamePage .

+3
1

:)

, , - :

SupportedOrientations="Portrait" Orientation="Portrait"

, :

public class Game1 : Game
{
    Accelerometer accelSensor;
    PageOrientation currentOrientation = PageOrientation.None;
    PageOrientation desiredOrientation = PageOrientation.None;
    ...

    public Game1()
    {
        accelSensor = new Accelerometer();
        accelSensor.ReadingChanged += new EventHandler<AccelerometerReadingEventArgs> AccelerometerReadingChanged);
        ...

    private void AccelerometerReadingChanged(object sender, AccelerometerReadingEventArgs e)
    {
        double angle = Math.Atan2(-e.X, e.Y) * 180.0 / Math.PI;
        double delta = 15;

        if (angle > -45 + delta && angle < 45 - delta) desiredOrientation = PageOrientation.PortraitDown;
        if (angle > 45 + delta && angle < 135 - delta) desiredOrientation = PageOrientation.LandscapeLeft;
        if (angle > -135 + delta && angle < -45 - delta) desiredOrientation = PageOrientation.LandscapeRight;
        if ((angle >= -180 && angle < -135 - delta) || (angle > 135 + delta && angle <= 180)) desiredOrientation = PageOrientation.PortraitUp;
    }

    protected override void Update(GameTime gameTime)
    {
        if (desiredOrientation != currentOrientation) SetOrientation(desiredOrientation);
        ...

, ...

+2

All Articles