rifter1818 Posted March 19, 2004 Posted March 19, 2004 What is the best way to get a more accurate tick count than enviornment.tickcount? is the directxTimer the best way to go? Quote
*Experts* Nerseus Posted March 19, 2004 *Experts* Posted March 19, 2004 Check out QueryPerformanceCounter on google or these forums. -Nerseus Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
wyrd Posted March 19, 2004 Posted March 19, 2004 Don't use Environment.TickCount for games. It's not very accurate and can lead to some performance issues, unless you wrap it in a singleton. Here's the timer I use in my games (it's included in the Phoenix library); Usage: // Notes: You can spit out the FPS at any time with timer.FramesPerSecond // You can access the timer at any time, anywhere, with Timer.Instance. // This way you don't have to pass a variable around to every method that updates // game logic if you don't want to. Timer timer = Timer.Instance; timer.Reset(); while (_gameIsRunning) { timer.BeginFrame(); // Update game logic, etc using the timer. UpdateGameLogic(timer.FrameTime); timer.EndFrame(); } The class itself: using System; using System.Runtime.InteropServices; namespace Phoenix.Game { /// <summary> /// The timer keeps track of ticks per second. /// </summary> public class Timer { #region Imported Methods /// <summary> /// The current system ticks (count). /// </summary> /// <param name="lpPerformanceCount">Current performance count of the system.</param> /// <returns>False on failure.</returns> [DllImport("Kernel32.dll")] private static extern bool QueryPerformanceCounter(out long lpPerformanceCount); /// <summary> /// Ticks per second (frequency) that the high performance counter performs. /// </summary> /// <param name="lpFrequency">Frequency the higher performance counter performs.</param> /// <returns>False if the high performance counter is not supported.</returns> [DllImport("Kernel32.dll")] private static extern bool QueryPerformanceFrequency(out long lpFrequency); #endregion #region Static Members private static readonly Timer _timer = new Timer(); #endregion #region Members private long _startTime; private long _endTime; private float _frameTime; #endregion #region Constructor /// <summary> /// Cannot instantiate the timer directly. /// </summary> private Timer() { } #endregion #region Timer Methods /// <summary> /// Resets the timer for a new game. /// </summary> public void Reset() { // Time needs to be initialized to current system count. _startTime = Timer.Ticks; } /// <summary> /// Stops the timer for current frame and records the frame time. /// </summary> public void BeginFrame() { _endTime = Timer.Ticks; _frameTime = (float) (_endTime - _startTime) / (float) Timer.Frequency; } /// <summary> /// Starts the timer for next frame. /// </summary> public void EndFrame() { _startTime = _endTime; } #endregion #region Static Properties /// <summary> /// Gets the timer instance. /// </summary> static public Timer Instance { get { return _timer; } } /// <summary> /// Gets the frequency that all timers performs at. /// </summary> static public long Frequency { get { long freq = 0; Timer.QueryPerformanceFrequency(out freq); return freq; } } /// <summary> /// Gets the current system ticks. /// </summary> static public long Ticks { get { long ticks = 0; Timer.QueryPerformanceCounter(out ticks); return ticks; } } #endregion #region Properties /// <summary> /// Gets the time recorded between frames. /// </summary> public float FrameTime { get { return _frameTime; } } /// <summary> /// Gets the frames per second based on the time between frames. /// </summary> public float FramesPerSecond { get { return 1.0F / _frameTime; } } #endregion } } Quote Gamer extraordinaire. Programmer wannabe.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.