C# 如何使用XNA调整窗口大小
我知道这个问题以前被问过很多次。然而,我在谷歌搜索了一个多小时后找到的所有解决方案基本上都是一样的。每个人都说,为了在XNA中调整窗口大小,只需在Game1类的Initiate()方法中添加以下代码行(或这些代码行的一些细微变化):C# 如何使用XNA调整窗口大小,c#,xna,resize,window,resolution,C#,Xna,Resize,Window,Resolution,我知道这个问题以前被问过很多次。然而,我在谷歌搜索了一个多小时后找到的所有解决方案基本上都是一样的。每个人都说,为了在XNA中调整窗口大小,只需在Game1类的Initiate()方法中添加以下代码行(或这些代码行的一些细微变化): //A lot of people say that the ApplyChanges() call is not necessary, //and equally as many say that it is. graphics.IsFul
//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条目似乎已经有一段时间没有更新了
我在Windows7上运行,上面有图形卡、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);
}
}
公共类MainApp:Microsoft.Xna.Framework.Game
{
图形管理器图形;
SpriteBatch SpriteBatch;
玩家;
公共MainApp()
{
graphics=新的GraphicsDeviceManager(此);
Content.RootDirectory=“Content”;
}
受保护的覆盖无效初始化()
{
player=新玩家();
//这没有任何作用
graphics.IsFullScreen=false;
graphics.PreferredBackBufferWidth=800;
graphics.PreferredBackBufferHeight=600;
graphics.ApplyChanges();
base.Initialize();
}
受保护的覆盖void LoadContent()
{
spriteBatch=新spriteBatch(图形设备);
Vector2 playerPosition=新的Vector2(GraphicsDevice.Viewport.TitleSafeArea.X,
GraphicsDevice.Viewport.TitleSafeArea.Y
+2*(graphics.GraphicsDevice.Viewport.TitleSafeArea.Height/3);
player.Initialize(Content.Load(“basePlayerTexture”),
播放位置);
}
受保护的覆盖无效UnloadContent()
{
}
受保护覆盖无效更新(游戏时间游戏时间)
{
if(GamePad.GetState(PlayerIndex.One).Buttons.Back==ButtonState.Pressed)
这是Exit();
更新(游戏时间);
}
受保护覆盖无效绘制(游戏时间游戏时间)
{
图形设备。清晰(颜色:矢车菊蓝);
spriteBatch.Begin();
球员。抽签(斯皮特巴特);
spriteBatch.End();
基础。抽签(游戏时间);
}
}
另外,这是我在C#的第二天,如果这是因为一个非常愚蠢的错误,我为浪费你的时间道歉 GraphicsDevice.Viewportwidth and height在我的计算机上默认为800x480,请尝试将其设置为明显的大小,如1024x768
graphics.PreferredBackBufferHeight = 768;
graphics.PreferredBackBufferWidth = 1024;
graphics.ApplyChanges();
Initialize
中的上述代码足以为我扩展窗口。令人沮丧的是(正如您所说)“很多人说ApplyChanges()调用是不必要的,同样有很多人说它是”——事实上,这取决于您正在做什么以及在哪里做
(我怎么知道这一切?我知道。另见:)
当您在游戏启动时设置初始分辨率时: 在构造函数中执行此操作(显然,如果重命名
Game1
,请在重命名的构造函数中执行此操作!)
并且在初始化()过程中不要触摸它。
并且不要调用ApplyChanges()
调用Game.Run()
时(默认模板项目在Program.cs
中调用它),它将调用GraphicsDeviceManager.CreateDevice
在调用初始化()
之前设置初始显示!这就是为什么您必须创建GraphicsDeviceManager
,并在游戏类的构造函数中设置所需的设置(在game.Run()之前调用)
如果试图在初始化中设置分辨率,则会导致图形设备设置两次。糟糕
(老实说,我很惊讶这会导致如此混乱。这是默认项目模板中提供的代码!)
在游戏运行时修改分辨率时:
如果您在游戏中的某个位置显示“分辨率选择”菜单,并且您希望响应用户(例如)单击该菜单中的某个选项,那么(并且只有在那时)您才应该使用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();
您是否尝试过800x600以上的分辨率?我以前在XNA中做过这项工作,并没有问题,只是更改为1024x768甚至1920x1080和其他分辨率。另外,我相信graphics.IsFullScreen
默认为false。您是否尝试过创建一个新的xna项目,然后添加GraphicsManager并尝试一下?还是只发生在这个产品上?什么都没有。这是我试过的。添加了以下内容:在MainApp类中:viewPort defaultViewport;然后在构造函数中:defaultViewport=GraphicsDevice.Viewport;defaultViewport.Width=1024;defaultViewport.Height=720;最后在Draw()中:GraphicsDevice.Viewport=defaultViewport;这样行吗?如果这是正确的,那么视口就不是问题所在。我怀疑这是因为视口用于限制渲染目标受图形影响的部分。不管怎样,还有其他想法吗?废话。我刚刚在一个空白项目中成功地设置了大小。谢谢你的帮助,对不起,我是个笨蛋。@user1494407-Na
graphics.IsFullScreen = !graphics.IsFullScreen;
graphics.ApplyChanges();