如何使用XNA调整窗口大小

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

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

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

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


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

在你的构造函数中执行此操作(显然,如果重命名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来设置初始显示, 然后调用Initialize() ! 这就是为什么你必须创建GraphicsDeviceManager并在你的游戏类的构造函数中设置你想要的设置(在Game.Run()之前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(); 

默认情况下,我的计算机上的GraphicsDevice.Viewport宽度和高度为800×480,尝试设置一个明显的大小 – 如1024×768。

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

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