Add FPS limit option.

This commit is contained in:
UnknownShadow200 2015-11-22 15:41:34 +11:00
parent 1a8ae3e8f8
commit da8bc3c733
3 changed files with 37 additions and 9 deletions

View file

@ -27,10 +27,11 @@ namespace ClassicalSharp {
(g, v) => { g.LocalPlayer.SpeedMultiplier = Single.Parse( v );
Options.Set( OptionsKey.Speed, v ); } ),
Make( -140, 0, "VSync active", Anchor.Centre, OnWidgetClick,
g => g.VSync ? "yes" : "no",
(g, v) => { g.Graphics.SetVSync( g, v == "yes" );
Options.Set( OptionsKey.VSync, v == "yes" ); } ),
Make( -140, 0, "FPS limit", Anchor.Centre, OnWidgetClick,
g => g.FpsLimit.ToString(),
(g, v) => { object raw = Enum.Parse( typeof(FpsLimitMethod), v );
g.SetFpsLimitMethod( (FpsLimitMethod)raw );
Options.Set( OptionsKey.FpsLimit, v ); } ),
Make( -140, 50, "View distance", Anchor.Centre, OnWidgetClick,
g => g.ViewDistance.ToString(),
@ -66,10 +67,12 @@ namespace ClassicalSharp {
(g, w) => g.SetNewScreen( new PauseScreen( g ) ), null, null ),
null,
};
buttons[2].Metadata = typeof(FpsLimitMethod);
validators = new MenuInputValidator[] {
new BooleanValidator(),
new RealValidator( 0.1f, 50 ),
new BooleanValidator(),
new EnumValidator(),
new IntegerValidator( 16, 4096 ),
network.IsSinglePlayer ? new BooleanValidator() : null,

View file

@ -63,6 +63,7 @@ namespace ClassicalSharp {
public string Mppass;
public int Port;
public int ViewDistance = 512;
public FpsLimitMethod FpsLimit;
public long Vertices;
public FrustumCulling Culling;
@ -177,8 +178,8 @@ namespace ClassicalSharp {
BlockHandRenderer = new BlockHandRenderer( this );
BlockHandRenderer.Init();
bool vsync = Options.GetBool( OptionsKey.VSync, true );
Graphics.SetVSync( this, vsync );
FpsLimitMethod method = Options.GetEnum( OptionsKey.FpsLimit, FpsLimitMethod.LimitVSync );
SetFpsLimitMethod( method );
Graphics.DepthTest = true;
Graphics.DepthTestFunc( CompareFunc.LessEqual );
//Graphics.DepthWrite = true;
@ -224,6 +225,7 @@ namespace ClassicalSharp {
double ticksAccumulator = 0, imageCheckAccumulator = 0, cameraAccumulator = 0;
protected override void OnRenderFrame( FrameEventArgs e ) {
PerformFpsLimit( (int)(e.Time * 1000) );
Graphics.BeginFrame( this );
Graphics.BindIb( defaultIb );
accumulator += e.Time;
@ -415,6 +417,29 @@ namespace ClassicalSharp {
MapRenderer.RedrawBlock( x, y, z, block, oldHeight, newHeight );
}
int limitMilliseconds;
public void SetFpsLimitMethod( FpsLimitMethod method ) {
FpsLimit = method;
limitMilliseconds = 0;
Graphics.SetVSync( this,
method == FpsLimitMethod.LimitVSync );
if( method == FpsLimitMethod.Limit120FPS )
limitMilliseconds = 1000 / (120 / 2);
if( method == FpsLimitMethod.Limit60FPS )
limitMilliseconds = 1000 / (60 / 2);
if( method == FpsLimitMethod.Limit30FPS )
limitMilliseconds = 1000 / (30 / 2);
}
void PerformFpsLimit( int msElapsed ) {
if( limitMilliseconds == 0 ) return; // vsync or no limit
// previous frame was too quick, sleep for a bit.
if( msElapsed < limitMilliseconds )
System.Threading.Thread.Sleep( limitMilliseconds - msElapsed );
}
public bool IsKeyDown( Key key ) { return InputHandler.IsKeyDown( key ); }
public bool IsKeyDown( KeyBinding binding ) { return InputHandler.IsKeyDown( binding ); }

View file

@ -21,12 +21,12 @@ namespace ClassicalSharp {
public const string MouseLeft = "mouseleft";
public const string MouseMiddle = "mousemiddle";
public const string MouseRight = "mouseright";
public const string VSync = "vsync";
public const string FpsLimit = "fpslimit";
public const string AutoCloseLauncher = "autocloselauncher";
}
// TODO: implement this
public enum FpsLimit {
public enum FpsLimitMethod {
LimitVSync,
Limit30FPS,
Limit60FPS,