5

I'm new to C Sharp, and writing a game w/ the XNA Framework. I've created a project that's a heavily modified version of the "Platformer" XNA starter kit.

I'm seeing (seemingly) random fluctuations with the framerate. Sometimes it'll run at 60 FPS the whole time, sometimes it'll start at 60 FPS then drop to 49-52, and othertimes it'll drop to 49-52 immediately. Using Fraps to display framerate (not recording video to disk).

The unique nature of this game requires that it run at 60 FPS consistently.

So it seems some race condition or random factor is causing a difference between individual runs of the exe. Numerous rebuilds make no difference.

This fluctuation occurs on both my desktop and laptop with exactly the same frequency, so it's not an issue w/ hardware, anti-virus, etc.

I've searched about how to lock framerates in XNA, and my code seems to be doing much of what it needs to do, including an attempt to clamp at 60 FPS (using IsFixedTimeStep, SynchronizeWithVerticalRetrace).

The game is absolutely capable of 60 FPS from beginning to end, I see it all the time. When it's running at 60 FPS it does not tax CPU, RAM or any other resource as far as I can tell.

Anyone else experienced this?

Thanks, - S

4

7 回答 7

2

您描述的不一致意味着问题是由

  • 环境因素,例如另一个过程;或者
  • 游戏中并非每次运行都采用的代码路径

最可能的原因是您的两台计算机上都在运行另一个进程。

关闭所有非必要进程,如媒体播放器。Windows Media Player 和 iTunes 都可以在播放时降低帧速率。Fraps 应该没问题,只要它不记录,但我会实现你自己的内置 FPS 显示以确保。

使用 windows 性能监视器检查是否有进程消耗 cpu 或内存。尤其要查找无法正常关闭且仍在后台运行的游戏实例。

您可以尝试缩小环境原因的其他事项包括:

  • 确定干净重启后游戏是否以 60fps 运行
  • 确定游戏在第一次运行时是否始终以 60fps 运行
  • 从资源管理器而不是 Visual Studio 启动游戏
  • 确定在 Release 或 Debug 模式下运行是否有任何影响
  • 在朋友的电脑上运行你的游戏

如果原因来自游戏中的代码路径,而该代码路径并非在每次运行时都执行,您可以:

  • 反复玩游戏并尝试确定您在游戏中所做的事情会触发减速。
  • 实现输入记录和回放系统,使游戏中的相同运行完全可重复
  • 分析您的垃圾收集和一般性能以查找任何突出的问题
于 2011-02-23T08:47:07.147 回答
1

我会检查这些周期性的减速是否与 GC 事件一致,特别是第 1 代或第 2 代。根据您的描述,这似乎很合理。如果是这样,看看你是否可以通过重用、堆栈分配等来减少代码中的对象流失。

于 2011-02-23T12:51:01.733 回答
1

我有同样的问题。我基于 Platformer 初学者工具包创建了一个游戏,我使用的是 Windows 7。该程序大部分时间以 60 FPS 运行良好,但有时它会下降到 52 FPS 约 30 秒,然后它会运行回到 60 帧/秒。

我发现通过关闭 Windows 7 Aero 功能,我的游戏现在始终保持在 60 FPS。我不得不切换到没有 Aero 的 Windows 7 Basic 主题,现在游戏运行良好且流畅。

于 2011-07-22T12:50:08.863 回答
0

你可以完全回避这个问题。只要您的游戏以至少约 35 fps 的速度运行,人眼就不会注意到帧率下降。为了回避这个问题,请确保您的所有更新代码都考虑到它传递的 GameTime 对象。通常,您会将任何计算乘以它,以便在帧速率高时获得较小的计算,而在游戏更新缓慢时获得更明显的结果。总体而言,它将使您的游戏视觉效果看起来更加流畅。

您是否尝试过让游戏以最大帧率运行?如果是这样,您仍然会收到巨大的帧率下降吗?如果您不知道如何执行此操作,如果您需要,我可以在回家时发布代码。

于 2011-02-24T19:17:25.377 回答
0

您将永远无法强制游戏以 60 fps 运行。

如果游戏真的需要以这种速度运行,您可能正在每帧更新游戏状态。如果是这样,您应该将其更改为考虑已用时间的更新。

于 2011-03-04T06:41:11.333 回答
0

尝试禁用垂直同步SynchronizeWithVerticalRetrace=false,看看是否有帮助。如果在同步时间发生事件或者如果帧稍微太长而无法渲染,则垂直同步具有令人讨厌的副作用,因为它必须等待下一次同步。但是,您可能会经历撕裂。但是,如果帧率稳定性比无撕裂显示更重要,那可能是一个很好的折衷方案。

于 2011-02-24T20:01:21.660 回答
0

您是否在可见区域之外绘制任何内容?我有同样的问题(帧率会突然下降)。在尝试了一切之后,结果发现我有时会在可见区域之外或屏幕可见区域和不可见区域之间的中间绘制东西。鉴于您使用的是平台游戏入门套件,这可能也是您的问题。

于 2011-03-04T06:34:02.920 回答