3

我正在尝试编写我的第一个 XNA 游戏,我想包含 ALT+ENTER 功能以在全屏和窗口模式之间切换。我的游戏现在非常基础,没什么特别的。我可以让它在我的 LoadContent 函数中以窗口(默认)或全屏(通过调用 ToggleFullScreen)运行,并且一切正常。

但是,当我在 Update 函数中调用 ToggleFullScreen 时,使用完全相同的代码,我的游戏失败了。如果我开始窗口化并切换到全屏,它似乎会冻结,只显示一帧并且不接受键盘输入(我必须 CTRL+ALT+DEL)。如果我开始全屏并切换到窗口,它会在 DrawIndexedPrimitive 上出错,并显示消息“当前顶点声明不包括当前顶点着色器所需的所有元素。Normal0 丢失。” (这不是真的;我的顶点缓冲区是 VertexPositionNormalTexture 并且包含数据,并且 GraphicsDevice 状态是正常的)。

这两个问题似乎都表明以某种方式与设备失去了联系,但我不知道为什么。我在网上找到的每个资源都说切换全屏就像调用 ToggleFullScreen 函数一样简单,没有提到重置设备或缓冲区。有任何想法吗?

编辑:这是一些代码,省略了无关的东西:

    protected override void LoadContent()
    {
        graphics.PreferredBackBufferWidth = 800; 
        graphics.PreferredBackBufferHeight = 600;
        graphics.ToggleFullScreen();

        basicEffect = new BasicEffect(graphics.GraphicsDevice);
        // etc.
    }

    protected override void Update(GameTime gameTime)
    {
        if (k.IsKeyDown(Keys.Enter) && (k.IsKeyDown(Keys.LeftAlt) || k.IsKeyDown(Keys.RightAlt)) && !oldKeys.Contains(Keys.Enter)) {
            graphics.ToggleFullScreen();
            gameWorld.Refresh();
        }
         // update the world's graphics; this fills my buffers
        GraphicsBuffers gb = gameWorld.Render(graphics.GraphicsDevice);
        graphics.GraphicsDevice.SetVertexBuffer(gb.vb, 0);
        graphics.GraphicsDevice.Indices = gb.ib;
   }

    protected override void Draw(GameTime gameTime)
    {
        // buffer reference again; these are persistent once filled in Update, they don't get created anew
        GraphicsBuffers gb = gameWorld.Render(graphics.GraphicsDevice);

        foreach (EffectPass effectPass in basicEffect.CurrentTechnique.Passes)
        {
            effectPass.Apply();

            basicEffect.Texture = textures;
            graphics.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, gb.vb.VertexCount, 0, gb.ib.IndexCount / 3);
        }
    }
4

4 回答 4

4

XNA Game 类和相关的 GraphicsDeviceManager 是围绕 Update 函数用于更新游戏状态而 Draw 函数用于渲染该状态的概念构建的。当您在更新功能期间设置图形设备的状态时,您会打破该假设。

当您切换到全屏时,设备会丢失。框架的幕后发生了很多神奇的事情来隐藏它的复杂性。必须重新创建与设备关联的资源等。框架假设您在下一个 Draw 函数之前不会再次执行任何渲染操作,因此在此之前它不能保证一致的状态。

所以...不要在更新功能期间在图形设备上设置缓冲区。在绘图功能开始时执行此操作。

我不确定这是否在任何地方都明确记录。方法文档更暗示了这一点。Game.Draw 文档说:

当游戏确定是时候绘制框架时调用。使用特定于游戏的渲染代码覆盖此方法。

并且更新方法说:

当游戏确定需要处理游戏逻辑时调用。这可能包括游戏状态的管理、用户输入的处理或模拟数据的更新。使用特定于游戏的逻辑覆盖此方法。

于 2011-01-18T02:52:03.170 回答
3

根据 App Hub 论坛上的一个答案,您必须确保重建投影矩阵。

http://forums.create.msdn.com/forums/p/31773/182231.aspx

从那个链接....

此外,在切换全屏时要注意不同的纵横比。如果您在 3D 中,则需要重建投影矩阵。如果您在 2D 中,您可能需要将缩放矩阵传递给 SpriteBatch.Begin 函数。本文包含有关在 2D 中执行此操作的信息。

所以你要确保你正在这样做(不幸的是,发布链接的文章在 Ziggyware 上并且该网站已经消失了很长一段时间,所以要找到他们链接到你的 2D 示例可能必须使用一些存档如果您有兴趣查看 WaybackMachine 之类的网站,因为您正在做 3D,我认为它可能不感兴趣)。

于 2011-01-17T15:17:24.277 回答
2

我能够使用以下类切换全屏/窗口模式:

/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;

    private Model grid;
    Model tank;
    int zoom = 1;

    Texture2D thumb;
    Vector2 position = new Vector2( 400, 240 );
    Vector2 velocity = new Vector2( -1, -1 );
    Matrix projection, view;
    public Game1()
    {
        graphics = new GraphicsDeviceManager( this );
        Content.RootDirectory = "Content";
    }

    /// <summary>
    /// Allows the game to perform any initialization it needs to before starting to run.
    /// This is where it can query for any required services and load any non-graphic
    /// related content.  Calling base.Initialize will enumerate through any components
    /// and initialize them as well.
    /// </summary>
    protected override void Initialize()
    {
        // TODO: Add your initialization logic here

        base.Initialize();

        projection = Matrix.CreatePerspectiveFieldOfView( MathHelper.PiOver4,
                                                                GraphicsDevice.Viewport.AspectRatio,
                                                                10,
                                                                20000 );

        view = Matrix.CreateLookAt( new Vector3( 1500, 550, 0 ) * zoom + new Vector3( 0, 150, 0 ),
                                          new Vector3( 0, 150, 0 ),
                                          Vector3.Up );

    }

    /// <summary>
    /// LoadContent will be called once per game and is the place to load
    /// all of your content.
    /// </summary>
    protected override void LoadContent()
    {
        // Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = new SpriteBatch( GraphicsDevice );

        // TODO: use this.Content to load your game content here
        thumb = Content.Load<Texture2D>( "GameThumbnail" );
        grid = Content.Load<Model>( "grid" );
        tank = Content.Load<Model>( "tank" );

    }

    /// <summary>
    /// UnloadContent will be called once per game and is the place to unload
    /// all content.
    /// </summary>
    protected override void UnloadContent()
    {
        // TODO: Unload any non ContentManager content here
    }

    int ToggleDelay = 0;
    /// <summary>
    /// Allows the game to run logic such as updating the world,
    /// checking for collisions, gathering input, and playing audio.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Update( GameTime gameTime )
    {
        // Allows the game to exit
        if ( GamePad.GetState( PlayerIndex.One ).Buttons.Back == ButtonState.Pressed )
            this.Exit();

        var kbState = Keyboard.GetState();
        if ( ( kbState.IsKeyDown( Keys.LeftAlt ) || kbState.IsKeyDown( Keys.RightAlt ) ) &&
             kbState.IsKeyDown( Keys.Enter ) && ToggleDelay <= 0 )
        {
            graphics.ToggleFullScreen();
            ToggleDelay = 1000;
        }

        if ( ToggleDelay >= 0 )
        {
            ToggleDelay -= gameTime.ElapsedGameTime.Milliseconds;
        }
        // TODO: Add your update logic here

        int x, y;
        x = (int)position.X;
        y = (int)position.Y;

        x += (int)velocity.X;
        y += (int)velocity.Y;
        if ( x > 480 - 64 )
            velocity.X = +1;
        if ( x < 0 )
            velocity.X = -1;
        if ( y < 0 )
            velocity.Y = +1;
        if ( y > 800 - 64 )
            velocity.Y = -1;

        position.X = x;
        position.Y = y;

        base.Update( gameTime );
    }

    /// <summary>
    /// This is called when the game should draw itself.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Draw( GameTime gameTime )
    {
        GraphicsDevice.Clear( Color.CornflowerBlue );

        Matrix rotation = Matrix.CreateRotationY( gameTime.TotalGameTime.Seconds * 0.1f );

        // Set render states.
        GraphicsDevice.BlendState = BlendState.Opaque;
        GraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise;
        GraphicsDevice.DepthStencilState = DepthStencilState.Default;
        GraphicsDevice.SamplerStates[ 0 ] = SamplerState.LinearWrap;

        grid.Draw( rotation, view, projection );

        DrawModel( tank, rotation, view, projection );

        // TODO: Add your drawing code here
        spriteBatch.Begin();
        spriteBatch.Draw( thumb, new Rectangle( (int)position.X, (int)position.Y, 64, 64 ), Color.White );
        spriteBatch.End();

        base.Draw( gameTime );
    }

    public void DrawModel( Model model, Matrix world, Matrix view, Matrix projection )
    {
        // Set the world matrix as the root transform of the model.
        model.Root.Transform = world;

        // Allocate the transform matrix array.
        Matrix[] boneTransforms = new Matrix[ model.Bones.Count ];

        // Look up combined bone matrices for the entire model.
        model.CopyAbsoluteBoneTransformsTo( boneTransforms );

        // Draw the model.
        foreach ( ModelMesh mesh in model.Meshes )
        {
            foreach ( BasicEffect effect in mesh.Effects )
            {
                effect.World = boneTransforms[ mesh.ParentBone.Index ];
                effect.View = view;
                effect.Projection = projection;

                //switch (lightMode)
                //{
                //    case LightingMode.NoLighting:
                //        effect.LightingEnabled = false;
                //        break;

                //    case LightingMode.OneVertexLight:
                //        effect.EnableDefaultLighting();
                //        effect.PreferPerPixelLighting = false;
                //        effect.DirectionalLight1.Enabled = false;
                //        effect.DirectionalLight2.Enabled = false;
                //        break;

                //    case LightingMode.ThreeVertexLights:
                //effect.EnableDefaultLighting();
                //effect.PreferPerPixelLighting = false;
                //        break;

                //    case LightingMode.ThreePixelLights:
                //        effect.EnableDefaultLighting();
                //        effect.PreferPerPixelLighting = true;
                //        break;
                //}

                effect.SpecularColor = new Vector3( 0.8f, 0.8f, 0.6f );
                effect.SpecularPower = 16;
                effect.TextureEnabled = true;
            }

            mesh.Draw();
        }
    }
}
于 2011-01-17T15:19:59.377 回答
0

因此,经过一些测试,切换全屏似乎会丢失顶点缓冲区。也就是说,我的缓冲区仍然存在,但它不再连接到设备。当我从 Update 函数中取出这一行并将其添加到 Draw 函数时,一切正常:

    graphics.GraphicsDevice.SetVertexBuffer(gb.vb, 0);

奇怪的是,索引缓冲区似乎没有受到影响。

不过,我仍然不明白为什么会这样,因为我在任何地方都找不到对这种现象的单一参考。我什至不知道这是否是我的配置所特有的,还是顶点缓冲区和全屏的一般问题,或者什么。如果有人有任何线索,我将不胜感激。

于 2011-01-18T01:36:37.197 回答