daily update on 2024.03.18 by itload youmu

This commit is contained in:
YoumuLiu
2024-03-19 02:41:04 +08:00
parent 15bf989285
commit 0c50c6282e
200 changed files with 7663 additions and 3455 deletions
+107 -129
View File
@@ -1,20 +1,41 @@
using StgSharp;
using StgSharp.Controlling;
using StgSharp.Logic;
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="Form.cs"
// Project: StgSharp
// AuthorGroup: Nitload Space
// Copyright (c) Nitload Space. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the “Software”), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software,
// and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and
// this permission notice shall be included in all copies
// or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”,
// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH
// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
using StgSharp.Math;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
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.Security.Principal;
using System.Text;
using System.Threading;
@@ -23,52 +44,45 @@ namespace StgSharp.Graphics
/// <summary>
///
/// </summary>
public abstract partial class Form
public abstract unsafe partial class Form
{
#region metadata
internal LinkedList<Control> allControl;
internal Vector4 backgroundColor;
internal IglFunc gl;
internal unsafe IntPtr graphicContextID;
internal IntPtr graphicContextID;
internal int height;
internal IntPtr monitor;
internal string name;
internal Form sharedForm;
internal int width;
internal IntPtr windowID;
protected readonly TimeProvider FrameTimeFrocider;
private GLFWwindow* windowID;
internal TimeSpanProvider frameTimeProvider;
#endregion metadata
#region form thread setting
private bool isSingleThread = true;
#endregion form thread setting
#region glapicount
private int missingAPIcount = 0;
private int problemAPIcount = 0;
#endregion glapicount
#region form main
#region form thread setting
private bool isSingleThread = true;
#endregion form thread setting
private int _shouldClose = 0;
protected Form()
{
}
/// <summary>
/// OpenGL package used by current form context.
/// OpenGL package binded to current form context.
/// A OpenGL context is only binded to current form.
/// </summary>
public IglFunc GL
{
get => gl;
internal set => gl = value;
}
public IglFunc GL => gl;
/// <summary>
/// Height of this form, represent in <see cref="int"/>
@@ -82,24 +96,13 @@ namespace StgSharp.Graphics
/// <summary>
/// Representing
/// </summary>
public bool IsDoubleRendering
{
get { return isSingleThread; }
}
public bool IsDoubleRendering => isSingleThread;
public bool ShouldClose
{
get
{
Interlocked.Exchange(ref _shouldClose, InternalIO.glfwWindowShouldClose(windowID));
return 0 != _shouldClose;
}
get => InternalIO.glfwWindowShouldClose(windowID) != 0;
set
{
InternalIO.glfwSetWindowShouldClose(windowID, value ? 1 : 0);
Interlocked.Exchange(ref _shouldClose, value ? 1 : 0);
}
set => InternalIO.glfwSetWindowShouldClose(windowID, value ? 1 : 0);
}
/// <summary>
@@ -107,7 +110,7 @@ namespace StgSharp.Graphics
/// </summary>
public Vec2d Size
{
get { return new Vec2d(width, height); }
get => new Vec2d(width, height);
set
{
width = (int)value.X;
@@ -124,10 +127,9 @@ namespace StgSharp.Graphics
set => width = value;
}
internal bool IsSingleThread
{
get { return isSingleThread; }
}
public unsafe IntPtr WindowID => (IntPtr)windowID;
internal unsafe GLFWwindow* id => windowID;
protected FrameBufferSizeHandler FrameBufferSizeCallback
{
@@ -138,12 +140,15 @@ namespace StgSharp.Graphics
}
}
internal bool IsSingleThread => isSingleThread;
/// <summary>
/// Init an OpenGl context if current form has not been binded to any context.
/// and set the context binded to current form as current OpenGL context.
/// Init an OpenGl context if current form has not been binded to any context. and set the context binded to
/// current form as current OpenGL context.
/// </summary>
/// <exception cref="Exception">Something goes wrong and
/// StgSharp fails in creating a new OpenGL context. </exception>
/// <exception cref="Exception">
/// Something goes wrong and StgSharp fails in creating a new OpenGL context.
/// </exception>
public unsafe void MakeAsCurrentContext()
{
if (windowID == default)
@@ -152,21 +157,19 @@ namespace StgSharp.Graphics
byte[] tittleBytes = Encoding.UTF8.GetBytes(name);
if (sharedForm == null)
{
windowPtr = InternalIO.glfwCreateWindow(
width, height, tittleBytes, monitor, IntPtr.Zero);
windowPtr = InternalIO.glfwCreateWindow(width, height, tittleBytes, monitor, (GLFWwindow*)IntPtr.Zero);
}
else
{
windowPtr = InternalIO.glfwCreateWindow(
width, height, tittleBytes, monitor, sharedForm.windowID);
windowPtr = InternalIO.glfwCreateWindow(width, height, tittleBytes, monitor, sharedForm.windowID);
}
if ((long)windowPtr == -1 || (long)windowPtr == -2)
if (((long)windowPtr == -1) || ((long)windowPtr == -2))
{
throw new Exception("Cannot create window instance!");
}
else
{
windowID = windowPtr;
windowID = (GLFWwindow*)windowPtr;
}
graphicContextID = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(GLcontext)));
@@ -191,18 +194,6 @@ namespace StgSharp.Graphics
this.monitor = monitor;
}
/// <summary>
/// Resize the current form
/// </summary>
/// <param name="width">New width of the window</param>
/// <param name="height">New height of the window</param>
public void SetSize(int width, int height)
{
this.Width = width;
this.Height = height;
InternalIO.glfwSetWindowSize(this.windowID, width, height);
}
/// <summary>
/// Resize the current form
/// </summary>
@@ -217,18 +208,15 @@ namespace StgSharp.Graphics
}
/// <summary>
/// Set the size limit of current form
/// Resize the current form
/// </summary>
/// <param name="minWidth">Minimum width of current form </param>
/// <param name="minHeight">Minimum height of current form</param>
/// <param name="maxWidth">Maximum width of curent form</param>
/// <param name="maxHeight">Maximum height of current form</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SetSizeLimit(int minWidth, int minHeight, int maxWidth, int maxHeight)
/// <param name="width">New width of the window</param>
/// <param name="height">New height of the window</param>
public void SetSize(int width, int height)
{
InternalIO.glfwSetWindowSizeLimits(
this.windowID,
minWidth, minHeight, maxWidth, maxHeight);
this.Width = width;
this.Height = height;
InternalIO.glfwSetWindowSize(this.windowID, width, height);
}
/// <summary>
@@ -245,19 +233,26 @@ namespace StgSharp.Graphics
{
InternalIO.glfwSetWindowSizeLimits(
this.windowID,
(int)minSize.X, (int)minSize.Y, (int)maxSize.X, (int)maxSize.Y);
(int)minSize.X,
(int)minSize.Y,
(int)maxSize.X,
(int)maxSize.Y);
}
unsafe ~Form()
{
Marshal.FreeHGlobal(graphicContextID);
}
/// <summary>
/// Set the size limit of current form
/// </summary>
/// <param name="minWidth">Minimum width of current form</param>
/// <param name="minHeight">Minimum height of current form</param>
/// <param name="maxWidth">Maximum width of curent form</param>
/// <param name="maxHeight">Maximum height of current form</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SetSizeLimit(int minWidth, int minHeight, int maxWidth, int maxHeight)
{ InternalIO.glfwSetWindowSizeLimits(this.windowID, minWidth, minHeight, maxWidth, maxHeight); }
#region form main
unsafe ~Form() { Marshal.FreeHGlobal(graphicContextID); }
private static unsafe delegate*<void> pollEventPtr = &InternalIO.glfwPollEvents;
private static unsafe delegate*<void> swapBufferPtr = &InternalSwapBuffers;
public abstract void Deinit(params object[] args);
@@ -268,22 +263,14 @@ namespace StgSharp.Graphics
public abstract void Init(params object[] args);
/// <summary>
/// Behaviour of current form when in a render cycle.
/// When this form
/// Behaviour of current form when in a render cycle. When this form
/// </summary>
/// <param name="args"></param>
public abstract void Main(params object[] args);
public unsafe void PollEvents()
{
if (isSingleThread)
{
InternalIO.glfwPollEvents();
}
else
{
gl.frameworkHandleEnqueue((IntPtr)pollEventPtr);
}
InternalIO.glfwPollEvents();
}
/// <summary>
@@ -303,61 +290,49 @@ namespace StgSharp.Graphics
{
Main(args);
SwapBuffers();
PollEvents();
}
}
else
{
GLonUpdate update = new GLonUpdate((GLcontext*)graphicContextID);
FrameThread renderThread = new FrameThread(update);
FrameThread updateThread = new FrameThread(update);
renderThread.SetNext(updateThread);
updateThread.SetNext(renderThread);
FrameAsyncTask renderTask = new FrameAsyncTask(update);
FrameAsyncTask updateTask = new FrameAsyncTask(update);
gl = update;
int a = 0;
while (!ShouldClose)
{
renderTask.StartAsync();
updateTask.StartAsync();
FrameAsyncTask.CombineAsync(renderTask, updateTask);
SwapBuffers();
PollEvents();
}
//TODO 对逻辑线程和渲染线程进行同步,两种gl模式包需要改名字
}
}
}
internal static unsafe void InternalSwapBuffers()
internal static unsafe void InternalSwapBuffers(FrameOperationBuffer operationBuffer, GLcontext* context)
{
IntPtr winHandle = default;
InternalIO.glfwSwapBuffers(winHandle);
IntPtr window = default;
operationBuffer.TryDequeue(out window);
InternalIO.glfwSwapBuffers((GLFWwindow*)window);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void InternalDeinit(params object[] args)
{
Deinit(args);
}
internal void InternalDeinit(params object[] args) { Deinit(args); }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void InternalInit(params object[] args)
{
Init(args);
}
internal unsafe void SwapBuffers()
{
if (isSingleThread)
{
InternalIO.glfwSwapBuffers(windowID);
}
else
{
M128 m = new M128();
m.Write<ulong>(0, (ulong)windowID);
gl.frameworkHandleEnqueue((IntPtr)swapBufferPtr, m);
}
InternalIO.glfwSwapBuffers(windowID);
}
/// <summary>
/// Get OpenGL library of this form's context.
/// </summary>
/// <returns>A <see cref="IglFunc"/> package contains all opengl APIs </returns>
/// <returns>A <see cref="IglFunc"/> package contains all opengl APIs</returns>
protected IglFunc GetGL()
{
if (StgSharp.API == GraphicAPI.GL)
@@ -366,7 +341,9 @@ namespace StgSharp.Graphics
}
else
{
InternalIO.InternalWriteLog("Attempt to get OpenGL API package in other graphic envitronment.", LogType.Error);
InternalIO.InternalWriteLog(
"Attempt to get OpenGL API package in other graphic envitronment.",
LogType.Error);
throw new InvalidOperationException("Attempt to get OpenGL API package in other graphic envitronment.");
}
}
@@ -396,7 +373,8 @@ namespace StgSharp.Graphics
throw new ArgumentException("Amount of threads should be bigger than 0");
}
}
#endregion form main
}
}