11using System . Diagnostics ;
22namespace LiveSplit . OriWotW {
33 public class FPSTimer {
4- private const long AveTimeTicks = 10000000 ;
5- private const long ShortTimeTicks = 1000000 ;
6- public float FPS = 0 ;
7- public float FPSShort = 0 ;
4+ private TimeCalculation averageTime ;
5+ private TimeCalculation shortTime ;
86 private Stopwatch frameTimer ;
97 private int lastFrameCount = 0 ;
10- private int lastFrameShort = 0 ;
118 private long lastTicks = 0 ;
12- private long lastTicksShort = 0 ;
13- public FPSTimer ( ) {
9+ private long updateDisplay ;
10+ private float cacheFPS ;
11+ public float FPS {
12+ get {
13+ if ( updateDisplay > 2500000 ) {
14+ updateDisplay = 0 ;
15+ cacheFPS = averageTime . FPS ;
16+ }
17+ return cacheFPS ;
18+ }
19+ }
20+ public float FPSShort { get { return shortTime . FPS ; } }
21+ public FPSTimer ( int aveSamples , int shortSamples ) {
1422 frameTimer = new Stopwatch ( ) ;
1523 frameTimer . Start ( ) ;
24+ averageTime = new TimeCalculation ( aveSamples ) ;
25+ shortTime = new TimeCalculation ( shortSamples ) ;
1626 }
1727 public void Reset ( ) {
18- lastTicksShort = 0 ;
1928 lastTicks = 0 ;
2029 lastFrameCount = 0 ;
21- lastFrameShort = 0 ;
30+ averageTime . Reset ( ) ;
31+ shortTime . Reset ( ) ;
2232 }
2333 public void Update ( int frameCount ) {
2434 long ticks = frameTimer . ElapsedTicks ;
25- if ( ticks >= lastTicks ) {
26- if ( lastFrameCount > 0 ) {
27- FPS = ( float ) ( ( double ) 10000000 * ( double ) ( frameCount - lastFrameCount ) / ( double ) ( ticks - lastTicks + AveTimeTicks ) ) ;
28- }
29- lastFrameCount = frameCount ;
30- lastTicks = ticks + AveTimeTicks ;
35+ long ticksDifference = ticks - lastTicks ;
36+ int frameDifference = frameCount - lastFrameCount ;
37+ if ( lastFrameCount > 0 ) {
38+ updateDisplay += ticksDifference ;
39+ averageTime . AddSample ( ticksDifference , frameDifference ) ;
40+ shortTime . AddSample ( ticksDifference , frameDifference ) ;
3141 }
32- if ( ticks >= lastTicksShort ) {
33- if ( lastFrameShort > 0 ) {
34- FPSShort = ( float ) ( ( double ) 10000000 * ( double ) ( frameCount - lastFrameShort ) / ( double ) ( ticks - lastTicksShort + ShortTimeTicks ) ) ;
35- }
36- lastFrameShort = frameCount ;
37- lastTicksShort = ticks + ShortTimeTicks ;
42+ lastTicks = ticks ;
43+ lastFrameCount = frameCount ;
44+ }
45+ }
46+ public class TimeCalculation {
47+ private TimeSlice [ ] samples ;
48+ private long totalTime ;
49+ private int totalFrames ;
50+ private int index ;
51+
52+ public TimeCalculation ( int sampleCount ) {
53+ samples = new TimeSlice [ sampleCount ] ;
54+ }
55+ public void Reset ( ) {
56+ totalTime = 0 ;
57+ totalFrames = 0 ;
58+ for ( int i = 0 ; i < samples . Length ; i ++ ) {
59+ samples [ i ] = default ( TimeSlice ) ;
60+ }
61+ }
62+ public void AddSample ( long ticks , int frames ) {
63+ TimeSlice slice = samples [ index ] ;
64+ totalTime += ticks - slice . Ticks ;
65+ totalFrames += frames - slice . Frames ;
66+ slice . Ticks = ticks ;
67+ slice . Frames = frames ;
68+ samples [ index ++ ] = slice ;
69+ if ( index >= samples . Length ) {
70+ index = 0 ;
3871 }
3972 }
73+ public float FPS {
74+ get { return ( float ) ( ( double ) 10000000 * ( double ) totalFrames / ( double ) totalTime ) ; }
75+ }
76+ public override string ToString ( ) {
77+ return FPS . ToString ( "0.0" ) ;
78+ }
79+ }
80+ public struct TimeSlice {
81+ public long Ticks ;
82+ public int Frames ;
83+
84+ public override string ToString ( ) {
85+ return $ "{ Ticks } -{ Frames } ";
86+ }
4087 }
4188}
0 commit comments