Skip to content

Instantly share code, notes, and snippets.

@JaggerJo
Created July 5, 2019 10:52
Show Gist options
  • Save JaggerJo/59326790ee97a2c2ed61b62990ab98d6 to your computer and use it in GitHub Desktop.
Save JaggerJo/59326790ee97a2c2ed61b62990ab98d6 to your computer and use it in GitHub Desktop.
OpenTK in Avalonia
type OpenGLControl() as this =
inherit Control()
let mode = new Graphics.GraphicsMode(new Graphics.ColorFormat(8, 8, 8, 8), 24, 0, 0, Graphics.ColorFormat.Empty, 1);
let mutable win : GameWindow = null
do
this.IsHitTestVisible <- false
override this.OnAttachedToVisualTree args =
win <- new GameWindow(640, 480, mode, "", GameWindowFlags.Default, DisplayDevice.Default, 3, 0, Graphics.GraphicsContextFlags.Default);
win.Visible <- false
win.MakeCurrent()
override this.MeasureOverride size =
win.Size <- new Size(int size.Width, int size.Height)
size
abstract GetFrame : unit -> unit
default this.GetFrame () =
GL.ClearColor(0.7f, 0.0f, 0.0f, 1.0f);
GL.Clear(ClearBufferMask.ColorBufferBit ||| ClearBufferMask.AccumBufferBit);
override this.Render ctx =
this.GetFrame()
GL.Flush()
use bitmap = new WriteableBitmap(
Avalonia.PixelSize(win.Width, win.Height),
Avalonia.Vector(96.0, 96.0),
Nullable(Avalonia.Platform.PixelFormat.Rgba8888)
)
using (bitmap.Lock()) (fun buffer ->
GL.PixelStore(PixelStoreParameter.PackRowLength, buffer.RowBytes / 4);
GL.ReadPixels(0, 0, win.Width, win.Height, PixelFormat.Rgba, PixelType.UnsignedByte, buffer.Address);
)
ctx.DrawImage(bitmap, 1.0, Avalonia.Rect(bitmap.Size), Avalonia.Rect(bitmap.Size), BitmapInterpolationMode.LowQuality)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment