Daily update on 20231211 by nitload youmu

This commit is contained in:
Nitload
2023-12-12 03:17:16 +08:00
parent ab85c1a9fe
commit f97fb137ba
100 changed files with 2699 additions and 3191 deletions
+116 -41
View File
@@ -6,6 +6,9 @@ using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
@@ -24,7 +27,7 @@ namespace StgSharp.Graphics
/// <summary>
///
/// </summary>
public unsafe abstract partial class Form
public abstract partial class Form
{
internal LinkedList<Control> allControl;
internal Vector4 color;
@@ -35,23 +38,32 @@ namespace StgSharp.Graphics
internal Form sharedForm;
internal int width;
internal IntPtr windowID;
protected IglFunc GL;
private GLonRender glRender;
private GLonUpdate glUpdate;
~Form()
{
Marshal.FreeHGlobal((IntPtr)contextID);
glRender.api = default;
}
internal FrameUpdateTimer timer = new FrameUpdateTimer();
private SemaphoreSlim ping = new SemaphoreSlim(1, 1);
private SemaphoreSlim pong = new SemaphoreSlim(0, 1);
private UpdateWhenRender updateWhenRender;
private DynamicMethod updateWhenRenderDM;
private ILGenerator updateWhenRenderIL;
private delegate void UpdateWhenRender();
/// <summary>
/// Height of this form, represent in <see cref="int"/>
/// </summary>
public int Height
{
get => height;
set => height = value;
}
/// <summary>
/// Width of this form, represent in <see cref="int"/>
/// </summary>
public Vec2d Size
{
get { return new Vec2d(width, height); }
@@ -68,30 +80,107 @@ namespace StgSharp.Graphics
set => width = value;
}
#region form main
public abstract void Deinit(object[] args);
public abstract void Init(object[] args);
public abstract void OnRenderFrame(object[] args);
public abstract void OnUpdateFrame(object[] args);
internal void internalShowForm(object arg)
unsafe ~Form()
{
object[] args = (object[])arg;
Init((object[])args[0]);
while (InternalIO.glfw.WindowShouldClose(windowID) == 0)
{
OnUpdateFrame((object[])args[1]);
}
Deinit((object[])args[2]);
Marshal.FreeHGlobal((IntPtr)contextID);
glRender.api = default;
}
#region form main
public abstract void Deinit(object[] args, IglFunc GL);
public abstract void Init(object[] args, IglFunc GL);
public abstract void OnRenderFrame(object[] args, IglFunc GL);
public abstract void OnUpdateFrame(object[] args, IglFunc GL);
/// <summary>
/// Init this form and render it on screen, and respond with operation
/// </summary>
public void Show(object[] args)
{
if (args == null)
{
args = new object[3] { null, null, null };
}
Operation.RegistForm(this);
try
{
Thread updateThread = new Thread(InternalUpdateFrame);
Thread renderThread = new Thread(InternalRenderFrame);
updateThread.Start();
renderThread.Start();
updateThread.Join();
renderThread.Join();
ping.Dispose();
pong.Dispose();
}
catch (Exception)
{
throw;
}
}
internal void InternalInit(object[] args)
{
updateWhenRenderDM = new DynamicMethod(
name: "updateWhenRender",
returnType: typeof(void),
parameterTypes: new Type[] { typeof(void) },
restrictedSkipVisibility: true
);
updateWhenRenderIL = updateWhenRenderDM.GetILGenerator();
Init((object[])args, this.glRender);
}
internal void InternalRenderFrame(object args)
{
while (InternalIO.glfw.WindowShouldClose(windowID) != 0)
{
ping.Wait();
try
{
OnRenderFrame((object[])args, glRender);
timer.GoNextFrame();
throw new Exception("Forget to reload GL apis");
pong.Release();
}
catch (NullReferenceException)
{
throw new Exception("Forget to reload GL apis");
pong.Release();
}
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void InternalUpdateFrame(object args)
{
OnUpdateFrame((object[])args);
while (InternalIO.glfw.WindowShouldClose(windowID) != 0)
{
pong.Wait();
try
{
OnUpdateFrame((object[])args, glUpdate);
timer.GoNextFrame();
throw new Exception("Forget to reload GL apis");
ping.Release();
}
catch (NullReferenceException)
{
throw new Exception("Forget to reload GL apis");
ping.Release();
}
}
}
#endregion
@@ -115,7 +204,7 @@ namespace StgSharp.Graphics
/// </summary>
/// <exception cref="Exception">Something goes wrong and
/// StgSharp fails in creating a new OpenGL context. </exception>
public void MakeAsCurrentContext()
public unsafe void MakeAsCurrentContext()
{
if (windowID == default)
{
@@ -207,20 +296,6 @@ namespace StgSharp.Graphics
(int)minSize.X, (int)minSize.Y, (int)maxSize.X, (int)maxSize.Y);
}
public void ShowForm(
object[] initArgs,
object[] updateArgs,
object[] deinitArgs)
{
Thread t = new Thread(new ParameterizedThreadStart(internalShowForm));
t.Start(new object[] { initArgs, updateArgs, deinitArgs });
}
public void ShowForm()
{
Thread t = new Thread(new ParameterizedThreadStart(internalShowForm));
t.Start(new object[3] { null, null, null });
}
/// <summary>
/// Bind this form to StgSharp platform.