161 lines
4.7 KiB
C#
161 lines
4.7 KiB
C#
using Dashboard.Drawing;
|
|
using System.Drawing;
|
|
using Dashboard.Drawing.OpenGL;
|
|
using OpenTK.Graphics;
|
|
using OpenTK.Platform;
|
|
using OpenTK.Graphics.OpenGL;
|
|
using OpenTK.Mathematics;
|
|
using TK = OpenTK.Platform.Toolkit;
|
|
using sys = System.Numerics;
|
|
using otk = OpenTK.Mathematics;
|
|
|
|
TK.Init(new ToolkitOptions()
|
|
{
|
|
ApplicationName = "Paper Punch Out!",
|
|
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,
|
|
Multisamples = 0,
|
|
|
|
SupportTransparentFramebufferX11 = true,
|
|
});
|
|
TK.Window.SetTitle(wnd, "Paper Punch Out!");
|
|
TK.Window.SetMinClientSize(wnd, 300, 200);
|
|
TK.Window.SetClientSize(wnd, new Vector2i(320, 240));
|
|
TK.Window.SetBorderStyle(wnd, WindowBorderStyle.ResizableBorder);
|
|
TK.Window.SetTransparencyMode(wnd, WindowTransparencyMode.TransparentFramebuffer, 0.1f);
|
|
|
|
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();
|
|
SolidBrush fg = new SolidBrush(Color.FromArgb(0, 0, 0, 0));
|
|
SolidBrush bg = new SolidBrush(Color.Black);
|
|
bool shouldExit = false;
|
|
|
|
GLEngine engine = new GLEngine();
|
|
engine.Initialize();
|
|
|
|
GlContext dbGlContext = new GlContext(wnd, context);
|
|
ContextExecutor executor = engine.GetExecutor(dbGlContext);
|
|
|
|
Vector2 mousePos = Vector2.Zero;
|
|
Random r = new Random();
|
|
EventQueue.EventRaised += (handle, type, eventArgs) =>
|
|
{
|
|
if (handle != wnd)
|
|
return;
|
|
|
|
switch (type)
|
|
{
|
|
case PlatformEventType.Close:
|
|
shouldExit = true;
|
|
break;
|
|
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;
|
|
}
|
|
};
|
|
|
|
TK.Window.SetMode(wnd, WindowMode.Normal);
|
|
|
|
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);
|
|
|
|
while (!shouldExit)
|
|
{
|
|
TK.Window.ProcessEvents(true);
|
|
|
|
TK.Window.GetFramebufferSize(wnd, out Vector2i framebufferSize);
|
|
executor.BeginFrame();
|
|
|
|
// 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);
|
|
|
|
GL.Viewport(0, 0, framebufferSize.X, framebufferSize.Y);
|
|
GL.ClearColor(0.9f, 0.9f, 0.7f, 1.0f);
|
|
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
|
|
GL.Disable(EnableCap.DepthTest);
|
|
// GL.Enable(EnableCap.Blend);
|
|
// GL.BlendFuncSeparate(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha, BlendingFactor.One, BlendingFactor.Zero);
|
|
GL.ColorMask(true, true, true, true);
|
|
|
|
executor.Draw(queue);
|
|
executor.EndFrame();
|
|
|
|
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<OpenGLContextHandle> _contexts = new List<OpenGLContextHandle>();
|
|
}
|