Beispiel #1
0
        void RenderWithSkia()
        {
            int width  = glControl1.Width;
            int height = glControl1.Height;

            if (field == null || field.width != width || field.height != height)
            {
                field = new Starfield.Field(100_000, width, height);
            }
            field.StepForward();

            // Create a Skia surface using the OpenGL control
            SKColorType colorType     = SKColorType.Rgba8888;
            GRContext   contextOpenGL = GRContext.Create(GRBackend.OpenGL, GRGlInterface.CreateNativeGlInterface());

            GL.GetInteger(GetPName.FramebufferBinding, out var framebuffer);
            GRGlFramebufferInfo glInfo = new GRGlFramebufferInfo((uint)framebuffer, colorType.ToGlSizedFormat());

            GL.GetInteger(GetPName.StencilBits, out var stencil);
            GRBackendRenderTarget renderTarget = new GRBackendRenderTarget(width, height, contextOpenGL.GetMaxSurfaceSampleCount(colorType), stencil, glInfo);
            SKSurface             surface      = SKSurface.Create(contextOpenGL, renderTarget, GRSurfaceOrigin.BottomLeft, colorType);
            SKCanvas canvas = surface.Canvas;

            // draw the starfield
            var paint = new SKPaint
            {
                Color       = SKColors.White,
                IsAntialias = true
            };

            canvas.Clear(SKColors.Black);
            foreach (Starfield.Star star in field.stars)
            {
                //canvas.DrawRect(star.X, star.Y, star.Size, star.Size, paint);
                canvas.DrawCircle(new SKPoint(star.X, star.Y), star.Size / 2, paint);
            }

            // Force a display
            surface.Canvas.Flush();
            glControl1.SwapBuffers();

            // dispose to prevent memory access violations while exiting
            renderTarget?.Dispose();
            contextOpenGL?.Dispose();
            canvas?.Dispose();
            surface?.Dispose();

            // update the FPS display
            renderCount += 1;
            double elapsedSeconds = (double)stopwatch.ElapsedMilliseconds / 1000;

            Text = string.Format("Rendered {0} frames in {1:0.00} seconds ({2:0.00} Hz)", renderCount, elapsedSeconds, renderCount / elapsedSeconds);
        }
Beispiel #2
0
        void Render()
        {
            // create or resize the bitmap if needed
            if (bmp == null || bmp.Size != pictureBox1.Size)
            {
                bmp   = new Bitmap(pictureBox1.Width, pictureBox1.Height);
                field = new Starfield.Field(100_000, bmp.Width, bmp.Height);
                stopwatch.Restart();
                renderCount = 0;
            }

            // move the stars forward in time
            field.StepForward();

            // draw on the bitmap
            using (Graphics gfx = Graphics.FromImage(bmp))
            {
                gfx.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                gfx.Clear(Color.Black);
                Brush starBrush = new SolidBrush(Color.White);

                foreach (Starfield.Star star in field.stars)
                {
                    gfx.FillEllipse(starBrush, star.X, star.Y, star.Size, star.Size);
                }
                //gfx.FillRectangle(starBrush, star.X, star.Y, star.Size, star.Size);
            }

            // apply the new bitmap to the picturebox and force a redraw
            pictureBox1.Image = bmp;

            // update the FPS display
            renderCount += 1;
            double elapsedSeconds = (double)stopwatch.ElapsedMilliseconds / 1000;

            Text = string.Format("Rendered {0} frames in {1:0.00} seconds ({2:0.00} Hz)", renderCount, elapsedSeconds, renderCount / elapsedSeconds);
        }