private static void CreateInitFile(string path, string initClassName) { using (StreamWriter streamWriter = new StreamWriter(path + initClassName + ".cs")) { CodeWriter code = new CodeWriter(streamWriter); GenerateHeader(code, initClassName); code.WriteLine("internal static Func<string, IntPtr> GetProcAddress;"); code.WriteLine(); code.WriteLine("public static void Init(Func<string, IntPtr> @procAddress)"); code.WriteOpenBraceAndIndent(); code.WriteLine("GetProcAddress = @procAddress;"); code.WriteCloseBraceAndDedent(); CloseFile(code); } }
private static void WriteInterop(CodeWriter code, string className, List<string> requiredCommands, Dictionary<GLCommandGroup, List<GLCommand>> commands) { code.WriteLine("#region Interop"); code.WriteLine("static {0}()", className); code.WriteOpenBraceAndIndent(); List<string> writtenInterop = new List<string>(); code.WriteLine("Console.WriteLine(\"Initalising {0} interop methods!\");", className); code.WriteLine(); code.WriteLine("if (OpenGLInit.GetProcAddress == null)"); code.WriteOpenBraceAndIndent(); code.WriteLine("throw new System.ArgumentException(\"Value OpenGLInit.GetProcAddress cannot be null, call \'OpenGLInit.Init(Func<string, IntPtr> @procAddress)\' before using OpenGl!\", \"OpenGLInit.GetProcAddress\");"); code.WriteCloseBraceAndDedent(); code.WriteLine(); foreach (var command in requiredCommands) { GLCommand obj = FindCommand(commands, command); if (obj != null) { code.WriteLine("load{0}();", obj.name.Replace("gl", "")); } } code.WriteCloseBraceAndDedent(); code.WriteLine("#endregion"); code.WriteLine(); }
private static void WriteCommands(CodeWriter code, VariableTypes types, List<string> requiredCommands, Dictionary<GLCommandGroup, List<GLCommand>> commands) { code.WriteLine("#region Commands"); int commandIID = requiredCommands.Count; // Get distinct elements and convert into a list again. List<string> distinct = requiredCommands.Distinct().ToList(); foreach (var command in distinct) { GLCommand obj = FindCommand(commands, command); if (obj != null) { var paramCode = ""; var argsInCall = ""; bool firstParam = true; foreach (var parameter in obj.GetParameters()) { if (!firstParam) { paramCode += ", "; argsInCall += ", "; } if (firstParam) { firstParam = false; } paramCode += types.GetCSName(parameter.Value) + " " + parameter.Key.Trim(); argsInCall += (parameter.Value.Contains("ref") ? "ref " : "") + parameter.Key.Trim(); } code.WriteLine("internal delegate {0} {1}Func({2});", types.GetCSName(obj.returnType), obj.name, paramCode); code.WriteLine("internal static {0}Func {0}Ptr;", obj.name); code.WriteLine("internal static void load{0}()", obj.name.Replace("gl", "")); code.WriteOpenBraceAndIndent(); code.WriteLine("try"); code.WriteOpenBraceAndIndent(); code.WriteLine(string.Format("{0}Ptr = ({0}Func)Marshal.GetDelegateForFunctionPointer(OpenGLInit.GetProcAddress(\"{0}\"), typeof({0}Func));", obj.name)); code.WriteCloseBraceAndDedent(); code.WriteLine("catch"); code.WriteOpenBraceAndIndent(); code.WriteLine("Console.WriteLine(\"Failed to get function pointer for '{0}'.\");", obj.name); // throw new InvalidOperationException code.WriteCloseBraceAndDedent(); code.WriteCloseBraceAndDedent(); code.WriteLine("public static {0} {1}({2}) => " + "{1}Ptr({3});", types.GetCSName(obj.returnType), obj.name, paramCode, argsInCall); // => " + (returnType != "void" ? "return {1}Ptr({3});" : "{1}Ptr({3});") if (commandIID > 1) { code.WriteLine(); } commandIID--; } } code.WriteLine("#endregion"); }
private static void GenerateHeader(CodeWriter code, string className) { Console.WriteLine("Generating class: {0}", className); code.WriteLine("using System;"); code.WriteLine("using System.Diagnostics;"); code.WriteLine("using System.Reflection;"); code.WriteLine("using System.Runtime.InteropServices;"); code.WriteLine(); code.WriteLine("using GLbitfield = System.UInt32;"); code.WriteLine("using GLboolean = System.Boolean;"); code.WriteLine("using GLbyte = System.SByte;"); code.WriteLine("using GLchar = System.Char;"); code.WriteLine("using GLcharARB = System.Char;"); code.WriteLine("using GLclampd = System.Double;"); code.WriteLine("using GLclampf = System.Single;"); code.WriteLine("using GLclampx = System.Int32;"); code.WriteLine("using GLDEBUGPROC = System.IntPtr;"); code.WriteLine("using GLDEBUGPROCAMD = System.IntPtr;"); code.WriteLine("using GLDEBUGPROCARB = System.IntPtr;"); code.WriteLine("using GLDEBUGPROCKHR = System.IntPtr;"); code.WriteLine("using GLdouble = System.Double;"); code.WriteLine("using GLeglImageOES = System.IntPtr;"); code.WriteLine("using GLenum = System.UInt32;"); code.WriteLine("using GLfixed = System.Int32;"); code.WriteLine("using GLfloat = System.Single;"); code.WriteLine("using GLhalfNV = System.Single;"); // Half code.WriteLine("using GLhandleARB = System.UInt32;"); code.WriteLine("using GLint64 = System.Int64;"); code.WriteLine("using GLint64EXT = System.Int64;"); code.WriteLine("using GLint = System.Int32;"); code.WriteLine("using GLintptr = System.IntPtr;"); code.WriteLine("using GLintptrARB = System.IntPtr;"); code.WriteLine("using GLshort = System.Int16;"); code.WriteLine("using GLsizei = System.Int32;"); code.WriteLine("using GLsizeipt = System.IntPtr;"); code.WriteLine("using GLsizeiptr = System.IntPtr;"); code.WriteLine("using GLsizeiptrARB = System.IntPtr;"); code.WriteLine("using GLsync = System.IntPtr;"); code.WriteLine("using GLubyte = System.Byte;"); code.WriteLine("using GLuint64 = System.UInt64;"); code.WriteLine("using GLuint64EXT = System.UInt64;"); code.WriteLine("using GLuint = System.UInt32;"); code.WriteLine("using GLushort = System.UInt16;"); code.WriteLine("using GLvdpauSurfaceNV = System.IntPtr;"); code.WriteLine("using GLvoid = System.IntPtr;"); code.WriteLine("using _cl_context = System.IntPtr;"); code.WriteLine("using _cl_event = System.IntPtr;"); code.WriteLine(); code.WriteLine("namespace OpenGL"); code.WriteOpenBraceAndIndent(); code.WriteLine("public static class {0}", className); code.WriteOpenBraceAndIndent(); }