WPF constant frame rate for game

I am creating a simple simulator 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 speed in each frame). That way, I could subscribe to an event that calculates and updates positions according to the physics of the game in the CompositionTarget.Rendering event.

CompositionTarget.Rendering += UpdatePositions; 

I searched for it for a long time and did not find the answer. Fps in WPF seem arbitrary , and using a stopwatch to find out how much time has passed between the frame and the previous one will not be clean at all.

I also thought about creating my own frame rate, calling UpdatePositions every few milliseconds and hoping that Rendering would happen accordingly and have smooth animation. It is like reinventing the wheel, and I can't think of a way to implement this in a clean and simple way.

Thanks!

+8
c # wpf rendering
source share
2 answers

It is not possible to get a constant frame rate in WPF. WPF is based on the idea of ​​dynamic frame rates, and this makes it almost useless for any game development.

Some related information:

Why is the frame rate in WPF irregular and not limited for monitor updates?

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

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

I have worked a lot with WPF animation, and it is very painful for me to get something to animate smoothly in WPF (if you want to, I have written some best practices here.)

I am sure there are many other reasons why WPF is not suitable for game development. For example, you cannot create a full-screen application in WPF because WPF applications will always be terminated. This means that you cannot change the resolution for your game to make it smooth in full-screen mode, which in fact means that you cannot create a full-screen game in WPF and expect good results.

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

+7
source share

Even if you don’t want to go with XNA to develop games, think about how time management is done - a fixed frame rate is not necessarily equal to or required for stable game time. Try to base your physics on playing time (it can be the same as in real time or calculated on the basis of real-time deltates, i.e. if you want to have a "fast forward" mode or pause the game) instead of the frame rate.

After two links, “playing time”, “real time” and related concepts are discussed: http://blogs.msdn.com/shawnhar/archive/2007/07/25/understanding-gametime.aspx , http: // blogs .msdn.com / shawnhar / archive / 2007/11/23 / game-timing-in-xna-game-studio-2-0.aspx

+2
source share

All Articles