Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
372 views
in Technique[技术] by (71.8m points)

c# - How to resize window using XNA

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

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.

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.

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

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. This is my second day with C#, so if this is due to a really stupid error I apologize for wasting your time.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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!

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


When you are setting the initial resolution when your game starts up:

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

    // ...
}

And do not touch it during Initialize()! And do not call ApplyChanges().

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.

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


When you are modifying the resolution while the game is running:

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

        // ...
    }

    // ...
}

Finally, note that ToggleFullScreen() is the same as doing:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...