⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 timer.cs

📁 《3d游戏编程入门经典》中范例小游戏。使用C#+DirectX开发。
💻 CS
字号:
using System;
using System.IO;
using System.Runtime.InteropServices;

/// <summary>
/// Generic high resolution timer class
/// </summary>
public class HighResTimer
{
	#region API Declares
    [System.Security.SuppressUnmanagedCodeSecurity] // We won't use this maliciously
    [DllImport("kernel32")]
    private static extern bool QueryPerformanceFrequency(ref long PerformanceFrequency);
    [System.Security.SuppressUnmanagedCodeSecurity] // We won't use this maliciously
    [DllImport("kernel32")]
    private static extern bool QueryPerformanceCounter(ref long PerformanceCount);
    #endregion

    #region Instance Data
    private long numberTicksPerSec = 0;
    private long startTime = 0;
    private long elapsedTime = 0;
    private long lastTime = 0;

    private bool isTimerRunning = false;
	#endregion

    #region Class Methods
    /// <summary>
    /// Initialize the timer
    /// </summary>
    public HighResTimer()
    {
        // Use QueryPerformanceFrequency() to get frequency of timer.  
        long ticksPerSec = 0;
        if (QueryPerformanceFrequency(ref ticksPerSec))
        {
            numberTicksPerSec = ticksPerSec;
        }
        else
        {
            // Unfortunately we can't use QPF, throw an error for this case.
            throw new InvalidOperationException("Your system does not support a high resolution timer.");
        }
    }

    /// <summary>
    /// Start the timer
    /// </summary>
    public void Start()
    {
        if (!QueryPerformanceCounter(ref startTime))
        {
            throw new InvalidOperationException("Failed to get the latest performance counter");
        }
        lastTime = startTime;
        isTimerRunning = true;
    }

    /// <summary>
    /// If we're stopping, update once, then stop
    /// </summary>
    public void Stop()
    {
        Update();
        isTimerRunning = false;
    }

    /// <summary>
    /// Called once per frame
    /// </summary>
    public void Update()
    {
        if (!isTimerRunning)
            return; // Nothing to do

        // Store the last elapsed time
        elapsedTime = lastTime;

        if (!QueryPerformanceCounter(ref lastTime))
        {
            throw new InvalidOperationException("Failed to get the latest performance counter");
        }
    }

    #endregion

    #region Properties
    /// <summary>
    /// Determines if a timer is actually running or not
    /// </summary>
    public bool IsRunning
    {
        get { return isTimerRunning; }
    }
    /// <summary>
    /// The number of seconds that have passed since the last update
    /// </summary>
    public float ElapsedTime
    {
        get
        {
            return ((float)(lastTime - elapsedTime) / (float)numberTicksPerSec);
        }
    }
    /// <summary>
    /// The number of seconds that have passed since the timer started
    /// </summary>
    public float TotalTime
    {
        get
        {
            return ((float)(lastTime - startTime) / (float)numberTicksPerSec);
        }
    }
    #endregion
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -