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 SimpleApplication Show(ApplicationOptions opts = null) { #if !DESKTOP throw new NotSupportedException(); #else if (SynchronizationContext.Current == null) { throw new NotSupportedException("SynchronizationContext.Current should not be null."); } //Close active UrhoSharp instances=windows if any StopCurrent().Wait(); opts = opts ?? new ApplicationOptions(); opts.ResizableWindow = true; opts.DelayedStart = true; if (opts.Width < 1) { opts.Width = 900; } if (opts.Height < 1) { opts.Height = 800; } var app = new SimpleApplication(opts); StartGameCycle(app); if (Platform == Platforms.Windows) { var handle = Process.GetCurrentProcess().MainWindowHandle; SetWindowPos(handle, new IntPtr(-1), 0, 0, 0, 0, 0x1 | 0x2 /*SWP_NOMOVE | SWP_NOSIZE */); } //on macOS it does [window setLevel:CGWindowLevelForKey(kCGMaximumWindowLevelKey)]; return(app); #endif }
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 }