Exemplo n.º 1
0
        protected override void Dispose(bool disposing)
        {
            if (IsDisposed)
            {
                return;
            }

            if (disposing)
            {
                if (MakeCurrent())
                {
                    GLStop?.Invoke(this, EventArgs.Empty);
                }

                if (HGLRC != IntPtr.Zero)
                {
                    wgl.MakeCurrent(HDC, IntPtr.Zero);
                    wgl.DeleteContext(HGLRC);
                    HGLRC = IntPtr.Zero;
                }
                if (HDC != IntPtr.Zero)
                {
                    gdi.ReleaseDC(Handle, HDC);
                    HDC = IntPtr.Zero;
                }
            }

            base.Dispose(disposing);
        }
Exemplo n.º 2
0
        protected override void OnHandleDestroyed(EventArgs e)
        {
            if (!IsDisposed)
            {
                if (MakeCurrent())
                {
                    GLStop?.Invoke(this, EventArgs.Empty);
                }

                if (HGLRC != IntPtr.Zero)
                {
                    wgl.MakeCurrent(HDC, IntPtr.Zero);
                    wgl.DeleteContext(HGLRC);
                    HGLRC = IntPtr.Zero;
                }
                if (HDC != IntPtr.Zero)
                {
                    gdi.ReleaseDC(Handle, HDC);
                    HDC = IntPtr.Zero;
                }
            }

            base.OnHandleDestroyed(e);
        }
Exemplo n.º 3
0
        private void Worker(object args)
        {
            var Args  = (ThreadArgs)args;
            var HWND  = Args.HWND;
            var HDC   = IntPtr.Zero;
            var HGLRC = IntPtr.Zero;

            try
            {
                GLInit(HWND, out HDC, out HGLRC);

                gl.Enable(GL.BLEND);
                gl.BlendFunc(GL.SRC_ALPHA, GL.ONE_MINUS_SRC_ALPHA);
                gl.DepthMask(GL.FALSE);
                gl.MatrixMode(GL.PROJECTION);

                gl.Viewport(0, 0, ClientSize.Width, ClientSize.Height);
                gl.LoadIdentity();
                gl.Ortho(-SurfaceSize.Width / 2f, SurfaceSize.Width / 2f,
                         -SurfaceSize.Height / 2f, SurfaceSize.Height / 2f, 0f, 1f);

                GLStart?.Invoke(this, EventArgs.Empty);

                var FrameTimer = new Stopwatch();
                while (RendererRunning)
                {
                    if (FPS <= 0)
                    {
                        Thread.Sleep(100);
                    }
                    else
                    {
                        var delay = 1000 / FPS - (int)FrameTimer.ElapsedMilliseconds;
                        if (delay > 0)
                        {
                            Thread.Sleep(delay);
                        }
                        FrameTimer.Restart();

                        lock (ActionRequests)
                            while (ActionRequests.Count > 0)
                            {
                                ActionRequests.Dequeue().Invoke();
                            }

                        if (!NoClear)
                        {
                            gl.ClearColor(BackColor.R / 256f, BackColor.G / 256f,
                                          BackColor.B / 256f, BackColor.A / 256f);
                            gl.Clear(GL.COLOR_BUFFER_BIT | GL.DEPTH_BUFFER_BIT);
                        }
                        GLPaint?.Invoke(this, EventArgs.Empty);

                        gdi.SwapBuffers(HDC);
                    }
                }
            }
            finally
            {
                GLStop?.Invoke(this, EventArgs.Empty);

                GLDispose(HWND, HDC, HGLRC);
            }
        }