如何使用XNA调整窗口大小

use*_*407 12 c# xna resize resolution window

我知道这个问题之前已被问过很多次了.但是,经过一个多小时的谷歌搜索,我发现的所有解决方案基本上都是一样的.每个人都说,为了在XNA中调整窗口大小,您只需在Game1类的Initiate()方法中添加以下代码行(或这些代码行的一些细微变化):

    //A lot of people say that the ApplyChanges() call is not necessary,
    //and equally as many say that it is.
    graphics.IsFullScreen = false;
    graphics.PreferredBackBufferWidth = 800;
    graphics.PreferredBackBufferHeight = 600;
    graphics.ApplyChanges();
Run Code Online (Sandbox Code Playgroud)

这对我不起作用.代码编译并运行,但绝对没有任何变化.我一直在搜索GraphicsDevice和GraphicsDeviceManager类的文档,但我一直无法找到任何信息表明我需要做除上述之外的任何事情.

我也很确定我的显卡是足够的(ATI HD 5870),虽然看起来XNA显卡兼容性上的wiki条目还没有更新一段时间.

我在Windows 7上运行,使用上面的显卡,Visual C#2010 Express和最新版本的XNA.

所以我只是希望有人可以帮我找到搞砸的地方.我将在下面发布我的整个Game1类(我将其重命名为MainApp).如果有人想看到任何其他被调用的类,请问我会发布它们.

public class MainApp : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;

    Player player;

    public MainApp()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
    }

    protected override void Initialize()
    {
        player = new Player();

        //This does not do ANYTHING
        graphics.IsFullScreen = false;
        graphics.PreferredBackBufferWidth = 800;
        graphics.PreferredBackBufferHeight = 600;
        graphics.ApplyChanges();

        base.Initialize();
    }

    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);

        Vector2 playerPosition = new Vector2(GraphicsDevice.Viewport.TitleSafeArea.X,
                                             GraphicsDevice.Viewport.TitleSafeArea.Y
                                             + 2*(graphics.GraphicsDevice.Viewport.TitleSafeArea.Height / 3));
        player.Initialize(Content.Load<Texture2D>("basePlayerTexture"),
                          playerPosition);
    }

    protected override void UnloadContent()
    {
    }

    protected override void Update(GameTime gameTime)
    {
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();
       base.Update(gameTime);
    }

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

        player.Draw(spriteBatch);

        spriteBatch.End();

        base.Draw(gameTime);
    }
}
Run Code Online (Sandbox Code Playgroud)

PS这是我用C#的第二天,所以如果这是由于一个非常愚蠢的错误,我为浪费你的时间而道歉.

And*_*ell 40

令人沮丧的是(正如你所说)"很多人都认为ApplyChanges()调用不是必需的,同样多的人说它是" - 事实上,这取决于你在做什么你在做什么!

(我怎么知道这一切?我已经实现了.参见:这个答案.)


当您在游戏启动时设置初始分辨率时:

构造函数中执行此操作(显然,如果重命名Game1,请在重命名的构造函数中执行此操作!)

public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;

    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        graphics.PreferredBackBufferHeight = 600;
        graphics.PreferredBackBufferWidth = 800;
    }

    // ...
}
Run Code Online (Sandbox Code Playgroud)

不要在去碰它Initialize()!不要打电话ApplyChanges().

Game.Run()被调用时(默认模板项目调用它Program.cs),它将调用GraphicsDeviceManager.CreateDevice Initialize()调用之前设置初始显示!这就是为什么你必须GraphicsDeviceManager在游戏类的构造函数中创建并设置所需的设置(之前调用Game.Run()).

如果尝试设置分辨率Initialize,则会导致图形设备设置两次.坏.

(说实话,我很惊讶这会造成这样的混乱.这是默认项目模板中提供的代码!)


在游戏运行时修改分辨率时:

如果您在游戏中的某个位置出现"分辨率选择"菜单,并且您想要响应用户(例如)单击该菜单中的选项 - 那么(并且只有这样)才能使用ApplyChanges.你应该只从内部调用它Update.例如:

public class Game1 : Microsoft.Xna.Framework.Game
{
    protected override void Update(GameTime gameTime)
    {
        if(userClickedTheResolutionChangeButton)
        {
            graphics.IsFullScreen = userRequestedFullScreen;
            graphics.PreferredBackBufferHeight = userRequestedHeight;
            graphics.PreferredBackBufferWidth = userRequestedWidth;
            graphics.ApplyChanges();
        }

        // ...
    }

    // ...
}
Run Code Online (Sandbox Code Playgroud)

最后,请注意ToggleFullScreen()与执行操作相同:

graphics.IsFullScreen = !graphics.IsFullScreen;
graphics.ApplyChanges();
Run Code Online (Sandbox Code Playgroud)


Bra*_*uck 2

我的计算机上默认的宽度GraphicsDevice.Viewport和高度是 800x480,尝试设置高于该值的尺寸,这样会很明显 - 例如 1024x768。

graphics.PreferredBackBufferHeight = 768;
graphics.PreferredBackBufferWidth = 1024;
graphics.ApplyChanges();
Run Code Online (Sandbox Code Playgroud)

上面的代码Initialize足以为我扩展窗口。