📄 dxtimer.cs
字号:
using System;
using System.Runtime.InteropServices;
using System.Security;
namespace DDGameHelper
{
#region Timer 用到的命令参数枚举
/// <summary>
/// Enumeration for various actions our timer can perform
/// </summary>
public enum TimerCmd
{
/// <summary>
/// 复位
/// </summary>
Reset,
/// <summary>
/// 开始
/// </summary>
Start,
/// <summary>
/// 暂停
/// </summary>
Stop,
/// <summary>
/// 令 Stop Time 增加 0.1 秒
/// </summary>
Advance,
/// <summary>
/// 获取自游戏运行以来到现在经过的绝对时间
/// </summary>
GetAbsoluteTime,
/// <summary>
/// 获取自系统运行以来到现在经过的绝对时间
/// </summary>
GetApplicationTime,
/// <summary>
/// 获取自上次调用以来经过的时间(微秒)
/// </summary>
GetElapsedTime
};
#endregion
/// <summary>
/// DXTimer类(改自MS的示例代码)
/// </summary>
public class DXTimer
{
#region 私有成员定义
[DllImport("kernel32"), SuppressUnmanagedCodeSecurity]
private static extern bool QueryPerformanceFrequency(ref long PerformanceFrequency);
[DllImport("kernel32"), SuppressUnmanagedCodeSecurity]
private static extern bool QueryPerformanceCounter(ref long PerformanceCount);
private bool isTimerInitialized = false;
private bool m_bTimerStopped = true;
private long m_llQPFTicksPerSec = 0;
private long m_llStopTime = 0;
private long m_llLastElapsedTime = 0;
private long m_llBaseTime = 0;
#endregion
#region FPS 计算相关
private int _frames=0;
private double _lastTime=0.0f;
private double _framePerSecond=0.0f;
/// <summary>
/// 计算并获取每秒帧速
/// </summary>
public double CalculateFPS()
{
double time = Timer(TimerCmd.GetAbsoluteTime);
++_frames;
// Update the scene stats once per second
if (time - _lastTime > 1.0f)
{
_framePerSecond = _frames / (time - _lastTime);
_lastTime = time;
_frames = 0;
}
return _framePerSecond;
}
/// <summary>
/// 每秒帧速
/// </summary>
public double FramePerSecond
{
get{return _framePerSecond;}
}
#endregion
#region Timer相关
/// <summary>
/// 开始计时
/// </summary>
public void Start()
{
Timer(TimerCmd.Start);
}
/// <summary>
/// 获取自上次调用以来,经过的毫秒数
/// </summary>
public double GetElapsedMillisecond()
{
return Timer(TimerCmd.GetElapsedTime)*1000.0;
}
/// <summary>
/// 复位 timer
/// </summary>
public void Reset()
{
Timer(TimerCmd.Reset);
}
/// <summary>
/// Performs timer opertations. Use the following commands:
///
/// TimerCmd.Reset - to reset the timer
/// TimerCmd.Start - to start the timer
/// TimerCmd.Stop - to stop (or pause) the timer
/// TimerCmd.Advance - to advance the timer by 0.1 seconds
/// TimerCmd.GetAbsoluteTime - to get the absolute system time
/// TimerCmd.GetApplicationTime - to get the current time
/// TimerCmd.GetElapsedTime - to get the time that elapsed between TIMER_GETELAPSEDTIME calls
///
/// </summary>
public double Timer(TimerCmd command)
{
if (!isTimerInitialized)
{
isTimerInitialized = true;
QueryPerformanceFrequency(ref m_llQPFTicksPerSec);
}
double time, fElapsedTime;
long qwTime = 0;
// Get either the current time or the stop time, depending
// on whether we're stopped and what command was sent
if (m_llStopTime != 0 && command != TimerCmd.Start && command != TimerCmd.GetAbsoluteTime)
qwTime = m_llStopTime;
else
QueryPerformanceCounter(ref qwTime);
// Return the elapsed time
if (command == TimerCmd.GetElapsedTime)
{
fElapsedTime = (double) (qwTime - m_llLastElapsedTime) / (double) m_llQPFTicksPerSec;
m_llLastElapsedTime = qwTime;
return (double)fElapsedTime;
}
// Return the current time
if (command == TimerCmd.GetApplicationTime)
{
double fAppTime = (double) (qwTime - m_llBaseTime) / (double) m_llQPFTicksPerSec;
return (double)fAppTime;
}
// Reset the timer
if (command == TimerCmd.Reset)
{
m_llBaseTime = qwTime;
m_llLastElapsedTime = qwTime;
m_llStopTime = 0;
m_bTimerStopped = false;
return 0.0f;
}
// Start the timer
if (command == TimerCmd.Start)
{
if (m_bTimerStopped)
m_llBaseTime += qwTime - m_llStopTime;
m_llStopTime = 0;
m_llLastElapsedTime = qwTime;
m_bTimerStopped = false;
return 0.0f;
}
// Stop the timer
if (command == TimerCmd.Stop)
{
if (!m_bTimerStopped)
{
m_llStopTime = qwTime;
m_llLastElapsedTime = qwTime;
m_bTimerStopped = true;
}
return 0.0f;
}
// Advance the timer by 1/10th second
if (command == TimerCmd.Advance)
{
m_llStopTime += m_llQPFTicksPerSec/10;
return 0.0f;
}
if (command == TimerCmd.GetAbsoluteTime)
{
time = qwTime / (double) m_llQPFTicksPerSec;
return (double)time;
}
return -1.0f; // Invalid command specified
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -