游戏的 WPF 中的恒定帧率 [英] Constant framerate in WPF for game

查看:50
本文介绍了游戏的 WPF 中的恒定帧率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 C# 在 WPF 中创建一个简单的车辆和弹丸模拟游戏.我需要有一个恒定的帧速率(即我需要知道在每帧中以特定速度移动对象的幅度).通过这种方式,我可以订阅一个根据游戏物理计算和更新位置的事件到 CompositionTarget.Rendering 事件.

Im creating a simple vehicle and projectile simulation game in WPF using C#. I need to have a constant frame rate (i.e. i need to know how much to move an object with a certain velocity in each frame). This way I could subscribe an event which calculates and updates positions according to the game's physics to the CompositionTarget.Rendering event.

CompositionTarget.Rendering += UpdatePositions;

我用谷歌搜索了一段时间,没有找到答案.WPF 中的 fps 似乎是任意的,并且使用秒表来知道之间经过了多少时间一帧和前一帧根本不干净.

I googled it a while, and found no answer. The fps in WPF seem to be arbitrary, and using a Stopwatch to know how much time elapsed between a frame and the previous one wouldn't be clean at all.

我还想通过每隔几毫秒调用一次 UpdatePositions 并希望相应地进行渲染并具有流畅的动画来创建自己的帧速率.这似乎是在重新发明轮子,我想不出一种以简洁明了的方式实现这一点的方法.

I thought also of creating my own frame rate, by calling UpdatePositions every some amount of milliseconds and hoping for the Rendering to occur accordingly and have a smooth animation. This seems like reinventing the wheel, and I cant think of a way of implementing this in a clean and simple manner.

谢谢!

推荐答案

在 WPF 中不可能获得恒定的帧率.WPF 基于动态帧速率的概念,这使得它对于任何游戏开发几乎毫无用处.

It is impossible to get a constant framerate in WPF. WPF is based on the idea of dynamic frame rates and this makes it near useless for any game development.

一些相关信息:

为什么是 FrameWPF 中的速率不规则且不限于监控刷新?

http://rhnatiuk.wordpress.com/2008/12/21/wpf-video-playback-problems/

不幸的是,正如其他人所指出的,解决方案是不使用 WPF 进行游戏开发.

As others have pointed out the solution is, unfortunately, not to use WPF for game development.

我已经广泛使用 WPF 动画,在 WPF 中获得任何平滑的动画绝对是一件痛苦的事情(如果您确实想这样做,我已经写下了一些 最佳实践在这里).

I have worked with WPF animations extensively and it is an absolute pain to get anything to smoothly animate in WPF (if you do want to do this I've written down a few best practices here).

我确信 WPF 不适合游戏开发的原因还有很多.一个示例是您无法在 WPF 中创建全屏应用程序,因为 WPF 应用程序始终是窗口化的.这意味着您无法更改游戏的分辨率以使其在全屏下流畅,这实际上意味着您无法在 WPF 中创建全屏游戏并期望获得良好的结果.

I am sure there are many other reasons why WPF is not good for game development. One example is that you cannot create a full screen application in WPF as WPF apps are always windowed. This means that you could not change the resolution for your game to make it smooth in full screen which effectively means that you could not create a full screen game in WPF and expect good results.

如果你对你的游戏或游戏开发很认真,而且这不仅仅是一个原型或只是为了好玩,那么我建议你放弃 WPF 并使用其他东西,否则你只会让自己失望和失望- 标准质量.

If you are serious about your game or game development in general and this is not just a prototype or just-for-fun then I recommend that you ditch WPF and use something else, otherwise you just set yourself up for disappointment and sub-par quality.

这篇关于游戏的 WPF 中的恒定帧率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆