示例#1
0
        public Window(WindowOptions options)
        {
            if (Application.MainWindow != null)
            {
                throw new Lime.Exception("Attempt to set Application.MainWindow twice");
            }
            Application.MainWindow = this;
            Input          = new WindowInput(this);
            Active         = true;
            AsyncRendering = options.AsyncRendering;
            fpsCounter     = new FPSCounter();
            ActivityDelegate.Instance.Paused += activity => {
                Active = false;
                RaiseDeactivated();
            };
            ActivityDelegate.Instance.Resumed += activity => {
                Active = true;
                RaiseActivated();
            };
            ActivityDelegate.Instance.GameView.Resize += (sender, e) => {
                RaiseResized(((ResizeEventArgs)e).DeviceRotated);
            };

            PixelScale = Resources.System.DisplayMetrics.Density;

            if (AsyncRendering)
            {
                renderThread = new Thread(RenderLoop);
                renderThread.IsBackground = true;
                renderThread.Start();
            }

            Application.WindowUnderMouse = this;

            var  ccb           = new ChoreographerCallback();
            long prevFrameTime = Java.Lang.JavaSystem.NanoTime();

            ccb.OnFrame += frameTimeNanos => {
                var delta = (float)((frameTimeNanos - prevFrameTime) / 1000000000d);
                prevFrameTime = frameTimeNanos;
                if (Active && ActivityDelegate.Instance.GameView.IsSurfaceCreated)
                {
                    fpsCounter.Refresh();
                    Update(delta);
                    if (AsyncRendering)
                    {
                        renderCompleted.WaitOne();
                        renderCompleted.Reset();
                        RaiseSync();
                        renderReady.Set();
                    }
                    else
                    {
                        RaiseSync();
                        Render();
                    }
                }
            };
            Choreographer.Instance.PostFrameCallback(ccb);
        }
示例#2
0
 public Window(WindowOptions options)
 {
     fpsCounter = new FPSCounter();
     CreateNativeWindow(options);
     if (Application.MainWindow == null)
     {
         Application.MainWindow = this;
     }
     Application.Windows.Add(this);
     Input      = new WindowInput(this);
     ClientSize = options.ClientSize;
     Title      = options.Title;
     if (options.Visible)
     {
         Visible = true;
     }
     if (options.Centered)
     {
         Center();
     }
     if (options.Type == WindowType.ToolTip)
     {
         window.Level = NSWindowLevel.Floating;
     }
     stopwatch = new Stopwatch();
     stopwatch.Start();
 }
示例#3
0
 public Window(WindowOptions options)
 {
     if (Application.MainWindow != null)
     {
         throw new Lime.Exception("Attempt to create a second window.");
     }
     Application.MainWindow = this;
     Active         = true;
     AsyncRendering = options.AsyncRendering;
     Input          = new WindowInput(this);
     uiWindow       = new UIWindow(UIScreen.MainScreen.Bounds);
     // UIApplicationDelegate must has a Window reference. This is an Apple's requirement.
     AppDelegate.Instance.Window = uiWindow;
     UIViewController            = new GameController(Application.Input);
     uiWindow.RootViewController = UIViewController;
     uiWindow.MakeKeyAndVisible();
     AppDelegate.Instance.Activated += () => {
         // Run() creates OpenGL context
         UIView.Run();
         UIViewController.LockDeviceOrientation = false;
         Active             = true;
         AudioSystem.Active = true;
         RaiseActivated();
         UIKit.UIViewController.AttemptRotationToDeviceOrientation();
     };
     AppDelegate.Instance.Deactivated += () => {
         UIView.Stop();
         UIViewController.LockDeviceOrientation = true;
         AudioSystem.Active = false;
         RaiseDeactivated();
         Active = false;
     };
     AppDelegate.Instance.WillTerminateEvent += () => {
         RaiseClosed();
     };
     UIViewController.OnResize += (sender, e) => {
         RaiseResized(((ResizeEventArgs)e).DeviceRotated);
     };
     UIView.RenderFrame          += OnRenderFrame;
     UIView.UpdateFrame          += OnUpdateFrame;
     display                      = new Display(UIScreen.MainScreen);
     Application.WindowUnderMouse = this;
 }
示例#4
0
        public Window(WindowOptions options)
        {
            if (Application.MainWindow != null && Application.RenderingBackend == RenderingBackend.ES20)
            {
                // ES20 doesn't allow multiple contexts for now, because of a bug in OpenTK
                throw new Lime.Exception("Attempt to create a second window for ES20 rendering backend. Use OpenGL backend instead.");
            }
            if (options.UseTimer && options.AsyncRendering)
            {
                throw new Lime.Exception("Can't use both timer and async rendering");
            }
            if (options.ToolWindow)
            {
                form = new ToolForm();
            }
            else
            {
                form = new Form();
            }
            Input = new WindowInput(this);
            using (var graphics = form.CreateGraphics()) {
                PixelScale = CalcPixelScale(graphics.DpiX);
            }
            if (options.Style == WindowStyle.Borderless)
            {
                borderStyle = FormBorderStyle.None;
            }
            else
            {
                borderStyle = options.FixedSize ? FormBorderStyle.FixedSingle : FormBorderStyle.Sizable;
            }
            form.FormBorderStyle = borderStyle;
            form.MaximizeBox     = !options.FixedSize;
            if (options.MinimumDecoratedSize != Vector2.Zero)
            {
                MinimumDecoratedSize = options.MinimumDecoratedSize;
            }
            if (options.MaximumDecoratedSize != Vector2.Zero)
            {
                MaximumDecoratedSize = options.MaximumDecoratedSize;
            }
            glControl = CreateGLControl();
            glControl.CreateControl();
            glControl.Context.MakeCurrent(null);
            glControl.Dock        = DockStyle.Fill;
            glControl.Paint      += OnPaint;
            glControl.KeyDown    += OnKeyDown;
            glControl.KeyUp      += OnKeyUp;
            glControl.KeyPress   += OnKeyPress;
            glControl.MouseDown  += OnMouseDown;
            glControl.MouseUp    += OnMouseUp;
            glControl.Resize     += OnResize;
            glControl.MouseWheel += OnMouseWheel;
            glControl.MouseEnter += (sender, args) => {
                Application.WindowUnderMouse = this;
            };
            glControl.MouseLeave += (sender, args) => {
                if (Application.WindowUnderMouse == this)
                {
                    Application.WindowUnderMouse = null;
                }
            };
            glControl.BeforeBoundsChanged += WaitForRendering;
            form.Move        += OnMove;
            form.Activated   += OnActivated;
            form.Deactivate  += OnDeactivate;
            form.FormClosing += OnClosing;
            form.FormClosed  += OnClosed;
            active            = Form.ActiveForm == form;

            if (options.UseTimer)
            {
                timer = new System.Windows.Forms.Timer {
                    Interval = (int)(1000.0 / 65),
                    Enabled  = true,
                };
                timer.Tick += OnTick;
            }
            else
            {
                vSync = options.VSync;
                glControl.MakeCurrent();
                glControl.VSync = vSync;
                glControl.Context.MakeCurrent(null);
                System.Windows.Forms.Application.Idle += OnTick;
            }

            form.Controls.Add(glControl);
            stopwatch = new Stopwatch();
            stopwatch.Start();

            if (options.Icon != null)
            {
                form.Icon = (System.Drawing.Icon)options.Icon;
            }
            Cursor     = MouseCursor.Default;
            Title      = options.Title;
            ClientSize = options.ClientSize;
            if (options.Visible)
            {
                Visible = true;
            }
            if (options.Screen != null && options.Screen >= 0 && Screen.AllScreens.Length > options.Screen)
            {
                form.Location = GetCenter(Screen.AllScreens[options.Screen.Value].WorkingArea);
            }
            if (options.Centered)
            {
                Center();
            }
            if (Application.MainWindow == null)
            {
                Application.MainWindow = this;
                Closing += reason => Application.DoExiting();
                Closed  += Application.DoExited;
            }
            else
            {
                Form.Owner         = Application.MainWindow.Form;
                Form.StartPosition = FormStartPosition.CenterParent;
            }
            AsyncRendering = options.AsyncRendering;
            if (AsyncRendering)
            {
                renderThreadTokenSource   = new CancellationTokenSource();
                renderThreadToken         = renderThreadTokenSource.Token;
                renderThread              = new Thread(RenderLoop);
                renderThread.IsBackground = true;
                renderThread.Start();
            }
            Application.Windows.Add(this);
        }