特に変わった事はしていないけれど、DXライブラリ関数を使わなくてもよいようにしてあります。
using System;
using System.Diagnostics;
using System.Threading;
class FPS
{
#region フィールド
///
/// 経過時間計測用
///
private Stopwatch m_stopwatch = new Stopwatch();
///
/// フレームカウンタ
///
private int m_count = 0;
///
/// リフレッシュレート
///
private int m_refresh = 60;
#endregion
#region プロパティ
///
/// リフレッシュレート(Hz)の設定&取得
///
public int RefreshRate
{
get
{
return m_refresh;
}
set
{
// ゼロ除算防止処置
if (value > 0)
{
m_refresh = value;
}
}
}
///
/// FPS値の取得
///
public double Value
{
get;
private set;
}
#endregion
#region メソッド
///
/// 更新処理
///
public void Update()
{
// 初回呼び出し?
if (m_count == 0)
{
// 時間計測開始
m_stopwatch.Reset();
m_stopwatch.Start();
}
// 更新タイミング?
else if (m_count == m_refresh)
{
// 平均値の算出
Value = 1000.0 / ((double)m_stopwatch.ElapsedMilliseconds / m_refresh);
// 時間計測開始
m_stopwatch.Reset();
m_stopwatch.Start();
// フレームカウンタの初期化
m_count = 0;
}
++m_count;
}
///
/// 待機処理
///
public void Wait()
{
// 目標時間を算出
int target = (int)(1000.0 / m_refresh * m_count);
// 待機時間 = 目標時間 - 実際の経過時間
int wait = target - (int)m_stopwatch.ElapsedMilliseconds;
// 待機時間が正値の場合は待機させる
if (wait > 0)
{
Thread.Sleep(wait);
}
}
#endregion
}