【问题标题】:SwapChainBackgroundPanel letterboxing Monogame Windows Store AppSwapChainBackgroundPanel 信箱 Monogame Windows Store App
【发布时间】:2014-04-14 05:07:59
【问题描述】:

我正在将我的太空射击游戏从 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>

【问题讨论】:

  • 作为移植时要考虑的旁注。 SwapChainBackgroundPanel 在 8.1 及之后版本将不可用或更改,他们告诉我们改用 SwapChainPanel

标签: windows xaml windows-store-apps monogame


【解决方案1】:

经过一番研究,我想我已经弄清楚了,感谢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;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-24
    • 1970-01-01
    • 1970-01-01
    • 2013-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多