private static void LoadSources(string skelPath, string name, VariableTypes types, List<Feature> features, List<Extension> extensions, Dictionary<GLCommandGroup, List<GLCommand>> commands, Dictionary<GLEnumGroup, List<GLEnum>> enumerations) { #if DEBUG Stopwatch sw = new Stopwatch(); sw.Start(); #endif foreach (var p in featuresFiles) { features.Add(Feature.Load(p)); } foreach (var p in extensionsFiles) { extensions.Add(Extension.Load(p)); } Command.Load(commands, skelPath + "/glCommands.glskel"); Enumeration.Load(enumerations, skelPath + "/glEnums.glskel"); #if DEBUG sw.Stop(); Console.WriteLine("Loading GLSkel's for {0} took {1} milliseconds.", name, sw.ElapsedMilliseconds); #endif }
private static void Generate(string name, VariableTypes types, List<Feature> features, List<Extension> extensions, Dictionary<GLCommandGroup, List<GLCommand>> commands, Dictionary<GLEnumGroup, List<GLEnum>> enumerations) { #if DEBUG Stopwatch sw = new Stopwatch(); sw.Start(); #endif var generatedSources = Configs.glTargetSourcesPath + "/generated/"; var generatedExtensions = generatedSources + "extensions/"; Directory.CreateDirectory(generatedSources); Directory.CreateDirectory(generatedExtensions); foreach (var feature in features) { using (var streamWriter = new StreamWriter(generatedSources + feature.name + ".cs")) { var code = new CodeWriter(streamWriter); var className = ""; GenerateHeader(code, (className = Regex.Replace(feature.api, @"\d", "").ToUpper() + feature.number.Replace(".", ""))); WriteInterop(code, className, feature.commands, commands); WriteEnums(code, types, feature.enums, enumerations); WriteCommands(code, types, feature.commands, commands); CloseFile(code); } } foreach (var extension in extensions) { var name_Split = extension.name.Split('_'); var folderName = name_Split[1]; var extensionFile = extension.name; Directory.CreateDirectory(generatedExtensions + folderName + "/"); using (var streamWriter = new StreamWriter(generatedExtensions + folderName + "/" + extension.name + ".cs")) { var code = new CodeWriter(streamWriter); GenerateHeader(code, extension.name); WriteInterop(code, extension.name, extension.commands, commands); WriteEnums(code, types, extension.enums, enumerations); WriteCommands(code, types, extension.commands, commands); CloseFile(code); } } #if DEBUG sw.Stop(); Console.WriteLine("Creating C# files for {0} took {1} milliseconds.", name, sw.ElapsedMilliseconds); #endif }
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 WriteEnums(CodeWriter code, VariableTypes types, List<string> requiredEnums, Dictionary<GLEnumGroup, List<GLEnum>> enumerations) { code.WriteLine("#region Enums"); // Get distinct elements and convert into a list again. List<string> distinct = requiredEnums.Distinct().ToList(); foreach (var enumeration in distinct) { GLEnum obj = FindEnumeration(enumerations, enumeration); if (obj != null) { code.WriteLine("public static {0} {1} = {2};", types.GetEnumName(obj.value), obj.name, obj.value); } } code.WriteLine("#endregion"); code.WriteLine(); }