protected override void OnPaint(PaintEventArgs e)
 {
     if (MakeCurrent())
     {
         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);
     }
 }
Exemple #2
0
        private void Draw()
        {
            if (GLControl.Visible)
            {
                var frameTime = stopwatch.ElapsedMilliseconds / 1000f;
                stopwatch.Restart();

                Camera.Tick(frameTime);
                Camera.HandleInput(Mouse.GetState(), Keyboard.GetState());

                SetFps(1f / frameTime);

                GL.ClearColor(Settings.BackgroundColor);
                GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

                GLPaint?.Invoke(this, new RenderEventArgs {
                    FrameTime = frameTime, Camera = Camera
                });

                GLControl.SwapBuffers();
                GLControl.Invalidate();
            }
        }
Exemple #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);
            }
        }