Dashboard/Dashboard.OpenTK/OpenTKPort.cs

108 lines
2.8 KiB
C#
Raw Normal View History

2023-07-28 21:37:49 +02:00
using System;
using OpenTK.Mathematics;
using OpenTK.Windowing.Desktop;
using OpenTK.Graphics.OpenGL4;
2024-07-17 22:18:20 +02:00
using Dashboard.OpenGL;
2024-07-28 11:34:22 +02:00
using Dashboard.ImmediateDraw;
2024-07-17 22:18:20 +02:00
using Dashboard.PAL;
using Dashboard.VertexGenerator;
2023-07-28 21:37:49 +02:00
2024-07-17 22:18:20 +02:00
namespace Dashboard.OpenTK
2023-07-28 21:37:49 +02:00
{
2024-07-18 19:34:39 +02:00
public class OpenTKPort : IDashHandle
2023-07-28 21:37:49 +02:00
{
private readonly NativeWindow _window;
private readonly GL21Driver _glDriver;
2024-07-28 11:37:33 +02:00
private readonly VertexDrawingEngine _vertexEngine;
2023-07-28 21:37:49 +02:00
public string Title
{
get => _window.Title;
set => _window.Title = value;
}
public Vector2 Size
2023-07-28 21:37:49 +02:00
{
get
{
Vector2i size = _window.ClientSize;
return new Vector2(size.X, size.Y);
2023-07-28 21:37:49 +02:00
}
set
{
// OpenTK being OpenTK as usual, you can't set the client size.
Vector2i extents = _window.Size - _window.ClientSize;
Vector2i size = extents + new Vector2i((int)value.X, (int)value.Y);
_window.Size = size;
}
}
public Vector2 Position
2023-07-28 21:37:49 +02:00
{
get
{
Vector2i location = _window.Location;
return new Vector2(location.X, location.Y);
2023-07-28 21:37:49 +02:00
}
set
{
Vector2i location = new Vector2i((int)value.X, (int)value.Y);
_window.Location = location;
}
}
public bool IsValid => !isDisposed;
2024-06-09 21:54:33 +02:00
public event EventHandler? EventRaised;
2023-07-28 21:37:49 +02:00
public OpenTKPort(NativeWindow window)
{
_window = window;
_glDriver = new GL21Driver();
2024-07-28 11:37:33 +02:00
_vertexEngine = new VertexDrawingEngine();
2023-07-28 21:37:49 +02:00
}
public void Focus()
{
_window.Focus();
}
2024-07-28 11:34:22 +02:00
public void Paint(DrawList queue)
2023-07-28 21:37:49 +02:00
{
2024-07-28 13:11:23 +02:00
Rectangle view = new Rectangle(Size, new Vector2(0, 0));
2023-07-28 21:37:49 +02:00
_vertexEngine.Reset();
2024-04-11 18:09:00 +02:00
_vertexEngine.ProcessCommands(view, queue);
2023-07-28 21:37:49 +02:00
2024-03-04 19:41:16 +01:00
if (!_window.Context.IsCurrent)
_window.Context.MakeCurrent();
2023-07-28 21:37:49 +02:00
if (!_glDriver.IsInit)
_glDriver.Init();
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
2023-07-28 21:37:49 +02:00
_glDriver.Draw(_vertexEngine.DrawQueue, view);
_window.Context.SwapBuffers();
}
public void Show(bool shown = true)
{
_window.IsVisible = shown;
}
private bool isDisposed;
private void Dispose(bool disposing)
{
if (isDisposed) return;
if (disposing)
{
_window?.Dispose();
GC.SuppressFinalize(this);
}
isDisposed = true;
}
public void Dispose() => Dispose(true);
}
}