Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force game updates at 60FPS (independent of Draw calls). Fixes issue #199 #207

Merged
merged 1 commit into from
Jun 14, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 23 additions & 14 deletions EndlessClient/GameExecution/EndlessGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class EndlessGame : Game, IEndlessGame
private GraphicsDeviceManager _graphicsDeviceManager;

private KeyboardState _previousKeyState;
private TimeSpan _lastFrameUpdate;

#if DEBUG
private SpriteBatch _spriteBatch;
Expand Down Expand Up @@ -167,23 +168,31 @@ protected override void LoadContent()

protected override void Update(GameTime gameTime)
{
//todo: this is a debug-only mode launched with the F5 key.
//todo: move this to be handled by some sort of key listener once function keys are handled in-game
var currentKeyState = Keyboard.GetState();
if (_previousKeyState.IsKeyDown(Keys.F5) && currentKeyState.IsKeyUp(Keys.F5))
// Force update at 60FPS
// Some game components rely on ~60FPS update times. See: https://github.com/ethanmoffat/EndlessClient/issues/199
// Using IsFixedTimeStep = true with TargetUpdateTime set to 60FPS also limits the draw rate, which is not desired
if ((gameTime.TotalGameTime - _lastFrameUpdate).TotalMilliseconds > 1000.0 / 60)
{
_testModeLauncher.LaunchTestMode();
}
//todo: this is a debug-only mode launched with the F5 key.
//todo: move this to be handled by some sort of key listener once function keys are handled in-game
var currentKeyState = Keyboard.GetState();
if (_previousKeyState.IsKeyDown(Keys.F5) && currentKeyState.IsKeyUp(Keys.F5))
{
_testModeLauncher.LaunchTestMode();
}

_previousKeyState = currentKeyState;
_previousKeyState = currentKeyState;

try
{
base.Update(gameTime);
}
catch (InvalidOperationException ioe) when (ioe.InnerException is NullReferenceException)
{
// hide "failed to compare two elements in the array" error from Monogame
try
{
base.Update(gameTime);
}
catch (InvalidOperationException ioe) when (ioe.InnerException is NullReferenceException)
{
// hide "failed to compare two elements in the array" error from Monogame
}

_lastFrameUpdate = gameTime.TotalGameTime;
}
}

Expand Down