CPU使用率过高与XNA [英] High CPU usage with XNA
问题描述
我今天才注意到,当我编译和运行一个新的XNA 4.0游戏中,CPU线程中的一个运行在100%和帧率下降到54 FPS。
I just noticed today, that when I compile and run a new XNA 4.0 game, one of CPU threads is running at 100% and the framerate drops to 54 FPS.
奇怪的是,有时它工作在60 FPS,但它只是下降到54 FPS。
The weird thing is that sometimes it works at 60 FPS, but then it just drops to 54 FPS.
我以前没有注意到这个问题,所以我不知道,如果这是正常的。我卸载了防病毒软件并重新安装XNA游戏工作室,XNA可再发行和.NET Framework 4。
I haven't noticed this behaviour before, so I don't know if this is normal. I uninstalled my antivirus and reinstalled XNA Game Studio, XNA Redistributable and .NET Framework 4.
如果我设置IsFixedTimeStep为false,比赛在60 FPS运行,CPU使用率最小(1-2%)。但据我所知,这需要我做什么用ElapsedGameTime速度计算,但我不知道该怎么做,因为我是相当新的XNA。但也有人说,它设置为false减少生涩的动画。
If I set IsFixedTimeStep to false, the game runs at 60 FPS and CPU usage is minimal (1-2%). but as far as I know, this requires me to do velocity calculations using ElapsedGameTime, but I don't know how to do it, since I'm fairly new to XNA. But some say that setting it to false reduces jerky animations.
我已经选中的this论坛主题,但都没有找到一个很好的解决方案。
I already checked this forum thread, but no one has found a good solution.
有没有人遇到过这个问题?
Has anyone experienced this problem?
编辑:
我做了一些更多的研究和我实现了一个FPS计数器(到现在为止,我用Fraps衡量的话),我的计数器显示游戏在60 FPS(与IsFixedTimeStep = TRUE)上运行,从而解决了FPS的问题,但高CPU使用率仍然存在。 ?是否有可能发生这种情况给大家。
I did some more research and I implemented a FPS counter (until now, I measured it with Fraps), and my counter shows the game running at 60 FPS (with IsFixedTimeStep = true), so this solves the FPS issue, but the high CPU usage remains. Is it possible that this happens to everyone?
推荐答案
据的这个讨论在Xbox Live独立游戏论坛,显然在某些处理器(和OS-S)XNA占用的 100%的CPU时间在一个核心时,的 Game.IsFixedTimeStep
使用
According to this discussion on XBox Live Indie Games forum , apparently on some processors (and OS-s) XNA takes up 100% CPU time on one core when the default value of Game.IsFixedTimeStep
is used.
一个<默认值STRONG>常见的解决方案(一说为我工作也一样)是把你的游戏
的构造函数如下:
A common solution (one that worked for me as well) is to put the following in your Game
constructor:
IsFixedTimeStep = false;
这是什么意思?
的
的 Game.IsFixedTimeStep
财产,当真正
,确保您的帧(更新()
,绘制()
,...)被称为在固定的时间间隔中规定 Game.TargetElapsedTime
。这将默认为每秒60
What does it mean?
The Game.IsFixedTimeStep
property, when true
, ensures that your frame (Update()
, Draw()
, ...) is called at a fixed time interval specified in Game.TargetElapsedTime
. This defaults to 60 calls per second.
在 Game.IsFixedTimeStep = FALSE
,当前一个结束到下一帧的呼叫会发生。
When Game.IsFixedTimeStep = false
, the calls to the next frame will happen when the previous one is finished. This is illustrated in the time graph below:
所有的固定时间计算(动作 定时等),将需要进行修改,以适应变化的时间步长。值得庆幸的是,这个很简单
All fixed time calculations (movements, timings, etc.) will need to be modified to accommodate variable time steps. Thankfully, this is very simple.
假设你有
Vector3 velocity;
Vector3 position;
对于一些对象,你正在更新与
for some object, and you are updating the position with
position += velocity;
在默认情况下,这将意味着您的对象的速度 <每秒code> 60 * velocity.Length()单位。你添加的速度每秒60次。
On default, this would mean that the speed of your object is 60 * velocity.Length()
units per second. You add velocity 60 times each second.
在您翻译的句子翻译成代码,你得到这个简单的修改
When you translate that sentence into code, you get this simple modification:
position += velocity * 60 * gameTime.ElapsedGameTime.TotalSeconds;
要说得简单:你缩放添加基于多少时间值已通过
通过在你执行移动的地方(或时间等),使这些修改,您会确保你的游戏行为,因为它会回来时,它被固定的时间步长。
By making these modifications in places where you perform moving (or timing, etc.), you'll ensure that your game acts as it would back when it was fixed time step.
这篇关于CPU使用率过高与XNA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!