This works :)
This commit is contained in:
+93
-16
@@ -2,6 +2,8 @@
|
||||
using Hexa.NET.ImGui;
|
||||
using Hexa.NET.ImGuizmo;
|
||||
using Hexa.NET.ImPlot;
|
||||
using Hexa.NET.OpenGL;
|
||||
using HexaGen.Runtime;
|
||||
using SDL3;
|
||||
|
||||
namespace SDL3_TestingSuite.SDL3;
|
||||
@@ -9,8 +11,7 @@ namespace SDL3_TestingSuite.SDL3;
|
||||
public sealed unsafe class SDL3Window : IDisposable
|
||||
{
|
||||
public readonly nint Window;
|
||||
public readonly nint Renderer;
|
||||
|
||||
|
||||
public readonly Vector4 DefaultClearColor = new Vector4(0.06f, 0.06f, 0.06f, 1f);
|
||||
|
||||
private Vector4? _clearColor;
|
||||
@@ -30,17 +31,37 @@ public sealed unsafe class SDL3Window : IDisposable
|
||||
|
||||
private readonly FileSystemWatcher? _watcher;
|
||||
|
||||
private readonly GL _gl;
|
||||
|
||||
public SDL3Window(string name, int posX, int posY, int width, int height, SDL.WindowFlags flags = SDL.WindowFlags.Resizable | SDL.WindowFlags.HighPixelDensity)
|
||||
{
|
||||
if (!SDL.Init(SDL.InitFlags.Events | SDL.InitFlags.Video | SDL.InitFlags.Gamepad))
|
||||
throw new Exception($"SDL_Init failed: {SDL.GetError()}");
|
||||
|
||||
SDL.GLSetAttribute(SDL.GLAttr.ContextFlags, (int)SDL.GLContextFlag.ForwardCompatible);
|
||||
SDL.GLSetAttribute(SDL.GLAttr.ContextProfileMask, (int)SDL.GLProfile.Core);
|
||||
SDL.GLSetAttribute(SDL.GLAttr.ContextMajorVersion, 3);
|
||||
SDL.GLSetAttribute(SDL.GLAttr.ContextMinorVersion, 3);
|
||||
|
||||
SDL.SetHint(SDL.Hints.IMEImplementedUI, "1");
|
||||
|
||||
// Create window & renderer
|
||||
if (!SDL.CreateWindowAndRenderer(name, width, height, flags, out Window, out Renderer))
|
||||
SDL.GLSetAttribute(SDL.GLAttr.DoubleBuffer, 1);
|
||||
SDL.GLSetAttribute(SDL.GLAttr.DepthSize, 24);
|
||||
SDL.GLSetAttribute(SDL.GLAttr.StencilSize, 8);
|
||||
Window = SDL.CreateWindow(name, width, height, flags | SDL.WindowFlags.OpenGL);
|
||||
if (Window == nint.Zero)
|
||||
throw new Exception($"SDL_CreateWindowAndRenderer failed: {SDL.GetError()}");
|
||||
|
||||
nint glContext = SDL.GLCreateContext(Window);
|
||||
if (glContext == nint.Zero)
|
||||
throw new Exception($"SDL_GL_CreateContext failed: {SDL.GetError()}");
|
||||
|
||||
SDL.GLMakeCurrent(Window, glContext);
|
||||
SDL.GLSetSwapInterval(1);
|
||||
|
||||
SDL.SetWindowPosition(Window, posX, posY);
|
||||
SDL.SetRenderVSync(Renderer, 1);
|
||||
// SDL.SetRenderVSync(Renderer, 1);
|
||||
SDL.ShowWindow(Window);
|
||||
|
||||
// Create ImGui context
|
||||
@@ -91,10 +112,12 @@ public sealed unsafe class SDL3Window : IDisposable
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
}
|
||||
|
||||
_gl = new GL(new BindingsContext(Window, glContext));
|
||||
|
||||
// Init platform and renderer
|
||||
ImGuiSDL3Platform.Init(Window, Renderer, this);
|
||||
ImGuiSDL3Renderer.Init(Renderer, this);
|
||||
ImGuiSDL3Platform.Init(_gl, glContext, Window, this);
|
||||
ImGuiSDL3Renderer.Init(_gl, "#version 330");
|
||||
}
|
||||
|
||||
public bool ShouldClose;
|
||||
@@ -103,6 +126,8 @@ public sealed unsafe class SDL3Window : IDisposable
|
||||
{
|
||||
while (!_disposed)
|
||||
{
|
||||
SDL.PumpEvents();
|
||||
|
||||
if (ImGui.GetIO().WantTextInput && !SDL.TextInputActive(Window))
|
||||
SDL.StartTextInput(Window);
|
||||
else if (!ImGui.GetIO().WantTextInput && SDL.TextInputActive(Window))
|
||||
@@ -121,6 +146,10 @@ public sealed unsafe class SDL3Window : IDisposable
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
_gl.MakeCurrent();
|
||||
_gl.ClearColor(ClearColor.X, ClearColor.Y, ClearColor.Z, ClearColor.W);
|
||||
_gl.Clear(GLClearBufferMask.ColorBufferBit);
|
||||
|
||||
ImGuiSDL3Platform.NewFrame();
|
||||
ImGuiSDL3Renderer.NewFrame();
|
||||
@@ -132,19 +161,18 @@ public sealed unsafe class SDL3Window : IDisposable
|
||||
|
||||
ImGui.Render();
|
||||
|
||||
SDL.SetRenderScale(Renderer, io.DisplayFramebufferScale.X, io.DisplayFramebufferScale.Y);
|
||||
SDL.SetRenderDrawColorFloat(Renderer, ClearColor.X, ClearColor.Y, ClearColor.Z, ClearColor.W);
|
||||
SDL.RenderClear(Renderer);
|
||||
_gl.MakeCurrent();
|
||||
|
||||
ImGuiSDL3Renderer.RenderDrawData(ImGui.GetDrawData(), Renderer);
|
||||
ImGuiSDL3Renderer.RenderDrawData(ImGui.GetDrawData());
|
||||
|
||||
if ((io.ConfigFlags & ImGuiConfigFlags.ViewportsEnable) != 0)
|
||||
{
|
||||
ImGui.UpdatePlatformWindows();
|
||||
ImGui.RenderPlatformWindowsDefault();
|
||||
}
|
||||
|
||||
SDL.RenderPresent(Renderer);
|
||||
|
||||
_gl.MakeCurrent();
|
||||
_gl.SwapBuffers();
|
||||
|
||||
if (ShouldClose)
|
||||
{
|
||||
@@ -203,10 +231,10 @@ public sealed unsafe class SDL3Window : IDisposable
|
||||
_imPlotContext = null;
|
||||
}
|
||||
|
||||
if (Renderer != nint.Zero)
|
||||
{
|
||||
SDL.DestroyRenderer(Renderer);
|
||||
}
|
||||
// if (Renderer != nint.Zero)
|
||||
// {
|
||||
// SDL.DestroyRenderer(Renderer);
|
||||
// }
|
||||
|
||||
if (Window != nint.Zero)
|
||||
{
|
||||
@@ -214,4 +242,53 @@ public sealed unsafe class SDL3Window : IDisposable
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public unsafe class BindingsContext : IGLContext
|
||||
{
|
||||
private readonly nint window;
|
||||
private readonly nint context;
|
||||
|
||||
public BindingsContext(nint window, nint context)
|
||||
{
|
||||
this.window = window;
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
public nint Handle => window;
|
||||
|
||||
public bool IsCurrent => SDL.GLGetCurrentContext() == context;
|
||||
|
||||
public void Dispose() { }
|
||||
|
||||
public nint GetProcAddress(string procName)
|
||||
{
|
||||
return (nint)SDL.GLGetProcAddress(procName).GetPtrForDelegate();
|
||||
}
|
||||
|
||||
public bool IsExtensionSupported(string extensionName)
|
||||
{
|
||||
return SDL.GLExtensionSupported(extensionName);
|
||||
}
|
||||
|
||||
public void MakeCurrent()
|
||||
{
|
||||
SDL.GLMakeCurrent(window, context);
|
||||
}
|
||||
|
||||
public void SwapBuffers()
|
||||
{
|
||||
SDL.GLSwapWindow(window);
|
||||
}
|
||||
|
||||
public void SwapInterval(int interval)
|
||||
{
|
||||
SDL.GLSetSwapInterval(interval);
|
||||
}
|
||||
|
||||
public bool TryGetProcAddress(string procName, out nint procAddress)
|
||||
{
|
||||
procAddress = (nint)SDL.GLGetProcAddress(procName).GetPtrForDelegate();
|
||||
return procAddress != 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user