private void tmrGameUpdate_Tick(object sender, EventArgs e) { tmrGameUpdate.Interval = Math.Max(1, 1000 / StaticDisplay.FPSCap); StaticDisplay.Begin(); { if (StaticEngine.IsGameRunning) { StaticEngine.CurrentGame.Update(); } else // Update Splash-Screen { StaticEngine.CurrentGame.UpdateSplash(); } Refresh(); } StaticDisplay.End(); StaticMouse.ResetDelta(); // Reset delta values // Goes back to GameMenu when game requested to stop if (StaticEngine.CurrentGame.IsStopRequested()) { StaticEngine.ChangeGame(null, GameMode.SINGLEPLAYER); } }
private void GameLoop() { Action RefreshAction = () => Refresh(); long lastUpdateTick = Environment.TickCount; long elapsedUpdateSteps = 0; long lastFpsTick = Environment.TickCount; int fpsCount = 0; while (Visible) { long startTick = Environment.TickCount; long elapsedTicks = startTick - lastUpdateTick; elapsedUpdateSteps += elapsedTicks; lastUpdateTick = startTick; long expectedMs = 1000 / FPSCap; while (elapsedUpdateSteps >= expectedMs) { if (StaticEngine.IsGameRunning) { StaticEngine.CurrentGame.Update(); } else // Update Splash-Screen { StaticEngine.CurrentGame.UpdateSplash(); } StaticMouse.ResetCache(); // Reset delta values StaticKeyboard.ResetCache(); // Reset delta values // Goes back to GameMenu when game requested to stop if (StaticEngine.CurrentGame.IsStopRequested()) { StaticEngine.ChangeGame(null, GameMode.SINGLEPLAYER); } elapsedUpdateSteps -= expectedMs; } try { Invoke(RefreshAction); fpsCount++; if (Environment.TickCount - lastFpsTick > 1000) { FPSCount = fpsCount; lastFpsTick = Environment.TickCount; fpsCount = 0; } } catch (Exception) { } { // Syncronize long endTime = startTick + 1; // 1000ms / 1000fps while (Environment.TickCount < endTime) { Thread.Sleep(1); } } } }