gpt4 book ai didi

c# - 我如何在 XNA 中暂停重绘?

转载 作者:可可西里 更新时间:2023-11-01 07:46:56 25 4
gpt4 key购买 nike

我制作了一个 XNA 图像查看器,但它总是重新绘制场景,即使它没有改变,它让我的上网本烧得厉害,所以我希望它在没有任何变化时暂停绘图。

将帧率降低到 1 是保持凉爽的一种方法,但会导致输出滞后。

如何在没有输入的情况下防止重绘?


这个问题解决了,但是又发现了一个问题——游戏在窗口处于焦点状态时会消耗大量的CPU,而在非焦点状态时,它只占用大约1%的CPU。有关如何解决此其他问题的详细信息,请参阅此问题:

How to reduce XNA game CPU usage while nothing worth computing is happening?

最佳答案

您可以使用 Game.SupressDraw为此目的的方法。从链接的评论:

Call this method during Update to prevent any calls to Draw until after the next call to Update. This method can be used on small devices to conserve battery life if the display does not change as a result of Update. For example, if the screen is static with no background animations, the player input can be examined during Update to determine whether the player is performing any action. If no input is detected, this method allows the game to skip drawing until the next update.

例如,下面的代码只会调用一次Draw函数。当我对此进行测试时,我注意到在没有 SuppressDraw 调用的情况下,CPU 使用率很高 (70%)。使用 SupressDraw 时,CPU 使用率急剧下降(低于 15%)。您的数字可能会有所不同。

public class Game1 : Microsoft.Xna.Framework.Game
{
...
private bool _drawn = false;
protected override void Update(GameTime gameTime)
{
if (_drawn)
SuppressDraw();
}

protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
/* draw stuff here */
spriteBatch.End();

_drawn = true;
}
...
}

请注意,SupressDraw 只会抑制一次 Draw 调用。为了防止对 Draw 的更多调用,您必须在 Update 中不断调用 SupressDraw。希望这是有道理的。

关于c# - 我如何在 XNA 中暂停重绘?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16554717/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com