Sign in to follow this  
SH_Algernon

[MDX] Solved: Class containing Timer method for using the high-resolution timer

Recommended Posts

I'm having a problem locating the class that contains the Timer method, which uses the CPU performance counter as a very-high precision timer. I'm using DirectX 9.0c complete SDK (February, 2006) with C# and VS2003 .NET. According to one book, but which was written for DirectX 9.0b, this method should be in DXUtil.cs class, in the DirectX SDK directory. The search for this class however, returned no results. At first I though I have to add assembly reference to another DX component, but the only components I can see in the list are standard DX components (Direct3D, Direct3DX, Direct3DX, DirectDraw, DirectInput, DirectSound, DirectPlay, AudioVideoPlayback and DirectX.Diagnostics). So, would anybody know how to use this timer in DirectX 9.0c, since it seems that Microsoft did quite some changes between DX9.0b and DX9.0c. Thanks in advance. [Edited by - SH_Algernon on March 25, 2006 12:25:54 PM]

Share this post


Link to post
Share on other sites
The timers are spread across dxmut.cs and dxmutmisc.cs
I think what you were looking for is found on line 267 and 271 in dxmutmisc.cs


[System.Security.SuppressUnmanagedCodeSecurity] // We won't use this maliciously
[DllImport("kernel32")]
public static extern bool QueryPerformanceFrequency(ref long, PerformanceFrequency);

[System.Security.SuppressUnmanagedCodeSecurity] // We won't use this maliciously
[DllImport("kernel32")]
public static extern bool QueryPerformanceCounter(ref long PerformanceCount);



You can find more information in dxmutmisc.cs as to how to use these counters and calculate different variables such as elapsed time.

I hope this helps.
Take care.

Share this post


Link to post
Share on other sites
Sorry for the late replay, I was a bit busy with other things.
Yes, I managed to get the high-resolution timer to work, thanks Armadon. Timer was indeed in dxmutmisc.cs class, so I just copied all the code required for the timer from there to a new FrameworkTimer class. If anyone needs it, here it is:


using System;
using System.IO;
using System.Collections;
using System.Runtime.InteropServices;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

#region Timer
public class FrameworkTimer
{
[System.Security.SuppressUnmanagedCodeSecurity] // We won't use this maliciously
[DllImport("kernel32")]
public static extern bool QueryPerformanceFrequency(ref long PerformanceFrequency);

[System.Security.SuppressUnmanagedCodeSecurity] // We won't use this maliciously
[DllImport("kernel32")]
public static extern bool QueryPerformanceCounter(ref long PerformanceCount);

#region Instance Data
private static bool isUsingQPF;
private static bool isTimerStopped;
private static long ticksPerSecond;
private static long stopTime;
private static long lastElapsedTime;
private static long baseTime;
#endregion

#region Creation
private FrameworkTimer() { } // No creation
/// <summary>
/// Static creation routine
/// </summary>
static FrameworkTimer()
{
isTimerStopped = true;
ticksPerSecond = 0;
stopTime = 0;
lastElapsedTime = 0;
baseTime = 0;
// Use QueryPerformanceFrequency to get frequency of the timer
isUsingQPF = QueryPerformanceFrequency(ref ticksPerSecond);
}
#endregion

/// <summary>
/// Resets the timer
/// </summary>
public static void Reset()
{
if (!isUsingQPF)
return; // Nothing to do

// Get either the current time or the stop time
long time = 0;
if (stopTime != 0)
time = stopTime;
else
QueryPerformanceCounter(ref time);

baseTime = time;
lastElapsedTime = time;
stopTime = 0;
isTimerStopped = false;
}

/// <summary>
/// Starts the timer
/// </summary>
public static void Start()
{
if (!isUsingQPF)
return; // Nothing to do

// Get either the current time or the stop time
long time = 0;
if (stopTime != 0)
time = stopTime;
else
QueryPerformanceCounter(ref time);

if (isTimerStopped)
baseTime += (time - stopTime);
stopTime = 0;
lastElapsedTime = time;
isTimerStopped = false;
}

/// <summary>
/// Stop (or pause) the timer
/// </summary>
public static void Stop()
{
if (!isUsingQPF)
return; // Nothing to do

if (!isTimerStopped)
{
// Get either the current time or the stop time
long time = 0;
if (stopTime != 0)
time = stopTime;
else
QueryPerformanceCounter(ref time);

stopTime = time;
lastElapsedTime = time;
isTimerStopped = true;
}
}

/// <summary>
/// Advance the timer a tenth of a second
/// </summary>
public static void Advance()
{
if (!isUsingQPF)
return; // Nothing to do

stopTime += ticksPerSecond / 10;
}

/// <summary>
/// Get the absolute system time
/// </summary>
public static double GetAbsoluteTime()
{
if (!isUsingQPF)
return -1.0; // Nothing to do

// Get either the current time or the stop time
long time = 0;
if (stopTime != 0)
time = stopTime;
else
QueryPerformanceCounter(ref time);

double absolueTime = time / (double)ticksPerSecond;
return absolueTime;
}

/// <summary>
/// Get the current time
/// </summary>
public static double GetTime()
{
if (!isUsingQPF)
return -1.0; // Nothing to do

// Get either the current time or the stop time
long time = 0;
if (stopTime != 0)
time = stopTime;
else
QueryPerformanceCounter(ref time);

double appTime = (double)(time - baseTime) / (double)ticksPerSecond;
return appTime;
}

/// <summary>
/// get the time that elapsed between GetElapsedTime() calls
/// </summary>
public static double GetElapsedTime()
{
if (!isUsingQPF)
return -1.0; // Nothing to do

// Get either the current time or the stop time
long time = 0;
if (stopTime != 0)
time = stopTime;
else
QueryPerformanceCounter(ref time);

double elapsedTime = (double)(time - lastElapsedTime) / (double)ticksPerSecond;
lastElapsedTime = time;
return elapsedTime;
}

/// <summary>
/// Returns true if timer stopped
/// </summary>
public static bool IsStopped
{
get { return isTimerStopped; }
}
}
#endregion



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