1
0
Fork 0
mirror of https://github.com/hawkeye-stan/msfs-popout-panel-manager.git synced 2024-11-25 15:20:10 +00:00
msfs-popout-panel-manager/SimconnectAgent/FpsCalc.cs

62 lines
1.8 KiB
C#
Raw Normal View History

2024-03-14 22:50:32 +00:00
using System;
using System.Linq;
namespace MSFSPopoutPanelManager.SimConnectAgent
{
public class FpsCalc
{
2024-09-19 18:30:52 +00:00
private const int FPS_LEN = 5;
2024-09-06 02:58:59 +00:00
private static readonly float[] FpsStatistic = new float[FPS_LEN];
2024-03-14 22:50:32 +00:00
private static int _fpsIndex = -1;
2024-09-06 02:58:59 +00:00
private static int _avgFps;
2024-09-19 18:30:52 +00:00
private static int _ignoreFpsSpikeCount = 0;
2024-03-14 22:50:32 +00:00
public static int GetAverageFps(int newValue)
{
if (_fpsIndex == -1)
{
2024-09-06 02:58:59 +00:00
// initialize FpsStatistic array
for (var i = 0; i < FPS_LEN; i++)
2024-03-14 22:50:32 +00:00
FpsStatistic[i] = newValue;
2024-09-06 02:58:59 +00:00
_avgFps = Convert.ToInt32(newValue);
2024-03-14 22:50:32 +00:00
_fpsIndex = 1;
}
else
{
2024-09-19 18:30:52 +00:00
var isSpike = Math.Abs(newValue - _avgFps) / (_avgFps * 1.0) > 0.1;
if (_ignoreFpsSpikeCount != 3 && isSpike) // if new FPS spikes more than 10%, ignore the value
{
_ignoreFpsSpikeCount++;
return _avgFps;
}
_ignoreFpsSpikeCount = 0;
2024-09-06 02:58:59 +00:00
var deltaFps = newValue - _avgFps;
if (deltaFps < 0 && Math.Abs(deltaFps) > _avgFps * 0.1) // FPS suddenly drops more than 10%
{
newValue += Math.Abs(deltaFps) / 2; // Let the new FPS to be only half the delta drop
}
2024-03-14 22:50:32 +00:00
FpsStatistic[_fpsIndex] = newValue;
_fpsIndex++;
2024-09-06 02:58:59 +00:00
if (_fpsIndex >= FPS_LEN)
2024-03-14 22:50:32 +00:00
_fpsIndex = 0;
2024-09-06 02:58:59 +00:00
_avgFps = Convert.ToInt32(FpsStatistic.Sum() / FPS_LEN);
}
return _avgFps;
}
2024-03-14 22:50:32 +00:00
2024-09-06 02:58:59 +00:00
public static void Reset()
{
_fpsIndex = -1;
_avgFps = 0;
2024-03-14 22:50:32 +00:00
}
}
}