diff --git a/src/SlatedGameToolkit.Framework/GameEngine.cs b/src/SlatedGameToolkit.Framework/GameEngine.cs index 454e7d3..62e9867 100644 --- a/src/SlatedGameToolkit.Framework/GameEngine.cs +++ b/src/SlatedGameToolkit.Framework/GameEngine.cs @@ -15,10 +15,11 @@ namespace SlatedGameToolkit.Framework { WriteTo.File("SlatedGameToolKit.Framework.Log", rollingInterval: RollingInterval.Day, fileSizeLimitBytes: 1048576, rollOnFileSizeLimit: true). CreateLogger(); private static readonly object ignitionLock = new object(); + private static readonly object deltaUpdateLock = new object(); private static Thread thread; private static volatile bool exit = false, stopped = true; - private static volatile bool deltaChanged = true; - private static long updateDeltaTime = 25, frameDeltaTime = 16; + private static volatile bool deltaChanged; + private static TimeSpan updateDeltaTime = TimeSpan.FromSeconds(1/200), frameDeltaTime = TimeSpan.FromSeconds(1/60); /// /// The amount of updates per second. @@ -28,10 +29,16 @@ namespace SlatedGameToolkit.Framework { /// The updates per second. public static double UpdatesPerSecond { get { - return 1 / TimeSpan.FromMilliseconds(updateDeltaTime).TotalSeconds; + return 1 / updateDeltaTime.TotalSeconds; } set { - Interlocked.Exchange(ref updateDeltaTime, Math.Max(5, (long) TimeSpan.FromSeconds(1 / value).TotalMilliseconds)); + lock (deltaUpdateLock) { + if (1 / value < 0.005) { + updateDeltaTime = TimeSpan.FromSeconds(1/200); + } else { + updateDeltaTime = TimeSpan.FromSeconds(1/value); + } + } deltaChanged = true; } } @@ -45,12 +52,16 @@ namespace SlatedGameToolkit.Framework { /// The target frames per second. public static double targetFPS { get { - if (frameDeltaTime == 0) return 0; - return 1 / TimeSpan.FromMilliseconds(frameDeltaTime).TotalSeconds; + return 1 / frameDeltaTime.TotalSeconds; } set { - if (value == 0) Interlocked.Exchange(ref frameDeltaTime, 0); - Interlocked.Exchange(ref frameDeltaTime, (long) TimeSpan.FromSeconds(1 / value).TotalMilliseconds); + lock (deltaUpdateLock) { + if (value == 0) { + frameDeltaTime = TimeSpan.FromSeconds(0); + } else { + frameDeltaTime = TimeSpan.FromSeconds(1/value); + } + } deltaChanged = true; } } @@ -65,34 +76,37 @@ namespace SlatedGameToolkit.Framework { if (!(o is Manager)) throw new InternalFrameworkException(String.Format("Expected manager object for asynchronous loop. Got {0}", o)); Manager manager = (Manager) o; manager.initialize(); - long currentTime = DateTimeOffset.Now.ToUnixTimeMilliseconds(); - long timePassedFromLastUpdate = 0; - long timePassedFromLastRender = 0; - long updateDeltaTime = 0; - long frameDeltaTime = 0; + DateTime currentTime = DateTime.Now; + TimeSpan timePassedFromLastUpdate; + TimeSpan timePassedFromLastRender; + TimeSpan updateDeltaTime; + TimeSpan frameDeltaTime; + deltaChanged = true; stopped = false; logger.Information("Game engine initiated."); while (!exit) { if (deltaChanged) { - updateDeltaTime = Interlocked.Read(ref GameEngine.updateDeltaTime); - frameDeltaTime = Interlocked.Read(ref GameEngine.frameDeltaTime); + lock (deltaUpdateLock) { + updateDeltaTime = GameEngine.updateDeltaTime; + frameDeltaTime = GameEngine.frameDeltaTime; + } deltaChanged = false; } - long frameStart = DateTimeOffset.Now.ToUnixTimeMilliseconds(); - long difference = frameStart - currentTime; + DateTime frameStart = DateTime.Now; + TimeSpan difference = frameStart - currentTime; currentTime = frameStart; timePassedFromLastUpdate += difference; while (timePassedFromLastUpdate > updateDeltaTime) { - manager.update(updateDeltaTime); + manager.update(updateDeltaTime.TotalMilliseconds); timePassedFromLastUpdate -= updateDeltaTime; } timePassedFromLastRender += difference; if (timePassedFromLastRender > frameDeltaTime) { - manager.render(timePassedFromLastUpdate / updateDeltaTime); - timePassedFromLastRender = 0; + manager.render(timePassedFromLastUpdate.TotalMilliseconds / updateDeltaTime.TotalMilliseconds); + timePassedFromLastRender = TimeSpan.Zero; } } manager.removeAllStates();