• XNA游戏开发之(四)——改变Draw频率


            【原创】Alex

      当运行XNA游戏时,XNA会以尽可能大的频率调用Draw方法

      Draw的频率大于屏幕刷新频率时,Draw设置失效。

      如果屏幕刷新频率只有每秒100次,每秒绘制110次是无用的。在PC和Xbox360平台上,,屏幕刷新率是由PC屏幕和它的设置决定的,Zune 30每秒刷新60次,其他Zune设备每秒刷新30次。

      Update方法每秒调用60次。如果游戏计算量太大,Draw方法调用次数会变少以保证Update方法可以每秒调用60次。

      在某些情况中,以最大频率调用Draw方法是有用的,需要在XNA游戏中使用的最大帧频率时,可以将graphics.SynchronizeWithVerticalRetrace变量设置为true。

         必须在Game的构造函数顶部加入这行代码,因为XNA需要在创建GraphicsDevice 之前知道这个设置

    1 using System;
    2  using System.Collections.Generic;
    3  using System.Linq;
    4 using Microsoft.Xna.Framework;
    5 using Microsoft.Xna.Framework.Audio;
    6 using Microsoft.Xna.Framework.Content;
    7 using Microsoft.Xna.Framework.GamerServices;
    8 using Microsoft.Xna.Framework.Graphics;
    9 using Microsoft.Xna.Framework.Input;
    10 using Microsoft.Xna.Framework.Media;
    11 using Microsoft.Xna.Framework.Net;
    12 using Microsoft.Xna.Framework.Storage;
    13
    14 namespace Alex
    15 {
    16 /// <summary>
    17 /// This is the main type for your game
    18 /// </summary>
    19 public class AlexGame : Microsoft.Xna.Framework.Game
    20 {
    21 GraphicsDeviceManager graphics;
    22 SpriteBatch spriteBatch;
    23
    24 public AlexGame()
    25 {
    26 graphics = new GraphicsDeviceManager(this);
    27 Content.RootDirectory = "Content";
    28 //将Draw刷新频率设置为最大
    29 graphics.SynchronizeWithVerticalRetrace = true;
    30 }
    31
    32 /// <summary>
    33 /// Allows the game to perform any initialization it needs to before starting to run.
    34 /// This is where it can query for any required services and load any non-graphic
    35 /// related content. Calling base.Initialize will enumerate through any components
    36 /// and initialize them as well.
    37 /// </summary>
    38 protected override void Initialize()
    39 {
    40 // TODO: Add your initialization logic here
    41 this.TargetElapsedTime = TimeSpan.FromSeconds(1.0f / 100.0f);
    42 base.Initialize();
    43 }
    44
    45 /// <summary>
    46 /// LoadContent will be called once per game and is the place to load
    47 /// all of your content.
    48 /// </summary>
    49 protected override void LoadContent()
    50 {
    51 // Create a new SpriteBatch, which can be used to draw textures.
    52 spriteBatch = new SpriteBatch(GraphicsDevice);
    53
    54 // TODO: use this.Content to load your game content here
    55 }
    56
    57 /// <summary>
    58 /// UnloadContent will be called once per game and is the place to unload
    59 /// all content.
    60 /// </summary>
    61 protected override void UnloadContent()
    62 {
    63 // TODO: Unload any non ContentManager content here
    64 }
    65
    66 /// <summary>
    67 /// Allows the game to run logic such as updating the world,
    68 /// checking for collisions, gathering input, and playing audio.
    69 /// </summary>
    70 /// <param name="gameTime">Provides a snapshot of timing values.</param>
    71 protected override void Update(GameTime gameTime)
    72 {
    73 // Allows the game to exit
    74 if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
    75 {
    76 this.Exit();
    77 }
    78 if (Keyboard.GetState().IsKeyDown(Keys.Escape))
    79 {
    80 this.Exit();
    81 }
    82
    83 this.Window.Title = gameTime.IsRunningSlowly.ToString();
    84
    85 // TODO: Add your update logic here
    86
    87 base.Update(gameTime);
    88 }
    89
    90 /// <summary>
    91 /// This is called when the game should draw itself.
    92 /// </summary>
    93 /// <param name="gameTime">Provides a snapshot of timing values.</param>
    94 protected override void Draw(GameTime gameTime)
    95 {
    96 GraphicsDevice.Clear(Color.CornflowerBlue);
    97
    98 // TODO: Add your drawing code here
    99
    100 base.Draw(gameTime);
    101 }
    102 }
    103 }
    104

    Update和Draw方法调用频率的重要性

    更新逻辑放置在Update方法中,Update频率的减少会导致游戏中的所有物体变慢。

    当Draw调用频率小于屏幕刷新频率时,只有游戏的视觉表现会暂时受影响,游戏帧频率暂时由每秒100帧降到80帧不容易察觉。

    如果必要,XNA会降低Draw的频率以保证Update能以每秒60帧的频率调用。

  • 相关阅读:
    Windows Forms中通过自定义组件实现统一的数据验证(二)
    The WindowsClient.NET Community Site Launches
    二十六岁,仍在路上
    Visual Studio 2008 Express版本下载
    Page Controller及其在ASP.NET中的实现
    iBATIS In Action:使用映射语句(二)
    在VS2005中创建项目模板来提高开发效率
    2007年,听见春天的脚步
    iBATIS In Action:使用映射语句(一)
    iBATIS In Action:序言和目录
  • 原文地址:https://www.cnblogs.com/AlexCheng/p/2120351.html
Copyright © 2020-2023  润新知