using StgSharp.Graphics; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; namespace StgSharp { public static class Platform { /// /// Version of current StgSharp platform. /// Main version represent in hundred's digit, /// and sub version represent in ten's digit. /// public const long version = 430; /// /// Init an instance of OpenGL program, /// This method should be called before other /// graphic methods, after LoadGL method. /// public static void InitGL(int majorVersion, int minorVersion) { if (InternalIO.glfw == null) { try { InternalIO.glfw = new glFramework_PInvoke(); InternalIO.glfw.Init(); InternalIO.InternalWriteLog("Successfully load GLFW APIs. StgSharp is now working on p/invoke mode", LogType.Info); } catch (Exception sig) { string log = $"Failed to load GLFW APIs by P/Invoke," + $"StgSharp will switch to delegate mode. The detailed info is:\n" + $"{sig.Message}\n"; if (InternalIO.SSClibPtr == default) { InternalIO.SSClibPtr = InternalIO.InternalLoadWINlib(InternalIO.SSC_libname + ".dll"); } InternalIO.glfw = new glFramework_Delegate(); InternalIO.SSClibPtr = InternalIO.InternalLoadWINlib(InternalIO.SSC_libname); FieldInfo[] glfwApis = typeof(glFramework_Delegate).GetFields(); foreach (FieldInfo item in glfwApis) { if (item.FieldType.BaseType == typeof(Delegate)) { try { IntPtr funcptr = InternalIO.InternalGetWINproc( InternalIO.SSClibPtr, item.Name); item.SetValue(null, ConvertAPI(funcptr, item.FieldType)); log += $"Successfully load GLFW API called {item.Name}"; } catch (Exception ex) { log += $"Failed to load GLFW API called {item.Name}, the detailed info is\n" + $"{ex.Message}"; continue; } } } InternalIO.InternalWriteLog(log, LogType.Warning); } } InternalIO.InternalInitGL(4, 6); } /// /// Write a log message to log file, and mark the level of this message. /// /// A string contains information of how current evironment runs. /// The severity of current message. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void WriteLog(string message, LogType logType) { InternalIO.InternalWriteLog(message, logType); } internal static TDele ConvertAPI(IntPtr funcPtr) { if (funcPtr == IntPtr.Zero) { throw new Exception("Failed to convert api, the function pointer is zero."); } try { return Marshal.GetDelegateForFunctionPointer(funcPtr); } catch (Exception ex) { throw; } } internal static Delegate ConvertAPI(IntPtr funcPtr, Type T) { if (funcPtr == IntPtr.Zero) { throw new Exception("Failed to convert api, the function pointer is zero."); } try { return Marshal.GetDelegateForFunctionPointer(funcPtr, T); } catch (Exception ex) { throw; } } } }