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

来源:互联网 发布:学党史知国情作文450字 编辑:程序博客网 时间:2024/05/06 19:57

当运行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 ///
17 /// This is the main type for your game
18 ///
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 ///
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 ///
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 ///
46 /// LoadContent will be called once per game and is the place to load
47 /// all of your content.
48 ///
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 ///
58 /// UnloadContent will be called once per game and is the place to unload
59 /// all content.
60 ///
61 protected override void UnloadContent()
62 {
63 // TODO: Unload any non ContentManager content here
64 }
65
66 ///
67 /// Allows the game to run logic such as updating the world,
68 /// checking for collisions, gathering input, and playing audio.
69 ///
70 ///

Provides a snapshot of timing values.
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 ///
91 /// This is called when the game should draw itself.
92 ///
93 /// Provides a snapshot of timing values.
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帧的频率调用。