Properly limit FPS to 30/60/120.

This commit is contained in:
UnknownShadow200 2016-01-11 17:42:54 +11:00
parent ac33a869af
commit dadaf54c29
2 changed files with 17 additions and 19 deletions

View File

@ -66,7 +66,6 @@
<DefineConstants>TRACE;USE_DX</DefineConstants> <DefineConstants>TRACE;USE_DX</DefineConstants>
<BaseIntermediateOutputPath>obj\</BaseIntermediateOutputPath> <BaseIntermediateOutputPath>obj\</BaseIntermediateOutputPath>
<StartAction>Project</StartAction> <StartAction>Project</StartAction>
<StartArguments>wwwf ver 127.0.0.1 25565</StartArguments>
</PropertyGroup> </PropertyGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.Targets" /> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.Targets" />
<ItemGroup> <ItemGroup>

View File

@ -1,4 +1,5 @@
using System; using System;
using System.Diagnostics;
using System.Drawing; using System.Drawing;
using System.IO; using System.IO;
using System.Net; using System.Net;
@ -177,8 +178,11 @@ namespace ClassicalSharp {
get { return activeScreen == null ? hudScreen : activeScreen; } get { return activeScreen == null ? hudScreen : activeScreen; }
} }
Stopwatch frameTimer = new Stopwatch();
protected override void OnRenderFrame( FrameEventArgs e ) { protected override void OnRenderFrame( FrameEventArgs e ) {
PerformFpsElapsed( e.Time * 1000 ); frameTimer.Reset();
frameTimer.Start();
Graphics.BeginFrame( this ); Graphics.BeginFrame( this );
Graphics.BindIb( defaultIb ); Graphics.BindIb( defaultIb );
accumulator += e.Time; accumulator += e.Time;
@ -237,6 +241,7 @@ namespace ClassicalSharp {
if( screenshotRequested ) if( screenshotRequested )
TakeScreenshot(); TakeScreenshot();
Graphics.EndFrame( this ); Graphics.EndFrame( this );
LimitFPS();
} }
const int ticksFrequency = 20; const int ticksFrequency = 20;
@ -386,32 +391,26 @@ namespace ClassicalSharp {
} }
float limitMilliseconds; float limitMilliseconds;
double limitAcc;
public void SetFpsLimitMethod( FpsLimitMethod method ) { public void SetFpsLimitMethod( FpsLimitMethod method ) {
FpsLimit = method; FpsLimit = method;
limitAcc = 0;
limitMilliseconds = 0; limitMilliseconds = 0;
Graphics.SetVSync( this, Graphics.SetVSync( this, method == FpsLimitMethod.LimitVSync );
method == FpsLimitMethod.LimitVSync );
if( method == FpsLimitMethod.Limit120FPS ) if( method == FpsLimitMethod.Limit120FPS )
limitMilliseconds = 1000f / 60; limitMilliseconds = 1000f / 120;
if( method == FpsLimitMethod.Limit60FPS ) if( method == FpsLimitMethod.Limit60FPS )
limitMilliseconds = 1000f / 30; limitMilliseconds = 1000f / 60;
if( method == FpsLimitMethod.Limit30FPS ) if( method == FpsLimitMethod.Limit30FPS )
limitMilliseconds = 1000f / 15; limitMilliseconds = 1000f / 30;
} }
void PerformFpsElapsed( double elapsedMs ) { void LimitFPS() {
limitAcc += elapsedMs; if( FpsLimit == FpsLimitMethod.LimitVSync ) return;
if( limitAcc >= limitMilliseconds ) { // going slower than limit?
limitAcc -= limitMilliseconds; double elapsed = frameTimer.Elapsed.TotalMilliseconds;
} else { // going faster than limit double leftOver = limitMilliseconds - elapsed;
double sleepTime = limitMilliseconds - limitAcc; if( leftOver > 0.001 ) // going faster than limit
sleepTime = Math.Ceiling( sleepTime ); Thread.Sleep( (int)Math.Round( leftOver, MidpointRounding.AwayFromZero ) );
Thread.Sleep( (int)sleepTime );
limitAcc = 0;
}
} }
public bool IsKeyDown( Key key ) { return InputHandler.IsKeyDown( key ); } public bool IsKeyDown( Key key ) { return InputHandler.IsKeyDown( key ); }