C#游戏开发中精确的时间调配

方法一:参考《精通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 = 0; long _previousElapsedTime = 0; public PreciseTimer() { QueryPerformanceFrequency(ref _ticksPerSecond); GetElapsedTime(); // Get rid of first rubbish result } public double GetElapsedTime() { long time = 0; QueryPerformanceCounter(ref time); double elapsedTime = (double)(time - _previousElapsedTime) / (double)_ticksPerSecond; _previousElapsedTime = time; return elapsedTime; } } }


                            

你可能感兴趣的:(游戏开发)