Ejemplo n.º 1
0
        private void Control_Render(object sender, NativeWindowEventArgs e)
        {
            long       now      = DateTime.Now.Ticks;
            const long inv60FPS = 10000000 / 60;

            if (now - lastRenderTime > inv60FPS)
            {
                lastRenderTime = now;
                SparrowSharp.Step();
            }
        }
Ejemplo n.º 2
0
        private void NativeWindow_ContextCreated(object sender, NativeWindowEventArgs e)
        {
            lastRenderTime            = DateTime.Now.Ticks;
            SparrowSharp.NativeWindow = this;
            SparrowSharp.Start(_width, _height, _width, _height, _rootClass);

            //SparrowSharp.MouseIconChange += OnCursorChange;

            _nativeWindow.MouseDown += OnMouseButtonDown;
            _nativeWindow.MouseUp   += OnMouseButtonUp;
            _nativeWindow.MouseMove += OnMouseMove;


            _nativeWindow.Render += Control_Render;
            _nativeWindow.Resize += OnResize;
        }
Ejemplo n.º 3
0
        private void RenderTimerCallback(object state)
        {
            // Rendering on main UI thread
            Android.App.Application.SynchronizationContext.Send(delegate {
                if (_DeviceContext == null)
                {
                    return;
                }
                bool needsSwap = SparrowSharp.Step();
                if (needsSwap)
                {
                    _DeviceContext.SwapBuffers();
                }

                int elapsed = (int)sw.ElapsedMilliseconds - 1;
                if (elapsed > _RenderTimerDueTime)
                {
                    elapsed = _RenderTimerDueTime;
                }
                _RenderTimer.Change(_RenderTimerDueTime - elapsed, Timeout.Infinite);
                sw.Restart();
            }, null);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// This is called immediately after the surface is first created.
        /// </summary>
        /// <param name="holder">
        /// The SurfaceHolder whose surface is being created.
        /// </param>
        public void SurfaceCreated(ISurfaceHolder holder)
        {
            Debug.WriteLine("Sparrow: SurfaceCreated");
            // Get actual native window handle
            _NativeWindowHandle = ANativeWindow_fromSurface(JNIEnv.Handle, holder.Surface.Handle);

            // Create device context
            _DeviceContext = DeviceContext.Create(IntPtr.Zero, _NativeWindowHandle);
            _DeviceContext.IncRef();

            // Set pixel format
            DevicePixelFormatCollection pixelFormats     = _DeviceContext.PixelsFormats;
            DevicePixelFormat           controlReqFormat = new DevicePixelFormat();

            controlReqFormat.RgbaUnsigned = true;
            controlReqFormat.RenderWindow = true;
            controlReqFormat.ColorBits    = 24;
            //controlReqFormat.DepthBits = (int)DepthBits;
            //controlReqFormat.StencilBits = (int)StencilBits;
            //controlReqFormat.MultisampleBits = (int)MultisampleBits;
            //controlReqFormat.DoubleBuffer = true;

            List <DevicePixelFormat> matchingPixelFormats = pixelFormats.Choose(controlReqFormat);

            if (matchingPixelFormats.Count == 0)
            {
                throw new InvalidOperationException("unable to find a suitable pixel format");
            }
            _DeviceContext.SetPixelFormat(matchingPixelFormats[0]);

            // Create OpenGL context using compatibility profile
            if ((_RenderContext = _DeviceContext.CreateContext(IntPtr.Zero)) == IntPtr.Zero)
            {
                throw new InvalidOperationException("unable to create render context");
            }
            // Make context current
            if (_DeviceContext.MakeCurrent(_RenderContext) == false)
            {
                throw new InvalidOperationException("unable to make context current");
            }

            Invalidate();

            if (_RenderTimer != null)
            {
                throw new InvalidOperationException("rendering already active");
            }

            if (SparrowSharp.Root == null)
            {
                SparrowSharp.NativeWindow = this;
                // TODO get viewport dimensions?
                SparrowSharp.Start((uint)Width, (uint)Height, (uint)Width, (uint)Height, _rootClass);
            }
            else
            {
                SparrowSharp.OnContextCreated();
            }

            _RenderTimerDueTime = (int)Math.Ceiling(1000.0f / 60.0f);
            _RenderTimer        = new Timer(RenderTimerCallback, null, _RenderTimerDueTime, Timeout.Infinite);
        }