【问题标题】:How to resize window using XNA如何使用 XNA 调整窗口大小
【发布时间】:2012-07-02 06:06:47
【问题描述】:

我知道这个问题已经被问过很多次了。但是,我在谷歌搜索一个多小时后找到的所有解决方案基本上都是一样的。每个人都说,为了在 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();

这对我不起作用。代码编译并运行,但绝对没有任何变化。我一直在搜索 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);
    }
}

附:这是我使用 C# 的第二天,所以如果这是由于一个非常愚蠢的错误造成的,我很抱歉浪费你的时间。

【问题讨论】:

  • 你试过800x600以上的分辨率吗?我以前在 XNA 中做过这个并且让它工作没有问题,只是更改为 1024x768 甚至 1920x1080 和其他分辨率。我也相信graphics.IsFullScreen 默认是假的。
  • 您是否尝试过创建一个新的 xna 项目,然后添加 GraphicsManager 并尝试一下?还是仅在这个项目中发生?

标签: c# xna resize window resolution


【解决方案1】:

令人沮丧的是(正如您所说)“很多人说 ApplyChanges() 调用没有必要,同样许多人说它是必要的”——事实上, 取决于你在做什么以及在哪里做!

(我怎么知道这一切?我有implemented it。另见:this answer。)


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

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

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

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

    // ...
}

并且不要Initialize()期间触摸它!并且不要打电话给ApplyChanges()

Game.Run()被调用时(默认模板项目在Program.cs中调用它),它会调用GraphicsDeviceManager.CreateDevice来设置初始显示之前@ 987654330@被调用!这就是为什么您必须创建 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();
        }

        // ...
    }

    // ...
}

最后注意ToggleFullScreen()和做的一样:

graphics.IsFullScreen = !graphics.IsFullScreen;
graphics.ApplyChanges();

【讨论】:

  • 很高兴知道,我自己还是 XNA 的新手(我想我在第 3 周),这是很好的信息。感谢您提供如此详细的答案。
  • 这是我看到的问题的第一个完全解释的答案。它有效。非常感谢!
  • 如果您在构造函数中调用 ApplyChanges,那么当 HiDef 配置文件不可用时游戏将崩溃(无一例外),而不是显示显示显卡不够好的漂亮窗口。
【解决方案2】:

我的电脑上默认的GraphicsDevice.Viewport 的宽度和高度是 800x480,尝试设置一个高于该值的大小,例如 1024x768。

graphics.PreferredBackBufferHeight = 768;
graphics.PreferredBackBufferWidth = 1024;
graphics.ApplyChanges();

Initialize 中的上述代码足以为我展开窗口。

【讨论】:

  • 什么都没有。这是我尝试过的。添加了以下内容:在 MainApp 类中:viewPort defaultViewport;然后在构造函数中:defaultViewport = GraphicsDevice.Viewport; defaultViewport.Width = 1024; defaultViewport.Height = 720;然后最后在 Draw(): GraphicsDevice.Viewport = defaultViewport;那样有用吗?如果这是正确的,那么视口不是问题。我怀疑它无论如何都是因为视口用于限制受绘图影响的渲染目标部分。无论如何,还有其他想法吗?
  • 废话。我只是在一个空白项目中成功设置了大小。感谢您的帮助,对不起,我是个笨蛋。
  • @user1494407 不,错误发生了。很高兴看到你解决了。
  • 你真的不应该在Initialize 中设置图形设备。看我的回答。
猜你喜欢
  • 2011-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-06
  • 2011-02-25
  • 2021-09-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多