方法一:参考《精通C#游戏编程》一书。根据学习WorldWind源码可知,WorldWind中采用的方法与该方法基本一致。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices; namespace GameLoop//根据需要修改名称空间
{
public class PreciseTimer
{
[System.Security.SuppressUnmanagedCodeSecurity]
[DllImport("kernel32")]
private static extern bool QueryPerformanceFrequency(ref long PerformanceFrequency); [System.Security.SuppressUnmanagedCodeSecurity]
[DllImport("kernel32")]
private static extern bool QueryPerformanceCounter(ref long PerformanceCount); long _ticksPerSecond = ;
long _previousElapsedTime = ; public PreciseTimer()
{
QueryPerformanceFrequency(ref _ticksPerSecond);
GetElapsedTime(); // Get rid of first rubbish result
} public double GetElapsedTime()
{
long time = ;
QueryPerformanceCounter(ref time); double elapsedTime = (double)(time - _previousElapsedTime) / (double)_ticksPerSecond;
_previousElapsedTime = time; return elapsedTime;
}
}
}