Dashboard/tests/Dashboard.TestApplication/Program.cs

161 lines
4.7 KiB
C#
Raw Normal View History

2024-12-13 19:57:07 +01:00
using Dashboard.Drawing;
using System.Drawing;
2025-01-14 18:34:47 +01:00
using Dashboard.Drawing.OpenGL;
using OpenTK.Graphics;
using OpenTK.Platform;
using OpenTK.Graphics.OpenGL;
using OpenTK.Mathematics;
using TK = OpenTK.Platform.Toolkit;
2025-01-18 21:30:56 +01:00
using sys = System.Numerics;
using otk = OpenTK.Mathematics;
2024-12-13 19:57:07 +01:00
2025-01-14 18:34:47 +01:00
TK.Init(new ToolkitOptions()
{
2025-01-18 21:30:56 +01:00
ApplicationName = "Paper Punch Out!",
2025-01-14 18:34:47 +01:00
Windows = new ToolkitOptions.WindowsOptions()
{
EnableVisualStyles = true,
IsDPIAware = true,
}
});
WindowHandle wnd = TK.Window.Create(new OpenGLGraphicsApiHints()
{
Version = new Version(3, 2),
ForwardCompatibleFlag = true,
DebugFlag = true,
Profile = OpenGLProfile.Core,
sRGBFramebuffer = true,
SwapMethod = ContextSwapMethod.Undefined,
RedColorBits = 8,
GreenColorBits = 8,
BlueColorBits = 8,
AlphaColorBits = 8,
2025-01-18 21:30:56 +01:00
Multisamples = 0,
2025-01-14 18:34:47 +01:00
SupportTransparentFramebufferX11 = true,
});
2025-01-18 21:30:56 +01:00
TK.Window.SetTitle(wnd, "Paper Punch Out!");
2025-01-14 18:34:47 +01:00
TK.Window.SetMinClientSize(wnd, 300, 200);
TK.Window.SetClientSize(wnd, new Vector2i(320, 240));
TK.Window.SetBorderStyle(wnd, WindowBorderStyle.ResizableBorder);
2025-01-18 21:30:56 +01:00
TK.Window.SetTransparencyMode(wnd, WindowTransparencyMode.TransparentFramebuffer, 0.1f);
2024-12-13 19:57:07 +01:00
2025-01-14 18:34:47 +01:00
OpenGLContextHandle context = TK.OpenGL.CreateFromWindow(wnd);
TK.OpenGL.SetCurrentContext(context);
TK.OpenGL.SetSwapInterval(1);
GLLoader.LoadBindings(new Pal2BindingsContext(TK.OpenGL, context));
DrawQueue queue = new DrawQueue();
2025-01-18 21:30:56 +01:00
SolidBrush fg = new SolidBrush(Color.FromArgb(0, 0, 0, 0));
SolidBrush bg = new SolidBrush(Color.Black);
2025-01-14 18:34:47 +01:00
bool shouldExit = false;
2024-12-14 17:46:56 +01:00
2025-01-14 18:34:47 +01:00
GLEngine engine = new GLEngine();
engine.Initialize();
2024-12-13 19:57:07 +01:00
2025-01-14 18:34:47 +01:00
GlContext dbGlContext = new GlContext(wnd, context);
ContextExecutor executor = engine.GetExecutor(dbGlContext);
2025-01-18 21:30:56 +01:00
Vector2 mousePos = Vector2.Zero;
Random r = new Random();
2025-01-14 18:34:47 +01:00
EventQueue.EventRaised += (handle, type, eventArgs) =>
2024-12-14 18:44:27 +01:00
{
2025-01-14 18:34:47 +01:00
if (handle != wnd)
return;
switch (type)
{
case PlatformEventType.Close:
shouldExit = true;
break;
2025-01-18 21:30:56 +01:00
case PlatformEventType.MouseMove:
mousePos = ((MouseMoveEventArgs)eventArgs).ClientPosition;
break;
case PlatformEventType.MouseUp:
MouseButtonUpEventArgs mouseUp = (MouseButtonUpEventArgs)eventArgs;
if (mouseUp.Button == MouseButton.Button1)
{
SolidBrush brush = new SolidBrush(Color.FromArgb(r.Next(256), r.Next(256), r.Next(256), 0));
queue.Point(new sys::Vector2(mousePos.X, mousePos.Y), 0f, 24f, brush);
}
break;
case PlatformEventType.KeyDown:
KeyDownEventArgs keyDown = (KeyDownEventArgs)eventArgs;
if (keyDown.Key == Key.Escape || keyDown.Key == Key.Delete || keyDown.Key == Key.Backspace)
queue.Clear();
break;
2025-01-14 18:34:47 +01:00
}
};
TK.Window.SetMode(wnd, WindowMode.Normal);
2024-12-14 18:44:27 +01:00
2025-01-18 21:30:56 +01:00
List<Vector3> points = new List<Vector3>();
queue.Line(new sys.Vector2(64, 256), new sys.Vector2(64+256, 256), 0, 64f, fg);
queue.Rect(new sys.Vector2(16, 16), new sys.Vector2(96, 96), 0, fg, bg, 8f);
2025-01-14 18:34:47 +01:00
while (!shouldExit)
{
TK.Window.ProcessEvents(true);
TK.Window.GetFramebufferSize(wnd, out Vector2i framebufferSize);
2025-01-18 21:30:56 +01:00
executor.BeginFrame();
2025-01-14 18:34:47 +01:00
2025-01-18 21:30:56 +01:00
// queue.Line(Vector3.Zero, new Vector3(System.Numerics.Vector2.One * 256f, 0), 4f, bg);
// queue.Rect(Vector3.UnitX, 2 * Vector3.UnitX + Vector3.UnitY, fg, bg, 2f);
2025-01-14 18:34:47 +01:00
GL.Viewport(0, 0, framebufferSize.X, framebufferSize.Y);
2025-01-18 21:30:56 +01:00
GL.ClearColor(0.9f, 0.9f, 0.7f, 1.0f);
2025-01-14 18:34:47 +01:00
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
2025-01-18 21:30:56 +01:00
GL.Disable(EnableCap.DepthTest);
// GL.Enable(EnableCap.Blend);
// GL.BlendFuncSeparate(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha, BlendingFactor.One, BlendingFactor.Zero);
GL.ColorMask(true, true, true, true);
2025-01-14 18:34:47 +01:00
executor.Draw(queue);
executor.EndFrame();
TK.OpenGL.SwapBuffers(context);
2024-12-14 18:44:27 +01:00
}
2025-01-14 18:34:47 +01:00
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<OpenGLContextHandle> _contexts = new List<OpenGLContextHandle>();
}