Advertisement
mattparks5855

More Theta OpenGL

Apr 10th, 2016
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.49 KB | None | 0 0
  1. import Theta::IO.*;
  2. import Theta::Web.*;
  3.  
  4. module Theta::Program;
  5.  
  6. // @summary A class capable of generation OpenGL for Theta.
  7. public static prototype Generator {
  8. fields
  9. {
  10. // @summary The main workspace that holds the project.
  11. public static String GLWorkspaceName = "Khronos.TEA";
  12.  
  13. // @summary The project where files will be placed.
  14. public static String GLTargetProjectName = "OpenGL.TEA";
  15.  
  16. // @summary The gl target path.
  17. public static mut String GLTargetPath;
  18.  
  19. // @summary The project to generate code in.
  20. public static mut String GLTargetSourcesPath;
  21.  
  22. // @summary The path to where to download / get OpenGL specs.
  23. public static mut String GLTargetSpecs;
  24. }
  25.  
  26. structures
  27. {
  28. private type SpecURL<String> =
  29. {
  30. EGL("https://cvs.khronos.org/svn/repos/ogl/trunk/doc/registry/public/api/egl.xml"),
  31. GL("https://cvs.khronos.org/svn/repos/ogl/trunk/doc/registry/public/api/gl.xml"),
  32. GLX("https://cvs.khronos.org/svn/repos/ogl/trunk/doc/registry/public/api/glx.xml"),
  33. WGL("https://cvs.khronos.org/svn/repos/ogl/trunk/doc/registry/public/api/wgl.xml"),
  34. README("https://cvs.khronos.org/svn/repos/ogl/trunk/doc/registry/public/api/readme.pdf"),
  35. }
  36.  
  37. private prototype CodeWriter
  38. {
  39. def CodeWriter<consructor>(IO::TextWriter writer)
  40. {
  41. m_writer = writer;
  42. }
  43.  
  44. def CodeWriter<deconsructor>()
  45. {
  46. }
  47.  
  48. structures
  49. {
  50. private TextWriter m_writer;
  51. private mut Int m_tabCount;
  52. private mut String m_tabString = String.Empty;
  53.  
  54. public Int TabCount
  55. {
  56. get
  57. {
  58. return m_tabCount;
  59. }
  60. set
  61. {
  62. m_tabCount = Math.Max(value, 0);
  63. m_tabString = new string(' ', m_tabCount * 4);
  64. }
  65. }
  66. }
  67.  
  68. methods
  69. {
  70. def WriteLine<public, void>()
  71. {
  72. m_writer.WriteLine();
  73. }
  74.  
  75. def WriteLine<public, void>(String line)
  76. {
  77. m_writer.Write(m_tabString);
  78. m_writer.WriteLine(line);
  79. }
  80.  
  81. def WriteLine<public, void>(String format, Object... args)
  82. {
  83. m_writer.Write(m_tabString);
  84. m_writer.WriteLine(format, args);
  85. }
  86.  
  87. def WriteOpenBraceAndIndent<public, void>()
  88. {
  89. WriteLine("{");
  90. TabCount++;
  91. }
  92.  
  93. def WriteCloseBraceAndDedent<public, void>()
  94. {
  95. TabCount--;
  96. WriteLine("}");
  97. }
  98. }
  99. }
  100. }
  101.  
  102. methods
  103. {
  104. def UpdateDirPaths<private, static, void>()
  105. {
  106. IO::DirectoryInfo directoryRoot = new IO::DirectoryInfo(IO::Directory.GetCurrentDirectory());
  107.  
  108. // Walk up to the solution file so we can then go into the project and write to the C# files directly.
  109. while (directoryRoot != null && !IO::File.Exists(IO::Path.Combine(directoryRoot.FullName, glWorkspaceName + ".sln")))
  110. {
  111. directoryRoot = directoryRoot.Parent;
  112. }
  113.  
  114. // If the solution wasn't found (maybe its not not running from the bin directory), just write out to the current directory.
  115. if (directoryRoot == null)
  116. {
  117. directoryRoot = new IO::DirectoryInfo(IO::Directory.GetCurrentDirectory());
  118. }
  119.  
  120. // Sets the specified gl paths.
  121. GLTargetPath = IO::Path.Combine(directoryRoot.FullName, glTargetProjectName);
  122. GLTargetSourcesPath = glTargetPath + "/" + "Sources";
  123. GLTargetSpecs = glTargetPath + "/" + "GLSpecs";
  124. }
  125.  
  126. def DownloadSpecs<private, static, void>()
  127. {
  128. // Creates the specs path directory if it does not exist!
  129. if (!IO::Directory.Exists(glTargetSpecs))
  130. {
  131. IO::Directory.CreateDirectory(glTargetSpecs);
  132. }
  133.  
  134. // Downloads the specs files.
  135. foreach (SpecURL url : SpecURL.ToList)
  136. {
  137. String[] spilit = url.Value.Split('/');
  138. String p = "";
  139.  
  140. if (!IO::File.Exists((p = IO::Path.Combine(glTargetSpecs, spilit[spilit.Length - 1]))))
  141. {
  142. if (Web::HasInternet)
  143. {
  144. IO::StdOut.PrintLn("Downloading the required {0} document!", spilit[spilit.Length - 1]);
  145. Web::WebClient glClient = new Web::WebClient();
  146. glClient.DownloadFile(url.Value, p);
  147. }
  148. else
  149. {
  150. IO::StdOut.PrintLn("Could not download {0}, no internet connection!", url.Name);
  151. }
  152. }
  153. }
  154. }
  155.  
  156. def Generate<private, static, void>(String xmlName, String dllPath)
  157. {
  158. var rootPath = "";
  159. var source = IO::XDocument.Load(GLTargetSpecs + "/" + xmlName + ".xml");
  160. var sourceTypes = XDocument.Load(GLTargetSpecs + "/type.xml");
  161. IO::Directory.CreateDirectory(rootPath = GLTargetSourcesPath + "/Graphics/");
  162.  
  163. // MOR CRAP:
  164.  
  165. CreateInitFile(Configs.glTargetSourcesPath + "/Graphics/", "OpenGLInit");
  166. }
  167.  
  168. // MOR FUNCTIONS:
  169.  
  170. def CreateInitFile<private, static, void>(String path, String initClassName)
  171. {
  172. using (IO::StreamWriter writer = new IO::StreamWriter(path + "/" + initClassName + ".tea"))
  173. {
  174. var code = new CodeWriter(writer);
  175. GenerateHeader(code, initClassName);
  176.  
  177. code.WriteLine("fields");
  178. code.WriteOpenBraceAndIndent();
  179. code.WriteLine("internal static mut Func<string, IntPtr> GetProcAddress;");
  180. code.WriteCloseBraceAndDedent();
  181. code.WriteLine();
  182.  
  183. code.WriteLine("structures");
  184. code.WriteOpenBraceAndIndent();
  185. code.WriteCloseBraceAndDedent();
  186. code.WriteLine();
  187.  
  188. code.WriteLine("methods");
  189. code.WriteOpenBraceAndIndent();
  190. code.WriteLine("def Init<public, static, void>(Func<string, IntPtr> @procAddress)");
  191. code.WriteOpenBraceAndIndent();
  192. code.WriteLine("GetProcAddress = @procAddress;");
  193. code.WriteCloseBraceAndDedent();
  194. code.WriteCloseBraceAndDedent();
  195. CloseFile(code);
  196. }
  197. }
  198.  
  199. def GenerateHeader<private, static, void>(CodeWriter code, String className)
  200. {
  201. Console.WriteLine("Generating class: {0}", className);
  202. code.WriteLine("module Theta::OpenGL");
  203. code.WriteLine();
  204. code.WriteLine("public static prototype {0}", className);
  205. code.WriteOpenBraceAndIndent();
  206. }
  207.  
  208. def CloseFile<private, static, void>(CodeWriter code)
  209. {
  210. code.WriteCloseBraceAndDedent();
  211. }
  212.  
  213. def FindCommand<private, static, GLCommand>(Command commands, string name)
  214. {
  215. foreach (var command : commands.Commands)
  216. {
  217. if (command.Name.Trim() == name.Trim())
  218. {
  219. return command;
  220. }
  221. }
  222.  
  223. return null;
  224. }
  225.  
  226. def FindEnumeration<private, static, GLEnum>(Enumeration enumeration, String name)
  227. {
  228. foreach (var enums : enumeration.Enums)
  229. {
  230. foreach (var enum2 : enums.Enums)
  231. {
  232. if (enum2.Name.Trim() == name.Trim())
  233. {
  234. return enum2;
  235. }
  236. }
  237. }
  238.  
  239. return null;
  240. }
  241.  
  242. def FindFeature<private, static, GLFeature>(Feature features, String name)
  243. {
  244. foreach (var feature : features.Features)
  245. {
  246. if (feature.Name.Trim() == name.Trim())
  247. {
  248. return feature;
  249. }
  250. }
  251.  
  252. return null;
  253. }
  254.  
  255. // @summary Entry point to the program, can be in any prototype.
  256. // @params args: Arguements provided to the program.
  257. // @returns int: Status code of the program.
  258. def Main<entry, Int>(String[] args)
  259. {
  260. // Sets up the console.
  261. IO::StdOut.Title = "TEA OpenGL";
  262. IO::StdOut.BackgroundColor = IO::ConsoleColour.Black;
  263. IO::StdOut.ForegroundColor = IO::ConsoleColour.Green;
  264.  
  265. // Initalize files and paths.
  266. UpdateDirPaths();
  267. DownloadSpecs();
  268.  
  269. // Generate files! TODO: OpenAL, OpenCL & GLFS
  270. Generate("glfw", "");
  271. Generate("gl", "");
  272. // Generate("wgl", "");
  273. // Generate("glx", "");
  274. // Generate("egl", "");
  275.  
  276. // Wait for the user to end the program.
  277. IO::StdOut.PrintLn("Press any key to exit...");
  278. IO::StdOut.Pause();
  279. }
  280. }
  281. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement