SwapChainBackgroundPanel letterboxing Monogame Windows应用商店应用

时间:2014-04-14 05:07:59

标签: windows xaml windows-store-apps monogame

我将我的太空射击游戏从Windows Phone移植到Windows Store App。在WP中,它总是以完全纵向方式播放。

对于Windows应用商店应用,虽然在横向模式下,我想在左右两侧使用信箱中心设置游戏屏幕。问题是我无法调整SwapChainBackgroundPanel的边距属性,因此游戏始终与左侧对齐,黑色屏幕在右侧。

这是我的代码

public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        GamePage.Current.SizeChanged += OnWindowSizeChanged;
        Content.RootDirectory = "Content";
    }

    private void OnWindowSizeChanged(object sender, Windows.UI.Xaml.SizeChangedEventArgs e)
    {
        var CurrentViewState = Windows.UI.ViewManagement.ApplicationView.Value;
        double width = e.NewSize.Width;
        double height = e.NewSize.Height;

        // using Windows.Graphics.Display;
        ResolutionScale resolutionScale = DisplayProperties.ResolutionScale;
        string orientation = null;

        if (ApplicationView.Value == ApplicationViewState.FullScreenLandscape)
        {
            orientation = "FullScreenLandscape";
            //Does not work because it's start on the center of the screen
            //Black screen is on the left and place the game screen on the right
            GamePage.Current.HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Center;

            //Gives error - WinRT information: Setting 'Margin' property is 
            //not supported on SwapChainBackgroundPanel.
            GamePage.Current.Margin = new Thickness(centerMargin, 0, 0, 0);
        }

        else if (ApplicationView.Value == ApplicationViewState.FullScreenPortrait)
        {
            orientation = "FullScreenPortrait";
        }
        else if (ApplicationView.Value == ApplicationViewState.Filled)
        {
            orientation = "Filled";
        }
        else if (ApplicationView.Value == ApplicationViewState.Snapped)
        {
            orientation = "Snapped";
        }

        Debug.WriteLine("{0} x {1}. Scale: {2}. Orientation: {3}", 
            width.ToString(), height.ToString(), resolutionScale.ToString(),
            orientation);
    }

GamePage.xaml是默认的

<SwapChainBackgroundPanel
    x:Class="SpaceShooterXW8.GamePage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:SpaceShooterXW8"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">    
</SwapChainBackgroundPanel>

1 个答案:

答案 0 :(得分:0)

经过一番研究后,我想我已经弄明白了this blog post。对于处于类似情况的人来说,这就是我所做的。

解决方案的优点在于信箱由Resolution类自动管理。我所要做的就是将代码中的batch.begin()行更新为

      batch.Begin(SpriteSortMode.Deferred,
            null, SamplerState.LinearClamp,
            null,
            null,
            null,
            Resolution.getTransformationMatrix());

要在方向更改时处理分辨率更改,请在我的Game1.cs

中使用此方法
public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        GamePage.Current.SizeChanged += OnWindowSizeChanged;
        Content.RootDirectory = "Content";
        Resolution.Init(ref graphics);
        Resolution.SetVirtualResolution(480, 800);
    }


private void OnWindowSizeChanged(object sender, Windows.UI.Xaml.SizeChangedEventArgs e)
    {
        var CurrentViewState = Windows.UI.ViewManagement.ApplicationView.Value;
        App.AppWidth = (int)e.NewSize.Width;
        App.AppHeight = (int)e.NewSize.Height;
        Resolution.SetResolution(App.AppWidth, App.AppHeight, true);
    }

App.AppWidthApp.AppHeight的初始值在GamePage.xaml.cs中设置。

public GamePage(string launchArguments)
    {
        this.InitializeComponent();
        App.AppWidth = (int)Window.Current.Bounds.Width;
        App.AppHeight = (int)Window.Current.Bounds.Height;
        Current = this;
        // Create the game.
        _game = XamlGame<Game1>.Create(launchArguments, Window.Current.CoreWindow, this);

    }

两者都是在App.xaml.cs

中创建的全局静态属性
    public static int AppWidth { get; set; }
    public static int AppHeight { get; set; }

到目前为止我遇到的唯一问题是,鼠标输入无法缩放到屏幕分辨率变化。不幸的是我没有触摸屏测试,但我认为触摸输入应该扩展。如果有人测试过触摸,请分享您的发现。感谢。

<强>更新

我已设法使用以下

缩放鼠标输入
    public static Vector2 ScaleGesture(Vector2 position)
    {
        int x = (int)(position.X / (float)App.AppWidth * (float)Screen.ScreenWidth);
        int y = (int)(position.Y / (float)App.AppHeight * (float)Screen.ScreenHeight);
        var scaledPosition = new Vector2(x, y);
        return scaledPosition;
    }
相关问题