Sign in to follow this  
GroZZleR

C# High and Low Precision Timers [Source Inside]

Recommended Posts

GroZZleR    820
Hey all, I just finished writing the timers for my newest game engine. I figured I might as well share it for those looking for a solution in their own projects. Snag it from here. There are a total of 4 classes (one test form) and 1 interface. ITimer (interface) It defines the interface for all the timers. It defines 2 methods (Update() and Reset()) and 2 properties (DeltaTime and TotalTime). Update() should be called every game loop and DeltaTime used to move your objects. HighPrecisionTimer (class) : ITimer This attempts to use the query performance counter (QPC). It's very accurate and should always be used if available. LowPrecisionTimer (class) : ITimer This busts out timeGetTime() and isn't very accurate (as you can see in the test app). If you're going to use this one, limit your frame rate so you always get a decent time step. SmartTimer (class) : ITimer This one attempts to use a HighPrecisionTimer for it's timing, but if that's not available, it'll use a LowPrecisionTimer. It wraps up all the QPC testing for you, so you should always use this one. Some may find this solution overkill - but I don't find OO overkill. If you prefer something like below you're most likely a massochist. if(highPerformance) DoHighPrecisionUpdateCode(); else DoLowPrecisionUpdateCode(); ;) So enjoy. Let me know if it works or doesn't work so I can track down any bugs. EDIT: Pause() and Resume() have been added to ITimer as suggested by Lenolian. [Edited by - GroZZleR on June 28, 2005 4:01:22 PM]

Share this post


Link to post
Share on other sites
Lenolian    140
It works well here, besides the usual warning that System.Windows.Forms.AutoScaleBaseSize is deprecated.

But i would add the possibility to start and stop the timer at will. Its then easy to pause the game and restart it.

Share this post


Link to post
Share on other sites
_DarkWIng_    602
Works just fine, but you should know that all this (+a bit more) is already implemented in .NET 2.0 in System.Diagnostics.Stopwatch. But it's really a nice solution for 1.1.

Share this post


Link to post
Share on other sites
GroZZleR    820
I'm still using version 1.1 of the framework, so I didn't know. Does the 2.0 version still use InterOp? It'd be nice to not have to use it in the future, but this works well enough for now.

I've gone ahead and added Pause() and Resume() to ITimer as Lenolian suggested. Snag the updated version here. Good call Lenolian.

If there's any problems with the timers (including updated pause / resume), let me know.

Share this post


Link to post
Share on other sites
Lenolian    140
Quote:
Works just fine, but you should know that all this (+a bit more) is already implemented in .NET 2.0 in System.Diagnostics.Stopwatch.


Doh, couldn't you told me that before. That would have save me tons of testing for my implementation... :) Anyway thanks for the tips, i have switched to that and its a lot more easier.


I was thinking of another feature that might be nice to a game timer : the ability to speed it up or to slow it down. That would allow to make slow motions and such quite easily.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this