Sunday, October 18, 2015

Millisecond Timer

   So a while back I discovered that windows can't do microsecond accurate timers (at least not in a straight forward way). Windows is not real time operating system so events are triggered differently for different computers. There are ways like the MicroLib I found on the internet a while back but it is very resource hungry using up almost 100 percent of one of my cores. This is because it is essentially polling a stopwatch and seeing if the number of ticks is equal to or greater than the specified Tick which is calculated per computer. I have tried to modify it to be less resource hungry but my attempts failed. I recently found some code which utilizes the windows multimedia api to achieve a more accurate millisecond but it was only accurate down to the millisecond and cant achieve for example 16.7 milliseconds. I then thought hey I can put the two together. This millisecond timer can achieve closer to the specified millisecond like 16.7ms which is the duration of a frame in a 60FPS game. Now this isn't perfect but I'd think this is as good as it gets (for me its exactly what I needed). If there are better methods/alternatives please comment or if you have any suggestions or improvements.

[UPDATE JAN 05 2016]
I got around to redoing my GameTimer. The previous one was very inaccurate. For example I would specify 240FPS but it would give me ~230FPS and when specifying some other rates it is also all over the place. This new one addresses those as well as keeping all the features of the old one.

Source (VB.NET):

No comments: