static async void StartGameCycle(SimpleApplication app) { app.Run(); const int FpsLimit = 60; while (app.IsActive) { var elapsed = app.Engine.RunFrame(); var targetMax = 1000000L / FpsLimit; if (elapsed >= targetMax) { await Task.Yield(); } else { var ts = TimeSpan.FromMilliseconds((targetMax - elapsed) / 1000d); await Task.Delay(ts); } } }
public static Task <SimpleApplication> RunAsync(ApplicationOptions options) { #if DESKTOP var dataDir = options.ResourcePaths?.FirstOrDefault(); Environment.CurrentDirectory = Path.GetDirectoryName(typeof(SimpleApplication).Assembly.Location); if (!File.Exists("CoreData.pak")) { using (Stream input = typeof(SimpleApplication).Assembly.GetManifestResourceStream("Urho.CoreData.pak")) using (Stream output = File.Create(Path.Combine("CoreData.pak"))) input.CopyTo(output); } if (!string.IsNullOrEmpty(dataDir)) { Directory.CreateDirectory("Data"); } #endif #if !IOS && !UWP var taskSource = new TaskCompletionSource <SimpleApplication>(); Action callback = null; callback = () => { Started -= callback; taskSource.TrySetResult(Current as SimpleApplication); }; Started += callback; Task.Factory.StartNew(() => new SimpleApplication(options).Run(), CancellationToken.None, TaskCreationOptions.DenyChildAttach, SynchronizationContext.Current == null ? TaskScheduler.Default : TaskScheduler.FromCurrentSynchronizationContext()); return(taskSource.Task); #else var app = new SimpleApplication(options); app.Run(); //for iOS and UWP it's not blocking return(Task.FromResult(app)); #endif }