From 2c957a0c1a4846cecc57bb61b03884da880dd254 Mon Sep 17 00:00:00 2001 From: "H. Utku Maden" Date: Sun, 2 Feb 2025 21:58:49 +0300 Subject: [PATCH] Create some high level classes. --- Dashboard.Common/IWindow.cs | 102 +++++++++++++++ Dashboard.Drawing.OpenGL/ContextExecutor.cs | 1 + .../ContextResourcePool.cs | 2 + .../Dashboard.Drawing.OpenGL.csproj | 3 +- Dashboard.Drawing.OpenGL/DrawCallRecorder.cs | 1 + Dashboard.Drawing.OpenGL/GLEngine.cs | 1 + .../GradientUniformBuffer.cs | 1 + Dashboard.Drawing.OpenGL/MappableBuffer.cs | 1 + Dashboard.Drawing.OpenGL/Text/BlurgEngine.cs | 13 +- Dashboard.Drawing.OpenGL/Text/DbBlurgFont.cs | 2 + Dashboard.Drawing/DrawQueue.cs | 4 +- Dashboard.ImmediateUI/DimUI.cs | 1 - Dashboard.OpenGL/Dashboard.OpenGL.csproj | 13 ++ .../IGLContext.cs | 19 +-- .../IGLDisposable.cs | 2 +- Dashboard.OpenTK/Dashboard.OpenTK.csproj | 18 +++ Dashboard.OpenTK/PAL2/OpenGLDeviceContext.cs | 117 ++++++++++++++++++ Dashboard.OpenTK/PAL2/PhysicalWindow.cs | 81 ++++++++++++ Dashboard.sln | 15 +++ Dashboard/Dashboard.csproj | 5 + .../Dashboard.TestApplication.csproj | 1 + tests/Dashboard.TestApplication/Program.cs | 71 +++-------- 22 files changed, 394 insertions(+), 80 deletions(-) create mode 100644 Dashboard.Common/IWindow.cs create mode 100644 Dashboard.OpenGL/Dashboard.OpenGL.csproj rename {Dashboard.Drawing.OpenGL => Dashboard.OpenGL}/IGLContext.cs (56%) rename {Dashboard.Drawing.OpenGL => Dashboard.OpenGL}/IGLDisposable.cs (94%) create mode 100644 Dashboard.OpenTK/Dashboard.OpenTK.csproj create mode 100644 Dashboard.OpenTK/PAL2/OpenGLDeviceContext.cs create mode 100644 Dashboard.OpenTK/PAL2/PhysicalWindow.cs diff --git a/Dashboard.Common/IWindow.cs b/Dashboard.Common/IWindow.cs new file mode 100644 index 0000000..565c88d --- /dev/null +++ b/Dashboard.Common/IWindow.cs @@ -0,0 +1,102 @@ +using System.Drawing; + +namespace Dashboard +{ + /// + /// Base class of all Dashboard windows. + /// + public interface IWindow : IDisposable + { + /// + /// Name of the window. + /// + string Title { get; set; } + + /// + /// The size of the window that includes the window extents. + /// + SizeF OuterSize { get; set; } + + /// + /// The size of the window that excludes the window extents. + /// + SizeF ClientSize { get; set; } + } + + /// + /// Base class for all Dashboard windows that are DPI-aware. + /// + public interface IDpiAwareWindow : IWindow + { + /// + /// DPI of the window. + /// + float Dpi { get; } + + /// + /// Scale of the window. + /// + float Scale { get; } + } + + /// + /// An object that represents a window in a virtual space, usually another window or a rendering system. + /// + public interface IVirtualWindow : IWindow + { + /// + /// Send an event to this window. + /// + /// The event arguments + /// TODO: + void SendEvent(EventArgs args); + } + + /// + /// Generic interface for the rendering system present in a + /// + public interface IDeviceContext + { + /// + /// The swap group for this device context. + /// + ISwapGroup SwapGroup { get; } + + /// + /// The size of the window framebuffer in pixels. + /// + Size FramebufferSize { get; } + } + + /// + /// Interface that is used to swap the buffers of + /// + public interface ISwapGroup + { + /// + /// The swap interval for this swap group. + /// + public int SwapInterval { get; set; } + + /// + /// Swap buffers. + /// + void Swap(); + } + + /// + /// An object that represents a native operating system window. + /// + public interface IPhysicalWindow : IWindow + { + /// + /// The device context for this window. + /// + IDeviceContext DeviceContext { get; } + + /// + /// True if the window is double buffered. + /// + public bool DoubleBuffered { get; } + } +} diff --git a/Dashboard.Drawing.OpenGL/ContextExecutor.cs b/Dashboard.Drawing.OpenGL/ContextExecutor.cs index b744d70..55513be 100644 --- a/Dashboard.Drawing.OpenGL/ContextExecutor.cs +++ b/Dashboard.Drawing.OpenGL/ContextExecutor.cs @@ -1,5 +1,6 @@ using System.Drawing; using Dashboard.Drawing.OpenGL.Executors; +using Dashboard.OpenGL; namespace Dashboard.Drawing.OpenGL { diff --git a/Dashboard.Drawing.OpenGL/ContextResourcePool.cs b/Dashboard.Drawing.OpenGL/ContextResourcePool.cs index 9e85daa..e408f2a 100644 --- a/Dashboard.Drawing.OpenGL/ContextResourcePool.cs +++ b/Dashboard.Drawing.OpenGL/ContextResourcePool.cs @@ -1,3 +1,5 @@ +using Dashboard.OpenGL; + namespace Dashboard.Drawing.OpenGL { public class ContextResourcePoolManager diff --git a/Dashboard.Drawing.OpenGL/Dashboard.Drawing.OpenGL.csproj b/Dashboard.Drawing.OpenGL/Dashboard.Drawing.OpenGL.csproj index 05c6aa9..60c7eed 100644 --- a/Dashboard.Drawing.OpenGL/Dashboard.Drawing.OpenGL.csproj +++ b/Dashboard.Drawing.OpenGL/Dashboard.Drawing.OpenGL.csproj @@ -9,11 +9,12 @@ - + + diff --git a/Dashboard.Drawing.OpenGL/DrawCallRecorder.cs b/Dashboard.Drawing.OpenGL/DrawCallRecorder.cs index 58e6a77..10f410d 100644 --- a/Dashboard.Drawing.OpenGL/DrawCallRecorder.cs +++ b/Dashboard.Drawing.OpenGL/DrawCallRecorder.cs @@ -1,6 +1,7 @@ using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; +using Dashboard.OpenGL; using OpenTK.Graphics.OpenGL; using OpenTK.Mathematics; using Vector2 = System.Numerics.Vector2; diff --git a/Dashboard.Drawing.OpenGL/GLEngine.cs b/Dashboard.Drawing.OpenGL/GLEngine.cs index 30e92d1..01e59e3 100644 --- a/Dashboard.Drawing.OpenGL/GLEngine.cs +++ b/Dashboard.Drawing.OpenGL/GLEngine.cs @@ -1,4 +1,5 @@ using Dashboard.Drawing.OpenGL.Text; +using Dashboard.OpenGL; using OpenTK; using OpenTK.Graphics; diff --git a/Dashboard.Drawing.OpenGL/GradientUniformBuffer.cs b/Dashboard.Drawing.OpenGL/GradientUniformBuffer.cs index 55628f5..71cb866 100644 --- a/Dashboard.Drawing.OpenGL/GradientUniformBuffer.cs +++ b/Dashboard.Drawing.OpenGL/GradientUniformBuffer.cs @@ -1,4 +1,5 @@ using System.Runtime.InteropServices; +using Dashboard.OpenGL; using OpenTK.Mathematics; namespace Dashboard.Drawing.OpenGL diff --git a/Dashboard.Drawing.OpenGL/MappableBuffer.cs b/Dashboard.Drawing.OpenGL/MappableBuffer.cs index 55a9082..2df1595 100644 --- a/Dashboard.Drawing.OpenGL/MappableBuffer.cs +++ b/Dashboard.Drawing.OpenGL/MappableBuffer.cs @@ -1,5 +1,6 @@ using System.Numerics; using System.Runtime.CompilerServices; +using Dashboard.OpenGL; using OpenTK.Graphics.OpenGL; namespace Dashboard.Drawing.OpenGL diff --git a/Dashboard.Drawing.OpenGL/Text/BlurgEngine.cs b/Dashboard.Drawing.OpenGL/Text/BlurgEngine.cs index a3dab4d..fc16cd2 100644 --- a/Dashboard.Drawing.OpenGL/Text/BlurgEngine.cs +++ b/Dashboard.Drawing.OpenGL/Text/BlurgEngine.cs @@ -2,6 +2,7 @@ using System.Diagnostics; using System.Drawing; using System.Numerics; using BlurgText; +using Dashboard.OpenGL; using OpenTK.Graphics.OpenGL; using OPENGL = OpenTK.Graphics.OpenGL; @@ -54,7 +55,7 @@ namespace Dashboard.Drawing.OpenGL.Text path = Path.GetTempFileName(); try { - dest = File.Open(path, FileMode.CreateNew, FileAccess.Write, FileShare.None); + dest = File.Open(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None); } catch (IOException ex) { @@ -71,14 +72,13 @@ namespace Dashboard.Drawing.OpenGL.Text dest.Dispose(); DbBlurgFont font = (DbBlurgFont)LoadFont(path); - File.Delete(path); return font; } public IFont LoadFont(string path) { BlurgFont? font = Blurg.AddFontFile(path) ?? throw new Exception("Failed to load the font file."); - return new DbBlurgFont(Blurg, font, 12f); + return new DbBlurgFont(Blurg, font, 12f) { Path = path }; } public IFont LoadFont(NamedFont font) @@ -103,7 +103,12 @@ namespace Dashboard.Drawing.OpenGL.Text { if (dblurg.Owner != Blurg) { - throw new Exception(); + if (dblurg.Path == null) + return (DbBlurgFont)LoadFont(new NamedFont(dblurg.Family, dblurg.Size, dblurg.Weight, + dblurg.Slant, + dblurg.Stretch)); + else + return (DbBlurgFont)LoadFont(dblurg.Path); } else { diff --git a/Dashboard.Drawing.OpenGL/Text/DbBlurgFont.cs b/Dashboard.Drawing.OpenGL/Text/DbBlurgFont.cs index f6fd7b0..859b98e 100644 --- a/Dashboard.Drawing.OpenGL/Text/DbBlurgFont.cs +++ b/Dashboard.Drawing.OpenGL/Text/DbBlurgFont.cs @@ -13,6 +13,8 @@ namespace Dashboard.Drawing.OpenGL.Text public FontSlant Slant => Font.Italic ? FontSlant.Italic : FontSlant.Normal; public FontStretch Stretch => FontStretch.Normal; + internal string? Path { get; init; } + public DbBlurgFont(Blurg owner, BlurgFont font, float size) { Owner = owner; diff --git a/Dashboard.Drawing/DrawQueue.cs b/Dashboard.Drawing/DrawQueue.cs index 5a9125b..140a7e8 100644 --- a/Dashboard.Drawing/DrawQueue.cs +++ b/Dashboard.Drawing/DrawQueue.cs @@ -2,9 +2,7 @@ using System.Collections; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; -using System.Drawing; using System.IO; -using System.Runtime.CompilerServices; namespace Dashboard.Drawing { @@ -160,7 +158,7 @@ namespace Dashboard.Drawing { byte b = bytes[i]; - value = (value << 7) | b; + value |= (b & 0x7F) << (7*i); if ((b & (1 << 7)) == 0) { diff --git a/Dashboard.ImmediateUI/DimUI.cs b/Dashboard.ImmediateUI/DimUI.cs index 50937f9..4f7da23 100644 --- a/Dashboard.ImmediateUI/DimUI.cs +++ b/Dashboard.ImmediateUI/DimUI.cs @@ -1,7 +1,6 @@ using System; using System.Diagnostics.CodeAnalysis; using System.Drawing; -using System.Net.Http; using System.Numerics; using System.Text; using Dashboard.Drawing; diff --git a/Dashboard.OpenGL/Dashboard.OpenGL.csproj b/Dashboard.OpenGL/Dashboard.OpenGL.csproj new file mode 100644 index 0000000..298a8bc --- /dev/null +++ b/Dashboard.OpenGL/Dashboard.OpenGL.csproj @@ -0,0 +1,13 @@ + + + + net8.0 + enable + enable + + + + + + + diff --git a/Dashboard.Drawing.OpenGL/IGLContext.cs b/Dashboard.OpenGL/IGLContext.cs similarity index 56% rename from Dashboard.Drawing.OpenGL/IGLContext.cs rename to Dashboard.OpenGL/IGLContext.cs index d38330e..072ba62 100644 --- a/Dashboard.Drawing.OpenGL/IGLContext.cs +++ b/Dashboard.OpenGL/IGLContext.cs @@ -1,11 +1,11 @@ using System.Drawing; -namespace Dashboard.Drawing.OpenGL +namespace Dashboard.OpenGL { /// /// Interface for GL context operations /// - public interface IGLContext + public interface IGLContext : IDeviceContext { /// /// The associated group for context sharing. @@ -22,21 +22,10 @@ namespace Dashboard.Drawing.OpenGL /// Called when the context is disposed. /// event Action Disposed; - } - - /// - /// Extension interface for GL contexts in a DPI-aware environment. - /// - public interface IDpiAwareGLContext : IGLContext - { - /// - /// Dpi for current context. - /// - public float Dpi { get; } /// - /// Scale for the current context. This will be used to scale drawn geometry. + /// Activate this OpenGL Context. /// - public float Scale { get; } + void MakeCurrent(); } } diff --git a/Dashboard.Drawing.OpenGL/IGLDisposable.cs b/Dashboard.OpenGL/IGLDisposable.cs similarity index 94% rename from Dashboard.Drawing.OpenGL/IGLDisposable.cs rename to Dashboard.OpenGL/IGLDisposable.cs index c1d9aba..35a2dfa 100644 --- a/Dashboard.Drawing.OpenGL/IGLDisposable.cs +++ b/Dashboard.OpenGL/IGLDisposable.cs @@ -1,4 +1,4 @@ -namespace Dashboard.Drawing.OpenGL +namespace Dashboard.OpenGL { /// /// Interface much like except GL resources are dropped. diff --git a/Dashboard.OpenTK/Dashboard.OpenTK.csproj b/Dashboard.OpenTK/Dashboard.OpenTK.csproj new file mode 100644 index 0000000..65c3e14 --- /dev/null +++ b/Dashboard.OpenTK/Dashboard.OpenTK.csproj @@ -0,0 +1,18 @@ + + + + net8.0 + enable + enable + + + + + + + + + + + + diff --git a/Dashboard.OpenTK/PAL2/OpenGLDeviceContext.cs b/Dashboard.OpenTK/PAL2/OpenGLDeviceContext.cs new file mode 100644 index 0000000..c279092 --- /dev/null +++ b/Dashboard.OpenTK/PAL2/OpenGLDeviceContext.cs @@ -0,0 +1,117 @@ +using System.Collections.Concurrent; +using System.Drawing; +using Dashboard.OpenGL; +using OpenTK.Mathematics; +using OpenTK.Platform; +using TK = OpenTK.Platform.Toolkit; + +namespace Dashboard.OpenTK.PAL2 +{ + public class OpenGLDeviceContext : IGLContext, IGLDisposable + { + public OpenGLContextHandle ContextHandle { get; } + public WindowHandle WindowHandle { get; } + + public ISwapGroup SwapGroup { get; } + public int ContextGroup { get; } + + public Size FramebufferSize + { + get + { + TK.Window.GetFramebufferSize(WindowHandle, out Vector2i size); + return new Size(size.X, size.Y); + } + } + + public event Action? Disposed; + + public OpenGLDeviceContext(WindowHandle window, OpenGLContextHandle context, ISwapGroup? group = null) + { + WindowHandle = window; + ContextHandle = context; + SwapGroup = group ?? new DummySwapGroup(context); + ContextGroup = GetContextGroup(context); + } + + public void MakeCurrent() + { + TK.OpenGL.SetCurrentContext(ContextHandle); + } + + private bool _isDisposed = false; + + public void Dispose() => Dispose(true); + + public void Dispose(bool safeExit) + { + if (_isDisposed) return; + _isDisposed = true; + + if (SwapGroup is IGLDisposable glDisposable) + { + glDisposable.Dispose(safeExit); + } + else if (SwapGroup is IDisposable disposable) + { + disposable.Dispose(); + } + + Disposed?.Invoke(); + } + + private static int _contextGroupId = 0; + private static ConcurrentDictionary _contextGroupRootContexts = new ConcurrentDictionary(); + + private static int GetContextGroup(OpenGLContextHandle handle) + { + OpenGLContextHandle? shared = TK.OpenGL.GetSharedContext(handle); + + if (shared == null) + { + if (_contextGroupRootContexts.TryGetValue(handle, out int group)) + return group; + + group = Interlocked.Increment(ref _contextGroupId); + _contextGroupRootContexts.TryAdd(handle, group); + return GetContextGroup(handle); + } + else + { + if (_contextGroupRootContexts.TryGetValue(shared, out int group)) + return group; + + return GetContextGroup(shared); + } + } + + public class DummySwapGroup : ISwapGroup + { + public OpenGLContextHandle ContextHandle { get; } + + public int SwapInterval + { + get + { + TK.OpenGL.SetCurrentContext(ContextHandle); + return TK.OpenGL.GetSwapInterval(); + } + set + { + TK.OpenGL.SetCurrentContext(ContextHandle); + TK.OpenGL.SetSwapInterval(value); + } + } + + public DummySwapGroup(OpenGLContextHandle handle) + { + ContextHandle = handle; + } + + public void Swap() + { + TK.OpenGL.SwapBuffers(ContextHandle); + } + } + } +} diff --git a/Dashboard.OpenTK/PAL2/PhysicalWindow.cs b/Dashboard.OpenTK/PAL2/PhysicalWindow.cs new file mode 100644 index 0000000..8c0943a --- /dev/null +++ b/Dashboard.OpenTK/PAL2/PhysicalWindow.cs @@ -0,0 +1,81 @@ +using System.Drawing; +using OpenTK.Mathematics; +using OpenTK.Platform; +using TK = OpenTK.Platform.Toolkit; + +namespace Dashboard.OpenTK.PAL2 +{ + public class PhysicalWindow : IPhysicalWindow + { + public WindowHandle WindowHandle { get; } + public IDeviceContext DeviceContext { get; } + public bool DoubleBuffered => true; // Always true for OpenTK windows. + + public string Title + { + get => TK.Window.GetTitle(WindowHandle); + set => TK.Window.SetTitle(WindowHandle, value); + } + + public SizeF OuterSize + { + get + { + TK.Window.GetSize(WindowHandle, out Vector2i size); + return new SizeF(size.X, size.Y); + } + set => TK.Window.SetSize(WindowHandle, new Vector2i((int)value.Width, (int)value.Height)); + } + + public SizeF ClientSize + { + get + { + TK.Window.GetClientSize(WindowHandle, out Vector2i size); + return new SizeF(size.X, size.Y); + } + set => TK.Window.SetClientSize(WindowHandle, new Vector2i((int)value.Width, (int)value.Height)); + } + + public PhysicalWindow(WindowHandle window, IDeviceContext dc) + { + WindowHandle = window; + DeviceContext = dc; + } + + public PhysicalWindow(WindowHandle window, OpenGLContextHandle context, ISwapGroup? swapGroup = null) + { + WindowHandle = window; + DeviceContext = new OpenGLDeviceContext(window, context, swapGroup); + } + + public PhysicalWindow(GraphicsApiHints hints) + { + WindowHandle = TK.Window.Create(hints); + DeviceContext = CreateDeviceContext(WindowHandle, hints); + } + + private static IDeviceContext CreateDeviceContext(WindowHandle window, GraphicsApiHints hints) + { + switch (hints.Api) + { + case GraphicsApi.OpenGL: + case GraphicsApi.OpenGLES: + OpenGLContextHandle context = TK.OpenGL.CreateFromWindow(window); + return new OpenGLDeviceContext(window, context); + default: + throw new Exception($"Unknown graphics API {hints.Api}."); + } + } + + private bool _isDisposed = false; + public void Dispose() + { + if (_isDisposed) return; + _isDisposed = true; + + (DeviceContext as IDisposable)?.Dispose(); + TK.Window.Destroy(WindowHandle); + } + } +} diff --git a/Dashboard.sln b/Dashboard.sln index c718d03..83febe3 100644 --- a/Dashboard.sln +++ b/Dashboard.sln @@ -21,6 +21,12 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dashboard.Drawing.OpenGL", EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dashboard.ImmediateUI", "Dashboard.ImmediateUI\Dashboard.ImmediateUI.csproj", "{3F33197F-0B7B-4CD8-98BD-05D6D5EC76B2}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Frameworks", "Frameworks", "{9B62A92D-ABF5-4704-B831-FD075515A82F}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dashboard.OpenTK", "Dashboard.OpenTK\Dashboard.OpenTK.csproj", "{7B064228-2629-486E-95C6-BDDD4B4602C4}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dashboard.OpenGL", "Dashboard.OpenGL\Dashboard.OpenGL.csproj", "{33EB657C-B53A-41B4-BC3C-F38C09ABA577}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -51,11 +57,20 @@ Global {3F33197F-0B7B-4CD8-98BD-05D6D5EC76B2}.Debug|Any CPU.Build.0 = Debug|Any CPU {3F33197F-0B7B-4CD8-98BD-05D6D5EC76B2}.Release|Any CPU.ActiveCfg = Release|Any CPU {3F33197F-0B7B-4CD8-98BD-05D6D5EC76B2}.Release|Any CPU.Build.0 = Release|Any CPU + {7B064228-2629-486E-95C6-BDDD4B4602C4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7B064228-2629-486E-95C6-BDDD4B4602C4}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7B064228-2629-486E-95C6-BDDD4B4602C4}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7B064228-2629-486E-95C6-BDDD4B4602C4}.Release|Any CPU.Build.0 = Release|Any CPU + {33EB657C-B53A-41B4-BC3C-F38C09ABA577}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {33EB657C-B53A-41B4-BC3C-F38C09ABA577}.Debug|Any CPU.Build.0 = Debug|Any CPU + {33EB657C-B53A-41B4-BC3C-F38C09ABA577}.Release|Any CPU.ActiveCfg = Release|Any CPU + {33EB657C-B53A-41B4-BC3C-F38C09ABA577}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection GlobalSection(NestedProjects) = preSolution {7C90B90B-DF31-439B-9080-CD805383B014} = {9D6CCC74-4DF3-47CB-B9B2-6BB75DF2BC40} + {7B064228-2629-486E-95C6-BDDD4B4602C4} = {9B62A92D-ABF5-4704-B831-FD075515A82F} EndGlobalSection EndGlobal diff --git a/Dashboard/Dashboard.csproj b/Dashboard/Dashboard.csproj index 88b90bb..e0b09df 100644 --- a/Dashboard/Dashboard.csproj +++ b/Dashboard/Dashboard.csproj @@ -6,4 +6,9 @@ enable + + + + + diff --git a/tests/Dashboard.TestApplication/Dashboard.TestApplication.csproj b/tests/Dashboard.TestApplication/Dashboard.TestApplication.csproj index a9c7e50..d1fded2 100644 --- a/tests/Dashboard.TestApplication/Dashboard.TestApplication.csproj +++ b/tests/Dashboard.TestApplication/Dashboard.TestApplication.csproj @@ -11,6 +11,7 @@ + diff --git a/tests/Dashboard.TestApplication/Program.cs b/tests/Dashboard.TestApplication/Program.cs index 9e13470..a63bd3e 100644 --- a/tests/Dashboard.TestApplication/Program.cs +++ b/tests/Dashboard.TestApplication/Program.cs @@ -1,18 +1,15 @@ using Dashboard.Drawing; using System.Drawing; using System.Text; -using Dashboard; using Dashboard.Drawing.OpenGL; -using Dashboard.Drawing.OpenGL.Text; using Dashboard.ImmediateUI; +using Dashboard.OpenTK.PAL2; using OpenTK.Graphics; using OpenTK.Platform; using OpenTK.Graphics.OpenGL; using OpenTK.Mathematics; using Box2d = Dashboard.Box2d; using TK = OpenTK.Platform.Toolkit; -using sys = System.Numerics; -using otk = OpenTK.Mathematics; TK.Init(new ToolkitOptions() { @@ -24,7 +21,7 @@ TK.Init(new ToolkitOptions() } }); -WindowHandle wnd = TK.Window.Create(new OpenGLGraphicsApiHints() +PhysicalWindow window = new PhysicalWindow(new OpenGLGraphicsApiHints() { Version = new Version(3, 2), ForwardCompatibleFlag = true, @@ -42,18 +39,19 @@ WindowHandle wnd = TK.Window.Create(new OpenGLGraphicsApiHints() SupportTransparentFramebufferX11 = true, }); -TK.Window.SetTitle(wnd, "DashTerm"); -TK.Window.SetMinClientSize(wnd, 300, 200); -TK.Window.SetClientSize(wnd, new Vector2i(320, 240)); -TK.Window.SetBorderStyle(wnd, WindowBorderStyle.ResizableBorder); + +window.Title = "DashTerm"; +TK.Window.SetMinClientSize(window.WindowHandle, 300, 200); +TK.Window.SetClientSize(window.WindowHandle, new Vector2i(320, 240)); +TK.Window.SetBorderStyle(window.WindowHandle, WindowBorderStyle.ResizableBorder); // TK.Window.SetTransparencyMode(wnd, WindowTransparencyMode.TransparentFramebuffer, 0.1f); -OpenGLContextHandle context = TK.OpenGL.CreateFromWindow(wnd); +OpenGLDeviceContext context = (OpenGLDeviceContext)window.DeviceContext; -TK.OpenGL.SetCurrentContext(context); -TK.OpenGL.SetSwapInterval(1); +context.MakeCurrent(); +context.SwapGroup.SwapInterval = 1; -GLLoader.LoadBindings(new Pal2BindingsContext(TK.OpenGL, context)); +GLLoader.LoadBindings(new Pal2BindingsContext(TK.OpenGL, context.ContextHandle)); DrawQueue queue = new DrawQueue(); SolidBrush fg = new SolidBrush(Color.FromArgb(0, 0, 0, 0)); @@ -63,8 +61,7 @@ bool shouldExit = false; GLEngine engine = new GLEngine(); engine.Initialize(); -GlContext dbGlContext = new GlContext(wnd, context); -ContextExecutor executor = engine.GetExecutor(dbGlContext); +ContextExecutor executor = engine.GetExecutor(context); DimUI dimUI = new DimUI(new DimUIConfig() { Font = new NamedFont("Noto Sans", 9f), @@ -74,7 +71,7 @@ Vector2 mousePos = Vector2.Zero; Random r = new Random(); EventQueue.EventRaised += (handle, type, eventArgs) => { - if (handle != wnd) + if (handle != window.WindowHandle) return; switch (type) @@ -88,7 +85,7 @@ EventQueue.EventRaised += (handle, type, eventArgs) => } }; -TK.Window.SetMode(wnd, WindowMode.Normal); +TK.Window.SetMode(window.WindowHandle, WindowMode.Normal); List points = new List(); @@ -98,7 +95,7 @@ StringBuilder builder = new StringBuilder(); while (!shouldExit) { TK.Window.ProcessEvents(true); - TK.Window.GetFramebufferSize(wnd, out Vector2i framebufferSize); + TK.Window.GetFramebufferSize(context.WindowHandle, out Vector2i framebufferSize); executor.BeginFrame(); dimUI.Begin(new Box2d(0, 0, framebufferSize.X, framebufferSize.Y), queue); @@ -168,41 +165,5 @@ while (!shouldExit) executor.EndFrame(); queue.Clear(); - TK.OpenGL.SwapBuffers(context); -} - -class GlContext : IGLContext -{ - public WindowHandle Window { get; } - public OpenGLContextHandle Context { get; } - public int ContextGroup { get; } = -1; - - public Size FramebufferSize - { - get - { - TK.Window.GetFramebufferSize(Window, out Vector2i framebufferSize); - return new Size(framebufferSize.X, framebufferSize.Y); - } - } - - public event Action? Disposed; - - public GlContext(WindowHandle window, OpenGLContextHandle context) - { - Window = window; - Context = context; - - OpenGLContextHandle? shared = TK.OpenGL.GetSharedContext(context); - if (shared != null) - { - ContextGroup = _contexts.IndexOf(shared); - } - else - { - _contexts.Add(context); - } - } - - private static readonly List _contexts = new List(); + context.SwapGroup.Swap(); }