如何使用XNA来调整窗口 [英] How to resize window using XNA

查看:247
本文介绍了如何使用XNA来调整窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题已经被问过很多次。不过,我在谷歌上搜索了一个小时后,发现所有的解决方案在本质上是一样的。大家都说,为了调整窗口大小在XNA中你只需code以下行(或code,这些线路的一些轻微的变化)添加到您的启动()方法Game1类:

I know this question has been asked many times before. However, all solutions I have found after over an hour of googling are essentially the same thing. Everyone says that in order to resize a window in XNA you simply add the following lines of code(or some slight variation of these lines of code) to your Initiate() method in the Game1 class:

    //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();

这不会为我工作。在code编译并运行,但绝对没有什么变化。我已经冲刷的GraphicsDevice的和GraphicsDeviceManager类的文档,但我一直无法找到这表明我需要做上述以外的任何事情的任何信息。

This does not work for me. The code compiles, and runs, but absolutely nothing changes. I've been scouring the documentation for the GraphicsDevice and GraphicsDeviceManager classes, but I have been unable to find any information indicating that I need to do anything other than the above.

我也相当肯定我的显卡就足够了(ATI HD 5870),虽然看起来,关于XNA显卡兼容性维基条目尚未一段时间更新。

I am also fairly sure my graphics card is sufficient(ATI HD 5870), although it appears that the wiki entry on XNA graphics card compatibility has not been updated for a while.

我在Windows 7上运行,与上面的显卡时,Visual C#2010前preSS和XNA的最新版本。

I'm running on Windows 7, with the above graphics card, Visual C# 2010 Express, and the latest version of XNA.

所以我只是希望有人可以帮我找到我在哪里搞乱了。我会后我的整个Game1类以下(我给它改名MainApp)。如果任何人希望看到任何被呼吁其他类的,问我会张贴。

So I'm just hoping that someone can help me find where I am messing up. I will post my entire Game1 class(I renamed it MainApp) below. If anyone would like to see any of the other classes that are called on, ask and I'll post them.

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);
    }
}

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

P.S. This is my second day with C#, so if this is due to a really stupid error I apologize for wasting your time.

推荐答案

这是令人沮丧的(如你所说)很多人说ApplyChanges()调用是没有必要的,同样因为很多人说,这是 - 问题的事实是,的这取决于的关于你正在做什么和你在哪里做

It is frustrating that (as you say) "A lot of people say that the ApplyChanges() call is not necessary, and equally as many say that it is" -- the fact of the matter is that it depends on what you are doing and where you are doing it!

(我怎么知道这一切,我实现了它的另请参见:的this~~MD~~aux回答。)

(How do I know all this? I've implemented it. See also: this answer.)

为此在您的构造的(当然如果重命名的Game1 ,做在你改名构造!)

Do this in your constructor (obviously if you rename Game1, do it in your renamed constructor!)

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

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

    // ...
}

的过程中触摸它初始化()的叫 ApplyChanges()

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

When Game.Run() gets called (the default template project calls it in Program.cs), it will call GraphicsDeviceManager.CreateDevice to set up the initial display before Initialize() is called! This is why you must create the GraphicsDeviceManager and set your desired settings in the constructor of your game class (which is called before Game.Run()).

如果您尝试设置在初始化的决议,可以使图形设备进行设置两次。坏的。

If you try to set the resolution in Initialize, you cause the graphics device to be set up twice. Bad.

(说实话,我很惊讶,这会导致这样的困惑,这是在默认项目模板提供的code!)

(To be honest I'm surprised that this causes such confusion. This is the code that is provided in the default project template!)

如果您present一个分辨率选择菜单中的某个地方在你的游戏,你想,​​以响应用户(例如)单击该菜单中的选项 - 然后的(也是唯一的那么)你应该使用 ApplyChanges 。您应该只从更新中调用它。例如:

If you present a "resolution selection" menu somewhere in your game, and you want to respond to a user (for example) clicking an option in that menu - then (and only then) should you use ApplyChanges. You should only call it from within Update. For example:

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来调整窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆