我知道这个问题以前已经被问过很多次了。然而,我在谷歌上搜索了一个多小时后找到的所有解决方案本质上都是一样的。每个人都说,为了在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 HD5870),尽管关于XNA显卡兼容性的维基条目似乎已经有一段时间没有更新了。
我使用的是Windows7,使用的是上述显卡、Visual XNA2010Express和最新版本的C#。
所以我只希望有人能帮我找出我哪里搞砸了。我将在下面发布我的整个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#的第二天,所以如果这是由于一个非常愚蠢的错误,我很抱歉浪费您的时间。
发布于 2012-07-02 10:51:58
令人沮丧的是(正如您所说的)“许多人说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
中调用它),它将在调用 Initialize()
之前调用GraphicsDeviceManager.CreateDevice
来设置初始显示!这就是为什么必须在游戏类的构造函数(在Game.Run()
之前调用)中创建GraphicsDeviceManager
并设置所需设置的原因。
如果尝试在Initialize
中设置分辨率,则会导致图形设备设置两次。坏的。
(老实说,我很惊讶这会造成这样的混乱。这是默认项目模板中提供的代码!)
在游戏运行期间修改分辨率时:
您只能从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();
发布于 2012-07-02 00:25:16
在我的电脑上,GraphicsDevice.Viewport
的默认宽度和高度是800x480,试着设置一个更大的尺寸,这样会更明显-比如1024x768。
graphics.PreferredBackBufferHeight = 768;
graphics.PreferredBackBufferWidth = 1024;
graphics.ApplyChanges();
上面用Initialize
编写的代码足以为我展开窗口。
https://stackoverflow.com/questions/11283294
复制相似问题